"print figure" to variable... getframe, with better resolution...
Mostrar comentarios más antiguos
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.
Respuestas (2)
Jan
el 25 de Feb. de 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);
8 comentarios
Oliver Woodford
el 26 de Feb. de 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
el 26 de Feb. de 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
el 7 de Mzo. de 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
Jan
el 23 de Mzo. de 2021
For R2019b try to remove all lines, which contain "EraseMode".
Jan
el 23 de Mzo. de 2021
Try to replace hardcopy() by
high = print('-RGBImage');
I cannot try it by my own currently.
Jiro Doke
el 25 de Feb. de 2012
0 votos
Take a look at export_fig which is a user-submitted file on File Exchange. I've used it, and it's good.
Categorías
Más información sobre Graphics Performance en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!