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

Solution 1:

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')

Solution 2:

The simple answer to your question is given by Bessi and Mr Fooz: set the 'Visible' setting for the figure to 'off'. Although it's very easy to use commands like IMSHOW and PRINT to generate figures, I'll summarize why I think it's not necessarily the best option:

  • As illustrated by Mr Fooz's answer, there are many other factors that come into play when trying to save figures as images. The type of output you get is going to be dependent on many figure and axes settings, thus increasing the likelihood that you will not get the output you want. This could be especially problematic if you have your figures set to be invisible, since you won't notice some discrepancy that could be caused by a change in a default setting for the figure or axes. In short, your output becomes highly sensitive to a number of settings that you would then have to add to your code to control your output, as Mr Fooz's example shows.

  • Even if you're not viewing the figures as they are made, you're still probably making MATLAB do more work than is really necessary. Graphics objects are still created, even if they are not rendered. If speed is a concern, generating images from figures doesn't seem like the ideal solution.

My suggestion is to actually modify the image data directly and save it using IMWRITE. It may not be as easy as using IMSHOW and other plotting solutions, but I think it is more efficient and gives more robust and consistent results that are not as sensitive to various plot settings. For the example you give, I believe the alternative code for creating a black rectangle would look something like this:

im = imread('image.tif');
[r,c,d] = size(im);
x0 = 100;
y0 = 100;
w = 10;
h = 10;
x = [x0:x0+w x0*ones(1,h+1) x0:x0+w (x0+w)*ones(1,h+1)];
y = [y0*ones(1,w+1) y0:y0+h (y0+h)*ones(1,w+1) y0:y0+h];
index = sub2ind([r c],y,x);
im(index) = 0;
im(index+r*c) = 0;
im(index+2*r*c) = 0;
imwrite(im,'image2.tif');

Solution 3:

I'm expanding on Bessi's solution here a bit. I've found that it's very helpful to know how to have the image take up the whole figure and to be able to tightly control the output image size.

% prevent the figure window from appearing at all
f = figure('visible','off'); 
% alternative way of hiding an existing figure
set(f, 'visible','off'); % can use the GCF function instead

% If you start getting odd error messages or blank images,
% add in a DRAWNOW call.  Sometimes it helps fix rendering
% bugs, especially in long-running scripts on Linux.
%drawnow; 

% optional: have the axes take up the whole figure
subplot('position', [0 0 1 1]); 

% show the image and rectangle
im = imread('peppers.png');
imshow(im, 'border','tight');
rectangle('Position', [100, 100, 10, 10]);

% Save the image, controlling exactly the output
% image size (in this case, making it equal to 
% the input's). 
[H,W,D] = size(im);
dpi = 100;
set(f, 'paperposition', [0 0 W/dpi H/dpi]);
set(f, 'papersize', [W/dpi H/dpi]);
print(f, sprintf('-r%d',dpi), '-dtiff', 'image2.tif');

If you'd like to render the figure to a matrix, type "help @avifile/addframe", then extract the subfunction called "getFrameForFigure". It's a Mathworks-supplied function that uses some (currently) undocumented ways of extracting data from figure.

Solution 4:

Here is a completely different answer:

If you want an image file out, why not just save the image instead of the entire figure?

im = magic(10)
imwrite(im/max(im(:)),'magic.jpg')

Then prove that it worked.

imshow('magic.jpg')

This can be done for indexed and RGB also for different output formats.