splitting video into images in matlab

旧时模样 提交于 2019-11-29 13:03:09

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

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.

yoyoabc

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