检测并跟踪多个面 - opencv3.0 c ++

时间:2016-02-18 09:15:36

标签: c++ visual-studio-2013 opencv3.0

我正在尝试根据用户输入检测视频文件中的多个面孔。

代码检测到所需面部的数量,但有时检测“跳”到其他人 - 与第一帧不同。

视频文件包含5-6个面孔,用户可以选择1到4的数字。 程序应该检测前面的X个面并在整个视频中进行跟踪,此外,程序会打开另一个带有检测到的面部的窗口。

这是我的算法代码:

capture >> cap_img;
waitKey(2);
cvtColor(cap_img, gray_img, CV_BGR2GRAY);
equalizeHist(gray_img, gray_img);
while (1)
{
    capture >> cap_img;
    waitKey(2);
    cvtColor(cap_img, gray_img, CV_BGR2GRAY);
    equalizeHist(gray_img, gray_img);
    // Detect faces
    face_cascade.detectMultiScale(gray_img, faces, 1.1, 5,0 | CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING | CASCADE_SCALE_IMAGE, Size(1, 1));
    // Set Region of Interest
    cv::Rect reg_b;
    cv::Rect reg_c;

    int i = 0; // i is index of current element
    int ac = 0; // ac is area of current element

    int ib = 0; // ib is index of biggest element
    int ab = 0; // ab is area of biggest element

    if (numOfFaces > faces.size())
    {
        numOfFaces = faces.size();
    }

    for (i = 0; i < numOfFaces; i++) // Iterate through all current elements (detected faces)
    {
        reg_c.x = faces[i].x;
        reg_c.y = faces[i].y;
        reg_c.width = (faces[i].width);
        reg_c.height = (faces[i].height);
        // Get the area of current element (detected face), at beginning it is same as "current" element
        ac = reg_c.width * reg_c.height; 

        reg_b.x = faces[ib].x;
        reg_b.y = faces[ib].y;
        reg_b.width = (faces[ib].width);
        reg_b.height = (faces[ib].height);

        ab = reg_b.width * reg_b.height;
        // Get the area of biggest element, at beginning it is same as "current" element
        if (ac > ab)
        {
            ib = i;
            reg_b.x = faces[ib].x;
            reg_b.y = faces[ib].y;
            reg_b.width = (faces[ib].width);
            reg_b.height = (faces[ib].height);
        }

        crop = cap_img(reg_b);
        resize(crop, res, Size(128, 128), 0, 0, INTER_LINEAR); // This will be needed later while saving images
        cvtColor(crop, gray, CV_BGR2GRAY); // Convert cropped image to Grayscale
        Point pt1(faces[i].x, faces[i].y); // Display detected faces on main window - live stream from camera
        Point pt2((faces[i].x + faces[i].height), (faces[i].y + faces[i].width));
        rectangle(cap_img, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0);
    }

    // Show image
    resize(cap_img, cap_img, Size(cap_img.cols / 2, cap_img.rows / 2)); // to half size or even smaller
    imshow("original", cap_img);

    if (!crop.empty())
    {
        imshow("detected", crop);
    }
    else
        destroyWindow("detected");
}

“numOfFaces”是要检测的面数。

我做错了什么?

0 个答案:

没有答案
相关问题