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

4 vues (au cours des 30 derniers jours)
Kip
Kip le 25 Fév 2012
Commenté : giannit le 26 Mar 2021
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.

Réponses (2)

Jan
Jan le 25 Fév 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 commentaires
Jan
Jan le 23 Mar 2021
Try to replace hardcopy() by
high = print('-RGBImage');
I cannot try it by my own currently.
giannit
giannit le 26 Mar 2021
Thank you @Jan very kind!
That print command substitute all the other lines of code right?
The drawback is that since it is print it is slow

Connectez-vous pour commenter.


Jiro Doke
Jiro Doke le 25 Fév 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 commentaires
Jan
Jan le 25 Fév 2012
export_fig is very good, but as far as I can see, it uses the slow way of print->imread also.
Jiro Doke
Jiro Doke le 26 Fév 2012
Oops, I didn't see the question about "being slow". I guess I'm slow.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by