Saturday 12 March 2016

Opencv C++ Tutorial of Face(object) Detection Using Haar Cascade


This OpenCV C++ Tutorial is about doing Face(object) Detection Using Haar Cascade.
The Steps of Doing Object Detection (Here it is face) using Haar Cascade are:-
  1. Load the Input Image.
  2. Load the Haar Cascade File (here it is haarcascade_frontalface_alt2.xml)
    Normally it is an XML file.
  3. Detect the Objects(here it is face) using detectMultiScale()
  4. Create ROI over the detected Objects(face).
    i.e. Draw a Rectangle or a Circle over the detected objects to mark them.
  5. Show the Output i.e. Marked Objects(faces)
Syntax:
C++: void CascadeClassifier::detectMultiScale(const Mat& image, vector& objects, double scaleFactor=1.1, int minNeighbors=3, int flags=0, Size minSize=Size(), Size maxSize=Size())
Parameters:

  • image – Matrix of the type CV_8U containing an image where objects are detected.
  • objects – Vector of rectangles where each rectangle contains the detected object.
  • scaleFactor – Parameter specifying how much the image size is reduced at each image scale.
  • minNeighbors – Parameter specifying how many neighbors each candidate rectangle should have to retain it.This parameter will affect the quality of the detected faces: higher value results in less detections but with higher quality.
  • flags – Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade.
  • minSize – Minimum possible object size. Objects smaller than that are ignored.
  • maxSize – Maximum possible object size. Objects larger than that are ignored.
//Opencv C++ Example on Face Detection Using Haar Cascade
 
/*We can similarly train our own Haar Classifier and Detect any object which we want
Only Thing is we need to load our Classifier in palce of cascade_frontalface_alt2.xml */
 
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
  
#include <iostream>
#include <stdio.h>
  
using namespace std;
using namespace cv;
  
int main( )
{
    Mat image;
    image = imread("C:\\Users\\arjun\\Desktop\\opencv-face-haarcascade-input.jpg", CV_LOAD_IMAGE_COLOR);  
    namedWindow( "window1", 1 );   imshow( "window1", image );
  
    // Load Face cascade (.xml file)
    CascadeClassifier face_cascade;
    face_cascade.load( "D:\\opencv2410\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml" );

 if(face_cascade.empty())
// if(!face_cascade.load("D:\\opencv2410\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml"))
 {
  cerr<<"Error Loading XML file"<<endl;
  return 0;
 }
 
    // Detect faces
    std::vector<Rect> faces;
    face_cascade.detectMultiScale( image, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
  
    // Draw circles on the detected faces
    for( int i = 0; i < faces.size(); i++ )
    {
        Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
        ellipse( image, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
    }
      
    imshow( "Detected Face", image );
      
    waitKey(0);                   
    return 0;
}
Input:-
OpenCV C++ Haar Training

Output:-
OpenCV C++ Haar Training Face Detection

4 comments:

  1. this is cool tutorial but I dont know how to run this code coz my program always give output ucrtbased.dll and opencv_world310d.dll cannot find or open the PDB file and also :
    Exception thrown at 0x000007FEFD849E5D in Project2.exe: Microsoft C++ exception: cv::Exception at memory location 0x00000000001FF210.
    Unhandled exception at 0x000007FEFD849E5D in Project2.exe: Microsoft C++ exception: cv::Exception at memory location 0x00000000001FF210.

    The program '[3760] Project2.exe' has exited with code 0 (0x0).
    i need help pls

    ReplyDelete
  2. May be your, there is a problem in your image inputing :)

    ReplyDelete
  3. When I run this code have a error: error loading xml file. Please tell me how to solve. Thanks a lot

    ReplyDelete
  4. You are missing the some assembly reference set the envirnment variable for that else just copy the opencv_world310d.dll and ucrtbased.dll files and pest it into you project working directory

    ReplyDelete