Image 如何在不显示图像的情况下修改图像?

Image 如何在不显示图像的情况下修改图像?,image,matlab,image-processing,drawing,Image,Matlab,Image Processing,Drawing,我有一个代码,将图像加载到绘图,在其上绘制一个矩形,然后将图像保存到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]=

我有一个代码,将图像加载到绘图,在其上绘制一个矩形,然后将图像保存到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
如果不在图形中显示,如何执行相同的操作?只是修改并保存,我不希望用户看到所有这些过程)


谢谢

可以使用以下方法使图形不可见:

figure('Visible', 'off');
然后通过以下方式将其写成Matlab fig:

saveas(gcf, 'path/to/filename');
或者使用
print
命令打印png就是这种情况

print('-dpng', 'path/to/filename');
类似的问题,有很好的答案和解释,其他在哪里

更新

感谢您指向此未记录的


我试过了,但没用。它不断以递增的数字显示图像。我更新了问题的代码。问题在于
getframe
。您可以按照我链接的另一个stackoverflow示例保存一个图形。矩形很容易光栅化,因此您应该能够修改图像矩阵并直接在其上绘制,然后将结果保存到磁盘。这样就避免了使用
GETFRAME
等函数进行屏幕捕获的过程。您可能会在此处找到类似的问题,因此……相关问题:
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);