Tuesday, March 13, 2012

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('output\frame-',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.

12 comments:

  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

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. Very nice post, exactly what I was looking for.

    ReplyDelete
  4. Nice post, exactly what I was looking for.

    ReplyDelete
  5. here reference about using matlab :
    http://repository.gunadarma.ac.id/bitstream/123456789/1229/1/50407547.pdf

    ReplyDelete
  6. 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('1\frame-',imagename));
    end

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

    ReplyDelete
  8. 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.

    ReplyDelete
  9. 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');

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

    ReplyDelete
  11. how to solve this error

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

    Error in ==> main at 2
    readerobj = VideoReader(vid);

    ReplyDelete