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’

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 extracted
vid = 'worm_gear_set.avi';
readerobj = VideoReader(vid);
vidFrames = read(readerobj);
%get number of frames
numFrames = get(readerobj, 'numberOfFrames');
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.

You can download the project files from here.

SHARE Extract Frames from Video MATLAB

You may also like...

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’;
    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

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

  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 :
    ??? 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’);

  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
    readerobj = VideoReader(vid);

  11. I think it is so wonderful that people can learn difficult programs from the internet!. On my opinion, writing services are useful.

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

Leave a Reply

Your email address will not be published.

Share