Matlab does not recognize the hasFrame for input type VideoReader

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 Kommentar

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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

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 Kommentare

I have R2014a, so maybe if i update the problem will go away thanks! Sairon
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().

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Hakin Gutiérrez
Hakin Gutiérrez am 27 Mai 2021

0 Stimmen

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 Kommentare

Hi Hakin - if I run the code that is posted above
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
then the hasFrame works as expected using version 2021a. What code have you written? What is the error?
I've been trying to make a motion detector with a live feed with a webcam. When I use the hasFrame it returns an error saying that it doesn't exist as a function. I'm currently using Matlab 2020b, 2021a has an error when i try to install the "MATLAB Support Package for USB Webcams" extension. I thik the problem lies around this section of the code.
function MotionBasedMultiObjectTrackingExample()
% Create System objects used for reading video, detecting moving objects,
% and displaying the results.
obj = setupSystemObjects();
tracks = initializeTracks(); % Create an empty array of tracks.
nextId = 1; % ID of the next track
% Detect moving objects, and track them across video frames.
while hasFrame(obj.reader)
frame = snapshot(obj.reader);
[centroids, bboxes, mask] = detectObjects(frame);
predictNewLocationsOfTracks();
[assignments, unassignedTracks, unassignedDetections] = detectionToTrackAssignment();
updateAssignedTracks();
updateUnassignedTracks();
deleteLostTracks();
createNewTracks();
displayTrackingResults();
end
function obj = setupSystemObjects()
% Initialize Video I/O
% objects in each frame, and playing the video.
cam = webcam();
% Create a video file reader.
obj.reader = snapshot(cam);
% Create two video players, one to display the video,
% and one to display the foreground mask.
obj.videoPlayer = vision.VideoPlayer('Position', [20, 400, 700, 400]);
obj.maskPlayer = vision.VideoPlayer('Position', [740, 400, 700, 400]);
% Create System objects for foreground detection and blob analysis
% The foreground detector is used to segment moving objects from
% the background. It outputs a binary mask, where the pixel value
% of 1 corresponds to the foreground and the value of 0 corresponds
% to the background.
obj.detector = vision.ForegroundDetector('NumGaussians', 3, ...
'NumTrainingFrames', 40, 'MinimumBackgroundRatio', 0.7);
% Connected groups of foreground pixels are likely to correspond to moving
% objects. The blob analysis System object is used to find such groups
% (called 'blobs' or 'connected components'), and compute their
% characteristics, such as area, centroid, and the bounding box.
obj.blobAnalyser = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'AreaOutputPort', true, 'CentroidOutputPort', true, ...
'MinimumBlobArea', 400);
end
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.")
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

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by