在内存中渲染MATLAB图 [英] Render MATLAB figure in memory
问题描述
是否有使用 getframe
和 saveas
的替代方法,用于将图形的内容保存为光栅图像进一步处理?
Are there any alternatives to using getframe
and saveas
for saving the contents of a figure to a raster image for further processing?
方法1: getframe
Approach 1: getframe
h = figure('visible', 'off');
a = axes('parent', h);
% render using `scatter3()` or other plot function.
content = frame2im(getframe(h));
这有一个严重的缺点,即显示图形在<$ c的调用中执行屏幕捕获$ c> getframe()并且在循环中执行这样的渲染时会出现问题(即在每次迭代时将内容
保存为视频帧) 。
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).
方法2: saveas
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');
这种方法存在写入磁盘的严重缺点,这在多线程应用程序中是有问题的,以及比直接渲染到内存慢。由于 saveas()
显然会在调用PNG编码器之前呈现给内存,我想要的是可能的,但我在MATLAB文档中找不到任何只执行它的函数渲染部分。
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.
问题:
你知道另一种方式吗?将任意轴
内容渲染到光栅图像?
Does you know of an alternate way of rendering an arbitrary axes
content to a raster image?
推荐答案
如果你使用 avifile
创建一个avi文件,然后用 addframe
添加框架,MATLAB不会打开额外的可见数字与 getframe
一样。
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
这篇关于在内存中渲染MATLAB图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!