You are currently viewing Matlab Code to Convert Video to Sequence of Frames

Matlab Code to Convert Video to Sequence of Frames

Spread the love

This code demonstrates to convert avi file formats to sequence of frames.This code could be used for video processing applications in matlab. Video processing is done in frames ,in order to convert video to frames you need the following code snippet.

Matlab Code to Convert Video to Sequence of Frames

% To get information about video file
file=aviinfo('test.avi');
% To get the no of frames in the video file  
frm_cnt=file.NumFrames

FileExtension='.bmp'
%Progress bar starts
h = waitbar(0,'Please wait...');

%Begin Loop process till End of Frames

for i=1:frm_cnt

    % Read the Frame of the Video file
frm(i)=aviread('test.avi',i);
   % Convert Frame to image file   
frm_name=frame2im(frm(i));
     %Create Filename to store
Filename=strcat(strcat(num2str(i)),FileExtension);
    % Write image file  to the current folder
imwrite(frm_name,Filename);

waitbar(i/frm_cnt,h)

end
%Close Progress bar
close(h)

This part of the sample code explains to convert to video to frames using videoreader

FileExtension='.bmp'
%Create a multimedia reader video
readerobj = VideoReader('xylophone.mp4', 'tag', 'myreader1');

% Read in all video frames.
vidFrames = read(readerobj);

% Get the number of frames.
numFrames = get(readerobj, 'NumberOfFrames');

% Create a MATLAB movie struct from the video frames.
for k = 1 : numFrames
     mov(k).cdata = vidFrames(:,:,:,k);
     frm_name=vidFrames(:,:,:,k);;
     %Create Filename to store
     Filename=strcat(strcat(num2str(k)),FileExtension);
     % Write image file  to the current folder
     imwrite(frm_name,Filename);
     mov(k).colormap = [];
end


% Create a figure
hf = figure; 

% Resize figure based on the video's width and height
set(hf, 'position', [150 150 readerobj.Width readerobj.Height])

% Playback movie once at the video's frame rate
movie(hf, mov, 1, readerobj.FrameRate);