how to convert video in to image frames using matlab?
Mostrar comentarios más antiguos
how to convert video in to image frames using matlab?
5 comentarios
Rose Mohammed
el 6 de Abr. de 2019
Hi, how to devide video sequence to groups of pictures? And the type of frame like i frame or p frame, is the type is specified already or i specify which frame is i and which frame is p, and how i implement this in matlab as code? Please help me...
Walter Roberson
el 6 de Abr. de 2019
Rose Mohammed:
"i", "p", "b" frames are only used by some video codecs.
The code doing the encoding of frames compares the current frame to a previous frame (and possibly a following frame) and decides how much change there has been. If it decides that there has been so much change that describing the changes would take more space than just sending the complete frame, then it sends the complete frame, marking it in the file as an "i" frame. There is not necessarily anything special in terms of content for the frame: it would simply have changed enough relative to the previous one to be hard to describe the changes in a compact form.
If a frame has mostly the same content as a previous frame, then sometimes it can be more efficient to describe the changes. For example if someone blinks, then you might only have to send enough information to describe the pixels where the eyes are, with everything else the same. Frames that can be described efficiently in terms of the content of the frame before are encoded into the file as "p" frames that just describe the changes to the frame "previous".
Some video standards require sending "i" frames at fixed intervals even if not much has changed. That makes it practical to seek into the video stream: instead of having to find an "i" frame and decode a possibly long list of changes, you can just advance every so-many frames (usually 6) to find a frame close to where you want to start, and decode the little bit from there.
But if you place "i" frames at fixed intervals then because a big change is not always right at that interval, you might want to refer to the next "i" frame and describe changes relative to it instead of to the previous "i" frame. Or you might want to use information from both, using a "b" (both) frame that describes changes from the previous and next frame. Again, there is not necessarily anything special about the frames that lead to this, and can simply be due to accumulated comparatively small changes.
If you think about this for a moment, you will realize that the kind of encoding used for any given frame depends upon what has happened in previous frames more than what is in the frame itself. If you only have one frame in front of you, you would have to encode it as an "i" frame. If you have several frames, then how you encode this one depends on how much change there is relative to previous frames.
The next thing you need to know is that all of the video readers provided by Mathworks will only return to you fully decoded frames. None of them will tell you how any given frame was encoded in the video file or video stream. And, as I indicated above, you cannot tell just by looking at the frame itself.
In order to find out how any given frame was encoded in the file or stream, you would need to call upon third-party decoding libraries and ask them to give you information about the file or stream. Some of the libraries you could investigate are "libmpeg" and "libmp4".
Rose Alhasany
el 11 de Abr. de 2019
Walter Roberson thank you alot for this useful description....prof my codec is H.264 and i read that in this codec the group of picture (GOP) size (no. of picture per group) must be specified and depending on it the encoder know the i frame which is the first frame in the GOP...beside that the pattern oF GOP must be also specified like (IBBPBBPBB) GOP sequence for example...
so prof is there algorithm, method or code to specify the GOP ?
please advice me...
Walter Roberson
el 11 de Abr. de 2019
H.264 is generally adaptive to determine GOP structure.
That said, there is also choice in the matter. For some purposes, the exact structure can be important and GOP that follow a fixed pattern can be generated. There are tradeoffs to be made for bitrate considerations (how fast does your media read information?); or for CPU speed considerations (slower CPUs are less expensive and might produce less heat, but require less complicated video streams); or for how quickly you can seek to arbitrary points (e.g. chapter search or user fast forward or (more difficult) reverse)
Rose Mohammed
el 12 de Abr. de 2019
Thank you very much prof
Respuestas (3)
Lalit Patil
el 7 de En. de 2013
vid=mmreader('video.avi');
numFrames = vid.NumberOfFrames;
n=numFrames;
for i = 1:2:n
frames = read(vid,i);
imwrite(frames,['Image' int2str(i), '.jpg']);
im(i)=image(frames);
end
14 comentarios
Namith VN
el 17 de Mzo. de 2016
Hi. Suppose I need a single frame from each second of a video, how can I modify this code for the same?
mh sarah
el 23 de En. de 2017
hi i have this error and i dont know what can i do please help me
vid = VideoReader('C:\Users\Mohammed\Desktop\New folder\twins.avi');
>> numFrames = vid.CurrentTime;
n = numFrames;
for i = 1:2:n
frames = readframe(vid,i);
imwrite(frames,['Image' int2str(i), '.jpg']);
im(i)=image(frames);
end
No appropriate method, property, or field CurrentTime for class
VideoReader.
Image Analyst
el 23 de En. de 2017
Take the semicolon off the end of the call to VideoReader and see what properties and methods it reports to the command window.
Walter Roberson
el 23 de En. de 2017
mh sarah, which MATLAB release are you using?
When CurrentTime is present, it is going to be initialized to 0. The part of the documentation of the NumberOfFrames property that suggests using CurrentTime is really suggesting that you loop through reading the entire video to find out how many frames are really present.
If you know for sure that you are using a video with fixed framerate, then you can use the Duration property and the FrameRate property to calculate the number of frames. This is, however, not accurate for variable-rate frames. To find the number of frames in a variable-rate frame video you have to loop and count them.
mh sarah
el 23 de En. de 2017
tanks it ok but i still have probleme i dont wont matlab to read a video befor because hi take a lot of time befor making the process and i wont to get all frames that is my new code:
Vid = VideoReader('twins.avi');
numFrames = vid.NumberOfFrames;
n=numFrames;
for i = 1:2:n
frames = read(vid,i);
imwrite(frames,['Image' int2str(i), '.pgm']);
im(i)=image(frames);
end
Image Analyst
el 23 de En. de 2017
If you don't want it to read a video, then why are you calling VideoReader()??? And how can you get all the frames from the video if you don't read the video?
Walter Roberson
el 23 de En. de 2017
I think the meaning is that they do not want to do a loop through reading frame by frame to count the number of frames.
Vidya Menon
el 26 de En. de 2017
imwrite(frames,['Image' int2str(i), '.pgm']) can u explain what this line of code does ?
Walter Roberson
el 26 de En. de 2017
int2str(i) formats the numeric value i as an character string that represents an integer. So, for example, changing 14 (numeric) to '14' (string)
[] around a series of strings is horzcat(), horizontal concatenation. So first take the string 'image' and then follow it by the formatted numeric value, and then add the string '.pgm' . This will be the file name to write into.
Then imwrite() is called, passing in frame as the first arguement, and passing the file name into the second argument. This call instructs MATLAB to write out the data contained in frame as the .pgm file whose name is given by the string expression that was built up by ['Image' int2str(i), '.pgm'])
Image Analyst
el 26 de En. de 2017
If you're used to the more "C-like" way of sprintf(), you can do it this way:
baseFileName = sprintf('Image%d.pgm', i); % Name without path in front of it.
fullFileName = fullfile(pwd, baseFileName); % Prepend current folder
imwrite(frames, fullFileName);
William Meaney
el 31 de Oct. de 2017
- I want to get screenshots every quarter second. How would I modify the code to do this?
- Also, must the initial video be an avi or can it be a mp4?
- Would this code run in GNU Octave, an open source of matlab?
- Can i set the output directory of the screenshots to my desktop??
- Can I set the directory for teh video file to be my desktop?
- I need this for a school project due first thing on thursday, so can i please get an answer sometime during weds? I don't know matlab, so instead of tellimg me teh general commands, I need the code to be modified so I can simply copy and paste it into GNU octave. Before onyone says that's plagerism, this is not for a matlab projcet - its for an ap class in which i need to cretae sprites that can move fluently for a scratch project. Thanks
Walter Roberson
el 31 de Oct. de 2017
Sorry, you will need to ask about Octave video reading in an Octave resource; details like supported file formats or checking frame times are not expecting to be the same in Octave.
BHANU SRINIVASA
el 27 de En. de 2020
can i get this mmreader file
im not able to find on the net
Walter Roberson
el 27 de En. de 2020
mmreader was removed sometime around 2011. You can use VideoReader to replace it in most cases.
Image Analyst
el 17 de Mzo. de 2016
1 voto
Namith:
You can use the VideoReader class to set the starting time of the video, then use read() to extract a frame from the video at exactly that time. Then use imwrite() to save it to disk. Attached is a demo that does something similar (but not exactly what you want). The demo writes out every single frame to files. You just need to set the time with the method for doing that before you extract the frame.
7 comentarios
Abhay Mathur
el 10 de Jun. de 2018
Thanks a lot for this code. It's great.
Image Analyst
el 9 de Jul. de 2018
In the loop just call imwrite() to write the binary images to a file. You might have to call im2uint8() or something on the logical images to get them to be uint8 images.
Just today I uploaded a new program to the File Exchange that can take a series of still images (for example from a time lapse series of photos or any other collection of still images) and make a movie out of them. See https://www.mathworks.com/matlabcentral/fileexchange/67974-makemovie
Image Analyst
el 10 de Jul. de 2018
What type are your images? If they're indexed, convert your image from indexed to color with ind2rgb().
rrv
el 10 de Jul. de 2018
Thanks for you help.
Randall Ang
el 10 de Mayo de 2021
Hi, I tried using the demo that you provided, and it worked well. However, is there a way to ensure that the aspect ratio of the images remain the same as the video?
Image Analyst
el 10 de Mayo de 2021
If you say
axis('image');
after you display an image, it will show it pixel-for-pixel and the aspect ratio will be the same.
mae magdadaro
el 7 de Dic. de 2016
0 votos
thank you it run
Categorías
Más información sobre Image Preview and Device Configuration en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!