When it comes to face detection, we must first understand the Haar feature classifier. To put it bluntly, the Haar feature classifier is a series of xml files, which describe the characteristic values of various parts of the human body, such as faces, eyes, etc. The following feature files are provided in OpenCV3.2.0:
haarcascade_eye.xml
haarcascade_eye_tree_eyeglasses.xml
haarcascade_frontalcatface.xml
haarcascade_frontalcatface_extended.xml
haarcascade_frontalface_alt.xml
haarcascade_frontalface_alt_tree.xml
haarcascade_frontalface_alt2.xml
haarcascade_frontalface_default.xml
haarcascade_fullbody.xml
haarcascade_lefteye_2splits.xml
haarcascade_licence_plate_rus_16stages.xml
haarcascade_lowerbody.xml
haarcascade_profileface.xml
haarcascade_righteye_2splits.xml
haarcascade_russian_plate_number.xml
haarcascade_smile.xml
haarcascade_upperbody.xml
By loading different feature files, the corresponding detection effect can be achieved.
Description of the detectMultiScale function parameter in OpenCV3.2.0:
detectMultiScale(Mat image, MatOfRect objects, double scaleFactor, int minNeighbors, int flags, Size minSize, Size maxSize)
image: The image to be detected is generally grayscale (improving efficiency)
objects: a rectangular box vector group of detected objects
scaleFactor: The scale coefficient of the search window in two consecutive scans. The default is 1.1, that is, each search window expands by 10% in sequence.
minNeighbors: The minimum number of adjacent rectangles that constitute the detection target (default is 3)
flags: either use the default value or use CV_HAAR_DO_CANNY_PRUNING. If set to CV_HAAR_DO_CANNY_PRUNING, the function will use Canny edge detection to exclude areas with too many or too few edges, so these areas will not usually be the area where the face is located
minSize: The minimum range of the obtained target area
maxSize: The maximum range of the obtained target area
Face detection sample code:
import org.opencv.core.Core;import org.opencv.core.Mat;import org.opencv.core.MatOfRect;import org.opencv.core.Point;import org.opencv.core.Rect;import org.opencv.core.Scalar;import org.opencv.imgcodecs.Imgcodecs;import org.opencv.imgproc.Imgproc;import org.opencv.objdetect.CascadeClassifier;public class FaceDetect{ public static void main(String[] args) { // TODO Auto-generated method stub System.loadLibrary(Core.NATIVE_LIBRARY_NAME); System.out.println("/nRunning FaceDetector"); CascadeClassifier faceDetector = new CascadeClassifier(); faceDetector.load( "C://Program Files//opencv//sources//data//haarcascades//haarcascades//haarcascades_frontalface_alt.xml"); Mat image = Imgcodecs.imread("F://1114.jpg"); MatOfRect faceDetections = new MatOfRect(); faceDetector.detectMultiScale(image, faceDetections); System.out.println(String.format("Detected %s faces", faceDetections.toArray().length)); for (Rect rect : faceDetections.toArray()) { Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0)); } String filename = "F://ouput.jpg"; Imgcodecs.imwrite(filename, image); }} Source image and result diagram:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.