Saturday, 14 June 2014

Convert Video to frames in matlab

The videoReader takes the input video. It creates a folder called snaps. If the directory is not there, it creates using mkdir
The frames are read now and stored in Frame. Later the file is taken and written into the output folder. The frame is then incremented.


clc;
close all;
clear all;
filename = 'rhinos.avi';
mov = VideoReader(filename);
opFolder = fullfile(cd, 'snaps');
if ~exist(opFolder, 'dir')
      mkdir(opFolder);
end
numFrames = mov.NumberOfFrames;
numFramesWritten = 0;
for t = 1 : numFrames
    Frame = read(mov, t);
    opBaseFileName = sprintf('%3.3d.png', t);
     opFullFileName = fullfile(opFolder, opBaseFileName);
     imwrite(Frame, opFullFileName, 'png');
     Indicate = sprintf('Wrote frame %4d of %d.', t, numFrames);
     disp(Indicate);
     numFramesWritten = numFramesWritten + 1;
end
Indicate = sprintf('Wrote %d frames to folder "%s"',numFramesWritten, opFolder);
disp(Indicate);

No comments:

Post a Comment