MATLAB Answers

Convert AVI file to series of images

171 views (last 30 days)
NS
NS on 10 Mar 2012
Answered: Elijah Galbreath on 30 Jun 2021
Hi all,
I have a AVI file that was formatted in MATLAB. I wish to convert it to a series of tif images. I am not able to do this in imageJ. Is it possible?
NS

Accepted Answer

Chandra Kurniawan
Chandra Kurniawan on 11 Mar 2012
Hi,
This is the simpler version :
obj = mmreader('rhinos.avi');
vid = read(obj);
frames = obj.NumberOfFrames;
for x = 1 : frames
imwrite(vid(:,:,:,x),strcat('frame-',num2str(x),'.tif'));
end
You can use your own video by replacing 'rhinos.avi' with your own.
I hope this works!
  4 Comments
Image Analyst
Image Analyst on 6 Jul 2017
He did not specify a folder, unfortunately, so the images will go into the current folder, the same one as the m-file.

Sign in to comment.

More Answers (3)

Image Analyst
Image Analyst on 11 Mar 2012
Here are the key lines:
outputFolder = 'c:/users/whatever'; % Change this!
% Read in the movie.
mov = aviread(movieFullFileName);
% Determine how many frames there are.
numberOfFrames = size(mov, 2);
for frame = 1 : numberOfFrames
% Extract the frame from the movie structure.
thisFrame = mov(frame).cdata;
% Create a filename.
outputBaseFileName = sprintf('Frame %4.4d.png', frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
% Write it out to disk.
imwrite(thisFrame, outputFullFileName, 'png');
end
  14 Comments
Image Analyst
Image Analyst on 5 Mar 2020
aviread() is a really old function and it may have been removed by now. You'll need to replace it with VideoReader. See attached demos for help and examples.

Sign in to comment.


Sushil  Sharma
Sushil Sharma on 23 Sep 2019
Edited: Sushil Sharma on 23 Sep 2019
Upadte answer
In the lestest veriosn of matlab, we have to use VideoReader instead of mmreader,then you are able to convert any video file into a frames
Here the simple of code to get the frames
%% Change .avi format to images frames
obj = VideoReader('test2.avi');
vid = read(obj);
frames = obj.NumberOfFrames;
for x = 1 : frames
imwrite(vid(:,:,:,x),strcat('frame-',num2str(x),'.png'));
end

Elijah Galbreath
Elijah Galbreath on 30 Jun 2021
outputFolder = uigetdir(path);
% Read in the video
viddir = uigetdir(path);
viddir = fullfile(viddir,'test.avi');
obj = VideoReader(viddir);
vid = read(obj);
% Number of frames
frames = obj.NumFrames;
for x = 1 : frames
%Create a filename
outputBaseFileName = sprintf('Frame %4.4d.png', x);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
imwrite(vid(:,:,:,x), outputFullFileName, 'png');
end

Community Treasure Hunt

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

Start Hunting!