Matlab does not recognize the hasFrame for input type VideoReader
Show older comments
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
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.
Accepted Answer
More Answers (1)
Hakin Gutiérrez
on 27 May 2021
0 votes
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
Geoff Hayes
on 27 May 2021
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?
Hakin Gutiérrez
on 27 May 2021
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
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
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
Categories
Find more on Computer Vision with Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!