facedetect.cpp
资源名称:EyeDetect.zip [点击查看]
上传用户:wry1372
上传日期:2022-08-07
资源大小:1438k
文件大小:9k
源码类别:
图形图像处理
开发平台:
C/C++
- #include "cv.h"
- #include "highgui.h"
- #include "cxcore.h"
- #include "mmsystem.h"//导入声音头文件
- #pragma comment(lib,"winmm.lib")//导入声音头文件库
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <assert.h>
- #include <math.h>
- #include <float.h>
- #include <limits.h>
- #include <time.h>
- #include <ctype.h>
- #include <iostream.h>
- #ifdef _EiC
- #define WIN32
- #endif
- static CvMemStorage* storage = 0;
- static CvHaarClassifierCascade* cascade = 0;
- void detect_and_draw( IplImage* image );
- double AreaPercent( IplImage* areadst );
- const char* cascade_name =
- "haarcascade_frontalface_alt.xml";
- /* "haarcascade_profileface.xml";*/
- double countsum;/*全局变量*/
- double average;/*全局变量*/
- int firstcount;/*全局变量*/
- int secondcount;/*全局变量*/
- int numberdetect;/*全局变量*/
- int main2(IplImage * img)
- {
- IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 );
- CvMemStorage* storage = cvCreateMemStorage(0);
- cvCvtColor( img, gray, CV_BGR2GRAY );
- cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9 ); // smooth it, otherwise a lot of false circles may be detected
- CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray->height/4, 150, 40 );
- int i;
- for( i = 0; i < circles->total; i++ )
- {
- float* p = (float*)cvGetSeqElem( circles, i );
- cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), 3, CV_RGB(0,255,0), -1, 8, 0 );
- cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 );
- cout<<"圆心坐标x= "<<cvRound(p[0])<<endl<<"圆心坐标y= "<<cvRound(p[1])<<endl;
- cout<<"半径="<<cvRound(p[2])<<endl;
- }
- cout<<"圆数量="<<circles->total<<endl;
- cvNamedWindow( "circles", 1 );
- cvShowImage( "circles", img );
- return 0;
- }
- int main1( IplImage * Img ){
- int height ,width ,step ,channels ;
- int i,j,k,same ,max,min,thresh;
- uchar*data0,*data1 ;
- IplImage *nimg;
- nimg = cvCreateImage(cvGetSize(Img),8,1);
- height = Img->height;
- width = Img->width;
- step = Img->widthStep/sizeof(uchar);
- channels = Img->nChannels;
- data0 = (uchar*)Img->imageData;
- data1 = (uchar*)nimg->imageData;
- printf("Processing a %d X %d image with %d channelsn",width,height,channels);
- int OffSetX[37] = { -1, 0, 1,
- -2,-1, 0, 1, 2,
- -3,-2,-1, 0, 1, 2, 3,
- -3,-2,-1, 0, 1, 2, 3,
- -3,-2,-1, 0, 1, 2, 3,
- -2,-1, 0, 1, 2,
- -1, 0, 1 };
- int OffSetY[37] = { -3,-3,-3,
- -2,-2,-2,-2,-2,
- -1,-1,-1,-1,-1,-1,-1,
- 0, 0, 0, 0, 0, 0, 0,
- 1, 1, 1, 1, 1, 1, 1,
- 2, 2, 2, 2, 2,
- 3, 3, 3 };
- max = min = data0[0];
- for(i=0;i<height;i++)
- for(j=0;j<width;j++){
- if(data0[i*step+j]>max) max = data0[i*step+j];
- if(data0[i*step+j]<min) min = data0[i*step+j];
- }
- thresh = (max - min )/10;//也可选取其他方法得到的阈值
- for(i=3;i<height-3;i++)
- for(j=3;j<width-3;j++){
- same =0;
- for(k=0;k<37;k++){
- if(fabs( data0[(i+OffSetY[k])*step+(j+OffSetX[k])]-data0[i*step+j])<thresh)
- same++;
- if(same<20)//值可改
- data1[i*step+j] = 255;
- else
- data1[i*step+j] = 0;
- }
- }
- //main2(cvCreateImage(cvSize(nimg->width, nimg->height), IPL_DEPTH_8U, 1));
- printf("aaan");
- cvNamedWindow( "Image", 1 ); //创建窗口
- cvShowImage( "Image", nimg ); //显示图像
- return 0;
- }
- int main( int argc, char** argv )
- {
- CvCapture* capture = 0;
- IplImage *frame, *frame_copy = 0;
- int optlen = strlen("--cascade=");
- const char* input_name;
- if( argc > 1 && strncmp( argv[1], "--cascade=", optlen ) == 0 )
- {
- cascade_name = argv[1] + optlen;
- input_name = argc > 2 ? argv[2] : 0;
- }
- else
- {
- cascade_name = "data/haarcascades/haarcascade_fullbody.xml";
- input_name = argc > 1 ? argv[1] : 0;
- }
- cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
- if( !cascade )
- {
- fprintf( stderr, "ERROR: Could not load classifier cascaden" );
- fprintf( stderr,
- "Usage: facedetect --cascade="<cascade_path>" [filename|camera_index]n" );
- return -1;
- }
- storage = cvCreateMemStorage(0);
- if( !input_name || (isdigit(input_name[0]) && input_name[1] == ' ') )
- capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
- else
- capture = cvCaptureFromAVI( input_name );
- cvNamedWindow( "result", 1 );
- cvNamedWindow( "dst", 1 );
- if( capture )
- {
- extern int firstcount;
- extern int secondcount;
- extern int numberdetect;
- extern double countsum;
- extern double average;
- /*for(firstcount=0;firstcount<100;) //取前100关键帧作为样本
- {
- if( !cvGrabFrame( capture ))
- break;
- frame = cvQueryFrame( capture );
- if( !frame )
- break;
- if( !frame_copy )
- frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
- IPL_DEPTH_8U, frame->nChannels );
- if( frame->origin == IPL_ORIGIN_TL )
- cvCopy( frame, frame_copy, 0 );
- else
- cvFlip( frame, frame_copy, 0 );
- detect_and_draw( frame_copy );
- if( cvWaitKey( 10 ) >= 0 )
- break;
- }*/
- average= countsum*0.6/100; //取样本平均值,与比例系数相乘作为后续参照值
- printf("naverage=%fn",average);
- int i = 2;
- IplImage* pFrame = NULL;
- while(pFrame = cvQueryFrame( capture )){
- //pFrame = cvLoadImage("test.jpg", 1);
- cvShowImage( "result", pFrame );
- if( cvWaitKey( 10 ) >= 0 )
- break;
- //IplImage * img = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U, 1);
- if(i % 50 == 1){
- printf("%dn", i);
- i = 2;
- detect_and_draw( pFrame );
- }
- i++;
- }
- cvReleaseImage( &frame_copy );
- cvReleaseCapture( &capture );
- }else{
- printf("请指定输入视频流!");
- }
- cvDestroyWindow("result");
- cvDestroyWindow("dst");
- return 0;
- }
- void detect_and_draw( IplImage* img )
- {
- static CvScalar colors[] =
- {
- {{0,0,255}},
- {{0,128,255}},
- {{0,255,255}},
- {{0,255,0}},
- {{255,128,0}},
- {{255,255,0}},
- {{255,0,0}},
- {{255,0,255}}
- };
- double scale = 1.3;
- IplImage* dsb = cvCloneImage( img );
- IplImage* dst = cvCloneImage( img );
- IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 );
- IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),
- cvRound (img->height/scale)),
- 8, 1 );
- int i;
- cvCvtColor( img, gray, CV_BGR2GRAY );
- cvResize( gray, small_img, CV_INTER_LINEAR );
- cvEqualizeHist( small_img, small_img );
- cvClearMemStorage( storage );
- if( cascade )
- {
- double length;
- extern int firstcount;
- extern int secondcount;
- extern double countsum;
- extern double average;
- double t = (double)cvGetTickCount();
- CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage,
- 1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/,
- cvSize(30, 30) );
- t = (double)cvGetTickCount() - t;
- printf( "detection time = %gmsn", t/((double)cvGetTickFrequency()*1000.) );
- for( i = 0; i < (faces ? faces->total : 0); i++ ){
- CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
- CvPoint left,right;
- left.x = cvRound((r->x + r->width/6)*scale);
- left.y = cvRound((r->y + r->height/4)*scale);
- right.x = cvRound((r->x + r->width*5/6)*scale);
- right.y = cvRound((r->y + r->height/2)*scale);
- cvRectangle( img, left, right, colors[i%8], 3, 8, 0 );
- cvSetImageROI( dsb, cvRect( left.x, left.y, right.x-left.x, right.y-left.y ));
- dst = cvCreateImage( cvSize( right.x-left.x, right.y-left.y ), 8, 1 );
- cvCvtColor( dsb, dst, CV_BGR2GRAY);
- cvThreshold( dst, dst, 50, 255, CV_THRESH_BINARY);
- cvAdaptiveThreshold( dst, dst, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 21, 5 );
- length = AreaPercent(dst);
- main1(dst);
- printf("Area Percent=%fn",length);
- if(firstcount < 100)
- {
- firstcount++;
- printf("first count number=%dn",firstcount);
- countsum+=length;
- }
- else
- {
- secondcount++;
- printf("second count number=%dn",secondcount);
- if(length < average)
- numberdetect++;
- }
- }
- }
- cvShowImage( "dst", dst );
- cvShowImage( "result", img );
- cvReleaseImage( &dsb );
- cvReleaseImage( &dst );
- cvReleaseImage( &gray );
- cvReleaseImage( &small_img );
- }
- double AreaPercent(IplImage* areadst )//计算图像面积,黑色像素的百分比
- {
- double areapercent,area;
- area=(areadst->width)*(areadst->height);
- areapercent=(double)(cvCountNonZero(areadst)/area);
- return 1-areapercent;
- }