Need to iterate through an array faster
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2019-1-29
编辑:Stephen23
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?
回答(1 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!