Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save a plotted image and maintain the original image size in MATLAB?

Tags:

plot

image

matlab

I'd like to show an image and plot something on it and then save it as an image with the same size as the original one. My MATLAB code is:

figH = figure('visible','off');
imshow(I);
hold on;
% plot something
saveas(figH,'1','jpg');
close(figH);

But the resulting image "1.jpg" has saved non-image areas in the plot as well as the image. How can I solve this problem?

like image 740
Tim Avatar asked Dec 04 '09 16:12

Tim


People also ask

How do I save a plotted image in MATLAB?

You can save plots as images or as vector graphics files using either the export button in the axes toolbar, or by calling the exportgraphics function.

How do I export a high quality figure in MATLAB?

To save a figure as an image at a specific resolution, call the exportgraphics function, and specify the 'Resolution' name-value pair argument. By default, images are saved at 150 dots per inch (DPI). For example, create a bar chart and get the current figure. Then save the figure as a 300-DPI PNG file.


1 Answers

The reason your new image is bigger than your original is because the SAVEAS function saves the entire figure window, not just the contents of the axes (which is where your image is displayed).

Your question is very similar to another SO question, so I'll first point out the two primary options encompassed by those answers:

  • Modify the raw image data: Your image data is stored in variable I, so you can directly modify the image pixel values in I then save the modified image data using IMWRITE. The ways you can do this are described in my answer and LiorH's answer. This option will work best for simple modifications of the image (like adding a rectangle, as that question was concerned with).

  • Modify how the figure is saved: You can also modify how you save the figure so that it better matches the dimensions of your original image. The ways you can do this (using the PRINT and GETFRAME functions instead of SAVEAS) are described in the answers from Azim, jacobko, and SCFrench. This option is what you would want to do if you were overlaying the image with text labels, arrows, or other more involved plot objects.

Using the second option by saving the entire figure can be tricky. Specifically, you can lose image resolution if you were plotting a big image (say 1024-by-1024 pixels) in a small window (say 700-by-700 pixels). You would have to set the figure and axes properties to accommodate. Here's an example solution:

I = imread('peppers.png');      %# Load a sample image
imshow(I);                      %# Display it
[r,c,d] = size(I);              %# Get the image size
set(gca,'Units','normalized','Position',[0 0 1 1]);  %# Modify axes size
set(gcf,'Units','pixels','Position',[200 200 c r]);  %# Modify figure size
hold on;
plot(100,100,'r*');             %# Plot something over the image
f = getframe(gcf);              %# Capture the current window
imwrite(f.cdata,'image2.jpg');  %# Save the frame data

The output image image2.jpg should have a red asterisk on it and should have the same dimensions as the input image.

like image 170
gnovice Avatar answered Oct 26 '22 10:10

gnovice