c++ - 面部检测不起作用

标签 c++ opencv face-detection

我在 64 位 Win 7 和 VS2010 (C++) 上使用 OpenCV。

我已经尝试了很多检测人脸的源代码,它们编译并运行,但没有检测发生。

我举两个例子: 1) 在第一个示例中,我使用的源代码来自:

http://www.bsd-noobz.com/opencv-guide/60-3-face-detection

我得到了图片,但不是正方形。

2) 在第二个示例中,我使用了一段时间前下载的代码(不记得从哪里下载的)。这一个显示来自网络摄像头的流,但同样没有检测到。

#include "stdafx.h"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <iostream>

using namespace std;
using namespace cv;

 /** Function Headers */
 void detectAndDisplay2( Mat frame );

 /** Global variables */
 String face_cascade_name = "haarcascade_frontalface_alt.xml";
 String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
 CascadeClassifier face_cascade;
 CascadeClassifier eyes_cascade;

//RNG rng(12345);

 /** @function main */
 int main()
 {
   CvCapture* capture;
   Mat frame;

   //-- 1. Load the cascades
   if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading face cascade\n"); return -1; };
   if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading eye cascade\n"); return -1; };

   //-- 2. Read the video stream
   capture = cvCaptureFromCAM( 1 );
   if( capture )
   {
     while( true )
     {
   frame = cvQueryFrame( capture );

   //-- 3. Apply the classifier to the frame
       if( !frame.empty() )
       { detectAndDisplay2( frame ); }
       else
       { printf(" --(!) No captured frame -- Break!"); break; }

       int c = waitKey(10);
       if( (char)c == 'c' ) { break; }
      }
   }
   cvReleaseCapture(&capture);
   cvDestroyWindow("Capture - Face detection");

   return 0;
 }

/** @function detectAndDisplay */
void detectAndDisplay2( Mat frame )
{
  vector<Rect> faces;
  Mat frame_gray;

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

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

  for( int 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] );
    vector<Rect> eyes;

    //-- In each face, detect eyes
    eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );

    for( int 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 );
     }
  }
  //-- Show what you got
  imshow( "Processed", frame_gray );
  imshow( "Capture - Face detection", frame );

 }

如果有人能提供帮助,我将不胜感激。

最佳答案

这是github中的opencv项目.这个错误源于我看到的两个问题。

1) 确保您安装了正确的 OpenCV 版本。我建议这个 install guide ……对我来说效果很好。

2) 然后,确保您已将 haarcascade_frontalface_alt.xml 添加到可执行文件的内部——它应该位于像 ../build/bin/haarcascade_frontalface_alt.xml< 这样的文件夹中 当你构建项目时)。

关于c++ - 面部检测不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14934780/

相关文章:

python - 如何在 bcc64 项目中嵌入 python?

c++如何确保我的函数在被杀死之前完全执行

opencv - cvBlobsLib 查找重叠的 blob

python - 如何使用 OpenCV 或深度学习从平面图扫描房间轮廓数据?

c++ - Qt和opencv中的人脸检测

c++ - 使用虚函数创建实例,为什么?

c++ - utfcpp 和 Win32 宽 API

c++ - 使用 cv::fisheye::unactorImage 取消鱼眼图像的扭曲

android - 在opencv中使用javacamera2view

opencv - 如何在“处理”中使用网络摄像头确定上唇和下唇之间的距离?