本文演示了如何利用Matlab对视频中的人脸进行识别。代码中主要运用ViolaJones算法,会对识别的眼部,鼻部,嘴部等进行点标注。
代码来源:
https://cn.mathworks.com/examples/matlab-computer-vision/mw/vision-ex10252853-face-detection-and-tracking-using-the-klt-algorithm
相应的工具箱下载:
https://cn.mathworks.com/matlabcentral/profile/authors/3215751-mathworks-image-acquisition-toolbox-team
录制结果
代码部分
% Detect objection by reading a video
clc
clear
% Create a cascade detector object.
faceDetector = vision.CascadeObjectDetector();
% Read a video frame and run the face detector.
videoFileReader = vision.VideoFileReader('ellen.mov');
videoFrame =step(videoFileReader);
bbox = step(faceDetector, videoFrame);
% Draw the returned bounding box around the detected face.
videoFrame = insertShape(videoFrame, 'Rectangle',bbox);
figure; imshow(videoFrame); title('Detected face');
% Convert the first box into a list of 4 points
% This is needed to be able to visualize the rotation of theobject.
bboxPoints = bbox2points(bbox(1, :));
% Detect feature points in the face region.
points = detectMinEigenFeatures(rgb2gray(videoFrame), 'ROI', bbox);
% Display the detected points.
figure, imshow(videoFrame), hold on, title('Detected features');
plot(points);
% Create a point tracker and enable the bidirectional errorconstraint to
% make it more robust in the presence of noise and clutter.
pointTracker = vision.PointTracker('MaxBidirectionalError',2);
% Initialize the tracker with the initial point locations and theinitial
% video frame.
points = points.Location;
initialize(pointTracker, points, videoFrame);
videoPlayer = vision.VideoPlayer('Position',...
[100 100[size(videoFrame, 2), size(videoFrame, 1)]+30]);
% Make a copy of the points to be used for computing the geometric
% transformation between the points in the previous and the currentframes
oldPoints = points;
while ~isDone(videoFileReader)
% get the next frame
videoFrame =step(videoFileReader);
% Track the points. Note that some points may be lost.
[points,isFound] = step(pointTracker, videoFrame);
visiblePoints= points(isFound, :);
oldInliers =oldPoints(isFound, :);
if size(visiblePoints, 1) >= 2 % need atleast 2 points
% Estimate the geometric transformationbetween the old points
% and the new points and eliminate outliers
[xform,oldInliers, visiblePoints] = estimateGeometricTransform(...
oldInliers, visiblePoints, 'similarity','MaxDistance', 4);
% Apply the transformation to the boundingbox points
bboxPoints= transformPointsForward(xform, bboxPoints);
% Insert a bounding box around the objectbeing tracked
bboxPolygon= reshape(bboxPoints', 1, []);
videoFrame= insertShape(videoFrame, 'Polygon', bboxPolygon, ...
'LineWidth', 2);
% Display tracked points
videoFrame= insertMarker(videoFrame, visiblePoints, '+', ...
'Color', 'white');
% Reset the points
oldPoints= visiblePoints;
setPoints(pointTracker,oldPoints);
end
% Display the annotated video frame using the video playerobject
step(videoPlayer,videoFrame);
end
% Clean up
release(videoFileReader);
release(videoPlayer);
release(pointTracker);
常见问题
程序无法运行,需要下载安装链接2中的相应工具包。下载时需要Matlab账号。
运行时出错,换一个视频或者转换视频格式。
领取专属 10元无门槛券
私享最新 技术干货