opencv 3.0.0 c++ detectMultiScale detects insane amount of faces

1.7k views Asked by At

I just started with using OpenCV, my setup is: OpenCV 3.0 visual studio 2013

my problem is that I am attempting to detect a face in an image but the function call detectMultiScale detects to many faces.

#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(int argc, const char** argv)
{
//create the cascade classifier object used for the face detection
CascadeClassifier face_cascade;
//use the haarcascade_frontalface_alt.xml library
if (!face_cascade.load("haarcascade_frontalface_alt.xml"))
{
    printf("Unable to load classifier XML");
    return 0;
}

//setup video capture device and link it to the first capture device
//VideoCapture captureDevice;
//captureDevice.open(0);

//setup image files used in the capture process
Mat captureFrame;
Mat grayscaleFrame;

captureFrame = imread("Test.png", IMREAD_COLOR);

if (captureFrame.empty()) // Check for invalid input
{
    cout << "Could not open or find the image" << std::endl;
    return 0;
}

//create a window to present the results
namedWindow("outputCapture", 1);

//create a loop to capture and find faces
while (true)
{
    //capture a new image frame
    //captureDevice >> captureFrame;

    //convert captured image to gray scale and equalize
    cvtColor(captureFrame, grayscaleFrame, CV_BGR2GRAY);
    equalizeHist(grayscaleFrame, grayscaleFrame);

    //create a vector array to store the face found
    std::vector<Rect> faces;

    //find faces and store them in the vector array
    face_cascade.detectMultiScale(grayscaleFrame, faces, 1.1, 3, 0 , Size(30, 30));

    ////draw a rectangle for all found faces in the vector array on the original image
    //for (int i = 0; i < faces.size(); i++)
    //{
    //  Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
    //  Point pt2(faces[i].x, faces[i].y);

    //  rectangle(captureFrame, pt1, pt2, cvScalar(0, 255, 0, 0), 1, 8, 0);
    //}

    //print the output
    imshow("outputCapture", captureFrame);

    //pause for 33ms
    waitKey(33);
}

return 0;
}

i am wondering if its my setup that is setup correctly or if CascadeClassifier is just not stopping the detection. when I look at the data some of it is in the right place but with 268158156 entries it crashes.

any advice would be welcome

1

There are 1 answers

0
X Chi On

you might want to check the size of "faces" after face_cascade.detectMultiScale is called. i've noticed sometimes, the size of "faces" is very large, but only the first few entries are actually valid. The rest usually has very width or height = 0, in which case you will need to go through each entry of "faces", and stop after the last valid entry.