save image with plotings

1 次查看(过去 30 天)
Yanqin
Yanqin 2012-10-18
Dear Sir/Madam,
How could I save an image with plotting labels on it and keep the same size of the image?
For example, the image size wiht labels:1024x768, once I save it by using "saveas" or "print", the saved image size becomes:1200x900.
What cause the change of the image size?
Best regards,

回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2012-10-18
set(gcf, 'PaperUnits', 'points','PaperPosition', [0 0 768 1024]);
saveas(gcf,'fig1.jpg')
  3 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2012-10-19
how did you check the size
Image Analyst
Image Analyst 2012-10-19
I imagine he read the image back in with imread().

请先登录,再进行评论。


Image Analyst
Image Analyst 2012-10-18
编辑:Image Analyst 2012-10-19
Use export_fig() as recommended in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_save_my_figure.2C_axes.2C_or_image.3F_I.27m_having_trouble_with_the_built_in_MATLAB_functions. However, even export_fig apparently cannot guarantee exact final image sizes. But that doesn't really matter since anyplace where you are going to use it (MS Office apps, etc.) will let you resize it).
If you want to "burn" text into images, then use imtext() in the Computer Vision System Toolbox. Then you can save the image directly, instead of the figure, and you'll have the text and image in their original pixel sizes.
format compact;
close all;
plot(rand(10,1), 'bo-', 'LineWidth', 3);
grid on;
% set(gcf, 'PaperUnits', 'points','PaperPosition', [0 0 768 1024]);
filename = 'plot.png';
% Save image at whatever size MATLAB decided to make it.
export_fig(filename, '-native');
imageReadBackIn = imread(filename);
[rows columns numberOfColorChannels] = size(imageReadBackIn)
delete(filename);
message = sprintf('This image is %d rows by %d columns.\nNote the small sizes, and then\nclick OK to enlarge it.',...
rows, columns);
title(message);
uiwait(msgbox(message));
% Enlarge figure to full screen.
set(gcf, 'Units', 'Pixels','Menu', 'None');
width = 1000;
height = 800;
set(gcf, 'Position', [0 0 width height]);
% set(gcf, 'Units', 'Pixels', 'Position', [0 0 600 1000], 'Menu', 'None');
export_fig(filename, '-native');
imageReadBackIn = imread(filename);
[rows columns numberOfColorChannels] = size(imageReadBackIn)
% figure;
% imshow(imageReadBackIn);
% set(gcf, 'Units', 'Pixels','Menu', 'None');
delete(filename);
message = sprintf('Now, the image is %d rows by %d columns',...
rows, columns);
title(message);
uiwait(msgbox(message));
  1 个评论
Yanqin
Yanqin 2012-10-24
Thank you so much.
I tired your code, the size of image does change with the Width/Height settings. The key is how to choose the Width/Height in order to get the desired size, say 1024x768? Do you have any suggestion

请先登录,再进行评论。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by