加速 MATLAB 中图形更新的帧速率
Accelerate framerate for figure update in MATLAB
我在 MATLAB 中读了这样一个视频:
v = VideoReader('testvid.wmv')
cnt = 0;
while hasFrame(v)
cnt = cnt + 1;
video(cnt,:,:,:) = readFrame(v);
end
如果我查看视频对象,我会告诉我该视频有 24 帧。
但是,如果会在阅读后直接显示它(所以基本上 imshow(readframe(v))
在 for 循环内它只会以每秒大约 5 帧的速度显示。
这就是为什么我像上面的代码一样编写它,以便将帧预存储到工作区中,现在我可以像这样显示它们
figure
for i=1:cnt
tic
imshow(squeeze(video(i,:,:,:)))
toc
end
但是,我仍然只能得到 10 帧 - MATLAB 在这方面有限制吗?有没有更好的方法在 MATLAB 中以足够快的帧速率显示视频?
您可以更新绘图 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 帧更快!
我在 MATLAB 中读了这样一个视频:
v = VideoReader('testvid.wmv')
cnt = 0;
while hasFrame(v)
cnt = cnt + 1;
video(cnt,:,:,:) = readFrame(v);
end
如果我查看视频对象,我会告诉我该视频有 24 帧。
但是,如果会在阅读后直接显示它(所以基本上 imshow(readframe(v))
在 for 循环内它只会以每秒大约 5 帧的速度显示。
这就是为什么我像上面的代码一样编写它,以便将帧预存储到工作区中,现在我可以像这样显示它们
figure
for i=1:cnt
tic
imshow(squeeze(video(i,:,:,:)))
toc
end
但是,我仍然只能得到 10 帧 - MATLAB 在这方面有限制吗?有没有更好的方法在 MATLAB 中以足够快的帧速率显示视频?
您可以更新绘图 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 帧更快!