Learning OpenCV: 一个简单的人眼检测程序

 

Learning OpenCV: 一个简单的人眼检测程序

分类: 图像处理   1814人阅读  评论(5)  收藏  举报

人眼检测 分两步骤:

 

1. 人脸检测,得到一个人脸矩形区域

2. 在人脸矩形区域进行人眼检测

 

以下是部分源代码:

 

[cpp:nogutter]  view plain copy
  1. #include "stdafx.h"  
  2. #include "cv.h"  
  3. #include "highgui.h"  
  4. #include <assert.h>  
  5. void detectEyes(IplImage *img)  
  6. {  
  7.     /*allocate storage*/  
  8.     CvMemStorage* storage = 0;  
  9.     storage = cvCreateMemStorage(0) ;  
  10.     /*load face cascade*/  
  11.     CvHaarClassifierCascade* cascade_f = 0;  
  12.     const char* cascade_name = "haarcascade_frontalface_alt2.xml";  
  13.     cascade_f = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );  
  14.       
  15.     /* detect faces */  
  16.     CvSeq *faces = cvHaarDetectObjects(  
  17.         img,            /* the source image */  
  18.         cascade_f,      /* the face classifier */  
  19.         storage,        /* memory buffer, created with cvMemStorage */  
  20.         1.1, 3, 0,      /* special parameters, tune for your app */  
  21.         cvSize(40, 40)  /* minimum detection scale */  
  22.     );  
  23.    
  24.     /* return if not found */  
  25.     if (faces->total == 0) return;  
  26.   
  27.     /* get the first detected face */  
  28.     CvRect *face = (CvRect*)cvGetSeqElem(faces, 0);  
  29.    
  30.     /* draw a red rectangle */  
  31.     cvRectangle(  
  32.         img,  
  33.         cvPoint(face->x, face->y),  
  34.         cvPoint(  
  35.             face->x + face->width,  
  36.             face->y + face->height  
  37.         ),  
  38.         CV_RGB(255, 0, 0),  
  39.         1, 8, 0  
  40.     );  
  41.    
  42.     /* reset buffer for the next object detection */  
  43.     cvClearMemStorage(storage);  
  44.     //cvRelease((void**)cascade_f);  
  45.        
  46.   
  47.     /* Set the Region of Interest: estimate the eyes' position */  
  48.     cvSetImageROI(  
  49.         img,                    /* the source image */  
  50.         cvRect(  
  51.             face->x,            /* x = start from leftmost */  
  52.             face->y + (face->height/5.5), /* y = a few pixels from the top */  
  53.             face->width,        /* width = same width with the face */  
  54.             face->height/3.0    /* height = 1/3 of face height */  
  55.         )  
  56.     );  
  57.   
  58.     /*load eye cascade*/  
  59.     CvHaarClassifierCascade* cascade_e = 0;  
  60.       
  61.     const char* cascade_name2 = "I://imgs//haarcascade_eye.xml";  
  62.     cascade_e = (CvHaarClassifierCascade*)cvLoad( cascade_name2, 0, 0, 0 );  
  63.     assert(cascade_e != NULL);  
  64.     //storage = cvCreateMemStorage(0) ;  
  65.     /* detect the eyes */  
  66.     CvSeq *eyes = cvHaarDetectObjects(  
  67.         img,            /* the source image, with the 
  68.                            estimated location defined */  
  69.         cascade_e,      /* the eye classifier */  
  70.         storage,        /* memory buffer */  
  71.         1.15, 3, 0,     /* tune for your app */  
  72.         cvSize(25, 15)  /* minimum detection scale */  
  73.     );  
  74.   
  75.     int i;  
  76.      
  77.     /* draw a rectangle for each detected eye */  
  78.     for( i = 0; i < (eyes ? eyes->total : 0); i++ ) {  
  79.         /* get one eye */  
  80.         CvRect *eye = (CvRect*)cvGetSeqElem(eyes, i);  
  81.          
  82.         /* draw a red rectangle */  
  83.         cvRectangle(  
  84.             img,  
  85.             cvPoint(eye->x, eye->y),  
  86.             cvPoint(eye->x + eye->width, eye->y + eye->height),  
  87.             CV_RGB(255, 0, 0),  
  88.             1, 8, 0  
  89.         );  
  90.     }  
  91.        
  92.    /* reset region of interest */  
  93.         cvResetImageROI(img);  
  94. }  
  95. /* end of detectEyes() */  
  96.    
  97. void detect_eye_and_display_pic(char *argv)  
  98. {  
  99.     IplImage* img = cvLoadImage( argv );  
  100.     //opens a window on the screen that can  
  101.     //contain and display an image  
  102.     cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );  
  103.     //detect eyes in the image  
  104.     detectEyes(img);  
  105.       
  106.     //Whenever we have an image in the form of an IplImage* pointer, we can display it in an  
  107.     //existing window with cvShowImage().  
  108.     //The cvShowImage() function requires that a named  
  109.     //window already exist (created by cvNamedWindow()).  
  110.     cvShowImage( "Example1", img );  
  111.     //The cvWaitKey() function asks the program to stop and wait for a keystroke  
  112.     cvWaitKey(0);  
  113.     //free the allocated memory  
  114.     cvReleaseImage( &img );  
  115.     cvDestroyWindow( "Example1" );  
  116. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值