/*************************************** **************************************** **************************************** **************************************** * ************************** Translation: mastering next to the previous article: opencv with practical computer vision projects (Chapter 1) continue reading
Reprinted! Please specify the source
**************************************** **************************************** **************************************** ***********/
Compared with human beings, we can effectively distinguish different people by comparing different faces. The Haar-based and lda-based detectors can be trained to find specific faces from a large number of face images, the training information is generally stored in XML files and will be used later. For Classifier Training, at least 1000 face photos and 1000 non-face photos are required, such as leaves, vehicles, and texts. The training process may take a lot of time. On a dual-core computer, generally, it takes several hours to train the KNN classifier, while the Haar classifier may take one week. Fortunately, opencv already contains some pre-trained classifiers for our use! We can select a classifier with different target features, such as Haar or lb, to detect the face, side face, eyes, nose, and mouth. We only need to load different XML files as needed.
Use opencv for Face Detection
As mentioned above, opencv2.4 and later versions already contain many trained classifiers, which are stored as XML files. We can select different XML files based on different intentions. The following table lists some common XML files:
Extension: Currently the latest opencv2.4.9 version of the pre-classifier has been much better, they are in the file directory D: \ OpenCV-4.9 \ opencv \ sources \ data: opened with three folders, haarcacsdes, lbpcacsdes, and hogcacsdes respectively. Open them separately and you will see the following information: (This text includes the following picture is not in the original text)
For our face recognition project, we want to be able to detect positive faces. Therefore, we use a facial recognition detector (guid), mainly because it is fast and license-free. Note that the library after opencv2.x contains a pre-trained facial recognition detector, but its accuracy is not high. If you want to obtain a high accuracy, then you may need to re-train your own Gbit/s detector or use the Haar detector.
Loading a Haar or lbdetector for object or face detectionBefore performing the target detection, we need to load these trained XML files in the following ways:
<span style="font-size:18px;">CascadeClassifier faceDetector;faceDetector.load(faceCascadeFilename);</span>
In this step, the corresponding Haar or lbdetector can be loaded using different file names. The common error here is that the file path is incorrect and loading fails. The file path is not absolute, it depends on your build environment.
Detecting an object using the Haar or lbclassifierAfter loading the classifier, we can use it to detect the faces in each frame of the video. However, before detection, we also need to pre-process the video image by taking the following steps:
- Grayscale: Face Detection is only performed on grayscale images. Therefore, we need to grayscale the video frames captured by the camera.
- Image shrinking: The face detection speed mainly depends on the size of the input image. (large images are slow to detect, while small images are fast to detect ,) the reliability of detection is good even in small sizes. So we should shrink the image to the most appropriate size.
- Histogram equalization: Face Detection is ineffective under Low Illumination, so we need to improve the brightness and contrast through histogram equalization.
Grayscale Color ConversionWe can use the cvtcolor () function to easily Convert RGB Images to grayscale images, but the premise is that we are not using a grayscale camera. During conversion, we need to define the format of the original image. Generally, the desktop captures RGB three-channel images, while the mobile device captures bgra four-channel images. Therefore, three input image formats must be allowed in the code, as shown below:
Mat gray;if (img.channels() == 3){cvtColor(img,gray,CV_BGR2GRAY);}else if (img.channels() == 4){cvtColor(img,gray,CV_BGRA2GRAY);}else{gray = img;}
Shrinking the camera imageWe can use resize () to scale the image to a specified size, or scale it proportionally. Face recognition can be used when the image size is greater than 240x240 pixels, because it can find a face with a size greater than 20x20 pixels. Below we set the image width to 320, regardless of whether the VGA Network Camera or HD camera is input. Note that after the detection is complete, remember to extend the detection result to the original size, because you are detecting in the scaled image, so the result is also scaled. We must ensure that the scaled image does not look too fat or thin. For example, if a 800x400 image is scaled to 300x200, the image looks thinner. Therefore, we need to make the scaled image have the same aspect ratio as the original image.
const int DETECTION_WIDTH = 320;Mat smallImg;float scale = img.cols/(float) DETECTION_WIDTH;if (img.cols>DETECTION_WIDTH){int scaleHeight = cvRound(img.rows/scale);resize(img,smallImg,Size(DETECTION_WIDTH,scaleHeight));} else{smallImg = img;}
Histogram equalization
We can improve the brightness and contrast of the image through histogram equalization. Sometimes it makes the image look strange, but in fact, it increases the brightness and contrast of the image and helps face detection. The equalizehist () function is used as follows:
Mat equalizedImg;equalizeHist(inputImg,equalizedImg);
Detecting the faceNow we have finished grayscale, scaled to the appropriate size, and histogram equalization. Next we will use the cascadeclassifier: detectmultiscale () function to detect faces. First, we need to pass some parameters to this function, as shown below:
- Minfeaturesize: this parameter determines the minimum size of the face we are detecting. Generally, we will set it to 20x20 or 30x30, of course, this value depends on the size of the image you are using. If you are using a mobile phone camera or a computer camera, the face is generally close to the camera, you can set the minimum size to 80x80, which greatly improves the detection speed. But if you want to detect faces that are far away from the camera, such as friends on the beach, you need to set the size to 20x20.
- Searchscalefactor: this parameter determines how many faces of different sizes can be detected. Generally, it is set to 1.1. If you want to make the detection faster, you can set it to 1.2, but some faces may be missed.
- Minneighbors: this parameter indicates the detector's grasp of the detected face. Generally, we set this parameter to 3. But if you want more reliable detection results, you can increase this parameter as appropriate, of course there may be a lot of faces that won't be detected.
- Flags: this parameter allows you to select a search method, such as whether to search all faces (this is the default mode), or to search only the largest faces (cascade_find_biggest_object ). If you only search for the largest face, the detection speed returns fast. Other parameters can be used to increase the detection speed by one or two, such as cascade_do_rough_search or cascade_scale_image.
The function detectmultiscale () Outputs a vector <rect>. For example, if you detect a face, It outputs two rectangles.
int flags = CASCADE_SCALE_IMAGE;Size minFeatureSize(20,20);float searchScaleFactor = 1.1f;int minNeighbors = 4;std::vector<Rect> faces;faceDetector.detectMultiScale(img,faces,searchScaleFactor,minNeighbors,flags,minFeatureSize);
It turns out that it is so hard to translate articles and write blogs ...... alas, there are a lot more. Today, let's go here ,, not complete .........................................