MATLAB Answers

Need to iterate through an array faster

7 ビュー (過去 30 日間)
hector martinez
hector martinez 2019 年 1 月 29 日
コメント済み: hector martinez 2019 年 1 月 29 日
I'm currently reading frames from one video and writing them to another, I have a huge bottleneck in my for loop.
vidObj = VideoReader('inputVideo.wmv');
outputVideo = VideoWriter('outputVideo.avi');
outputVideo.FrameRate = 24;
open(outputVideo);
%Change FrameRate without changing video length
numberFrames=vidObj.Duration * vidObjB.FrameRate;
Frames=(1:vidObjB.Framerate/24:numberFrames);
Frames=round(Frames);
for i=1:length(Frames)
nextFrame=read(vidObjB,Frames(i));
writeVideo(outputVideo,nextFrame);
fprintf('%d\n',i);
end
close(outputVideo)
Is there a way to pass Frames directly without indexing? Python allows you to do 'for item in list....' is there something in matlab that does the same?

  2 件のコメント

Stephen Cobeldick
Stephen Cobeldick 2019 年 1 月 29 日
"Is there a way to pass Frames directly without indexing? Python allows you to do 'for item in list....' is there something in matlab that does the same?"
You are decompressing and compressing video data, and you imagine that changing the loop index will make a difference: why do you think that indexing is the bottleneck in your code?
hector martinez
hector martinez 2019 年 1 月 29 日
In messing with my code some more I've realized indexing is not the issue. Passing the frame number directly
for k=1:vidObj.FrameRate/24:numberFrames
nextFrame=read(vidObjB,round(k));
writeVideo(outputVideo,nextFrame);
fprintf('%d\n',round(k));
makes no difference.
In a previous exercise the individual frames were already available in a folder. I didn't realize reading from a video file would take a lot longer.

サインインしてコメントする。

回答 (1 件)

OCDER
OCDER 2019 年 1 月 29 日
Do not use read. Use readFrame instead, since read will re-read everything from beginning to end.
i = 1;
while hasFrame(vidObjB)
writeVideo(outputVideo, readFrame(vidObjB));
fprintf('%d\n', i);
i = i + 1;
end
close(outputVideo)
If that doesn't work, are you using a solid state hard drive, or a spinning magnetic hard drive? The SSD might speed things up as this could be a read/write speed issue. Otherwise the compression/decompression could be the slow step.

  1 件のコメント

hector martinez
hector martinez 2019 年 1 月 29 日
The reason I am using read instead of readFrame is because I need to access specific frames to be able to change the framerate without changing the video length. As far as I know readFrame doesn't allow reading specific frames.
But your comment about ssd vs magnetic drives is a valid point. I was doing all my testing on a machine with a magnetic drive with an elapsed time of 337.96 sec, the final version will run on a machine with an ssd. I tested my this code on that machine and the elapsed time is 13.03 seconds. So not really an issue anymore.
Thank you for making that point.

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by