用matlab提取视频帧
extracting frames of video with matlab
我正在尝试使用此代码提取某些视频的所有帧(每个视频包含大约 13000 帧),但提取所有帧所需的时间太长。有没有更快捷的方法?
filename = 'S.avi';
mov = VideoReader(filename);
% Output folder
outputFolder = fullfile(cd, 'S');
mkdir(outputFolder);
%getting no of frames
framee=100000;
numberOfFrames = mov.NumberOfFrames;
numberOfFramesWritten = 0;
for frame = 1:12525
thisFrame = read(mov, frame);
framee=framee+1;
outputBaseFileName = sprintf('%3.3d.png', framee);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
imwrite(thisFrame, outputFullFileName, 'png');
% progressIndication = sprintf('Wrote frame %4d of %d.', frame,numberOfFrames);
% disp(progressIndication);
numberOfFramesWritten = numberOfFramesWritten + 1;
end
progressIndication = sprintf('Wrote %d frames to folder "%s"',numberOfFramesWritten, outputFolder);
disp(progressIndication);
从概念上讲,这与您所做的并没有太大不同,但是您的代码中有许多迂回步骤。
大概你可以这样做:
mov = VideoReader(filename);
vidFrames = read(mov);
将您的电影收集为 4D 数组,然后就可以了
for frame = 1 : size(vidFrames, 4)
outputBaseFileName = sprintf('%3.3d.png', frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
imwrite( vidFrames(:,:,:,frame), outputFullFileName, 'png');
end
而且它可能会更快。此外,您可能会使用 parfor
(或使用计数器函数句柄的 spmd
)来进一步并行化
我正在尝试使用此代码提取某些视频的所有帧(每个视频包含大约 13000 帧),但提取所有帧所需的时间太长。有没有更快捷的方法?
filename = 'S.avi';
mov = VideoReader(filename);
% Output folder
outputFolder = fullfile(cd, 'S');
mkdir(outputFolder);
%getting no of frames
framee=100000;
numberOfFrames = mov.NumberOfFrames;
numberOfFramesWritten = 0;
for frame = 1:12525
thisFrame = read(mov, frame);
framee=framee+1;
outputBaseFileName = sprintf('%3.3d.png', framee);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
imwrite(thisFrame, outputFullFileName, 'png');
% progressIndication = sprintf('Wrote frame %4d of %d.', frame,numberOfFrames);
% disp(progressIndication);
numberOfFramesWritten = numberOfFramesWritten + 1;
end
progressIndication = sprintf('Wrote %d frames to folder "%s"',numberOfFramesWritten, outputFolder);
disp(progressIndication);
从概念上讲,这与您所做的并没有太大不同,但是您的代码中有许多迂回步骤。
大概你可以这样做:
mov = VideoReader(filename);
vidFrames = read(mov);
将您的电影收集为 4D 数组,然后就可以了
for frame = 1 : size(vidFrames, 4)
outputBaseFileName = sprintf('%3.3d.png', frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
imwrite( vidFrames(:,:,:,frame), outputFullFileName, 'png');
end
而且它可能会更快。此外,您可能会使用 parfor
(或使用计数器函数句柄的 spmd
)来进一步并行化