How to extract specific frames from a video

Hey guys, does anyone know how to extract specific frames from a video? e.g, i want to extract 90, 150 and 160th frame and not any other frames. Does anyone know how to do it in a single for loop?
Right now i am using this code a=VideoReader('test.mp4'); for img = 1:a.NumberOfFrames; filename=strcat('frame',num2str(img),'.jpg'); b = read(a, img); imwrite(b,filename); end
But with this i can either extract all frames and not specific one.

 Respuesta aceptada

OCDER
OCDER el 1 de Ag. de 2018
Try this one. Seems like read is no longer recommended, and it's replaced by readFrame. I changed the variable names too, to tell you a description of the variable as opposed to a, b, c, etc.
ReadObj = VideoReader('test.mp4');
CurFrame = 0;
GetFrame = [90 150 160];
while hasFrame(ReadObj)
CurImage = readFrame(ReadObj);
CurFrame = CurFrame+1;
if ismember(CurFrame, GetFrame)
imwrite(CurImage, sprintf('frame%d.jpg', CurFrame));
end
end

5 comentarios

Thanks a lot. Code works and i get my desired framees
Ashray Inala
Ashray Inala el 25 de Sept. de 2019
How do i get frames at a particilar time.. like 6 seconds 14 seconds and 20 seconds..
Agostino Gibaldi
Agostino Gibaldi el 20 de Mzo. de 2020
Hi guys, the code works, but depending on the task at hand, can be slow.
Let's say I have a 2 hours video, and I want the frame at 1 hour and 59 min. With this approach I have to parse the whole video for nothing, just to get to the right point.
I could not find another wau, though... any idea from anyone?
Thank you,
A
Thanks a lot. Works perfectly. A thing more to add its if you want to extract all of frames from your video you need to change GetFrame vector as:
GetFrame = 1:1:TotalframesofYourVideo;
And TotalframesofYourVideo is the length Frames of your video that you can see click on ReadObj variable or:
TotalframesofYourVideo=ReadObj.NumFrames;
Best regards
Felipe
Imranbasha Syed
Imranbasha Syed el 24 de Mayo de 2022
I had a query?
what does the Get Frame vecotr does?? Can u explain?

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 31 de Jul. de 2018

Comentada:

el 24 de Mayo de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by