如何在 OpenCV 中使用 C++ 裁剪檢測到的面部?


我們將學習如何在 OpenCV 中裁剪檢測到的面部。要裁剪檢測到的面部,我們需要多個矩陣。最合適的方法是使用影像陣列。在這個程式中,使用以下兩行程式碼,我們聲明瞭兩個影像矩陣:

  • Mat cropped_faces[4];
  • Mat faceROI[4];

第一個矩陣用於儲存裁剪後的影像,第二個矩陣用於定義感興趣區域。在檢測過程中,程式首先定位面部並將它們儲存在向量中。在我們的程式中,向量的名稱為“faces”。向量可以包含多個元素。

使用以下兩行程式碼,我們識別向量並定位它們在影像中的位置,最後在“faceROI[i]”矩陣中裁剪面部區域。

  • faceROI[]=image_with_humanface(faces[i]);
  • cropped_faces[i]=faceROI[i];

第一行定位包含名為“image_with_humanface”影像上面部的向量,裁剪它並將其儲存到名為“faceROI[i]”的矩陣中。在第二行中,裁剪後的影像被傳遞到另一個矩陣陣列。此矩陣陣列已用於顯示裁剪後的影像。

以下程式裁剪檢測到的面部並在單獨的視窗中顯示它們。

示例

#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//
   Mat cropped_faces[3];//Declaring an array of matrix of 4 elements to show the cropped faces//
   Mat faceROI[3];//Declaring an array of matrix of 4 elements to hold the cropped faces//
   image_with_humanface = imread("friends3.jpg");//loading an image that contains human face in it//
   namedWindow("Face1");//Declaring an window to show 1st cropped face//
   namedWindow("Face2");//Declaring an window to show 2nd cropped face//
   namedWindow("Face3");//Declaring an window to show 3rd cropped face//  
   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//
      faceROI[i] = image_with_humanface(faces[i]);
      cropped_faces[i] = faceROI[i];
      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("Face1", cropped_faces[0]);//Showing the 1st cropped face//
   imshow("Face2", cropped_faces[1]);//Showing the 2nd cropped face//
   imshow("Face3", cropped_faces[2]);//Showing the 3rd cropped face//
   waitKey(0);//To wait for a keystroke to terminate the program
   return 0;
}

輸出

更新於: 2021年3月10日

655 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告