opencv2实现人脸及人眼检测_相当稳定


  1. //opencv2检测人脸人眼的方法有很多,我试了集中,下面的程序得到的结果最准确。人可以随意动,只要摄像头可以,就没有问题。  

  1. /* */  
  2.   
  3. #include "opencv2/objdetect/objdetect.hpp"  
  4.  #include "opencv2/highgui/highgui.hpp"  
  5.  #include "opencv2/imgproc/imgproc.hpp"  
  6.   
  7.  #include <iostream>  
  8.  #include <stdio.h>  
  9. #include <opencv2/core/core.hpp>   
  10.  using namespace std;  
  11.  using namespace cv;  
  12.   
  13.  // Function Headers   
  14.  void detectAndDisplay( Mat frame );  
  15.   
  16.  // Global variables   
  17.  String face_cascade_name = "haarcascade_frontalface_alt.xml";  
  18.  String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";  
  19.  CascadeClassifier face_cascade;  
  20.  CascadeClassifier eyes_cascade;  
  21.  string window_name = "Capture - Face detection";  
  22.  RNG rng(12345);  
  23.   
  24.  // @function main   
  25.  int main( int argc, const char** argv )  
  26.  {  
  27.   
  28.   // CvCapture* capture;  
  29.    Mat frame;  
  30.   
  31.    //-- 1. Load the cascades  
  32.    if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };  
  33.    if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };  
  34.   
  35.    //-- 2. Read the video stream  
  36.    CvCapture* capture=cvCaptureFromCAM(0);  
  37.    //capture = cvCaptureFromCAM( -1 );  
  38.    if( capture )  
  39.    {  
  40.      whiletrue )  
  41.      {  
  42.    frame = cvQueryFrame( capture );  
  43.   
  44.    //-- 3. Apply the classifier to the frame  
  45.        if( !frame.empty() )  
  46.        { detectAndDisplay( frame ); }  
  47.        else  
  48.        { printf(" --(!) No captured frame -- Break!"); break; }  
  49.   
  50.        int c = waitKey(10);  
  51.        if( (char)c == 'c' ) { break; }  
  52.       }  
  53.    }  
  54.    return 0;  
  55.  }  
  56.   
  57. // @function detectAndDisplay   
  58. void detectAndDisplay( Mat frame )  
  59. {  
  60.   std::vector<Rect> faces;  
  61.   Mat frame_gray;  
  62.   
  63.   cvtColor( frame, frame_gray, CV_BGR2GRAY );  
  64.   equalizeHist( frame_gray, frame_gray );  
  65.   
  66.   //-- Detect faces  
  67.   face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );  
  68.   
  69.   forsize_t i = 0; i < faces.size(); i++ )  
  70.   {  
  71.     Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );  
  72.     ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 2, 8, 0 );  
  73.   
  74.     Mat faceROI = frame_gray( faces[i] );  
  75.     std::vector<Rect> eyes;  
  76.   
  77.     //-- In each face, detect eyes  
  78.     eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );  
  79.   
  80.     forsize_t j = 0; j < eyes.size(); j++ )  
  81.      {  
  82.        Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );  
  83.        int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );  
  84.        circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );  
  85.      }  
  86.   }  
  87.   //-- Show what you got  
  88.   imshow( window_name, frame );  
  89.  }  
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值