how to extract and save frames in a video ??

134 views (last 30 days)
I am using the following code to read and extract entire frames of a video, but problem is the frames are stored in "multi" as shown in the attached image. It is not stored as 1.jpg, 2.jpg, 3.jpg..... .How to resolve this problem. or tell me how can we extract and store entire frames and how to use it in any for loop.
clc;
clear all;
close all;
tic;
vid=VideoReader('I:\testing\video1.avi');
numFrames = vid.NumberOfFrames;
n=numFrames;
for i = 1:1:n
frames = read(vid,i);
imwrite(frames,['I:\testing\abrupt\' int2str(i), '.jpg']);
end
multi = dir('I:\testing\abrupt\*.jpg*');
for i = 1:1:length(multi)
--------------
--------------
end

Accepted Answer

Jan
Jan on 4 Apr 2017
Edited: Jan on 4 Apr 2017
The shown code creates the file "1.jpg", "2.jpg" and so on. After getting the file names by dir they are ordered alphabetically, which is not the original order. Solve this by:
Folder = 'I:\testing\abrupt\';
for iFrame = 1:n
frames = read(vid, iFrame);
imwrite(frames, fullfile(Folder, sprintf('%06d.jpg', iFrame)));
end
FileList = dir(fullfile(Folder, '*.jpg'));
for iFile = 1:length(FileList)
aFile = fullfile(Folder, FileList(iFile).name);
img = imread(aFile);
end
fullfile cares about file separators. Now changing the work folder requires a single change in the code only.
"iFile" and "iFrame" is more descritpive than "i", which might shadow the imaginary unit also.
  7 Comments
suman jain
suman jain on 10 Jan 2019
It doesn't work if you just want to read

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 11 Jan 2019
Try my attached demo, which does exactly that, plus a lot more fun things.
  12 Comments
Image Analyst
Image Analyst on 19 Jun 2020
Try this. But instead of surf(), use imread(), and then rgb2gray() if needed to convert to gray scale.
% Demo to create a movie file from a Gaussian and then optionally save it to disk as a gray scale MP4 video file.
%==============================================================================================
% Initialization code
clear all;
clc;
workspace;
numberOfFrames = 61;
x1d = linspace(-3, 3, numberOfFrames);
y1d = x1d;
t = linspace(0, 5, numberOfFrames);
hFigure = figure;
% Set up the movie structure.
vidHeight = 344;
vidWidth = 446;
% Need to change from the default renderer to zbuffer to get it to work right.
% openGL doesn't work and Painters is way too slow.
set(gcf, 'renderer', 'zbuffer');
%==============================================================================================
% Create the movie.
fullFileName = fullfile(pwd, 'Deleteme.mp4');
% Create a video writer object with that file name.
% The VideoWriter object must have a profile input argument, otherwise you get jpg.
% Determine the format the user specified:
[folder, baseFileName, ext] = fileparts(fullFileName);
switch lower(ext)
case '.jp2'
profile = 'Archival';
case '.mp4'
profile = 'MPEG-4';
otherwise
% Either avi or some other invalid extension.
profile = 'Uncompressed AVI';
end
writerObj = VideoWriter(fullFileName, profile);
open(writerObj);
% Get a list of x and y coordinates for every pixel in the x-y plane.
[x, y] = meshgrid(x1d, y1d);
% After this loop starts, BE SURE NOT TO RESIZE THE WINDOW AS IT'S SHOWING THE FRAMES, or else you won't be able to save it.
for frameIndex = 1 : numberOfFrames
z = exp(-(x-t(frameIndex)).^2-(y-t(frameIndex)).^2);
cla reset;
% OPTION : Enlarge figure to full screen.
% set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
surf(x,y,z);
axis('tight')
zlim([0, 1]);
caption = sprintf('Frame #%d of %d, t = %.1f', frameIndex, numberOfFrames, t(frameIndex));
title(caption, 'FontSize', 15);
drawnow;
thisFrame = getframe(gca);
% Write this frame out to a new video file.
grayImage = rgb2gray(thisFrame.cdata);
writeVideo(writerObj, grayImage);
end
close(writerObj);
% Display the current folder panel so they can see their newly created file.
cd(folder);
filebrowser;
if contains(computer, 'WIN')
% If using Windows, play the movie in the default video player.
winopen(fullFileName);
end
message = sprintf('Finished creating movie file\n %s.\n\nDone with demo!', fullFileName);
uiwait(helpdlg(message));
Alix Dujardin
Alix Dujardin on 23 Jan 2023
Hello,
Thanks for this code. I know this question is a little bit old but do you know how to extract a frame every seconde and then create a new movie with it? Thanks!

Sign in to comment.


Guram Tsirekizde
Guram Tsirekizde on 16 Mar 2019
I am quite new to matlab, so silly question. how to show for example the first image from matrix above with code? thank you in advance.
  1 Comment
Image Analyst
Image Analyst on 17 Mar 2019
videoObject = VideoReader(movieFullFileName)
% Extract the first frame from the movie structure.
thisFrame = read(videoObject, 1);

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!