How do I extract frames iteratively?

10 vues (au cours des 30 derniers jours)
Daniel Mendoza Hermosillo
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 commentaire
Daniel Mendoza Hermosillo
Modifié(e) : Walter Roberson le 7 Avr 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.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 7 Avr 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 commentaires
Walter Roberson
Walter Roberson le 7 Avr 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

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by