Matlab does not recognize the hasFrame for input type VideoReader

18 views (last 30 days)
I'm trying to just run the code to get to learn how to mess with video files. Unfortunately, one of the functions (hasFrame) doesn't seem to be working . Every time I try to use it it seems to think that the file type VideoReader is not a legitimate input. the code :
vidObj = VideoReader('xylophone.mp4');
vidHeight = vidObj.Height;
vidWidth = vidObj.Width;
s = struct('cdata',zeros(vidHeight,vidWidth,3,'uint8'),...
'colormap',[]);
k = 1;
while hasFrame(vidObj)
s(k).cdata = readFrame(vidObj);
k = k+1;
end
error says :
Undefined function 'hasFrame' for input arguments of type 'VideoReader'.
is there some package I should have downloaded ? what's up with this?
  1 Comment
Stephan Köhnen
Stephan Köhnen on 21 Jul 2016
Hey dude, maybe you can try this function to get your result.
Startscript:
vid = VideoReader('VideoFile.mp4');
frames = get_frames(vid, 1, 10)
Function:
function [ frame_array ] = get_frames( vid_input, start_frame, end_frame )
k = start_frame;
while k <= end_frame
frame_array{k,1} = read(vid_input,k);
disp(['Frame ' num2str(k) ' was taken.']);
k = k + 1;
end
end
Hope this will be helpfull,
cheers.

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 4 Dec 2015
isaironi - according to hasFrame, this function was introduced in version R2014b so you will need at least that version in order to make use of it. (If you are using an earlier version of MATLAB, you will probably have an issue with the http://www.mathworks.com/help/matlab/ref/videoreader.readframe.htm readframe> function too.)
As an alternative, try the following
numFrames = get(vidObj,'NumberOfFrames');
for k=1:numFrames
s(k).cdata = read(vidObj,k);
end
  2 Comments
isaironi
isaironi on 5 Dec 2015
I have R2014a, so maybe if i update the problem will go away thanks! Sairon
Image Analyst
Image Analyst on 5 Dec 2015
If that solves it, can you accept the answer, and Vote for it, to give Geoff reputation points?
For what it's worth, I'm attaching a video processing demo. Not sure what MATLAB version it requires, but it does use VideoReader().

Sign in to comment.

More Answers (1)

Hakin Gutiérrez
Hakin Gutiérrez on 27 May 2021
So, its 2021, I have this error displayed: "Unrecognized function or variable 'hasFrame'." And I already tried with 2019b, 2020b, and 2021a version, but it doesn't seems to be working.
  4 Comments
Steven Lord
Steven Lord on 27 May 2021
The field reader in the obj struct on which you try to call hasFrame is the output of the snapshot function on a webcam object. The snapshot function returns an image and images do not have a hasFrame method. In fact I'm not sure if any of the objects you're creating has a hasFrame method. In particular it's not listed as an object function for webcam objects on its documentation page.
Instead of asking the question "do you have frames available?" which a webcam is not prepared to answer you may want to model your approach using the techniques described on this documentation page. That uses a fixed number of frames to retrieve from the webcam; you might want to instead use a while loop to process the frames as long as there's "something interesting" in the image (for some definition of "something interesting.")
Hakin Gutiérrez
Hakin Gutiérrez on 9 Jun 2021
Thanks a lot, I'm currently working on it. Instead of creating a loop with the hasFrame function, I used a simple loop and it seem to work just fine

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!