How to Extract Frames From a Video in MATLAB?

Let us see how to extract frames from a video in MATLAB.

Approach :

  1. Import the video which is to be converted into frames into the current matlab environment.
  2. Extract the total number of frames in the video.
  3. Make an empty directory named frames before the execution.
  4. Run a for loop and start extracting the frames into the directory.

In our demonstration, we shall consider the following video :

filter_none

edit
close

play_arrow

link
brightness_4
code

% import the video file
obj = VideoReader('org.mp4');
vid = read(obj);
  
 % read the total number of frames
frames = obj.NumberOfFrames;
  
% file format of the frames to be saved in
ST ='.jpg';
  
% reading and writing the frames 
for x = 1 : frames
  
    % converting integer to string
    Sx = num2str(x);
  
    % concatenating 2 strings
    Strc = strcat(Sx, ST);
    Vid = vid(:, :, :, x);
    cd frames
  
    % exporting the frames
    imwrite(Vid, Strc);
    cd ..  
end

chevron_right


Output :
The frames directory will look something like this :

Images saved in the frames folder

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.