在内存中渲染 MATLAB 图 [英] Render MATLAB figure in memory

查看:31
本文介绍了在内存中渲染 MATLAB 图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有使用 getframesaveas 将图形内容保存到光栅图像以供进一步处理的替代方法?

方法一:getframe

h = figure('visible', 'off');a = 轴('父',h);% 使用 `scatter3()` 或其他绘图函数进行渲染.内容 = frame2im(getframe(h));

这有一个严重的缺点,即在调用 getframe() 时显示图形以执行屏幕捕获,并且在循环中执行此类渲染时会出现问题(即保存 content 在每次迭代时作为一个视频帧).

方法二:saveas

h = figure('visible', 'off');a = 轴('父',h);% 使用 `scatter3()` 或其他绘图函数进行渲染.saveas(h, '/path/to/file.png');content = imread(/path/to/file.png');

这种方法有写入磁盘的严重缺点,这在多线程应用程序中是有问题的,并且比直接渲染到内存更慢.由于 saveas() 显然会在调用 PNG 编码器之前渲染到内存,所以我想要的是可能的,但是我在 MATLAB 文档中找不到任何只执行渲染部分的函数.>

问题:

您是否知道将任意 axes 内容渲染为光栅图像的替代方法?

解决方案

如果你用avifile创建一个avi文件,然后用addframe向其中添加帧,MATLAB不会像使用 getframe 那样打开额外的可见图形.

avi = avifile('/path/to/output');Figure_handle = figure('可见', '关闭');% ...对于某事 = 1:1000类%(画东西...)avi = addframe(avi, figure_handle);结尾

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

这篇关于在内存中渲染 MATLAB 图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆