MATLAB Answers

"print figure" to variable... getframe, with better resolution...

37 views (last 30 days)
Kip
Kip on 25 Feb 2012
Commented: Patrick Tuohy on 7 Mar 2019
Short version: I want to read my current figure into a variable, with settable resolution. getframe only uses the screen resolution.
Long version: I plot lots of data, and generate "publication ready" png's often... which demand higher resolution (300dpi or better). I'd also like to change some defaults between what's on the screen and the .png file (cropping, color mods, transparency, in addition to the critical resolution).
My curret setup is: scatter(x,y,S,C); print('-dpng','-r300','plot.png'); imdata=imread('plot.png'); ...some manipulations to imdata... imwrite(imdata,'plot.png');
The above code (which saves the file, reads it, and resaves) is not only embarrassing, but slow for multiple images.
Any guidance is appreciated! PS: I do not have any toolboxes.

  0 Comments

Sign in to comment.

Answers (2)

Jan
Jan on 25 Feb 2012
Creating a copy of a figure in a higher resolution is time consuming, because a lot of work has to be done. You can reduce the time required by print by writing to a SSD or RAM-disk. Which OS are you using?
Another way is using the undocumented hardcopy, which is the core function of print. hardcopy replies an RGB array, but it demands for some specific modifications of the figure - otherwise Matlab crashs. I give a short example, which works with Matlab 6.5, 7.8 and 7.13 without any guarantee:
ResolutionStr = sprintf('-r%d', round(Resolution));
% Prepare figure for hardcopy:
drawnow;
fig_Renderer = get(FigH, 'Renderer');
fig_Paperposmode = get(FigH, 'PaperPositionMode');
fig_PaperOrient = get(FigH, 'PaperOrientation');
fig_Invhardcopy = get(FigH, 'InvertHardcopy');
set(FigH, ...
'PaperPositionMode', 'auto', ...
'PaperOrientation', 'portrait', ...
'InvertHardcopy', 'off');
% Create hard copy in high resolution:
% Simulate PRINT command (save time for writing and reading image file):
% Set units of axes and text from PIXELS to POINTS to keep their sizes
% independent from from the output resolution:
% See: graphics/private/preparehg.m
root_SHH = get(0, 'ShowHiddenHandles');
set(0, 'ShowHiddenHandles', 'on');
text_axes_H = [findobj(FigH, 'Type', 'axes'); ...
findobj(FigH, 'Type', 'text')];
pixelObj = findobj(text_axes_H, 'Units', 'pixels');
fontPixelObj = findobj(text_axes_H, 'FontUnits', 'pixels');
set(pixelObj, 'Units', 'points');
set(fontPixelObj, 'FontUnits', 'points');
% Set image driver:
if strcmpi(fig_Renderer, 'painters')
imageDriver = '-dzbuffer';
else
imageDriver = ['-d', fig_Renderer];
end
fig_ResizeFcn = get(FigH, 'ResizeFcn');
set(FigH, 'ResizeFcn', '');
% "Normal" is the only erasemode, which can be rendered!
% See: NOANIMATE.
EraseModeH = findobj(FigH, 'EraseMode', 'normal', '-not');
EraseMode = get(EraseModeH, {'EraseMode'});
set(EraseModeH, 'EraseMode', 'normal');
% Get image as RGB array:
high = hardcopy(FigH, imageDriver, ResolutionStr);
% Restore units of axes and text objects, and EraseMode:
set(pixelObj, 'Units', 'pixels');
set(fontPixelObj, 'FontUnits', 'pixels');
set(EraseModeH, {'EraseMode'}, EraseMode);
set(0, 'ShowHiddenHandles', root_SHH);
set(FigH, 'ResizeFcn', fig_ResizeFcn);

  3 Comments

Oliver Woodford
Oliver Woodford on 26 Feb 2012
The image format you choose to write to disk in also makes a difference. Sometimes the compression may be slower than writing a larger file.
Jan
Jan on 26 Feb 2012
The byte-order in the files matters also. I think BMPs are the fastest solution, if the disk is fast.
The posted code is from a function I have written for the creation of anti-aliased version of GETFRAME I'm using for the creation of an animation: For the export of 10'000 images, I need the maximum speed.
Patrick Tuohy
Patrick Tuohy on 7 Mar 2019
(years later) I'm working to solve a similar problem, trying to save large images from figure into a file. I'm not sure how to implement your code example Jan, or if it's even the best solution (or functional) in r2018b

Sign in to comment.


jiro
jiro on 25 Feb 2012
Take a look at export_fig which is a user-submitted file on File Exchange. I've used it, and it's good.

  2 Comments

Jan
Jan on 25 Feb 2012
export_fig is very good, but as far as I can see, it uses the slow way of print->imread also.
jiro
jiro on 26 Feb 2012
Oops, I didn't see the question about "being slow". I guess I'm slow.

Sign in to comment.


Translated by