在MATLAB中加速帧数更新

时间:2017-08-23 07:26:36

标签: matlab video frame-rate

我在MATLAB中的视频中读到了这样的内容:

v = VideoReader('testvid.wmv')

cnt = 0;
while hasFrame(v)
    cnt = cnt + 1;
    video(cnt,:,:,:) = readFrame(v);
end

如果我查看视频对象,我告诉我视频是24帧。 但是,If会在读取之后直接显示它(所以在for循环中基本上imshow(readframe(v))它只会以每秒大约5帧的速度显示。

这就是为什么我像上面的代码一样编写它,以便框架预先存储到工作区中,现在我可以显示它们像

figure
for i=1:cnt
   tic
   imshow(squeeze(video(i,:,:,:)))
   toc
end

然而,我仍然只得到10帧 - 这个方向的MATLAB是否有限制?有没有更好的方法在MATLAB中以足够快的帧速率显示视频?

1 个答案:

答案 0 :(得分:2)

您可以更新您的情节CData,而不是每次都重新绘制它。

% Prepare 24 fake RGB coloured frames
A = randn(100,100,3,24);

figure

% Time the frame display time when replotting
tic
for k = 1 : 24
     h = imshow(A(:,:,:,k));
     drawnow
end
t = toc;
disp(t / 24)


% Time the frame display time when updating CData
tic
for k = 1 : 24
    if k == 1
        % Create the image object
        h = imshow(A(:,:,:,k));
    else
       % Update the Cdata property of image
       set(h , 'cdata' , A(:,:,:,k));
    end
    drawnow
end
t = toc;
disp(t / 24)

我的输出是:

0.0854

0.0093

所以我在更新CData时获得了十倍的改进。这实际上比每秒24帧更快!