There may be situations when we need certain frames from video. You can extract the video frames easily in MATLAB and save to any image format that MATLAB imwrite function supports or you can process the frames directly.
There is a function class called ‘VideoReader’
OBJ = VIDEOREADER(FILENAME)
– constructs a multimedia reader object, OBJ, that can read in video data from a multimedia file.
– FILENAME is a string specifying the name of a multimedia file. There are no restrictions on file extensions.
MMREADER can also be used but MMREADER will be removed in a future release. So better use ‘VideoReader’.
Here is the code to do it.
%vid = video which needs to be extractedvid = 'worm_gear_set.avi';readerobj = VideoReader(vid);vidFrames = read(readerobj);%get number of framesnumFrames = get(readerobj, 'numberOfFrames');for k = 1 : numFramesmov(k).cdata = vidFrames(:,:,:,k);mov(k).colormap = [];%imshow(mov(k).cdata);imagename=strcat(int2str(k), '.jpg');%save inside output folderimwrite(mov(k).cdata, strcat('outputframe-',imagename));endThe frames extracted are saved inside the output folder which will be inside the project directory.
You can download the project files from here.

March 28, 2012 at 1:38 pm
It is very rare these days to find blogs that provide information someone is looking for. I am glad to see that your blog share valued information that can help to many readers. Thanks and keep writing!
Plagiarism detection
May 3, 2012 at 1:09 pm
Very nice post, exactly what I was looking for.
May 3, 2012 at 1:10 pm
Nice post, exactly what I was looking for.
May 3, 2012 at 4:13 pm
good work. keep working
June 7, 2012 at 12:14 am
here reference about using matlab :
http://repository.gunadarma.ac.id/bitstream/123456789/1229/1/50407547.pdf
July 5, 2012 at 5:02 pm
Thank you for sharing. To solve memory problems with too large videos:
vid = ‘worm_gear_set.avi’;
readerobj = VideoReader(vid);
%get number of frames
numFrames = get(readerobj, ‘numberOfFrames’);
for k = 1 : numFrames
vidFrame = read(readerobj,k);
mov(k).cdata = vidFrame;
mov(k).colormap = [];
%imshow(mov(k).cdata);
imagename=strcat(int2str(k), ‘.jpg’);
%save inside output folder
imwrite(mov(k).cdata, strcat(‘1frame-‘,imagename));
end
August 19, 2012 at 4:17 am
how can we change the number of frames..
i mean how to change the frame rate..
reply
October 24, 2012 at 3:29 pm
Hi, I’m trying to use your code but I can’t seem to make it work. What should be in the place of the ‘k’? Does something need to be in its place? I’m sorry for being so uneducated with MATlab. This is the first time I’ve ever used MATlab.
November 12, 2012 at 10:20 am
Hi, I am trying to use the VideoReader function but I got an error regarding the path of the video. The video is on the Desktop. May I have to move/save it somewhere else or how to use the path of the video in VideoReader function. See the error below :
??? Error using ==> VideoReader.VideoReader>VideoReader.getFullPathName at 373
The filename specified was not found in the MATLAB path.
Error in ==> VideoReader.VideoReader>VideoReader.init at 415
fullName = VideoReader.getFullPathName(fileName);
Error in ==> VideoReader.VideoReader>VideoReader.VideoReader at 133
obj.init(fileName);
Error in ==> EBMA at 11
xyloObj = VideoReader(‘foreman_qcif.y4m’);
December 6, 2012 at 9:48 pm
Can you tell me how to extract frames from .y4m or .yuv format ? Thanks
September 29, 2013 at 5:32 am
how to solve this error
??? Undefined function or method ‘VideoReader’ for input arguments of type ‘char’.
Error in ==> main at 2
readerobj = VideoReader(vid);
March 15, 2016 at 3:30 pm
I think it is so wonderful that people can learn difficult programs from the internet!. On my opinion, writing services are useful.
December 23, 2016 at 3:19 pm
I like your blog design. Quite unusual and it looks comfortable.I like it and I hope that you’ll add here more unusual details.