Ask Your Question
0

Face detection of an image using opencv c++

asked 2018-03-15 07:09:34 -0600

shive gravatar image

updated 2018-03-15 07:15:11 -0600

Hi.. I have been trying to implement the face, eyes, smile detection using haar cascade classifiers, Iam using opencv 3.2.0 on ubuntu 16.04 OS.. actually am getting the output of face,eyes and smile with image pixel above 225x225.But when am trying to test with image of yale database which is of pixel(92x112) size, it showing only face detection am unable to get the eyes and smile detection of the image.... I have gone through some sites but i din't find any solution.. Someone plz help me with this problem....

This is my code.....

 #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;
      Mat image_gray;
      image = imread("/home/veena/Desktop/7.jpeg", CV_LOAD_IMAGE_COLOR);  
      //   namedWindow( "window1", 1 ); //  imshow( "window1", image );

       CascadeClassifier face_cascade;
       CascadeClassifier eyes_cascade;
       CascadeClassifier smile_cascade;

       face_cascade.load( "/home/veena/Downloads/opencv-3.2.0/data/haarcascades /haarcascade_frontalface_alt.xml" );
       eyes_cascade.load( "/home/veena/Downloads/opencv-3.2.0/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml"); 
       smile_cascade.load("/home/veena/Downloads/opencv-3.2.0/data/haarcascades/haarcascade_smile.xml");
// Detect faces
       std::vector<Rect> faces;
       cvtColor(image, image_gray, COLOR_BGR2GRAY);
       equalizeHist(image_gray, image_gray);
       face_cascade.detectMultiScale( image_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(80, 80) );
       for( int i = 0; i < faces.size(); i++ )
         {
            Point center( faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2 );
            ellipse( image, center, Size( faces[i].width/2, faces[i].height/2), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
            // rectangle(image,Point (faces[i].x,faces[i].y),Point (faces[i].x+faces[i].width, faces[i].y+faces[i].height),Scalar(255,0,255),4,8,0);

             Mat faceROI = image_gray(faces[i]);
             std::vector<Rect> eyes;
             eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30,30));
             for(int j=0;j < eyes.size(); j++)
               {
                 Point eye_center( faces[i].x + eyes[j].x + eyes[j].width/2, faces[i].y  +eyes[j].y + eyes[j].height/2 );
                 int radius = cvRound((eyes[j].width+eyes[j].height)*0.25);        
                 circle( image,eye_center,radius,Scalar(255, 0, 0),4,8,0 );
                }

               std::vector<Rect> smile;
               smile_cascade.detectMultiScale(faceROI, smile, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(80,80));
               for(int j = 0; j < smile.size(); j++)
                 {
                   Point smile_center(faces[i].x + smile[j].x + smile[j].width/2,  faces[i].y + smile[j].y + smile[j].height/2);
                   int radius = cvRound((smile[j].width+smile[j].height)*0.25);        
                   circle( image,smile_center,radius,Scalar(0, 255, 0),4,8,0 );

                   }
             }
           imshow( "Detected Face", image );

           waitKey(0);                   
           return 0;
    }
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-03-15 07:19:21 -0600

berak gravatar image

updated 2018-03-15 07:35:06 -0600

there is a minimum size that can be detected with Cascades (the size of the train window) , you can look it up, it's in the 1st lines of the resp. xml file.

so if your images are too small, it won't find the eyes or mouth. try to resize the image:

 cv::resize(img, img, Size(), 2,2); // keep same aspect ratio

also, a minSize of Size(80,80) will prevent any eye detection, if your image is 90x100.

please again, don't blindly copy/paste code, but read the documentation, and try to understand it.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-03-15 07:09:34 -0600

Seen: 2,604 times

Last updated: Mar 15 '18