How do I extract frames iteratively?

8 views (last 30 days)
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 Comment
Daniel Mendoza Hermosillo
Edited: Walter Roberson on 7 Apr 2022
% 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.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 7 Apr 2022
% 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 Comments
Walter Roberson
Walter Roberson on 7 Apr 2022
% 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.
Daniel Mendoza Hermosillo
Thank you this is what I was looking for

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!