人脸位置识别

haar classifier cascade是opencv下自带的人脸检测的级联分类器,支持haar特征,新版本的CascadeClassifier好像支持LBP特征,没试过。

HaarDetect.h

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #ifndef HAARDETECT_H  
  2. #define HAARDETECT_H  
  3. #include <opencv2\highgui\highgui.hpp>  
  4. #include <opencv2\imgproc\imgproc.hpp>  
  5. #include <opencv2\objdetect\objdetect.hpp>  
  6. using namespace std;  
  7. using namespace cv;  
  8.   
  9. CvHaarClassifierCascade* Load_Haar_Cascade();  
  10.   
  11. void detectFace(IplImage* frame,CvHaarClassifierCascade *faceCascade,CvRect *faceRect,int &count);  
  12.   
  13. #endif  


HaarDetect.cpp

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include "HaarDetect.h"  
  2. #include <iostream>  
  3. #include <opencv2/opencv.hpp>  
  4. using namespace std;  
  5. CvHaarClassifierCascade* Load_Haar_Cascade()  
  6. {  
  7.     CvHaarClassifierCascade* faceCascade = NULL;  
  8.     const char *faceCascadeFilename = "D:\\软件\\opencv\\sources\\data\\cascades\\haarcascade_frontalface_alt.xml";  
  9.     faceCascade = (CvHaarClassifierCascade*)cvLoad(faceCascadeFilename,0,0,0);  
  10.     if(faceCascade==NULL)  
  11.     {  
  12.         cout<<"error in Load_Haar_Cascade:faceCascade is NULL"<<endl;  
  13.     }  
  14.     return faceCascade;  
  15. }  
  16. void detectFace(IplImage* frame,CvHaarClassifierCascade *faceCascade,CvRect *faceRect,int &count)  
  17. {  
  18.       
  19.     const int flag = CV_HAAR_DO_CANNY_PRUNING;  
  20.     CvSeq *detectedFaces;  
  21.     CvMemStorage* storage;  
  22.     const double scale_factore = 1.1f;  
  23.     storage = cvCreateMemStorage(0);  
  24.     cvClearMemStorage(storage);  
  25.     if(faceCascade==NULL)  
  26.     {  
  27.         cout<<"error in detectFace:faceCascade is NULL!"<<endl;  
  28.         exit(1);  
  29.     }  
  30.     IplImage* grayFrame = NULL;  
  31.     grayFrame = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);  
  32.     cvCvtColor(frame,grayFrame,CV_RGB2GRAY);  
  33.     detectedFaces = cvHaarDetectObjects(grayFrame,faceCascade,storage,scale_factore,3,flag,cvSize(20,20));  
  34.     count = detectedFaces->total;  
  35.     if(detectedFaces==NULL)  
  36.     {  
  37.         cout<<"error in detectFace:detectedFaces is NULL"<<endl;  
  38.     }  
  39.     for(int i=0;i<(detectedFaces->total);i++)  
  40.     {  
  41.         faceRect[i] = *(CvRect *)cvGetSeqElem(detectedFaces,i);  
  42.     }  
  43.     if(faceRect == NULL)  
  44.     {  
  45.         cout<<"error in detectFace:faceRect is NULL!"<<endl;  
  46.     }  
  47.     cvReleaseMemStorage(&storage);  
  48.     cvReleaseImage(&grayFrame);  
  49. }  

main.cpp

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <windows.h>  
  3. //#include <conio.h>  
  4. #include <opencv2\highgui\highgui.hpp>  
  5. #include <opencv2\imgproc\imgproc.hpp>  
  6. #include <opencv2\objdetect\objdetect.hpp>  
  7. #include "HaarDetect.h"  
  8. using namespace std;  
  9. using namespace cv;  
  10.   
  11. int main()  
  12. {  
  13.     CvCapture *camera;  
  14.     CvHaarClassifierCascade* faceCascade = NULL;  
  15.     CvRect faceRect[30];  
  16.     int faceCount;  
  17.     faceCascade = Load_Haar_Cascade();  
  18.     if(!faceCascade)  
  19.         return 0;  
  20.     camera = cvCreateCameraCapture(0);  
  21.     if(camera==NULL)  
  22.     {  
  23.         cout<<"camera is NULL"<<endl;  
  24.         return 0;  
  25.     }  
  26.     IplImage *frame;  
  27.     /*IplImage *frame = cvCreateImage(  
  28.     cvSize( cvGetCaptureProperty(camera,CV_CAP_PROP_FRAME_WIDTH),cvGetCaptureProperty(camera,CV_CAP_PROP_FRAME_HEIGHT) ), 
  29.     IPL_DEPTH_8U,1);*/  
  30.     Sleep(5000);    //wait for init the camera  
  31.     while(true)  
  32.     {  
  33.         /*faceRect = NULL;*/  
  34.         faceCount = 0;  
  35.         frame = cvQueryFrame(camera);  
  36.         if(frame==NULL)  
  37.         {  
  38.             cout<<"frame is NULL"<<endl;  
  39.             break;  
  40.         }  
  41.         detectFace(frame,faceCascade,faceRect,faceCount);  
  42.         //if(faceRect == NULL)  
  43.         //{  
  44.         //  exit(1);  
  45.         //}  
  46.         for(int i=0;i < faceCount;i++)  
  47.         {  
  48.             cvRectangle(frame,cvPoint(faceRect[i].x,faceRect[i].y),cvPoint(faceRect[i].x+faceRect[i].width-1,faceRect[i].y+faceRect[i].height-1), CV_RGB(0,255,0), 1, 8, 0);  
  49.         }  
  50.         cvNamedWindow("Camera",CV_WINDOW_AUTOSIZE);  
  51.         cvShowImage("Camera",frame);  
  52.         cvWaitKey(10);  
  53.     }  
  54.     destroyAllWindows();  
  55.     cvReleaseCapture(&camera);  
  56.     return 0;  
  57. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别人脸识别

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值