I am on a problem. My problem is that I want to split all image frames from an avi video. First of all I used aviread() function it gives me the out of memory error. Then from online help I were using mmreader() and read() function to split image frames, but the problem is read images from read() function could not be showed with imshow() function. I have the following code snippet,
function test()
A='G:\ims\avi\nh.avi';
B=mmreader(A);
ims=read(B,[2000 2200]);
figure(1),imshow(ims(1));
end
I hoped this code would show the first image frame but it does not. In this code I am free of out of memory error because I only read 200 frames. But the problem still remains when I try to read all the frames. So mainly I have the following two problems,
- how can I get rid of out of memory problem using mmreader() and read() ?
- why not imshow() above does not show the image frame?
To get rid of the out of memory error consider reading in a single frame inside of a loop as shown in the mmreader documentation (doc mmreader
):
for k = 2000 : 2200
ims = read(B, k);
end
The reason imshow
is not working is that the value returned by read(...)
is Height x Width x Colors x NumFrames Where Height is the height of the video, Width is the width of the video, Colors is the number of colors (usually 3) and NumFrames is the number of frames you read.
To display the first frame use:
imshow(ims(:,:,:,1));
If you want to implement a basic video player, here is an example:
mov = VideoReader('xylophone.mpg'); %# use mmreader on older versions
for i=1:mov.NumberOfFrames
img = read(mov,i);
imshow(img)
drawnow
end
This reads one frame at a time, and displays it using IMSHOW. Note that it is required to call DRAWNOW (or pause with some small value) so that the GUI event queue gets flushed.
If you are interested, I showed in a previous answer an example of a GUI for browsing the frames of a video file.
This splits a video into frames with no need for extra codecs:
clc;
close all;
% Open an sample avi file
filename = '.\003.AVI';
mov = MMREADER(filename);
% Output folder
outputFolder = fullfile(cd, 'frames');
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
%getting no of frames
numberOfFrames = mov.NumberOfFrames;
numberOfFramesWritten = 0;
for frame = 1 : numberOfFrames
thisFrame = read(mov, frame);
outputBaseFileName = sprintf('%3.3d.png', frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
imwrite(thisFrame, outputFullFileName, 'png');
progressIndication = sprintf('Wrote frame %4d of %d.', frame,numberOfFrames);
disp(progressIndication);
numberOfFramesWritten = numberOfFramesWritten + 1;
end
progressIndication = sprintf('Wrote %d frames to folder "%s"',numberOfFramesWritten, outputFolder);
disp(progressIndication);
Here is what I use to split up videos and recombine them into animated GIF's. I am sure you can adapt it into whatever you like. It is entirely based on code snippets from MATLAB help.
function [ startframe, endframe ] = catgif( inputvideoname, outputfilename,...
startframe, endframe, preview)
InputVideo = VideoReader(inputvideoname);
filename = outputfilename;
if (endframe > InputVideo.NumberOfFrames)
endframe = InputVideo.NumberOfFrames;
end
figure(1)
for ii = 1:endframe
if (ii >= startframe)
img = read(InputVideo,ii);
%Resize or rotate as appropriate.
%img = imresize(imrotate(img, -90),0.5, 'bicubic');
img = imresize(img,0.5, 'bicubic');
imshow(img,'Border','tight');
drawnow
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if ~strcmp(preview, 'yes')
if ii == startframe;
imwrite(imind,cm,filename,'gif', 'DelayTime', 0, 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','DelayTime', 0, 'WriteMode','append');
end
end
end
end
来源:https://stackoverflow.com/questions/11711716/splitting-video-into-images-in-matlab