如何使用C++在OpenCV中檢測靜態影像中的面部?


我們從影像中檢測人臉。為了檢測人臉,我們使用了 `detectMultiScale()` 函式。

該函式的實際格式如下:

語法

detectMultiScale(source matrix, vector, searchScaleFactor, minNeighbours, flags, minfeatureSize)

透過更改函式引數,我們可以控制 `detectMultiScale()` 函式。此函式採用以下引數。

源矩陣

這是將要檢測人臉的矩陣。在本例中,它將是儲存影片幀的矩陣。

向量

`detectMultiScale()` 函式將是一個矩形型別的向量。在OpenCV中,矩形是一個向量,我們必須將其定義為向量。

搜尋比例因子 (searchScaleFactor)

搜尋比例因子決定了函式將查詢多少種不同大小的人臉。我們通常使用 1.1。如有必要,我們可以使用 1.2 來加快檢測系統的速度。但是,在這種情況下,人臉的檢測頻率不如使用 1.1 時高。

最小鄰域 (minNeighbours)

此引數檢測檢測器的置信度級別。這意味著此函式顯示檢測器檢測到人臉的置信度有多高。為了獲得更好的可靠性,我們可以使用更大的數字,但這會減慢處理速度。為了加快處理速度但降低可靠性,我們可以使用較小的數字。我們通常使用 3 或 4 作為 minNeighbours。

標誌 (flags)

預設情況下,該函式將查詢所有的人臉。如果我們使用 `CASCADE_FIND_BIGGEST_OBJECT` 作為標誌的值,它將只查詢最大的人臉。在這種情況下,系統的效能會更快。使用 `CASCADE_SCALE_IMAGE`,我們可以搜尋多個人臉。

最小特徵大小 (minFeatureSize)

minFeatureSize 決定人臉的最小大小。如果我們想要檢測遠離攝像機的人臉,那麼我們應該使用較小的值。如果人臉靠近攝像機,我們應該使用較大的值。對於典型的距離,我們使用 (20 x 20) 或 (30 x 30) 的大小。在示例中,我們使用 `detectMultiScale()` 函式如下:

faceDetector.detectMultiScale(image_with_humanface, faces, 1.1, 4, CASCADE_SCALE_IMAGE, Size(20, 20));//

以下程式碼演示瞭如何在OpenCV中從靜態影像中檢測人臉。

示例

#include<iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
//This header includes definition of 'rectangle()' function//
#include<opencv2/objdetect/objdetect.hpp>
//This header includes the definition of Cascade Classifier//
#include<string>
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
   Mat image_with_humanface;//Declaring a matrix to load image with human faces//
   image_with_humanface = imread("friends.jpg");//loading an image that contains human face in it//
   namedWindow("Face Detection");//Declaring a window to show the result//
   string trained_classifier_location = "C:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml";//Defining the location our XML Trained Classifier in a string//
   CascadeClassifier faceDetector;//Declaring an object named 'face detector' of CascadeClassifier class//
   faceDetector.load(trained_classifier_location);//loading the XML trained classifier in the object//
   vector<Rect>faces;//Declaring a rectangular vector named faces//
   vector<Rect>boundary;//Declaring a rectangular vector named rectangle//
   faceDetector.detectMultiScale(image_with_humanface, faces, 1.1, 4, CASCADE_SCALE_IMAGE, Size(20, 20));//Detecting the faces in 'image_with_humanfaces' matrix//
   for (size_t i = 0; i < faces.size(); i++){ //Loop to draw rectangle around the faces//
      Mat faceROI = image_with_humanface(faces[i]);//Storing the face in a matrix//
      int x = faces[i].x;//Getting the initial row value of face rectangle's starting point//
      int y = faces[i].y;//Getting the initial column value of face rectangle's starting point//
      int h = y + faces[i].height;//Calculating the height of the rectangle//
      int w = x + faces[i].width;//Calculating the width of the rectangle//
      rectangle(image_with_humanface, Point(x, y), Point(w, h), Scalar(255, 0, 255), 2, 8, 0);//Drawing a rectangle using around the faces//
   }
   imshow("Face Detection", image_with_humanface);//Showing the detected face//
   waitKey(0);//To wait for keystroke to terminate the program//
   return 0;
}

輸出

更新於:2021年3月10日

623 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告