In MATLAB, how do I plot to an image and save the result without displaying it?

Joe Soul-bringer picture Joe Soul-bringer · Jun 8, 2009 · Viewed 58.9k times · Source

This question kind of starts where this question ends up. MATLAB has a powerful and flexible image display system which lets you use the imshow and plot commands to display complex images and then save the result. For example:

im = imread('image.tif');
f = figure, imshow(im, 'Border', 'tight');
rectangle('Position', [100, 100, 10, 10]);
print(f, '-r80', '-dtiff', 'image2.tif');

This works great.

The problem is that if you are doing a lot of image processing, it starts to be real drag to show every image you create - you mostly want to just save them. I know I could start directly writing to an image and then saving the result. But using plot/rectangle/imshow is so easy, so I'm hoping there is a command that can let me call plot, imshow etc, not display the results and then save what would have been displayed. Anyone know any quick solutions for this?

Alternatively, a quick way to put a spline onto a bitmap might work...

Answer

Bessi picture Bessi · Jun 8, 2009

When you create the figure you set the Visibile property to Off.

f = figure('visible','off')

Which in your case would be

im = imread('image.tif');
f = figure('visible','off'), imshow(im, 'Border', 'tight');
rectangle('Position', [100, 100, 10, 10]);
print(f, '-r80', '-dtiff', 'image2.tif');

And if you want to view it again you can do

set(f,'visible','on')