验证了matlab官网例程,例程如下:
https://ww2.mathworks.cn/help/vision/examples/face-detection-and-tracking-using-camshift.html
在运行的时候发现有一行代码报错。
错误使用 matlab.system.SystemProp/parseInputs (line 862)
vision.CascadeObjectDetector 系统对象构造函数仅支持 1 个仅实值输入。您指定了 3 个仅实值输入。此错误的常见原因是属性名称拼写错误。
出错 matlab.system.SystemProp/sysObjSetProperties (line 294)
出错 vision.CascadeObjectDetector (line 193)
setProperties(obj,nargin,varargin{:},'ClassificationModel');
出错 faceDetectionDemo (line 19)
noseDetector = vision.CascadeObjectDetector('Nose','UseROI',true);
原因大概是版本问题,可以更换到最新版本的matlab
如果不更换的话可以用下面这个方法:
也就是将其中两行代码进行替换
%noseDetector = vision.CascadeObjectDetector('Nose', 'UseROI', true);
%noseBBox = step(noseDetector, videoFrame, bbox(1,:));
noseDetector = vision.CascadeObjectDetector('Nose');
noseBBox = step(noseDetector, videoFrame);
解决的方法参考下面这个网站:
完整代码如下
% Create a cascade detector object.
faceDetector = vision.CascadeObjectDetector();
% Read a video frame and run the detector.
videoFileReader = vision.VideoFileReader('visionface.avi');
videoFrame = step(videoFileReader);
bbox = step(faceDetector, videoFrame);
% Draw the returned bounding box around the detected face.
videoOut = insertObjectAnnotation(videoFrame,'rectangle',bbox,'Face');
figure, imshow(videoOut), title('Detected face');
[hueChannel,~,~] = rgb2hsv(videoFrame);
% Display the Hue Channel data and draw the bounding box around the face.
figure, imshow(hueChannel), title('Hue channel data');
rectangle('Position',bbox(1,:),'LineWidth',2,'EdgeColor',[1 1 0])
%noseDetector = vision.CascadeObjectDetector('Nose','UseROI',true);
noseDetector = vision.CascadeObjectDetector('Nose');
noseBBox = step(noseDetector, videoFrame);
% Create a tracker object.
tracker = vision.HistogramBasedTracker;
% Initialize the tracker histogram using the Hue channel pixels from the
% nose.
initializeObject(tracker, hueChannel, noseBBox(1,:));
% Create a video player object for displaying video frames.
videoInfo = info(videoFileReader);
videoPlayer = vision.VideoPlayer('Position',[300 300 videoInfo.VideoSize+30]);
% Track the face over successive video frames until the video is finished.
while ~isDone(videoFileReader)
% Extract the next video frame
videoFrame = step(videoFileReader);
% RGB -> HSV
[hueChannel,~,~] = rgb2hsv(videoFrame);
% Track using the Hue channel data
bbox = step(tracker, hueChannel);
% Insert a bounding box around the object being tracked
videoOut = insertObjectAnnotation(videoFrame,'rectangle',bbox,'Face');
% Display the annotated video frame using the video player object
step(videoPlayer, videoOut);
end
% Release resources
release(videoFileReader);
release(videoPlayer);