利用opencv实现简单的人脸识别

2 篇文章 0 订阅
1 篇文章 0 订阅
// face.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "cv.h"#include#include #include #include #include #include #include #include #include #include #ifdef _EiC #define WIN32 #endif static CvMemStorage* storage = 0; static CvHaarClassifierCascade* cascade = 0; void detect_and_draw( IplImage* image ); const char* cascade_name = "haarcascade_frontalface_alt.xml"; /* "haarcascade_profileface.xml";*/ 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 = "H://opencv1.0//OpenCV//data//haarcascades//haarcascade_frontalface_alt2.xml"; //input_name = argc > 1 ? argv[1] : 0; input_name="C:\\Users\\Administrator\\Desktop\\1.jpg"; } cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );//加载分类器 printf("加载分类器:haarcascade_frontalface_alt2.xml\n"); if( !cascade ) { fprintf( stderr, "ERROR: Could not load classifier cascade\n" ); fprintf( stderr, "Usage: facedetect --cascade=\"\" [filename|camera_index]\n" ); return -1; } storage = cvCreateMemStorage(0); //读入待检测图像。读入图片或者视频 if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') ){ MessageBox(NULL,"消息","调用摄像头",MB_OK); capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );//用于你打开视频那窗口} else {printf("判断为非摄像源!执行本地视频,图片加载\n"); capture = cvCaptureFromAVI( input_name ); } cvNamedWindow( "result", 1 ); if( capture ) { for(;;) { //读取视频帧 if( !cvGrabFrame( capture )) //从摄像头或者视频文件中抓取帧 break; frame = cvRetrieveFrame( capture ); //函数cvRetrieveFrame返回由函数cvGrabFrame 抓取的图像的指针。返回的图像不可以被用户释放或者修改。 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; } cvReleaseImage( &frame_copy ); cvReleaseCapture( &capture ); } else { //读取默认图片printf("判断资源为本地图片源执行加载.....\n"); const char* filename = input_name ? input_name : (char*)"lena.jpg"; //读取图片 IplImage* image = cvLoadImage( filename, 1 ); printf("加载图片%s........\n",filename); if( image ) { detect_and_draw( image ); cvWaitKey(0); cvReleaseImage( &image ); } else { /* assume it is a text file containing the list of the image filenames to be processed - one per line */ FILE* f = fopen( filename, "rt" ); if( f ) { char buf[1000+1]; while( fgets( buf, 1000, f ) ) { int len = (int)strlen(buf); while( len > 0 && isspace(buf[len-1]) ) len--; buf[len] = '\0'; image = cvLoadImage( buf, 1 ); if( image ) { detect_and_draw( image ); cvWaitKey(0); cvReleaseImage( &image ); } } fclose(f); } } } cvDestroyWindow("result"); return 0; } //人脸检测和标记 void detect_and_draw( IplImage* img ) { int checkNum = 0; double t = (double)cvGetTickCount(); 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* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 ); IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),// double型的数进行四舍五入 cvRound (img->height/scale)), 8, 1 ); int i; // Opencv里的颜色空间转换函数,可以实现RGB颜色向HSV,HSI等颜色空间的转换,也可以转换为灰度图像。 cvCvtColor( img, gray, CV_BGR2GRAY ); // cvResize( gray, small_img, CV_INTER_LINEAR );// cvEqualizeHist( small_img, small_img ); // 函数cvResize 重新调整图像src(或它的ROI),使它精确匹配目标dst(或其ROI)。这里需要说明的是,cvResize可以用来调整3通道图像(如RGB图像)和单通道图像的大小。 cvClearMemStorage( storage ); //cvEqualizeHist该函数为开放计算机视觉(OpenCV)库库函数,用来使灰度图象直方图均衡化。 if( cascade ) { //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 = %gms\n", t/((double)cvGetTickFrequency()*1000.) ); //结果标记 for( i = 0; i < (faces ? faces->total : 0); i++ ) { CvRect* r = (CvRect*)cvGetSeqElem( faces, i ); CvPoint center; int radius; center.x = cvRound((r->x + r->width*0.5)*scale); center.y = cvRound((r->y + r->height*0.5)*scale); radius = cvRound((r->width + r->height)*0.25*scale); cvCircle( img, center, radius, colors[i%8], 3, 8, 0 );checkNum++; printf("检测%d个人脸并标识\n",checkNum); } } printf("----------------------------------------------------\n");printf( "检测 用时time = %gms\n", t/((double)cvGetTickFrequency()*1000.) ); printf("检测%d个人脸",checkNum); cvShowImage( "result", img ); cvReleaseImage( &gray ); cvReleaseImage( &small_img ); }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值