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?
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