import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
/**
* Detect and return a face
*
* @param frame The total webcam view. Must be in grayscale.
* @return A Mat that contains only the face
*/
public static Mat getFace(Mat frame, Mat drawFrame) {
MatOfRect faces = new MatOfRect();
// compute minimum face size (20% of the frame height)
if (absoluteFaceSize == 0) {
int height = frame.rows();
if (Math.round(height * 0.2f) > 0) {
absoluteFaceSize = Math.round(height * 0.2f);
}
}
// detect faces
faceCascade.detectMultiScale(frame, faces, 1.1, 3, 0 | CASCADE_SCALE_IMAGE,
new Size(absoluteFaceSize, absoluteFaceSize), new Size());
// each rectangle in faces is a face
Rect[] facesArray = faces.toArray();
for (int i = 0; i < facesArray.length; i++) {
Point center = new Point(facesArray[i].x + (facesArray[i].width / 2), facesArray[i].y + facesArray[i].height / 2);
Mat face = new Mat(frame, facesArray[i]);
MatOfRect eyes = new MatOfRect();
eyesCascade.detectMultiScale(face, eyes, 1.1, 2,
0 | CASCADE_SCALE_IMAGE, new Size(30, 30), new Size());
if (eyes.size().width > 0 && eyes.size().height > 0) {
Imgproc.ellipse(drawFrame, center, new Size(facesArray[i].width / 2, facesArray[i].height / 2),
0, 0, 360, new Scalar(0, 0, 0), 4, 8, 0);
}
}
return drawFrame;
}