C++ USES opencv for face detection

  • 2020-06-01 10:43:35
  • OfStack

All posts on this site are based on the unbuntu system, of course, a little modification of the same trial in windows, after this site's brain, the just posted that python face and eye detection procedures with C++ implementation, of course, also referred to a lot of great god's blog, let's take a look at the following:

Install opencv on the Linux system and I'll say it again just in case someone doesn't install or debug the program that sprays this site is a trap,
sudo apt-get install libcv-dev
sudo apt-get install libopencv-dev
See your usr/share/opencv/haarcascades directory have appeared a few training set. XML file, then I take play under 1 face and eye detection as a instance, procedure is as follows:

Many people can't compile opencv, so let me write a few more sentences to solve the problem of many beginners

When you have finished your copy code, save it as xiaorun.cpp. Please compile and try g++ -o xiaorun. / xiaorun.cpp-lopencv_imgproc-lopencv_core_lopencv_objdetect

Can be realized

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <iostream>
using namespace cv;
using namespace std;

void detectAndDraw( Mat& img, CascadeClassifier& cascade,
          CascadeClassifier& nestedCascade,
          double scale, bool tryflip );

int main()
  CascadeClassifier cascade, nestedCascade;
  bool stop = false;
  // frame = imread("renlian.jpg");
  VideoCapture cap(0);  // Turn on the default camera 
    return -1;
  Mat frame;
  Mat edges;
 detectAndDraw( frame, cascade, nestedCascade,2,0 );
 if(waitKey(30) >=0)
 stop = true;
  //CascadeClassifier cascade, nestedCascade;
  // bool stop = false;
  // The name of the trained file is placed in the same directory as the executable 
  // cascade.load("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml");
//  nestedCascade.load("/usr/share/opencv/haarcascades/aarcascade_eye.xml");
//  frame = imread("renlian.jpg");
//  detectAndDraw( frame, cascade, nestedCascade,2,0 );
  // waitKey();
  //  cap>>frame;
  //  detectAndDraw( frame, cascade, nestedCascade,2,0 );
    if(waitKey(30) >=0)
   stop = true;
  return 0;
void detectAndDraw( Mat& img, CascadeClassifier& cascade,
          CascadeClassifier& nestedCascade,
          double scale, bool tryflip )
  int i = 0;
  double t = 0;
  // Create a vector container for your face 
  vector<Rect> faces, faces2;
  // define 1 Some colors are used to indicate different faces 
  const static Scalar colors[] = {
    CV_RGB(255,0,255)} ;
  // Create a smaller image to speed up detection 
  //nt cvRound (double value)  right 1 a double Number of type 4 Give up 5 Enter and return 1 Integer number! 
  Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );
  // Convert to a grayscale image, Harr Features are based on grayscale 
  cvtColor( img, gray, CV_BGR2GRAY );
  // imshow(" gray ",gray);
  // Change the image size and use the bilinear difference 
  resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
 // imshow(" Smaller size ",smallImg);
  // The transformed image was averaged by histogram 
  equalizeHist( smallImg, smallImg );
  //imshow(" Histogram mean value processing ",smallImg);
  // Program start and end insert this function to get the time, the algorithm is calculated to get the execution time 
  t = (double)cvGetTickCount();
  // To detect human faces 
  //detectMultiScale In the function smallImg Represents the input image to be detected as smallImg . faces Represents the detected face target sequence, 1.1 said 
  // The ratio of each image size reduction is 1.1 . 2 each 1 At least two targets must be detected 3 Second is the real goal ( Because the pixels around and the different Windows are big 
  // Small can detect faces ),CV_HAAR_SCALE_IMAGE Means that instead of scaling the classifier to detect, it scales the image, Size(30, 30) As the goal of 
  // Minimum and maximum size 
  cascade.detectMultiScale( smallImg, faces,
    1.1, 2, 0
    ,Size(30, 30));
  // If enabled, the image is flipped to continue detection 
  if( tryflip )
    flip(smallImg, smallImg, 1);
  //  imshow(" Inverted image ",smallImg);
    cascade.detectMultiScale( smallImg, faces2,
      1.1, 2, 0
      ,Size(30, 30) );
    for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); r++ )
      faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height));
  t = (double)cvGetTickCount() - t;
  //  qDebug( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
  for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
    Mat smallImgROI;
    vector<Rect> nestedObjects;
    Point center;
    Scalar color = colors[i%8];
    int radius;

    double aspect_ratio = (double)r->width/r->height;
    if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
      // The face is marked on the image before it is shrunk, so it is scaled back here 
      center.x = cvRound((r->x + r->width*0.5)*scale);
      center.y = cvRound((r->y + r->height*0.5)*scale);
      radius = cvRound((r->width + r->height)*0.25*scale);
      circle( img, center, radius, color, 3, 8, 0 );
      rectangle( img, cvPoint(cvRound(r->x*scale), cvRound(r->y*scale)),
      cvPoint(cvRound((r->x + r->width-1)*scale), cvRound((r->y + r->height-1)*scale)),
      color, 3, 8, 0);
    if( nestedCascade.empty() )
    smallImgROI = smallImg(*r);
    // The same method is used to detect human eyes 
    nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
      1.1, 2, 0
      ,Size(30, 30) );
    for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
      center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
      center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
      radius = cvRound((nr->width + nr->height)*0.25*scale);
      circle( img, center, radius, color, 3, 8, 0 );
  // imshow( " Identify the results ", img );

Related articles: