# Extract Frames from Video MATLAB

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’

– 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 extracted
vid = 'worm_gear_set.avi';
%get number of frames
for k = 1 : numFrames
mov(k).cdata = vidFrames(:,:,:,k);
mov(k).colormap = [];
%imshow(mov(k).cdata);
imagename=strcat(int2str(k), '.jpg');
%save inside output folder
imwrite(mov(k).cdata, strcat('outputframe-',imagename));
end

The frames extracted are saved inside the output folder which will be inside the project directory.

SHARE Extract Frames from Video MATLAB

### 13 Responses

1. 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

2. armiof1 says:

Very nice post, exactly what I was looking for.

3. Nice post, exactly what I was looking for.

4. kranthi says:

good work. keep working

5. Daniel says:

Thank you for sharing. To solve memory problems with too large videos:

vid = ‘worm_gear_set.avi’;

%get number of frames
for k = 1 : numFrames
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

6. how can we change the number of frames..
i mean how to change the frame rate..

7. Anonymous says:

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.

8. pyles says:

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 :

obj.init(fileName);

Error in ==> EBMA at 11

9. Unknown says:

Can you tell me how to extract frames from .y4m or .yuv format ? Thanks

10. Anonymous says:

how to solve this error

??? Undefined function or method ‘VideoReader’ for input arguments of type ‘char’.

Error in ==> main at 2