【问题标题】:OpenCV face deteciton returns too many facesOpenCV 人脸检测返回太多人脸
【发布时间】:2016-05-05 23:25:06
【问题描述】:

我有一个小问题。我正在尝试通过 Kinect v1 进行人脸检测。我从 kinect 获取数据并将其转换为 OpenCV mat。然后我试图检测图像中的人脸,但函数返回 face.size() = cca 250000000。你知道问题出在哪里吗?

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);
}

【问题讨论】:

  • 我认为你使用的是VS并且你的配置有误
  • 您打印了一张图片吗?只是为了检查它的外观,以防万一它与您的期望无关。我在你的代码中看到你注释掉了cvShowImage("color image", image),你试过了吗?
  • 是的,我试过了。图像还可以。我试图把它变成释放模式,它只找到一张脸?正常吗?为什么在 Debug 或 Release 中使用它会有所不同?我从来没有这个问题。 @sturkmen 也许你是对的。但是我该如何解决呢?
  • 我不使用 VS 也许看this post 会给你一个线索(#if defined(NDEBUG)...)
  • 实际问题是 C++ 没有标准 ABI,并且使用 debug 配置链接到 release 库会导致错误。

标签: c++ opencv face-detection


【解决方案1】:

我知道这个老了,但我也有类似的问题,所以我认为其他人可能会受益。

在调试构建期间与发布的 opencv_objdetect 库链接将导致 detectMultiScale() 返回包含数十万个错误矩形的巨大向量。以下是在 Visual Studio 2017 中修复它的方法(我使用的是 OpenCV 版本 3.2,因此请调整提到的库的名称以与您使用的版本相对应):

  1. 项目 | (您的项目名称)属性...(或右键单击 解决方案资源管理器中的项目并选择属性)
  2. Configuration 更改为 Debug 并将 Platform 更改为 All Platforms(或者您可以 为每个调试平台单独重复这些步骤)
  3. 转到链接器|输入 并确保 opencv_objdetect320d.libAddition Dependencies 列出并删除 opencv_objdetect320.lib 如果 它就在那里。
  4. 配置 == 发布重复上述步骤,但是 这次确保 Linker |输入包括opencv_objdetect320.lib (名称中没有尾随 d)在 Addition Dependencies 列表中并删除 opencv_objdetect320d.lib(如果存在)。

【讨论】:

    猜你喜欢
    • 2012-02-04
    • 2013-05-24
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    • 2021-07-17
    • 2015-12-18
    • 1970-01-01
    • 2014-01-31
    相关资源
    最近更新 更多