0

我在 OpenCV(C++ 中)中有一个代码,它使用“haarcascade_mcs_upperbody.xml”来检测上身。它检测单个上身。我怎样才能让它检测到多个上半身。我认为 CV_HAAR_FIND_BIGGEST_OBJECT 只检测到最大的物体。但我不知道如何解决这个问题

代码如下:

int main(int argc, const char** argv)
{
CascadeClassifier body_cascade;
body_cascade.load("haarcascade_mcs_upperbody.xml");

VideoCapture captureDevice;
captureDevice.open(0);

Mat captureFrame;
Mat grayscaleFrame;

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> bodies;

    //find faces and store them in the vector array
    body_cascade.detectMultiScale(grayscaleFrame, faces, 1.1, 3,                                                                                                                                     
    CV_HAAR_FIND_BIGGEST_OBJECT|CV_HAAR_SCALE_IMAGE, 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(bodies[i].x + bodies[i].width, bodies[i].y + bodies[i].height);
        Point pt2(bodies[i].x, bodies[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;
}
4

1 回答 1

2

您的代码似乎有些不一致,因为face_cascade没有在任何地方定义,但我认为它的类型是CascadeClassifier.
detectMultiScale将所有检测到的对象存储在faces向量中。你确定它只包含一个对象吗?
尝试删除CV_HAAR_FIND_BIGGEST_OBJECT标志,因为您希望检测到所有对象,而不仅仅是最大的对象。
此外,请确保正确设置minSizemaxSize参数(请参阅文档),因为这些参数决定了可检测对象的最小和最大尺寸。

于 2013-03-10T08:01:25.157 回答