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

 採用された回答

Chandra Kurniawan
Chandra Kurniawan 2012 年 3 月 11 日

4 投票

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 件のコメント

NS
NS 2012 年 3 月 11 日
Thanks Chandra. The code works perfectly.
SBunny
SBunny 2013 年 6 月 14 日
Thank you so much
Farhan Ferdous
Farhan Ferdous 2017 年 7 月 6 日
which directory will I get the images?
Image Analyst
Image Analyst 2017 年 7 月 6 日
He did not specify a folder, unfortunately, so the images will go into the current folder, the same one as the m-file.

サインインしてコメントする。

その他の回答 (3 件)

Elijah Galbreath
Elijah Galbreath 2021 年 6 月 30 日

1 投票

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
Image Analyst
Image Analyst 2012 年 3 月 11 日

0 投票

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 件のコメント

NS
NS 2012 年 3 月 11 日
Hi Image Analyst.
Thank you for your answer. The code is very long and I am a little confused with the content. Could you shorten the code if possible?
Image Analyst
Image Analyst 2012 年 3 月 11 日
Go ahead. It's well commented so you should be able to understand it. You can shorten it if you want. I think the comments should explain it very well -- what parts are confusing?
NS
NS 2012 年 3 月 11 日
Thanks. It works now. :) I just got confused with the sheer amount of data you had given in the code.
Image Analyst
Image Analyst 2012 年 3 月 11 日
Yes, when you make code robust and flexible like I do, and do things like displaying images with nice titles above them, and add lots of explanatory comments like I do, it does tend to make the code longer, but I think it's worth it.
AT
AT 2017 年 4 月 21 日
Hello! I am trying this code and I get this error
No appropriate method, property, or field CData for class VideoReader.
Error in labexc (line 15) thisFrame = mov(frame).CData;
Do you know why?
Image Analyst
Image Analyst 2017 年 4 月 21 日
That code I posted 5 years ago was before they had invented VideoReader. See newer, attached code that does use it.
AT
AT 2017 年 4 月 24 日
thank you! Do you have code that only takes the video and creates frames and saves to file?
Image Analyst
Image Analyst 2017 年 4 月 24 日
That's in the first half of the demo. Just chop off the second half of the script. You can also delete the part where it calls imshow() if you don't want that.
Joe Sheppard
Joe Sheppard 2018 年 3 月 28 日
Hey, I was wondering, does converting AVI to tiff with this script compress the data at all? If so is it lossless?
Image Analyst
Image Analyst 2018 年 3 月 28 日
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.
Joe Sheppard
Joe Sheppard 2018 年 3 月 29 日
very nice, thanks :)
Guillaume
Guillaume 2018 年 3 月 29 日
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.
Gaia Gbola
Gaia Gbola 2020 年 3 月 5 日
Hello, I am using the DEMO imaging version for MATLAB, but your code seems not to run on it, it does not recognize the function aviread. Is it possible that is because of the version that I have?
Image Analyst
Image Analyst 2020 年 3 月 5 日
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.

サインインしてコメントする。

Sushil  Sharma
Sushil Sharma 2019 年 9 月 23 日
編集済み: Sushil Sharma 2019 年 9 月 23 日

0 投票

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

2 件のコメント

William Thielicke
William Thielicke 2023 年 4 月 3 日
... but there is no other way than the frame-by-frame read and write? This is really slow... I am capturing data with the imaq toolbox. RAM is too small, so I am logging to disk. But I need to have individual TIFF images. Converting on a frame-to-frame basis is extremely slow with 12 bit uncompressed grayscale data...
Image Analyst
Image Analyst 2023 年 4 月 3 日
@William Thielicke that reads the whole video into RAM and then writes out to disk one frame at a time. That's probably as fast as it can go. It's probably the call to imwrite() that is taking the time. I assume you're using SSD for your drive. You might try using a ramdisk instead of your SSD to save your individual files.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeConvert Image Type についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by