How to crop the detected faces in OpenCV using C++?

OpenCVC++Server Side ProgrammingProgramming

We will know how to crop the detected faces in OpenCV. To crop detected faces, we need multiple matrices. The most appropriate way is to use an image array. In this program using the following two lines, we have declared two image matrices −

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

The first matrix is to store the cropped images, and the second matrix is to define the region of interest. In the detection process, first, the program locates the faces and store them in vectors. In our program, the name of the vector is 'faces' Vectors can contain multiple elements.

Using the following two lines, we identify the vectors and locate their position in the image and finally crop the face region in the 'faceROI[i]' matrices.

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

The first line locate the vector containing faces on image named 'image_with_humanface' and crop it and store it into the matrix named 'faceROI[i]'. In the second line, the cropped images are being passed to another matrix array. This matrix array has been used to show the cropped images.

The following program crops the detected faces and show them in separate windows.

Example

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

Output

raja
Published on 10-Mar-2021 08:58:47
Advertisements