How do I extract frames iteratively?

8 次查看(过去 30 天)
So far I have been able to trim the video and went from 4007 frames to 1509 frames, from those frames I only want to get every 3rd frame starting from the first. Currently I only know how to trim the video and to check how much frames the video has not much else.
  1 个评论
Daniel Mendoza Hermosillo
编辑:Walter Roberson 2022-4-7
% Import video file
obj = VideoReader('D:\Spring 2022\MARS Capstone\Trimmed Videos\Reef 1\GH010063.MP4');
vid = read(obj);
% Read the total number of frames
frames = obj.NumFrames;
% File format of the frames to be saved
ST = ' .jpg';
% Reading and writing the frames
for x = 1 : frames
% Converting integers to string
Sx = num2str(x);
% Concatening 2 string
Strc = strcat(Sx, ST);
vid = vid(:, :, :, x);
% Exporting the frames
imwrite(vid, Strc);
end
this is a code i found online that helps me write the frames into images that I can use later but it stops because it needs 23.2 GB storage to run, so I wanted to reduce the number of frames that I pick up from this code.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2022-4-7
% Import video file
obj = VideoReader('D:\Spring 2022\MARS Capstone\Trimmed Videos\Reef 1\GH010063.MP4');
% File format of the frames to be saved
ST = ' .jpg';
% Reading and writing the frames
i = 1;
while hasframe(obj)
% Converting integers to string
Sx = num2str(x);
% Concatening 2 string
Strc = strcat(Sx, ST);
vid = readframe(obj);
% Exporting the frames
imwrite(vid, Strc);
i = i + 1;
end
  3 个评论
Walter Roberson
Walter Roberson 2022-4-7
% Import video file
obj = VideoReader('D:\Spring 2022\MARS Capstone\Trimmed Videos\Reef 1\GH010063.MP4');
% File format of the frames to be saved
ST = ' .jpg';
% Reading and writing the frames
x = 0;
while hasframe(obj)
vid = readframe(obj);
x = x + 1;
if mod(x, 3) ~= 1; continue; end
% Converting integers to string
Sx = num2str(x);
% Concatening 2 string
Strc = strcat(Sx, ST);
% Exporting the frames
imwrite(vid, Strc);
end
It is also possible to ask to read() a particular frame by frame index, but I suspect this code would be faster.

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by