Let us see how to extract frames from a video in MATLAB.
Approach :
- Import the video which is to be converted into frames into the current matlab environment.
- Extract the total number of frames in the video.
- Make an empty directory named frames before the execution.
- Run a for loop and start extracting the frames into the directory.
In our demonstration, we shall consider the following video :
% 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 |
Output :
The frames directory will look something like this :