首页 > 解决方案 > 在循环内显示图像时减少循环开销

问题描述

我正在循环触发视频,利用它的数据,并使用imshow. 外置摄像头有 30 fps,但是imshow在循环中使用时,它会大幅降低帧速率到 5-7 fps 左右。有解决方法吗?以下是片段:

Vid = videoinput('some_cam',1);
triggerconfig(Vid, 'manual');
Vid.FramesPerTrigger = 1;
Vid.TriggerRepeat = inf;
viewer = vision.DeployableVideoPlayer();

start(Vid);
himg = figure;
while ishandle(himg)
    trigger(Vid);
    [image, timeStamp] = getdata(Vid);
    imshow(image); %This line significantly reduce the performance
    %Do something with the images...This part is important and would need as many frame rate as possible
end

所以我在想,如果我发送imshow到另一个线程上运行呢?但在这种情况下,我不知道该怎么做,因为我需要访问image. 预览或任何其他命令在我的情况下都不起作用,因为我必须image事先进行一些更改。

标签: imagematlabloopsmatlab-figure

解决方案


在 MATLAB 中加速动画图形的方法是不使用imshowor之类的函数plot来清除坐标轴、创建新的图形对象、调整坐标轴属性等。这些函数在幕后做了很多不必要的工作在屏幕上制作动画时。

相反,修改现有的图形对象:

start(Vid);
hfig = figure;
trigger(Vid);
[image, timeStamp] = getdata(Vid);
himg = imshow(image);
while ishandle(hfig)
    trigger(Vid);
    [image, timeStamp] = getdata(Vid);
    himg.CData = image; % update the existing graphics object
    %...
end

对于非常旧版本的 MATLAB,请使用set(himg,'CData',image).

您可能需要drawnow在循环内添加一个以查看图像更改。


推荐阅读