Porkodi Devi
Porkodi Devi

Reputation: 27

Cannot determine number of frames using VideoReader

I have loaded a video in MATLAB using VideoReader and have converted it to frames. However, I have read only 200 frames and have saved it. For these 200 frames read, 640 images are saved in the current folder.

How come 200 frames are converted into 640 images?

The code I wrote is shown below:

xyloObj = VideoReader(filename);
vid = read(xyloObj,[1 200]);  
frm_cnt=length(vid);
str2='.jpg';
for i=1:frm_cnt
    frm(i)=aviread(filename,i); % read the Video file
    frm_name=frame2im(frm(i));  % Convert Frame to image file
    filename1=strcat( num2str(i),str2);
    imwrite(frm_name,filename1);      % Write image file
end

Upvotes: 1

Views: 344

Answers (1)

rayryeng
rayryeng

Reputation: 104535

That's because you are incorrectly retrieving the total number of frames. vid is a H x W x B x F matrix, such that:

  • H is the image frame height
  • W is the image frame width
  • B is the number of bands in the image (e.g. 3 for RGB),
  • F is the number of frames read

length retrieves the largest dimension for a matrix. In this case, this is reporting 640, which probably means that either your width or height of your video is 640 pixels. This does not return the total number of frames. If you want the total number of frames that is read in, you can take a look at the 4th dimension of the matrix you grabbed using VideoReader/read:

frm_cnt = size(vid, 4);

However, if you're curious about how many total frames there are in the video sequence, you can use get on the VideoReader object to obtain this:

total_frames = get(xyloObj, 'numberOfFrames');

Upvotes: 2

Related Questions