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

This question begins with where this question ends. MATLAB has a powerful and flexible image display system that allows you to 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 do a lot of image processing, it starts to be a real drag and drop to show each created image - you basically just want to save them. I know that I can start writing the image directly, and then save the result. But using plot / rectangle / imshow is so simple, so I hope there is a command that can let me call plot, imshow etc., Do not display the results, and then save what would be displayed. Does anyone know any quick fixes for this?

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

+41
matlab graphics
Jun 08 '09 at 6:42
source share
5 answers

When you create a shape, you set the Visibile property to Off.

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

What would be in your case

 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') 
+41
Jun 08 '09 at 12:19
source share

A simple answer to your question is given by Bessi and Mr Fooz : set 'Visible' by setting the digit to 'off'. Although it is very easy to use commands such as IMSHOW and PRINT to generate numbers, I will summarize why I think this is not necessarily the best option:

  • As illustrated by Mr. Fooz, there are many other factors that arise when trying to save shapes as images. The type of output you get will depend on many settings for the shape and axes, thus increasing the likelihood that you will not get the result you need. This can be especially problematic if you have invisible numbers set, as you will not notice any inconsistencies that might be caused by changing the default value for the shape or axes. In short, your output becomes very sensitive to a number of settings that you will need to add to your code to control your output, as the example of Mr. Fooz shows.

  • Even if you do not look at the numbers as they are created, you are still likely doing MATLAB more work than is really necessary. Graphics are still being created, even if they are not displayed. If speed is a concern, generating images from numbers doesn't seem like the perfect solution.

My suggestion is to directly modify the image data and save it using IMWRITE . It may not be as simple as using IMSHOW and other graphical solutions, but I think that it is more efficient and gives more reliable and consistent results that are not so sensitive to different plot settings. For the example you are giving, I believe that 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([rc],y,x); im(index) = 0; im(index+r*c) = 0; im(index+2*r*c) = 0; imwrite(im,'image2.tif'); 
+20
Jun 08 '09 at 16:40
source share

I am expanding the Bessi solution here a bit. I found it very useful to know how to make the image occupy the entire figure and be able to tightly control the size of the output image.

 % 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 want to display the shape in the matrix, enter "help @ avifile / addframe" and then extract the subfunction called "getFrameForFigure". This is a feature provided by Mathworks that uses some (currently) undocumented ways to extract data from a drawing.

+4
Jun 08 '09 at 12:57
source share

Here is a completely different answer:

If you need an image file, why not just save the image instead of the whole image?

 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.

+4
Jun 08 '09 at 14:39
source share

You can use - noFigureWindows to disable all digits.

0
Mar 17 '15 at 21:54
source share



All Articles