我有一个代码可以将图像加载到一个绘图上,在它上面绘制一个矩形,然后将图像保存到png文件中:
figure('Visible', 'off');
imshow(im)
hold on
for n=1:size(windowCandidates,1)
rectangle('Position',[x,y,w,h],'EdgeColor','g','LineWidth',2)
end
f=getframe;
[img_bound,map]=frame2im(f);
imwrite(img_bound, strcat(directory, 'name.', 'png'));
hold off
如果不在图中显示,我怎么能这样做?只是修改它并保存,我不希望用户看到所有这个过程)
谢谢!
答案 0 :(得分:2)
您可以使用以下方法使图形不可见:
figure('Visible', 'off');
然后把它写成Matlab fig via:
saveas(gcf, 'path/to/filename');
或使用print
命令来执行png
print('-dpng', 'path/to/filename');
上有好的答案和解释的类似问题
<强>更新强>
感谢Steve指向此未记录的matlab function
function so;
close all;
im = imread('cameraman.tif');
hfig = figure('Visible', 'off'), imshow(im, 'Border', 'tight');
for n=1:2
rectangle('Position', [20*n, 20*n, 50, 50], 'EdgeColor', 'g', 'LineWidth', 2)
hold on;
end
F = im2frame(zbuffer_cdata(gcf));
imwrite(F.cdata, 'test.png');
% Function copied from
% http://www.mathworks.com/support/solutions/en/data/1-3NMHJ5/?solution=1
% -3NMHJ5
%
function cdata = zbuffer_cdata(hfig)
% Get CDATA from hardcopy using zbuffer
% Need to have PaperPositionMode be auto
orig_mode = get(hfig, 'PaperPositionMode');
set(hfig, 'PaperPositionMode', 'auto');
cdata = hardcopy(hfig, '-Dzbuffer', '-r0');
% Restore figure to original state
set(hfig, 'PaperPositionMode', orig_mode);