opencv 中的人臉識別源程序 基於haar特徵的adaboost算法

  1. #include "cv.h"  
  2. #include "highgui.h"  
  3.   
  4. #include <stdio.h>  
  5. #include <stdlib.h>  
  6. #include <string.h>  
  7. #include <assert.h>  
  8. #include <math.h>  
  9. #include <float.h>  
  10. #include <limits.h>  
  11. #include <time.h>  
  12. #include <ctype.h>  
  13.   
  14. static CvMemStorage* storage = 0;  
  15. static CvHaarClassifierCascade* cascade = 0;  
  16.   
  17. void detect_and_draw( IplImage* image );  
  18.   
  19. const char* cascade_name =  
  20. "haarcascade_frontalface_alt.xml";  
  21. /* "haarcascade_profileface.xml";*/  
  22.   
  23. int main()  
  24. {  
  25.     CvCapture* capture = 0;  
  26.   
  27.     cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );  
  28.   
  29.     if( !cascade )  
  30.     {  
  31.         fprintf( stderr, "ERROR: Could not load classifier cascade/n" );  
  32.         fprintf( stderr,  
  33.             "Usage: facedetect --cascade=/"<cascade_path>/" [filename|camera_index]/n" );  
  34.         return -1;  
  35.     }  
  36.     storage = cvCreateMemStorage(0);  
  37.   
  38.   
  39.     cvNamedWindow( "result", 1 );  
  40.   
  41.   
  42.     const char* filename = "leader2.jpg";  
  43.     IplImage* image = cvLoadImage(filename );  
  44.   
  45.     if( image )  
  46.     {  
  47.         detect_and_draw( image );  
  48.         cvWaitKey(0);  
  49.         cvReleaseImage( &image );  
  50.     }  
  51.   
  52.     cvDestroyWindow("result");  
  53.     cvWaitKey(0);  
  54.     return 0;  
  55. }  
  56.   
  57. void detect_and_draw( IplImage* img )  
  58. {  
  59.     static CvScalar colors[] =   
  60.     {  
  61.         {{0,0,255}},  
  62.         {{0,128,255}},  
  63.         {{0,255,255}},  
  64.         {{0,255,0}},  
  65.         {{255,128,0}},  
  66.         {{255,255,0}},  
  67.         {{255,0,0}},  
  68.         {{255,0,255}}  
  69.     };  
  70.   
  71.     double scale = 1.3;  
  72.     IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 );  
  73.     IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),  
  74.         cvRound (img->height/scale)),  
  75.         8, 1 );  
  76.     int i;  
  77.   
  78.     cvCvtColor( img, gray, CV_BGR2GRAY );  
  79.     cvResize( gray, small_img, CV_INTER_LINEAR );  
  80.     cvEqualizeHist( small_img, small_img );  
  81.     cvClearMemStorage( storage );  
  82.   
  83.     if( cascade )  
  84.     {  
  85.         double t = (double)cvGetTickCount();  
  86.         CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage,  
  87.             1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/,  
  88.             cvSize(30, 30) );  
  89.         t = (double)cvGetTickCount() - t;  
  90.         printf( "detection time = %gms/n", t/((double)cvGetTickFrequency()*1000.) );  
  91.         for( i = 0; i < (faces ? faces->total : 0); i++ )  
  92.         {  
  93.             CvRect* r = (CvRect*)cvGetSeqElem( faces, i );  
  94.             CvPoint center;  
  95.             int radius;  
  96.             center.x = cvRound((r->x + r->width*0.5)*scale);  
  97.             center.y = cvRound((r->y + r->height*0.5)*scale);  
  98.             radius = cvRound((r->width + r->height)*0.25*scale);  
  99.             cvCircle( img, center, radius, colors[i%8], 3, 8, 0 );  
  100.         }  
  101.     }  
  102.   
  103.     cvShowImage( "result", img );  
  104.     cvReleaseImage( &gray );  
  105.     cvReleaseImage( &small_img );  
  106. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章