Extracting & Saving frames from a Video file using Matlab Code

A video file consists of frames. These frames when appear before us in a rate more than our perception of vision, gives a sensation of an object moving before us, by looking just at the screen on which frames are appearing at high rate.
Thus one can say that frames are the fundamental entity of a video file.

In this project we are opening one video file, & grabbing each video frame. At last we are saving them frame by frame as an image file, in the folder "snaps" in the working directory.

OUTPUT AT MATLAB COMMAND PROMPT while executing the code for Extracting & Saving of frames from a Video file through Matlab.
Extracting & Saving of frames from a Video file through Matlab Code, MATLAB Command Window Output
Extracting & Saving of frames from a Video file through Matlab Code, MATLAB Command Window Output
Screen Shot of the frames OUTPUT AT The Folder while executing the code for Extracting & Saving of frames from a Video file through Matlab.
The Folder At Which The Frames Are Saved

Above Screen Shot of the frames OUTPUT AT The Folder while executing the code for Extracting & Saving of frames from a Video file through Matlab.

Code:

%%Extracting & Saving of frames from a Video file through Matlab Code%%
clc;
close all;
clear all;


% assigning the name of sample avi file to a variable
filename = 'rhinos.avi';

%reading a video file
mov = VideoReader(filename);

% Defining Output folder as 'snaps'
opFolder = fullfile(cd, 'snaps');
%if  not existing
if ~exist(opFolder, 'dir')
%make directory & execute as indicated in opfolder variable
mkdir(opFolder);
end


%getting no of frames
numFrames = mov.NumberOfFrames;

%setting current status of number of frames written to zero
numFramesWritten = 0;

%for loop to traverse & process from frame '1' to 'last' frames
for t = 1 : numFrames
currFrame = read(mov, t);   
%reading individual frames
opBaseFileName = sprintf('%3.3d.png', t);
opFullFileName = fullfile(opFolder, opBaseFileName);
imwrite(currFrame, opFullFileName, 'png');  
%saving as 'png' file
%indicating the current progress of the file/frame written
progIndication = sprintf('Wrote frame %4d of %d.', t, numFrames);
disp(progIndication);
numFramesWritten = numFramesWritten + 1;
end     
%end of 'for' loop
progIndication = sprintf('Wrote %d frames to folder "%s"',numFramesWritten, opFolder);
disp(progIndication);

%End of the code

Additional MATLAB related Posts That You May Like!

# Implementation of RSA Cryptography Algorithm Using MATLAB Code
# Recording a Video From Webcam Using MATLAB Code 
# Accessing the Serial Port using MATLAB Code: Serial communication through MATLAB
# Accessing Webcam Through MATLAB Code
# Motion Tracking/Detection in MATLAB using Kalman Filter
# Optimizing MATLAB Code
# Controlling Mouse Pointer/Cursor Using MATLAB Code
# Controlling Keyboard Keys (Pressing & Releasing) with Matlab code 
# Starting MATLAB without GUI through Command Prompt/LxTERMINAL
# Some MATLAB Unusual Commands, That You Must Know ( Just For Fun! ) Part 2
# Some MATLAB Unusual Commands, That You Must Know ( Just For Fun! ) Part 1 

6 comments:

  1. hey vibhutesh I am unable to run whole code successfully
    its giving me following errors
    Error using pngwritec
    PNG library failed: Write Error.

    Error in writepng (line 280)
    pngwritec(data, map, filename, colortype, bitdepth, ...

    Error in imwrite (line 472)
    feval(fmt_s.write, data, map, filename, paramPairs{:});

    Error in Laryngoscopysegmentation (line 31)
    imwrite(currFrame, opFullFileName, 'png'); %saving as 'png' file

    ReplyDelete
    Replies
    1. You can use other formats like JPG, TIFF etc.

      Delete
  2. It's shows unrefined functions or variable 'numFrames'

    ReplyDelete
    Replies
    1. Its due to the fact that you havnt copied it correctly. Or whatever you have changed in the code, haven't done in accordance with the code.

      Delete