MATLAB Answers

NS
2

Convert AVI file to series of images

Asked by NS
on 10 Mar 2012
Latest activity Commented on by Guillaume
on 29 Mar 2018
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

  0 Comments

Sign in to comment.

2 Answers

Answer by Chandra Kurniawan on 11 Mar 2012
 Accepted Answer

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

Show 1 older comment
SBunny
on 14 Jun 2013
Thank you so much
which directory will I get the images?
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.


Answer by 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

  12 Comments

The scripts I saw converted the frames to PNG, which is a lossless compression. If you saw some that used TIFF, that is uncompressed unless an option for compression is used, such as LZW. PNG usually compresses to around a third of the size. JPG can compress more but can have bad artifacts and should be avoided if you're going to do image analysis.
very nice, thanks :)
Guillaume
on 29 Mar 2018
In matlab, the default TIFF compression mode (for RGB images) is 'packbits', so TIFF are compressed by default. packbit is a run-length encoding compression method. It is completely lossless. You can override the compression mode, the only lossy TIFF mode supported by imwrite is 'jpeg'.
PNG is always lossless and in my opinion a better format than TIFF.

Sign in to comment.