在matlab中保存GUI特定区域的图像
Saving image of specific area of GUI in matlab
我开发了一个 GUI,它可以浏览 mat 文件并在 matlab 中绘制绘图,现在我想使用 GUI 中的保存按钮将这些绘图保存为图像
我把保存回调函数的编码写成
[file,path]=uiputfile({'*.bmp','BMP'},'Save Image As');
f=getframe(handles.axes);
[x,map]=frame2im(f);
imwrite(x,fullfile(path, file),'bmp');
但是这段代码只给出了没有标记轴的图表。
有人建议我使用 explore_fig
但我无法将其用于我的目的
如果我想将我的 GUI 的特定区域保存为图像,我应该使用什么代码
谢谢
如您所见,使用轴的 getframe
it grabs solely the contents of the axes and no more. In order to get the axes labeled, you can take advantage of the TightInset
属性 来查找轴的边界矩形。 TightInset
是添加到轴 Position
以便为标签腾出空间的边距。
此外,您可以利用 getframe
的第二个输入,它指定要抓取的矩形(以像素为单位)。您可以使用 hgconvertunits
(未记录)进行计算。
% Position INCLUDING labels/ticks
position = get(hax, 'Position');
inset = get(hax, 'TightInset');
outerpos = [position(1:2) - inset(1:2), position(3:4) + inset(3:4)];
% Ensure that the units are PIXELS
rect = hgconvertunits(hfig, outerpos, get(hax1, 'Units'), 'pixels', hfig);
% Grab only the specified rectangle from the figure
im = getframe(hfig, rect);
如果我们对某些样本数据执行此操作
% Load Sample Data
load mri
img = squeeze(D(:,:,12));
% Display axes in middle of figure
hfig = figure();
hax = axes('Position', [0.2 0.2 0.5 0.5]);
imagesc(img);
xlabel('columns');
ylabel('rows');
axis image;
原来是这样的
getframe
和一个矩形的结果是
我开发了一个 GUI,它可以浏览 mat 文件并在 matlab 中绘制绘图,现在我想使用 GUI 中的保存按钮将这些绘图保存为图像
我把保存回调函数的编码写成
[file,path]=uiputfile({'*.bmp','BMP'},'Save Image As');
f=getframe(handles.axes);
[x,map]=frame2im(f);
imwrite(x,fullfile(path, file),'bmp');
但是这段代码只给出了没有标记轴的图表。
有人建议我使用 explore_fig
但我无法将其用于我的目的
如果我想将我的 GUI 的特定区域保存为图像,我应该使用什么代码
谢谢
如您所见,使用轴的 getframe
it grabs solely the contents of the axes and no more. In order to get the axes labeled, you can take advantage of the TightInset
属性 来查找轴的边界矩形。 TightInset
是添加到轴 Position
以便为标签腾出空间的边距。
此外,您可以利用 getframe
的第二个输入,它指定要抓取的矩形(以像素为单位)。您可以使用 hgconvertunits
(未记录)进行计算。
% Position INCLUDING labels/ticks
position = get(hax, 'Position');
inset = get(hax, 'TightInset');
outerpos = [position(1:2) - inset(1:2), position(3:4) + inset(3:4)];
% Ensure that the units are PIXELS
rect = hgconvertunits(hfig, outerpos, get(hax1, 'Units'), 'pixels', hfig);
% Grab only the specified rectangle from the figure
im = getframe(hfig, rect);
如果我们对某些样本数据执行此操作
% Load Sample Data
load mri
img = squeeze(D(:,:,12));
% Display axes in middle of figure
hfig = figure();
hax = axes('Position', [0.2 0.2 0.5 0.5]);
imagesc(img);
xlabel('columns');
ylabel('rows');
axis image;
原来是这样的
getframe
和一个矩形的结果是