Home > Programming > Read frames from a given video using MATLAB code

Read frames from a given video using MATLAB code

Here is the code to read the frames from a given video using MATLAB. It is very easy to read the individual frames from a given video. The code as follows

function [ frame ] = readFrames( vid )

readerobj = mmreader(vid);
vidFrames = read(readerobj);
numFrames = get(readerobj, ‘numberOfFrames’);
for k = 1 : numFrames
mov(k).cdata = vidFrames(:,:,:,k);
mov(k).colormap = [];
imagename=strcat(int2str(k), ‘.jpeg’);
imwrite(mov(k).cdata, strcat(‘vasanth’,imagename));


mmreader() function supports more number of video format if you go for avireader() it allows only avi files. In the above code mov(k).cdata contains the actual image information. Using imwrite() we are actually writting the image on the harddisk. imagename=strcat(int2str(k), ‘.jpeg’); Here I’m appending the k.jpeg ex: 2.jpeg if you want to have in the other formats you can simply write .bmp or .jpg or .png instead of jpeg in the above code. vidFrames(:,:,:,k); returns the video frames.

  1. gouri
    April 11, 2011 at 4:08 pm

    great job,dude……….. i m working on video retrival mini project, but not getting, hw 2 detect boundary and index frames, could u help me plz?????/

  2. April 12, 2011 at 8:17 am

    There are different ways of extracting key frames. You can get many of research papers. I used similarity based key frame extraction.

  3. Ehsan
    April 15, 2011 at 7:26 am


    Nice work Man. I have recently started working on videos in matlab. I have a query regarding reading and writing videos in matlab. I hope you can help. Actually I want to perform following steps,
    (1). Read simple colored video file in matlab.
    (2). Extract frames in some structure.
    (3). Convert frames to grayscale.
    (4). Perform some operation on gray scale images.
    (5). Make a video from processed gray scale images/frames.
    (6). Write the video to harddisk.

    Actually I am getting problem in step 5 and 6. I hope you can guide me in this regard. Anxiously waiting for your reply.


    • April 17, 2011 at 8:11 pm

      The following will work for you

      Mov1(n) = im2frame(uint8(fg),gray);
      movie2avi(Mov1,’mixture_of_gaussians_output’,’fps’,30); % save movie as avi

  4. neethidevan
    July 18, 2011 at 7:05 am

    Great work. Thanks for sharing. Can you please share code for detecting vehicles in a Lane of video.


  5. Eliza
    August 30, 2011 at 2:37 pm

    i used ur code to read the frames of video..i ended wit an error like tis……1st frame of an video is displayed…but not able to save as image…wat might b the pbm?????
    ??? Error using ==> imwrite at 457
    Can’t open file “vasanth1.jpeg” for writing.
    You may not have write permission.

    Error in ==> readFrames at 11
    imwrite(mov(k).cdata, strcat(‘vasanth’,imagename));

    • August 31, 2011 at 7:44 am

      You may not have the write permission to write the frame into the current working directory. Check once.

  6. Basli
    September 27, 2011 at 2:13 am

    can u plz help me to extract the I,P and B frames of mpeg2 video


  7. October 29, 2011 at 9:59 am

    hai ehsan, i mentioned in your last command, you have the problem in 5th steps,. but i need the solution for first two step, pls give me the coding for step 1 and 2.

    and i also request Mr.vasanth raja. to give me the coding for that one

  8. October 29, 2011 at 10:00 am

    hai ehsan, you mentioned in your last command, you have the problem in 5th steps,. but i need the solution for first two step, pls give me the coding for step 1 and 2.

    and i also request Mr.vasanth raja. to give me the coding for that one

  9. prsna
    November 17, 2011 at 8:54 pm

    Hi vasanth
    I tried to read a 32bit codec video in 64bit matlab. I get the error message like this
    ??? Initialization failed. (No combination of intermediate filters could be found to make the connection.)

    Error in ==> mmreader.mmreader>mmreader.init at 423
    obj.MMReaderImpl = audiovideo.mmreader(fullName);

    Error in ==> mmreader.mmreader>mmreader.mmreader at 133
    Can this problem be fixed using 64 bit matlab?

  10. bhuvana
    December 14, 2011 at 8:56 am

    sir i want to compare the captured image with the image which i hav already stored….so pls could u help me

  11. Nuwan
    December 25, 2011 at 3:26 am

    This Matlab code is helpful!
    However the typical video I have has 3000 frames and I just need to look at a certain sequence of frames ( say from frames 1834 to frames 1875 ) so then how am I going to modify the above code?
    Also how do I get information like the time between two frames ( frame rate ) in my case its 300fps or 3.33 ms between two adjacent frames.

  12. shwetha
    January 12, 2012 at 5:14 am

    good nice work

  13. shwetha
    January 12, 2012 at 5:15 am

    nice one..:)

  14. abeer
    January 16, 2012 at 12:34 pm

    hi, i need to extract the I,P and B frames of mpg video too could u help me, and how can we discrimintate between them, please help me?


  15. deepa
    January 19, 2012 at 7:37 am

    I am need of Matlab codes for my project “Query by Video clips”

  16. karthick
    January 31, 2012 at 2:35 pm

    hi , can pls help me i need matlab code for dividing(any format video) into frames i need it for project pls pls help me………

  17. karthick
    January 31, 2012 at 2:37 pm

    hi , can u pls help me…. i need matlab code for dividing(any format video) into frames i need it for project pls pls help me………

  18. arti
    February 9, 2012 at 5:53 am

    Hi all!
    I am doing a project in which i have to take frames out of a running video and make them available for some function. I had run the code given above but I am getting an error written below. could any of you please tell reason for this error.

    ??? Undefined function or variable “vidFrames”
    Error in ==> readFrames at 8
    mov(k).cdata = vidFrames(:,:,:,k);

  19. sowmya
    February 11, 2012 at 3:22 pm

    can u plz help me to extract the I,P and B frames from a video file in matlab.


  20. raj
    February 11, 2012 at 5:19 pm

    hi, i dnt knw how to use the cell and zeros func in matlab ..can u help me out…and here is my code..i am nt able to store required frames in a single array and use t ltr,,plz help me out.

    numFrames = vid.NumberOfFrames;

    for i = 1:8:n
    frames = read(vid,i);
    imwrite(frames,[int2str(i) ‘.jpg’]);

  21. yogesh
    February 17, 2012 at 2:25 pm

    hi, i have tried above code in matlab but Warning: Unable to determine the number of frames in this file.

  22. vijaya
    February 18, 2012 at 8:33 am

    hi, can u pls help me. i need a matlab code for video using mmreader. In this case, video stream is converted into number of frames. That frames are stored in separate folder. how to save the frames in folder using mmreader function?..pls help me

  23. vijaya
    February 20, 2012 at 5:06 am

    hi sir,
    thank sir…but I need a code for read the frames that are stored in separate folder using mmreader function….pls can u help me sir?

    • arti
      February 20, 2012 at 5:57 am

      Thanks for ur reply!!! I have alredy done dis much.. i am able to view images from frames seperately but m also not able to store them in a seperate foleder and them read them again so that they can become input for my another function.

  24. vijaya
    February 20, 2012 at 5:27 am

    nice code…
    hi sir,I need a code for background subtraction using mmreader with a background reference image and current frame in video using matlab.In this case, number of frames that are stored in seperate folder?…pls help me sir

  25. gurushanmuganathan p
    February 27, 2012 at 7:25 am

    nice, i am doing project in dip i want to get animated gif image in matlab shall u help me

  26. Sahil
    March 1, 2012 at 10:40 am

    sir i want to convert video into image frames in matlab , so sir can you guide me.
    I will be very thankful to you.

  27. March 5, 2012 at 4:15 pm

    hai..i m working on video segmentation and summarization using genetic algorithm..can anyone suggest me a code for this topic?

  28. March 5, 2012 at 4:17 pm

    suggest me a matlab code for video segmantation and summarization using genetic algorithm…

  29. damodar
    March 17, 2012 at 6:33 am

    i am working on colorizatioin using optimization algorithm by anat levin.. i am not able to bring out an output for video colorization.. can i be assisted regarding that.. plzz. i can reachd over email at damodar27.ece@gmail.com

  30. Archana t
    March 18, 2012 at 3:49 pm

    hi plz send me matlab code for extraction of key frames from a video

  31. Marcvivet
    April 18, 2012 at 6:47 am

    You can use the Matlab VideoUtils toolbox:


    It is very easy to use.

  32. sajid
    April 19, 2012 at 2:00 pm

    thanks for the code

  33. Saima
    April 21, 2012 at 2:08 pm

    I really am in need of help on another post i left for db linking with matlab and also on this… If u can please.
    1. Getting frequency response(FFT) of audio file brought to matlab gui from db in ms access .
    2. How do i show several plots for different frequencies(noises) in a single audio file in my GUI.
    3. How do i show output of fol noises and show their frequency ranges on GUI:-
    > humming
    > human voices
    > male
    > female
    > chirping of birds
    > helicopters sounds including:-
    >> engine
    >> rotary wings
    With many thanx in advance if u can help. Coz im not finding anything helpful otherwise. And m totally lost in doing above.:(
    Many regards

  34. Andrei
    May 28, 2012 at 9:06 pm

    I have to break a video into temporal sequences, and then select from each video sequence the most representative frame, and generate a hash based on local and global variables (texture, color histogram, etc), that I have to save in a database. I could use some help with any of the above. Thank you!

  35. June 25, 2012 at 9:57 am

    How to divide a .wav file into frames and place the frames in a matrix and access them later?

  36. vasanth kishore
    August 17, 2012 at 4:13 pm

    i am unable to run the code for video filtering for grayscale avi videos kindly help? where can i download color avi videos

  37. Muhammed
    August 27, 2012 at 4:42 pm

    Great work man

  38. cbo
    November 10, 2012 at 11:05 am

    i’m unable to read a video file by using matlab .

  39. Mrad
    December 30, 2012 at 8:53 am

    i have a mini-project where i should read frames from the video and trick them..it means for example change the order of the frames or insert an additional frame in the sequence..i am new to matlab if somebody can give me the code for doing so..thank u 🙂

  40. Kiran
    December 30, 2012 at 9:09 am

    dear sir,
    I extract frame from video …but I need to detect an excitment clip based on audio frequency of video … can u plz help me hoe to detect an audio frequency of video or frame for comparing it with thresshold value…
    plz sir help me…

  41. steffi rapol
    January 17, 2013 at 5:09 pm

    hi vasanth… i need lil help regarding a project of mine.. can u help with extraction of two frames of two diff images of a moving object.

  42. yoyoabc
    January 26, 2013 at 8:55 am

    visit to get the code for extracting video frames from an avi file


  43. yoyoabc
    January 26, 2013 at 8:56 am
  44. Anaya
    March 7, 2013 at 7:05 pm

    can any one give help regarding process of extracting key frame(when important event occur) from a video sequence……as i have done with extracting frame from the video bt nw im confused how to extract main key feature of video sequence…
    i will b very greatfull..thankss

  45. Abhinav tripathi
    August 30, 2013 at 10:39 am

    hey i m working on video compression using fpga…so IN MATLAB i want to divide the video in its frames
    and want to generate the rgb matrix of all the individual frames..so that i can further process my video in fpga


  46. majid
    September 12, 2013 at 12:25 pm

    Thanks a lot
    I have searched more than 5 hours on internet and Matlab Help to find well-working code for storing all frames of video in a matrix, and because the high dimension of this matrix,all founded codes made errors.but ur code is complete and acts fast in my project.Thank u for ur help

  47. December 7, 2013 at 7:07 am

    Nice n Thanks

  48. January 17, 2014 at 8:31 am

    Great work dear but it gives a error
    Not enough memory available for 898 frames.
    please help how to increase memory or solve this problem

  49. preeti
    February 25, 2014 at 2:37 pm

    can u please provide a matlab code for converting mpeg2 video into frames

  50. February 26, 2014 at 5:29 pm

    hi…..i am working on video compression using motion compensation .if u have any idea related about this or matlab code i required urgently…

  51. neha
    October 24, 2014 at 9:09 pm

    I have extracted the frames, but i want to perform encryption only on I-frames. My ques is how will i extract I-frames from the rest of frames.

  52. October 26, 2014 at 11:35 pm


    I am having a problem with the first part.
    function [ frame ] = readFrames( vid )

    I get the error:

    function [ frame ] = readFrames(vid1)
    Error: Function definitions are not permitted in this context.

    Can you please help me?

  53. nandhini
    November 15, 2014 at 3:39 pm

    i want to seperate i,p,b frames from mpeg video and also to identify the i frame from it fr watermarking can u pls help me.

  1. No trackbacks yet.

Leave a Reply to vibhutesh Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: