




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
#include opencv2/objdetect/objdetect.hpp#include opencv2/highgui/highgui.hpp#include opencv2/imgproc/imgproc.hpp#include #include #include #include using namespace std;using namespace cv;static void help() cout nThis program demonstrates the cascade recognizer. Now you can use Haar or LBP features.n This classifier can recognize many kinds of rigid objects, once the appropriate classifier is trained.n Its most known use is for faces.n Usage:n ./facedetect -cascade= this is the primary trained classifier such as frontal facen -nested-cascade=nested_cascade_path this an optional secondary classifier such as eyesn -scale=n -try-flipn filename|camera_indexnn see facedetect.cmd for one call:n ./facedetect -cascade=././data/haarcascades/haarcascade_frontalface_alt.xml -nested-cascade=././data/haarcascades/haarcascade_eye.xml -scale=1.3nn During execution:ntHit any key to quit.n tUsing OpenCV version CV_VERSION n endl;void detectAndDraw( Mat& img, CascadeClassifier& cascade, CascadeClassifier& nestedCascade, double scale, bool tryflip );string cascadeName = ././data/haarcascades/haarcascade_frontalface_alt.xml;string nestedCascadeName = ././data/haarcascades/haarcascade_eye_tree_eyeglasses.xml;int main( int argc, const char* argv ) CvCapture* capture = 0; Mat frame, frameCopy, image; const string scaleOpt = -scale=; size_t scaleOptLen = scaleOpt.length(); const string cascadeOpt = -cascade=; size_t cascadeOptLen = cascadeOpt.length(); const string nestedCascadeOpt = -nested-cascade; size_t nestedCascadeOptLen = nestedCascadeOpt.length(); const string tryFlipOpt = -try-flip; size_t tryFlipOptLen = tryFlipOpt.length(); string inputName; bool tryflip = false; help(); CascadeClassifier cascade, nestedCascade; double scale = 1; for( int i = 1; i argc; i+ ) cout Processing i argvi endl; if( cascadeOpare( 0, cascadeOptLen, argvi, cascadeOptLen ) = 0 ) cascadeName.assign( argvi + cascadeOptLen ); cout from which we have cascadeName= cascadeName endl; else if( nestedCascadeOpare( 0, nestedCascadeOptLen, argvi, nestedCascadeOptLen ) = 0 ) if( argvinestedCascadeOpt.length() = = ) nestedCascadeName.assign( argvi + nestedCascadeOpt.length() + 1 ); if( !nestedCascade.load( nestedCascadeName ) ) cerr WARNING: Could not load classifier cascade for nested objects endl; else if( scaleOpare( 0, scaleOptLen, argvi, scaleOptLen ) = 0 ) if( !sscanf( argvi + scaleOpt.length(), %lf, &scale ) | scale 1 ) scale = 1; cout from which we read scale = scale endl; else if( tryFlipOpare( 0, tryFlipOptLen, argvi, tryFlipOptLen ) = 0 ) tryflip = true; cout will try to flip image horizontally to detect assymetric objectsn; else if( argvi0 = - ) cerr WARNING: Unknown option %s argvi endl; else inputName.assign( argvi ); if( !cascade.load( cascadeName ) ) cerr ERROR: Could not load classifier cascade endl; help(); return -1; if( inputName.empty() | (isdigit(inputName.c_str()0) & inputName.c_str()1 = 0) ) capture = cvCaptureFromCAM( inputName.empty() ? 0 : inputName.c_str()0 - 0 ); int c = inputName.empty() ? 0 : inputName.c_str()0 - 0 ; if(!capture) cout Capture from CAM c didnt work endl; else if( inputName.size() ) image = imread( inputName, 1 ); if( image.empty() ) capture = cvCaptureFromAVI( inputName.c_str() ); if(!capture) cout Capture from AVI didnt work endl; else image = imread( lena.jpg, 1 ); if(image.empty() cout Couldnt read lena.jpg endl; cvNamedWindow( result, 1 ); if( capture ) cout In capture . origin = IPL_ORIGIN_TL ) frame.copyTo( frameCopy ); else flip( frame, frameCopy, 0 ); detectAndDraw( frameCopy, cascade, nestedCascade, scale, tryflip ); if( waitKey( 10 ) = 0 ) goto _cleanup_; waitKey(0);_cleanup_: cvReleaseCapture( &capture ); else cout In image read 0 & isspace(buflen-1) ) len-; buflen = 0; cout file buf endl; image = imread( buf, 1 ); if( !image.empty() ) detectAndDraw( image, cascade, nestedCascade, scale, tryflip ); c = waitKey(0); if( c = 27 | c = q | c = Q ) break; else cerr Aw snap, couldnt read image buf endl; fclose(f); cvDestroyWindow(result); return 0;void detectAndDraw( Mat& img, CascadeClassifier& cascade, CascadeClassifier& nestedCascade, double scale, bool tryflip ) int i = 0; double t = 0; vector faces, faces2; const static Scalar colors = CV_RGB(0,0,255), CV_RGB(0,128,255), CV_RGB(0,255,255), CV_RGB(0,255,0), CV_RGB(255,128,0), CV_RGB(255,255,0), CV_RGB(255,0,0), CV_RGB(255,0,255) ; Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 ); cvtColor( img, gray, CV_BGR2GRAY ); resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR ); equalizeHist( smallImg, smallImg ); t = (double)cvGetTickCount(); cascade.detectMultiScale( smallImg, faces, 1.1, 2, 0 /|CV_HAAR_FIND_BIGGEST_OBJECT /|CV_HAAR_DO_ROUGH_SEARCH |CV_HAAR_SCALE_IMAGE , Size(30, 30) ); if( tryflip ) flip(smallImg, smallImg, 1); cascade.detectMultiScale( smallImg, faces2, 1.1, 2, 0 /|CV_HAAR_FIND_BIGGEST_OBJECT /|CV_HAAR_DO_ROUGH_SEARCH |CV_HAAR_SCALE_IMAGE , Size(30, 30) ); for( vector:const_iterator r = faces2.begin(); r != faces2.end(); r+ ) faces.push_back(Rect(smallImg.cols - r-x - r-width, r-y, r-width, r-height); t = (double)cvGetTickCount() - t; printf( detection time = %g msn, t/(double)cvGetTickFrequency()*1000.) ); for( vector:const_iterator r = faces.begin(); r != faces.end(); r+, i+ ) Mat smallImgROI; vector nestedObjects; Point center; Scalar color = colorsi%8; int radius; double aspect_ratio = (double)r-width/r-height; if( 0.75 aspect_ratio & aspect_ratio 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); circle( img, center, radius, color, 3, 8, 0 ); else rectangle( img, cvPoint(cvRound(r-x*scale), cvRound(r-y*scale), cvPoint(cvRound(r-x + r-width-1)*
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年美术联考试题及答案
- 中医排淤师考试题及答案
- 中药制剂工考试题及答案
- 2025供应商合同与订购合同
- 标准租赁合同下载8篇
- 2025平安银行招聘笔试题及答案
- 2025民生银行校招笔试题目及答案
- 2025人工智能技术研发劳务合同
- 无锡幼儿园食品安全培训课件
- 《医院消毒供应中心管理规范》试题(附答案)
- 2024年贵州水城能投发电有限公司招聘笔试冲刺题(带答案解析)
- 中国古代的声学成就
- 第1课 社会主义在中国的确立与探索(课件)-【中职专用】高一思想政治《中国特色社会主义》(高教版2023·基础模块)
- 二年级奥数(从课本到奥数-第一学期B版)
- 北美所有护理诊断
- 吊篮施工验收标准及规范
- 银行养生沙龙策划方案
- 小学生主题班会通用版《军章闪耀》 课件
- 软件系统权限申请表
- 自主实习申请表
- 长途用车申请表
评论
0/150
提交评论