使用open cv和c ++检测并计算图像上的面部

时间:2022-02-01 21:23:36

I am using opencv and C++ although i'm beginner. I am trying to detect and count faces from a set of image using Haarcascade . I only want to get the number of faces on each image . how can i edit this code to get the number of faces on image????

我使用的是opencv和C ++,虽然我是初学者。我试图使用Haarcascade从一组图像中检测和计算面部。我只想获得每张图片上的面孔数量。如何编辑此代码以获取图像上的面数?

// Function detectAndDisplay
void detectAndDisplay(Mat frame)
{
std::vector<Rect> faces;
Mat frame_gray;
Mat crop;
Mat res;
Mat gray;
string text;
stringstream sstm;

cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);

// Detect faces
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 |     CASCADE_SCALE_IMAGE, Size(30, 30));

// Set Region of Interest
cv::Rect roi_b;
cv::Rect roi_c;

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

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

for (ic = 0; ic < faces.size(); ic++) // Iterate through all current  elements (detected faces)

{
    roi_c.x = faces[ic].x;
    roi_c.y = faces[ic].y;
    roi_c.width = (faces[ic].width);
    roi_c.height = (faces[ic].height);

    ac = roi_c.width * roi_c.height; // Get the area of current element (detected face)

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

    ab = roi_b.width * roi_b.height; // Get the area of biggest element, at beginning it is same as "current" element

    if (ac > ab)
    {
        ib = ic;
        roi_b.x = faces[ib].x;
        roi_b.y = faces[ib].y;
        roi_b.width = (faces[ib].width);
        roi_b.height = (faces[ib].height);
    }

    crop = frame(roi_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

    // Form a filename
    filename = "";
    stringstream ssfn;
    ssfn << filenumber << ".png";
    filename = ssfn.str();
    filenumber++;

    imwrite(filename, gray);
    printf("filename");
    Point pt1(faces[ic].x, faces[ic].y); // Display detected faces on main window - live stream from camera
    Point pt2((faces[ic].x + faces[ic].height), (faces[ic].y + faces[ic].width));
    rectangle(frame, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0);
}

// Show image
/*sstm << "Crop area size: " << roi_b.width << "x" << roi_b.height << " Filename: " << filename;
text = sstm.str();

putText(frame, text, cvPoint(30, 30), FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(0, 0, 255), 1, CV_AA);
imshow("original", frame);

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

}

2 个解决方案

#1


modified your posted code sample to just return the number of detected faces in the image...

修改了您发布的代码示例,只返回图像中检测到的面部数量...

// Function to count the detected faces in your image
void countFacesInImage(Mat frame)
{
    std::vector<Rect> faces;
    Mat frame_gray;

    cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
    equalizeHist(frame_gray, frame_gray);

    // Detect faces
    face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 |     CASCADE_SCALE_IMAGE, Size(30, 30));

    return faces.size();
}

if you want to get an impression of which faces were detected and which weren't you can add this code before the return:

如果你想得到一个关于检测到哪些面孔的印象,哪些不是,你可以在返回之前添加这个代码:

cv::Mat tmpImage = frame.clone();
for(unsigned int i=0; i<faces.size(); ++i)
{
    cv::rectangle(tmpImage, faces[i], cv::Scalar(0,255,0), 2);
}
cv::imshow("faces", tmpImage);
cv::waitKey(0);

after each image you have to press a key with active window "faces". You can change to cv::waitKey(n) to wait n milliseconds instead of the need to press a key.

在每个图像之后,您必须按下具有活动窗口“面孔”的键。您可以更改为cv :: waitKey(n)等待n毫秒,而不是需要按键。

#2


I had to do something similar and used the example of a CascadeClassifier on the OpenCV website.

我不得不做类似的事情并在OpenCV网站上使用CascadeClassifier的例子。

The rough steps to follow are:

要遵循的粗略步骤是:

  1. Load all images you want to process.
  2. 加载要处理的所有图像。

  3. For each image, apply the CascadeClassifier as in the example, you will need to pass a std::vector<cv::Rect> as parameter. After detection, this vector will contain the location of all detected objects (in your case, faces).
  4. 对于每个图像,应用示例中的CascadeClassifier,您需要传递一个std :: vector 作为参数。检测后,此向量将包含所有检测到的对象的位置(在您的情况下,面部)。

  5. For each image, return the size of the vector to know the number of faces that were detected.
  6. 对于每个图像,返回向量的大小以了解检测到的面数。


To be honest, the example I linked is something you could have found on your own without much effort.

说实话,我链接的例子是你可以自己找到的,而不需要太多努力。

#1


modified your posted code sample to just return the number of detected faces in the image...

修改了您发布的代码示例,只返回图像中检测到的面部数量...

// Function to count the detected faces in your image
void countFacesInImage(Mat frame)
{
    std::vector<Rect> faces;
    Mat frame_gray;

    cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
    equalizeHist(frame_gray, frame_gray);

    // Detect faces
    face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 |     CASCADE_SCALE_IMAGE, Size(30, 30));

    return faces.size();
}

if you want to get an impression of which faces were detected and which weren't you can add this code before the return:

如果你想得到一个关于检测到哪些面孔的印象,哪些不是,你可以在返回之前添加这个代码:

cv::Mat tmpImage = frame.clone();
for(unsigned int i=0; i<faces.size(); ++i)
{
    cv::rectangle(tmpImage, faces[i], cv::Scalar(0,255,0), 2);
}
cv::imshow("faces", tmpImage);
cv::waitKey(0);

after each image you have to press a key with active window "faces". You can change to cv::waitKey(n) to wait n milliseconds instead of the need to press a key.

在每个图像之后,您必须按下具有活动窗口“面孔”的键。您可以更改为cv :: waitKey(n)等待n毫秒,而不是需要按键。

#2


I had to do something similar and used the example of a CascadeClassifier on the OpenCV website.

我不得不做类似的事情并在OpenCV网站上使用CascadeClassifier的例子。

The rough steps to follow are:

要遵循的粗略步骤是:

  1. Load all images you want to process.
  2. 加载要处理的所有图像。

  3. For each image, apply the CascadeClassifier as in the example, you will need to pass a std::vector<cv::Rect> as parameter. After detection, this vector will contain the location of all detected objects (in your case, faces).
  4. 对于每个图像,应用示例中的CascadeClassifier,您需要传递一个std :: vector 作为参数。检测后,此向量将包含所有检测到的对象的位置(在您的情况下,面部)。

  5. For each image, return the size of the vector to know the number of faces that were detected.
  6. 对于每个图像,返回向量的大小以了解检测到的面数。


To be honest, the example I linked is something you could have found on your own without much effort.

说实话,我链接的例子是你可以自己找到的,而不需要太多努力。