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 = [];
%imshow(mov(k).cdata);
imagename=strcat(int2str(k), ‘.jpeg’);
imwrite(mov(k).cdata, strcat(‘vasanth’,imagename));
%extractComponents(mov(k).cdata);

end

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.

About these ads
  1. gouri
    April 11, 2011 at 4:08 pm | #1

    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 | #2

    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 | #3

    Hi:

    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.

    Regards

    • April 17, 2011 at 8:11 pm | #4

      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 | #5

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

    V.Neethidevan
    neethidevan@gmail.com

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

    Hi,
    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));

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

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

    regards

  7. October 29, 2011 at 9:59 am | #9

    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 | #10

    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 | #11

    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
    obj.init(fileName);
    Can this problem be fixed using 64 bit matlab?

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

    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 | #15

    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 | #16

    good nice work

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

    nice one..:)

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

    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?

    regards

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

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

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

    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 | #21

    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 | #22

    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 | #23

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

    regards

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

    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.

    vid=mmreader(‘ccc.avi’);
    numFrames = vid.NumberOfFrames;
    n=numFrames;

    for i = 1:8:n
    frames = read(vid,i);
    imwrite(frames,[int2str(i) '.jpg']);
    im(i)=image(frames);
    figure,imshow(im(i));
    end

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

    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 | #26

    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 | #27

    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 | #28

      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 | #29

    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 | #30

    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 | #31

    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 | #32

    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 | #33

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

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

    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 | #35

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

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

    You can use the Matlab VideoUtils toolbox:

    http://sourceforge.net/projects/videoutils/

    It is very easy to use.

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

    thanks for the code

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

    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
    Saima

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

    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 | #40

    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 | #41

    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 | #42

    Great work man

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

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

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

    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 | #45

    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 | #46

    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 | #47

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

    http://matlabnstuff.blogspot.in/2013/01/extracting-frames-from-video-file.html

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

    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 | #50

    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

    PLZ HELP

  46. majid
    September 12, 2013 at 12:25 pm | #51

    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 | #52

    Nice n Thanks

  48. January 17, 2014 at 8:31 am | #53

    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 | #54

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

  50. February 26, 2014 at 5:29 pm | #55

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

  1. No trackbacks yet.

Leave a 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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: