Reading and displaying a video file by frame

I have been working with Matlab recently. I want to read a video file and do some calculations in each frame and display each frame. I wrote the following code, but each time it displays only the first frame. anyone can help.

mov=VideoReader('c:\vid\Akiyo.mp4');
nFrames=mov.NumberOfFrames;
for i=1:nFrames
  videoFrame=read(mov,i);
  imshow(videoFrame);

end
+5
source share
3 answers

Note: the API mmreaderwas terminated by MATLAB, so prefer to use VideoReader.

See the comment from @Vivek.

I usually do this:

obj=mmreader('c:\vid\Akiyo.mp4');
nFrames=obj.NumberOfFrames;
for k=1:nFrames
    img=read(obj,k);
    figure(1),imshow(img,[]);
end

As for your code, I saw MATLAB documentation. You should do things in the following order:

mov=VideoReader('c:\vid\Akiyo.mp4');
vidFrames=read(mov);
nFrames=mov.NumberOfFrames;
for i=1:nFrames
   imshow(vidFrames(:,:,i),[]);  %frames are grayscale
end
+9
source

The read () function and NumberOfFrames () field are now deprecated, Matlab suggests using

xyloObj = VideoReader(file);
vidHeight = xyloObj.Height;
vidWidth = xyloObj.Width;
mov = struct('cdata',zeros(vidHeight, vidWidth, 3,'uint8'), 'colormap',[]);

while hasFrame(xyloObj)
    mov(k).cdata = readFrame(xyloObj,'native');     
end

If you want to estimate the number of frames in a video, use nFrames = floor(xyloObj.Duration) * floor(xyloObj.FrameRate);

+1

imshow(vidFrames(:,:,i),[]);

obj = VideoReader('path/to/video/file');

for img = 1:obj.NumberOfFrames;
    filename = strcat('frame',num2str(img),'.jpg');
    b = read(obj,img);
    imwrite(b,filename);
end

. , Vivek Parag

You need to use VideoReader since mmreader has been discontinued by MATLAB.

0
source

All Articles