MATLAB中的实时人脸检测

时间:2013-10-20 15:32:49

标签: matlab tracking face-detection viola-jones matlab-cvst

我正在尝试使用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);

提前致谢!

1 个答案:

答案 0 :(得分:3)

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

或者,请查看此example that uses the KLT algorithmvision.PointTracker)以跟踪积分。您可以修改它以跟踪多个面,但这是相当多的工作。你必须做很多记账才能跟踪哪些点属于哪个面。

修改 以下是vision.PointTracker如何使用{{1}}跟踪多个面孔。