我们如何更准确地检测面部

时间:2014-03-01 04:07:02

标签: c++ opencv image-processing

我正在通过视频进行人脸检测。所以我写了一个小代码来检测脸部。

#include<opencv2/objdetect/objdetect.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include<cv.h>

using namespace std;
using namespace cv;

CvCapture *capture=cvCaptureFromFile("foot.mp4");
double min_face_size=30;
double max_face_size=400;

Mat detectFace(Mat src);

int main( )
{
  namedWindow( "window1", 1 );  
  while(1)
  {
    Mat frame,frame1;
    frame1=cvQueryFrame(capture);;
    frame=detectFace(frame1);
    imshow( "window1", frame );
    if(waitKey(1) == 'c') break; 
  }

  waitKey(0);                 
  return 0;
}

Mat detectFace(Mat image)
{
  CascadeClassifier face_cascade( "haarcascade_frontalface_alt2.xml" );
  CvPoint ul,lr;
  std::vector<Rect> faces;
  face_cascade.detectMultiScale( image, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE,   Size(min_face_size, min_face_size),Size(max_face_size, max_face_size) );
 for( int i = 0; i < faces.size(); i++ )
  {
  min_face_size = faces[i].width*0.8;
  max_face_size = faces[i].width*1.2;
  ul.x=faces[i].x;
  ul.y=faces[i].y;
  lr.x=faces[i].x + faces[i].width;
  lr.y=faces[i].y + faces[i].height;
  rectangle(image,ul,lr,CV_RGB(1,255,0),3,8,0);
}
return image;
}

我拍摄了一个用于脸部检测的视频,其中包含小脸和大脸。我的问题是使用我的代码,它只检测小脸,并且还显示一些不需要的检测。

我需要检测视频中的小脸和大脸。我该怎么办? 缩放因子有问题吗?

请帮我理解这个问题。

1 个答案:

答案 0 :(得分:0)

  1. 尝试增加'double max_face_size',它控制您想要检测的面部大小。

  2. 您还可以在'detectMultiScale()'的参数中增加'2',这将控制面部的质量。

相关问题