Converting mpg video file to image file?

조회 수: 2 (최근 30일)
Ivan
Ivan 2011년 10월 4일
Hi everyone,
I would like to find out is there a way to convert mpg video files to image files?
Thank you very much!

채택된 답변

Walter Roberson
Walter Roberson 2011년 10월 4일
  댓글 수: 2
Ivan
Ivan 2011년 12월 12일
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 2011년 12월 12일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bjorn Gustavsson
Bjorn Gustavsson 2011년 12월 12일
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.

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by