Converting mpg video file to image file?

2 vues (au cours des 30 derniers jours)
Ivan
Ivan le 4 Oct 2011
Hi everyone,
I would like to find out is there a way to convert mpg video files to image files?
Thank you very much!

Réponse acceptée

Walter Roberson
Walter Roberson le 4 Oct 2011
  2 commentaires
Ivan
Ivan le 12 Déc 2011
hi Walter, thanks for your reply.
I have decided to use mmreader for my video.
xyloObj = mmreader('20 cm view.mpg');
nFrames = xyloObj.NumberOfFrames;
vidHeight = xyloObj.Height;
vidWidth = xyloObj.Width;
% Preallocate movie structure.
mov(1:nFrames) = struct('jpg', zeros(vidHeight, vidWidth, 3, 'uint8'), 'colormap', []);
% Read one frame at a time.
for k = 1 : nFrames
mov(k).jpg = read(xyloObj, k);
end
* I have changed cdata to jpg as I want it in jpg format. Is it correct?
* I presume only up to this step is necessary as the rest are about reading the video.
* So from here how do I read the mov(1).jpg?
Is it right to say this?
A=imread(mov(1).jpg);
A = rgb2gray(A);
%convert jpg into gif
imwrite(A,'mov1.gif','gif')
[x,map] = imread('mov1.gif');
*but apparently it is not working. as i keep getting the following error:
??? Error using ==> strfind
Input strings must have one row.
Error in ==> imread at 340
if (strfind(filename, '://'))
* there isnt a imread at 340 at all.
Walter Roberson
Walter Roberson le 12 Déc 2011
No, do not change cdata to jpg, you will only confuse matters and it will have no benefit at all.
You do not need to imread() anything as you have already read the image in to mov(k). Using your variable names, A=mov(1).jpg is the replacement command instead of using imread().
I suggest you replace your existing reading code with
xyloObj = mmreader('20 cm view.mpg');
mov = read(xyloObj);
After which,
nFrames = size(mov,4);
ndigits = max(1,ceil(log10(nFrames+1)));
for k = 1 : nFrames
A = rgb2gray(mov(:,:,:,k));
imwrite(A, sprintf('mov%.0*d.gif',ndigits,k), 'gif');
end
The bit with nDigits and the odd format in the sprintf() are to calculate the number of decimal digits required to represent the highest frame number, and then to cause each individual frame number to be formatted with leading zeros. For example, you do not want mov9.gif to be followed by mov10.gif because if you do that, an alphabetic sort by file names would sort them as mov1.gif mov10.gif mov2.gif . So if the maximum frame number was (say) 3 digits you would instead want the files named mov009.gif mov010.gif so that numerically adjacent frames sort alphabetically next to each other.
You might notice that I left out the imread() of the following after you imwrite() it. Most of the time you do not want to read back in frames you just wrote out: about the only exception is when you are using a file format that has lossy compression (such as JPEG with default parameters) and it is important to you that you fetch the reconstructed (imprecise) data instead of the using the exact data that you already have in memory.

Connectez-vous pour commenter.

Plus de réponses (1)

Bjorn Gustavsson
Bjorn Gustavsson le 12 Déc 2011
If you only want to convert the video frames to images I suggest that you use mplayer to do that part of the job:
Then you'll have the frames as images to continue your processing on.

Catégories

En savoir plus sur Convert Image Type dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by