绘图中的像素:getframe 太慢

Pixels from plot: getframe is too slow

我需要做以下事情:

我做的是

function getPixels(state)
    fig = figure('visible','off')
    hold all
    plot_environment()              % calls patch and other stuff 
    plot(state(1),state(2),'r+')
    f = getframe();
    data = f.cdata;
    close(fig)

问题是它很慢(0.6s 这对我来说实在是太慢了)。 我尝试使用 persistent fig,我可以降到 0.4s,但还是太多了。

我阅读了有关使用 printhardcopy 的信息,但没有帮助。即使将像素数减少 -r201/5 我的默认大小)也没有加快速度。

有什么建议吗?有没有更快的方法获取像素?

编辑:其他详细信息

state 只是一个二维点。

环境由一些用于绘制形状的固定已知变量定义。更具体地说,我有几点

points = [c11 c12
           c21 c22
           .....]

用于修补矩形、圆形和三角形。为此,我使用 patchcircles.

所以最后我想将所有内容绘制在一起并得到结果 像素 。有没有不用 getframe 或加快速度的方法?

完整示例

需要circles.

启动tic, getPixels([0.1, 0.2]'); toc 平均需要 0.43sgetframe 命令单独需要 0.29s.

function data = getPixels(state)

fig = figure('visible','off');
hold all

c1 = [0.1 0.75; 
    0.45 0.75];
c2 = [0.45 0.4; 
    0.45 0.8];

radius = 0.1;

grey = [0.4,0.4,0.4];

% Circles
p = [c1; c2];
circles(p(:,1), p(:,2), radius, 'color', grey, 'edgecolor', grey)

% Rectangles
patch([0.1 0.45 0.45 0.1], [0.65 0.65 0.85 0.85], grey, 'EdgeAlpha', 0)
patch([0.35 0.55 0.55 0.35], [0.4 0.4 0.8 0.8], grey, 'EdgeAlpha', 0)

% Triangle
x = [0.95, 1.0, 1.0];
y = [1.0, 0.95, 1.0];
fill(x, y, 'r')

axis([0 1 0 1])

box on
axis square

% Point
plot(state(1),state(2),'ro','MarkerSize',8,'MarkerFaceColor','r'); 

f = getframe();
data = f.cdata;
close(fig)

您可以将 Matlab 的 getframe() 函数的执行时间减少十倍。诀窍在于每次调用 getPixels() 函数 不是创建 图形,而是使用 现有图形。您可以通过 函数参数传递图形句柄 。并使用 Matlab 的函数 clf 清除两次调用之间的当前数字 window。


编辑

这是我玩 figuregetframe 的例子。

以下性能图表

给出
%%

clear al
close all
clc

nbSim = 10  %number of getframe calls
tElapsed = zeros(nbSim, 2); %two types of getting frames


%% METHOD 1: figure within loop

for ind_sim = 1:nbSim

    fig = figure;

    %some graphical elements
    hold all
    patch(rand(1,4), rand(1,4), rand(1,3), 'EdgeAlpha', 0)
    patch(rand(1,4), rand(1,4), rand(1,3), 'EdgeAlpha', 0)
    fill(rand(1,3), rand(1,3), 'r')
    plot(rand,rand,'ro','MarkerSize',8,'MarkerFaceColor','k');

    %some axes properties
    axis([0 1 0 1])
    box on
    axis square

    tStart = tic;
    f = getframe();
    tElapsed(ind_sim,1) = toc(tStart);
    data = f.cdata;
    close(fig)

end


%% METHOD 2: figure outside loop

fig = figure;
for ind_sim = 1:nbSim

    %some graphical elements
    hold all
    patch(rand(1,4), rand(1,4), rand(1,3), 'EdgeAlpha', 0)
    patch(rand(1,4), rand(1,4), rand(1,3), 'EdgeAlpha', 0)
    fill(rand(1,3), rand(1,3), 'r')
    plot(rand,rand,'ro','MarkerSize',8,'MarkerFaceColor','k');

    %some axes properties
    axis([0 1 0 1])
    box on
    axis square

    tStart = tic;
    f = getframe();
    tElapsed(ind_sim,2) = toc(tStart);
    data = f.cdata;
    clf

end
close(fig)

%% plot results

plot(tElapsed);
set(gca, 'YLim', [0 max(tElapsed(:))+0.1])
xlabel('Number of calls');
ylabel('Execution time');
legend({'within (method 1)';'outside (method 2)'});
title('GetFrame exectution time');

您必须放弃图形的创建,即使它被声明为不可见。这会缩短执行时间。