Patrik
Patrik

Reputation: 11

OpenCV face deteciton returns too many faces

I have a little problem. I am trying to make face detection via Kinect v1.I get data from kinect and convert it to OpenCV mat. Then I am trying to detect faces in my image but the function return face.size() = cca 250000000. Do you know where is the problem ?

void getKinectData(GLubyte* dest) {
    NUI_IMAGE_FRAME imageFrame; //structure of frame ( number,res etc )
    NUI_LOCKED_RECT LockedRect; //pointer to actual data
    if (sensor->NuiImageStreamGetNextFrame(rgbStream, 0, &imageFrame) < 0) return;
    INuiFrameTexture* texture = imageFrame.pFrameTexture; // manages the frame data
    texture->LockRect(0, &LockedRect, NULL, 0);
    IplImage* image = cvCreateImageHeader(cvSize(COLOR_WIDTH, COLOR_HIGHT), IPL_DEPTH_8U, 4);
    if (LockedRect.Pitch != 0) // pitch - how many bytes are in each row of the frame
    {
        BYTE* curr = (BYTE*)LockedRect.pBits;
        cvSetData(image, curr, LockedRect.Pitch);
        const BYTE* dataEnd = curr + (widthX*heightX) * 4;
        while (curr < dataEnd) {
            *dest++ = *curr++;
        }
    }
    //cvShowImage("color image", image);
    m = cv::cvarrToMat(image).clone();
    DetectAndDisplay(m);

    texture->UnlockRect(0);
    sensor->NuiImageStreamReleaseFrame(rgbStream, &imageFrame);

}

void DetectAndDisplay(cv::Mat frame)
{
std::vector<cv::Rect> faces;

    cv::Mat frame_gray;

    cvtColor(frame, frame_gray, cv::COLOR_BGR2GRAY);
    equalizeHist(frame_gray, frame_gray);
    //-- Detect faces
    face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, cv::Size(24, 24));

    for (size_t i = 0; i < faces.size(); i++)
    {
        cv::Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);
        ellipse(frame, center, cv::Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, cv::Scalar(255, 0, 255), 4, 8, 0);

        cv::Mat faceROI = frame_gray(faces[i]);
        std::vector<cv::Rect> eyes;
        /*
        //-- In each face, detect eyes
        eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));

        for (size_t 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(frame, eye_center, radius, Scalar(255, 0, 0), 4, 8, 0);
        }*/
    }
    //-- Show what you got
    imshow(window_name, frame);
}

Upvotes: 1

Views: 421

Answers (1)

latentcode
latentcode

Reputation: 43

I know this old, but I had a similar problem, so I thought others may benefit.

Linking with the release opencv_objdetect library during a debug build will lead to detectMultiScale() returning an enormous vector containing hundreds of thousands of erroneous rectangles. Here's how to fix it in Visual Studio 2017 (I'm using OpenCV version 3.2, so adjust the names of the libraries mentioned to correspond with the version you are using):

  1. Project | (your project name) Properties... (or right click on project in Solution Explorer & select Properties)
  2. Change Configuration to Debug and Platform to All Platforms (or you can repeat these steps individually for each Debug platform)
  3. Go to Linker | Input and make sure opencv_objdetect320d.lib is in the Addition Dependencies list and remove opencv_objdetect320.lib if it's there.
  4. Repeat the above steps for Configuration == Release, but this time make sure Linker | Input includes opencv_objdetect320.lib (no trailing d in the name) in the Addition Dependencies list and remove opencv_objdetect320d.lib if it's there.

Upvotes: 1

Related Questions