Computer >> Máy Tính >  >> Lập trình >> C ++

Làm cách nào để cắt các khuôn mặt được phát hiện trong OpenCV bằng C ++?

Chúng ta sẽ biết cách cắt các khuôn mặt được phát hiện trong OpenCV. Để cắt các khuôn mặt được phát hiện, chúng ta cần nhiều ma trận. Cách thích hợp nhất là sử dụng một mảng hình ảnh. Trong chương trình này sử dụng hai dòng sau, chúng tôi đã khai báo hai ma trận hình ảnh -

  • Mat crop_faces [4];
  • Mat faceROI [4];

Ma trận đầu tiên là để lưu trữ các hình ảnh đã cắt và ma trận thứ hai là để xác định vùng quan tâm. Trong quá trình phát hiện, đầu tiên, chương trình định vị các khuôn mặt và lưu trữ chúng trong các vectơ. Trong chương trình của chúng tôi, tên của vectơ là 'mặt' Các vectơ có thể chứa nhiều phần tử.

Sử dụng hai dòng sau, chúng tôi xác định các vectơ và xác định vị trí của chúng trong hình ảnh và cuối cùng cắt vùng khuôn mặt trong ma trận 'faceROI [i]'.

  • faceROI [] =image_with_humanface (khuôn mặt [i]);
  • crop_faces [i] =faceROI [i];

Dòng đầu tiên xác định vị trí vector chứa các khuôn mặt trên hình ảnh có tên 'image_with_humanface' và cắt nó và lưu trữ nó vào ma trận có tên 'faceROI [i]'. Trong dòng thứ hai, các hình ảnh đã cắt đang được chuyển sang một mảng ma trận khác. Mảng ma trận này đã được sử dụng để hiển thị các hình ảnh đã cắt.

Chương trình sau cắt các khuôn mặt được phát hiện và hiển thị chúng trong các cửa sổ riêng biệt.

Ví dụ

#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;
}

Đầu ra

Làm cách nào để cắt các khuôn mặt được phát hiện trong OpenCV bằng C ++?