问题
Are there any alternatives to using getframe
and saveas
for saving the contents of a figure to a raster image for further processing?
Approach 1: getframe
h = figure(\'visible\', \'off\');
a = axes(\'parent\', h);
% render using `scatter3()` or other plot function.
content = frame2im(getframe(h));
This has the serious drawback of showing the figure to perform a screen capture in the call to getframe()
and it is problematic when performing such a render in a loop (i.e. saving content
at each iteration as a video frame).
Approach 2: saveas
h = figure(\'visible\', \'off\');
a = axes(\'parent\', h);
% render using `scatter3()` or other plot function.
saveas(h, \'/path/to/file.png\');
content = imread(/path/to/file.png\');
This approach has the serious drawback of writing to disk, which is problematic in multithreaded applications, as well as being slower than rendering directly to memory. Since saveas()
will obviously render to memory before invoking the PNG encoder, what I want is possible, but I can\'t find any function it in the MATLAB documentation that only performs the rendering part.
Question:
Does you know of an alternate way of rendering an arbitrary axes
content to a raster image?
回答1:
If you create an avi file with avifile
, and then add frames to it with addframe
, MATLAB doesn't open up extra visible figures like it does with getframe
.
avi = avifile('/path/to/output');
figure_handle = figure('visible', 'off');
% ...
for something = 1:1000
cla
% (draw stuff...)
avi = addframe(avi, figure_handle);
end
回答2:
I realize this is an old thread, but I ran into this problem again lately, so I wanted to summarize my findings. My main source is this page (cached). According to it, there are three alternatives:
using ADDFRAME directly with the figure handle (without using GETFRAME). This is exactly what @rescdsk has shown in his answer.
hFig = figure('Visible','off'); aviobj = avifile('file.avi'); for k=1:N %#plot(...) aviobj = addframe(aviobj, hFig); end aviobj = close(aviobj);
using PRINT/SAVEAS/HGEXPORT to export the figure to an image file, then reading the image back from disk. This is approach#2 that you listed yourself in the question above.
hFig = figure('Visible','off'); set(hFig, 'PaperPositionMode','auto', 'InvertHardCopy','off') aviobj = avifile('file.avi'); for k=1:N %#plot(...) print(['-f' num2str(hFig)], '-zbuffer', '-r0', '-dpng', 'file.png') img = imread('file.png'); aviobj = addframe(aviobj, im2frame(img)); end aviobj = close(aviobj);
using the undocumented HARDCOPY function to capture the figure in-memory.
hFig = figure('Visible','off'); set(hFig, 'PaperPositionMode','auto') aviobj = avifile('file.avi'); for k=1:N %#plot(...) img = hardcopy(hFig, '-dzbuffer', '-r0'); aviobj = addframe(aviobj, im2frame(img)); end aviobj = close(aviobj);
In fact, this is the underlying function that other functions use either directly or indirectly. By inspecting the source codes where possible, here is an illustration of the dependencies of the related functions where
A --> B
denotesA calls B
:saveas [M-file] --> print [M-file] --> render [private M-file] --> hardcopy [P-file] hgexport [P-file] --> print [M-file] --> ... @avifile/addframe [M-file] --> hardcopy [P-file]
On the other hand, GETFRAME does not call HARDCOPY but an undocumented built-in function named CAPTURESCREEN (although it seems that it will be using PRINT for the upcoming HG2 system where there is a new
-RGBImage
print flag):getframe [M-file] --> capturescreen [builtin]
Note: Since AVIFILE is now deprecated, you can replace it with the newer VIDEOWRITER in (2) and (3), but not in (1) since it does not support passing figure handle directly.
回答3:
Start MATLAB in headless mode: matlab -noFigureWindows
MATLAB is running in headless mode. Figure windows will not be displayed.
then simply plot and save the figures as usual (you won't see any graphical output of course). Example:
surf(peaks);
print output.eps %# SAVEAS works as well
close
I tested the above on a Windows machine running R2010a. I don't have access to a Unix machine right now, but I answered a similar question in the past, and it worked just fine at the time (you will need to unset the $DISPLAY
variable before starting MATLAB)
EDIT
Another option, in case you want to keep your normal workspace, is to start a new MATLAB instance in the background which will generate and save the plots (source).
Run this from the command prompt of your current MATLAB session (all on the same line):
!start /B /MIN matlab -noFigureWindows
-automation
-r "cd('c:\yourpath'); myscript; quit"
This will start a new MATLAB session in the background (using COM Automation), and execute a script called myscript
(a simple M-file) that contains all your plotting code:
c:\yourpath\myscript.m
surf(peaks);
saveas(gcf, 'output.eps');
回答4:
With avifile
being deprecated, this is how you do it with VideoWriter:
hFig = figure('Visible','off');
set(hFig, 'PaperPositionMode','auto')
aviobj = VideoWriter('file','Archival');
for k=1:N
%#plot(...)
img = hardcopy(hFig, '-dzbuffer', '-r0');
writeVideo(aviobj, im2frame(img));
end
close(aviobj);
来源:https://stackoverflow.com/questions/4137628/render-matlab-figure-in-memory