Extract Frames from Video MATLAB
There may be situations when we need certain frames from video. You can extract the video frames easily in MATLAB and save to any image format that MATLAB imwrite function supports or you can process the frames directly.
There is a function class called ‘VideoReader’
OBJ = VIDEOREADER(FILENAME)
– constructs a multimedia reader object, OBJ, that can read in video data from a multimedia file.
– FILENAME is a string specifying the name of a multimedia file. There are no restrictions on file extensions.
MMREADER can also be used but MMREADER will be removed in a future release. So better use ‘VideoReader’.
Here is the code to do it.
%vid = video which needs to be extracted vid = 'worm_gear_set.avi'; readerobj = VideoReader(vid); vidFrames = read(readerobj); %get number of frames numFrames = get(readerobj, 'numberOfFrames'); for k = 1 : numFrames mov(k).cdata = vidFrames(:,:,:,k); mov(k).colormap = ; %imshow(mov(k).cdata); imagename=strcat(int2str(k), '.jpg'); %save inside output folder imwrite(mov(k).cdata, strcat('outputframe-',imagename)); end
The frames extracted are saved inside the output folder which will be inside the project directory.
You can download the project files from here.