Convert AVI file to series of images
조회 수: 46 (최근 30일)
이전 댓글 표시
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
채택된 답변
Chandra Kurniawan
2012년 3월 11일
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
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
2021년 6월 30일
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
댓글 수: 0
Image Analyst
2012년 3월 11일
See my code for doing that here: http://www.mathworks.com/matlabcentral/answers/30847-how-save-frames-that-exit-from-video-in-a-specific-folder
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
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
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
2019년 9월 23일
편집: Sushil Sharma
2019년 9월 23일
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
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
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!