简体   繁体   English

如何在OpenCV中检测多个面孔?

[英]how to detect multiple faces in OpenCV?

I am having problem with detecting multiple faces at a time.the following code detect a frontal face and eyes for one person only.I tried to make it work for all available faces but i can't...please help 我一次检测多个脸部时遇到问题。以下代码仅检测一个人的正面和眼睛。我试图使其适用于所有可用的脸部,但我不能...请帮助

  CvCapture* capture;
    Mat frame;
    std::vector<Rect> faces;
    Mat frame_gray;
    Mat frame;
    CascadeClassifier face_cascade;
    CascadeClassifier eyes_cascade;

    face_cascade.load( "haarcascade_frontalface_alt.xml"; );
    eyes_cascade.load(  "haarcascade_eye_tree_eyeglasses.xml"; );


   capture = cvCaptureFromCAM( -1 );

  frame = cvQueryFrame( capture );
  cvtColor( frame, frame_gray, CV_BGR2GRAY );
  equalizeHist( frame_gray, frame_gray );

  face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

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

    Mat faceROI = frame_gray( faces[i] );
    std::vector<Rect> eyes;    
    eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
    for( size_t j = 0; j < eyes.size(); j++ )
     {
       Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
       int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
       circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
     }
  }

  imshow( "window", frame );

Using basically your code (just changed to load a single fixed image and removed the equalizeHist call) gives me those results: 基本上使用您的代码(只是更改为加载单个固定图像并删除了equalizeHist调用)就可以得到以下结果:

input: 输入:

在此处输入图片说明

output: 输出:

在此处输入图片说明

input: 输入:

在此处输入图片说明

output: 输出:

在此处输入图片说明

so your code basically does what it should do. 因此您的代码基本上可以完成应做的事情。 Your problem seems to be in your image data or parameters regarding your image data. 您的问题似乎出在图像数据或与图像数据有关的参数中。 Post sample images please. 请发布样本图片。

Just for you to be sure that I didn't change big parts of the code, I used: 为了确保您没有更改大部分代码,我使用了:

using namespace cv;
using namespace std;

int main()
{

    cv::Mat input = cv::imread("../inputData/MultiLena.png");

    cv::Mat gray;
    cv::cvtColor(input,gray,CV_BGR2GRAY);


    //CvCapture* capture;
    //Mat frame;
    std::vector<Rect> faces;
    Mat frame_gray;
    Mat frame;
    CascadeClassifier face_cascade;
    CascadeClassifier eyes_cascade;

    face_cascade.load( "haarcascade_frontalface_alt.xml" );
    eyes_cascade.load(  "haarcascade_eye_tree_eyeglasses.xml" );


   //capture = cvCaptureFromCAM( -1 );

  //frame = cvQueryFrame( capture );
  //cvtColor( frame, frame_gray, CV_BGR2GRAY );
  //equalizeHist( frame_gray, frame_gray );

    frame_gray = gray;
    frame = input;


  face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

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

    Mat faceROI = frame_gray( faces[i] );
    std::vector<Rect> eyes;    
    eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
    for( size_t j = 0; j < eyes.size(); j++ )
     {
       Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
       int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
       circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
     }
  }

  imshow( "window", frame );


  cv::imshow("input", input);
    cv::imwrite("../outputData/multiFaces.png", input);
    cv::waitKey(0);
    return 0;
}

maybe you could implement the following strategy: 也许您可以实施以下策略:

  • Detect the first face, it will generate a rect. 检测到第一张脸,它将生成一个矩形。
  • Create a new Mat without that rect. 创建一个没有该矩形的新Mat。
  • Analyze the new mat and generate a new face detection rect. 分析新垫子并生成新的人脸检测矩形。
  • Now draw both rects into a video capture stream. 现在将两个矩形都吸引到视频捕获流中。

Keep in mind to display on screen only the background video stream with selected rects, so the system remains efficient. 请记住,仅将具有选定矩形的背景视频流显示在屏幕上,因此系统仍然有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM