splitting video into images in matlab

血红的双手。 提交于 2019-11-28 06:39:12

问题


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,

  1. how can I get rid of out of memory problem using mmreader() and read() ?
  2. why not imshow() above does not show the image frame?

回答1:


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));



回答2:


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.




回答3:


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);



回答4:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!