Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 766
Joe Soul-bringer Avatar asked Jun 08 '09 06:06

Joe Soul-bringer


People also ask

How do I save a plotted image in MATLAB?

Save the chart to a file by hovering over the export button in the axes toolbar and selecting the first item in the drop-down list. MATLAB displays the Save As dialog box with the file type options.

How do I stop MATLAB from displaying figures?

Accepted Answer To avoid showing figures in MATLAB you can start MATLAB using the noFigureWindows option. This option is not available on UNIX.

How do I save an output figure in MATLAB?

To save the current figure, specify fig as gcf . saveas( fig , filename , formattype ) creates the file using the specified file format, formattype .


2 Answers

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') 
like image 148
Bessi Avatar answered Sep 21 '22 13:09

Bessi


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'); 
like image 24
gnovice Avatar answered Sep 20 '22 13:09

gnovice