mpeg file

Hi
I have two task that are similar that I dont know how to do:
1 - I want to be able to import an mpeg video and save each frame of this movie as individual images into a folder.
2 - Also, I want to be able to create an mpeg video given a number of individual images saved in a folder.

Answers (2)

Walter Roberson
Walter Roberson on 19 Jul 2011

0 votes

If you have a new enough MATLAB version, you could perhaps us the VideoReader class; older versions called it the mmreader. Extract frame by frame and use imwrite(). See http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F for information on automatic filename generation for such purposes.
Given a series of frames read in by imread() (in conjunction with dir() to find the file names), you can use the VideoWriter() class to produce a .avi .
James
James on 20 Jul 2011
Sorry I need further help...
For task 1: - I have imported the mpeg video using:
object = mmreader('train.mpg')
and I know how to scroll through each frame using:
for i=1:NumberOfFrames
read(object,i)
end
But how do I save this frame as a jpg?

11 Comments

Walter Roberson
Walter Roberson on 20 Jul 2011
for i=1:NumberOfFrames
thisframe = read(object,i)
imwrite(thisframe, AppropriateFilename, 'jpg')
end
For more information on how to construct the AppropriateFilename, see the FAQ I referenced earlier.
James
James on 20 Jul 2011
for i=1:10
thisframe=read(object,i)
thisframe=rgb2gray(thisframe)
filename-strcat('frame',num2str(i),'.jpg')
imwrite(thisframe,filename,'jpg')
end
It creates the first image (frame1), however I then get the following error:
??? Error using ==> minus
Matrix dimensions must agree.
Walter Roberson
Walter Roberson on 20 Jul 2011
filename-strcat('frame',num2str(i),'.jpg')
is a request to *subtract* something from filename. You want assignment.
James
James on 20 Jul 2011
oops absolute school boy error! Cheers.
James
James on 20 Jul 2011
I was just wondering....
I need to really put the frame into double format so I can do some calculations.
When I put this into my code as shown below, then the image appears differently. Why is this? And how do I change it back?
for i=1:10
thisframe=read(object,i)
thisframe=rgb2gray(thisframe)
thisframe=double(thisframe)
filename=strcat('frame',num2str(i),'.jpg')
imwrite(thisframe,filename,'jpg')
end
Walter Roberson
Walter Roberson on 20 Jul 2011
See im2double() http://www.mathworks.com/help/toolbox/images/ref/im2double.html
James
James on 20 Jul 2011
Sorry i dont really understand this and I dont really know why I have to convert it to double to be able to perform calculations on the frame?!
James
James on 20 Jul 2011
It seems that im2double() worked though instead of double().
But in my previous work I used double() as shown below. Does this mean what I have done below is wrong?
for k = 1 : nFrames
mov(k).cdata = read(videoobject, k);
end
Img = double(Video(1,n).cdata);
Walter Roberson
Walter Roberson on 20 Jul 2011
Using double() is not necessarily wrong, but you have to keep track of the fact that you used it and you either have to convert back to the original class later before displaying (or writing) the image, or else you have to do the appropriate rescaling later.
Data arrays of class double are presumed to represent color values as a numeric value in the range 0 to 1 (inclusive). Data read via imread() or mmreader is almost always an unsigned integer data type such as uint8 or uint16, which represent colors as integers in the range 0 to 255, or 0 to 65535 (respectively). If you double() something in the range 0 to 255 or 0 to 65535, the double precision value will mostly be outside of the expected value for double precision color representation, 0 to 1, and the graphics subsystem will convert the 0.0 to 255.0 by clamping everything about 1.0 to be 1.0, if you ever try to display it or write it.
im2double() takes care of looking at the source data type and if necessary scales the data, such as by doing double(I)./255 so that the data comes out in the range 0 to 1, suitable for display and writing purposes.
James
James on 21 Jul 2011
Hi
It seems when I double()a frame I still have the same numbers, which is not what you are saying?! However, when I use im2double() then I do get numbers between 0 and 1.
Walter Roberson
Walter Roberson on 21 Jul 2011
Yes, double() keeps the same numeric values, such as uint8(42) becoming the double precision number 42.0. That is fine for some kinds of computations, but if you try to display or imwrite() a double precision array with such values you will have problems, as the expected range for representing graphics in double precision variables is 0 to 1. im2double() converts the data type _and_ rescales the values to be in the range 0 to 1.

Sign in to comment.

Asked:

on 19 Jul 2011

Community Treasure Hunt

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

Start Hunting!