1

我正在尝试使用 MATLAB 制作实时人脸检测器。我在 Mathworks 的页面上找到了示例代码,但它使用了示例视频。我遇到的问题是,即使在开始框架中有几张面孔,此代码也只能跟踪它选择的那个。我需要它一次跟踪多个面孔。是否可以通过不剧烈的更改此代码来实现。我在 MathWorks 的网页上找到了以下代码:

% 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');

% Get the skin tone information by extracting the Hue from the video frame
% converted to the HSV color space.
[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])

% Detect the nose within the face region. The nose provides a more accurate
% measure of the skin tone because it does not contain any background
% pixels.
noseDetector = vision.CascadeObjectDetector('Nose');
faceImage    = imcrop(videoFrame,bbox(1,:));
noseBBox     = step(noseDetector,faceImage);

% The nose bounding box is defined relative to the cropped face image.
% Adjust the nose bounding box so that it is relative to the original video
% frame.
noseBBox(1,1:2) = noseBBox(1,1:2) + bbox(1,1:2);

% 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);

提前致谢!

4

1 回答 1

4

该示例旨在仅跟踪单个人脸。要跟踪多个对象,请查看此示例,该示例使用vision.KalmanFilter对象进行跟踪。您可以将本例中的检测部分替换为检测人脸的代码。

或者,看看这个使用 KLT 算法( vision.PointTracker) 跟踪点的示例。您也可以对其进行修改以跟踪多个面孔,但这是相当多的工作。您将不得不做很多簿记来跟踪哪些点属于哪个面。

编辑: 这是一个如何使用跟踪多个面的示例。vision.PointTracker

于 2013-10-23T15:41:11.503 回答