MATLAB Answers

1

how to extract and save frames in a video ??

Asked by NAVNEET NAYAN on 4 Apr 2017
Latest activity Commented on by Image Analyst
on 17 Mar 2019 at 14:52
Accepted Answer by Jan
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

  0 Comments

Sign in to comment.

5 Answers

Answer by Jan
on 4 Apr 2017
Edited by Jan
on 4 Apr 2017
 Accepted Answer

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.

  4 Comments

Show 1 older comment
Thanks a lot. This is exactly what I am looking for.
It doesn't work if you just want to read

Sign in to comment.


Answer by Joanna Sznajder on 18 Jul 2018

Thx, it's great solution!

  0 Comments

Sign in to comment.


Answer by HEPHZIBAH THOMAS on 10 Nov 2018

It worked perfect. Thanks

  0 Comments

Sign in to comment.


Answer by Image Analyst
on 11 Jan 2019

Try my attached demo, which does exactly that, plus a lot more fun things.

  1 Comment

When I am converting video in to different frame. The resolution is changes. I have video frame 2048x1000 an I am end up with 1475x 337 ? How can I fix this issue

Sign in to comment.


Answer by Guram Tsirekizde on 16 Mar 2019 at 16:18

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

videoObject = VideoReader(movieFullFileName)
% Extract the first frame from the movie structure.
thisFrame = read(videoObject, 1);

Sign in to comment.