1.å¦ä½ç¨OPENCVåVCå®ç°äººç¼è¯å«åè½ï¼
å¦ä½ç¨OPENCVåVCå®ç°äººç¼è¯å«åè½ï¼
ããç»ä½ 个代ç ï¼äººç¼çè¯å«ï¼
ãã#include "stdafx.h"
ãã#include "cv.h"
ãã#include "highgui.h"
ãã#include <stdio.h>
ãã#ifdef _EiC
ãã#define WIN
ãã#endif
ããstatic CvMemStorage* storage = 0;
ããstatic CvHaarClassifierCascade* cascade = 0;
ããvoid detect_and_draw( IplImage* image );
ããconst char* cascade_name =
ãã"haarcascade_eye.xml";//人è¸æ£æµåç±»å¨
ããint main( int argc,源码dlll版ce源码 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 = "C:/OpenCV2.0/data/haarcascades/haarcascade_eye.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 cascade\n" );
ããfprintf( stderr,
ãã"Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n" );
ããreturn -1;
ãã}
ããstorage = cvCreateMemStorage(0);
ããcapture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );//读åæå头
ããif(!capture)//å¦æ没ææå头读åè§é¢æ件
ããcapture = cvCaptureFromAVI("æ£æµ.avi");
ããcvNamedWindow( "result", 1);//å建çªå£
ããif( capture )
ãã{
ããfor(;;)
ãã{
ããif( !cvGrabFrame( capture ))//ä»æå头ä¸æå帧
ããbreak;
ããframe = cvRetrieveFrame( 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( ) >= 0 )
ããbreak;
ãã}
ããcvReleaseImage( &frame_copy );
ããcvReleaseCapture( &capture );
ãã}
ããelse//没æ£æµå°è§é¢æ件æè æå头
ãã{
ããconst char* filename = (char*)"æ£æµ.jpg";//读å¾ç
ããIplImage* image = cvLoadImage( filename, 1 );
ããif( image )
ãã{
ããdetect_and_draw( image );
ããcvWaitKey(0);
ããcvReleaseImage( &image );
ãã}
ããelse
ãã{
ããFILE* f = fopen( filename, "rt" );
ããif( f )
ãã{
ããchar buf[+1];
ããwhile( fgets( buf, , 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 )
ãã{
ããstatic CvScalar colors[] =
ãã{
ãã{ { 0,0,}},
ãã{ { 0,,}},
ãã{ { 0,,}},
ãã{ { 0,,0}},
ãã{ { ,,0}},
ãã{ { ,,0}},
ãã{ { ,0,0}},
ãã{ { ,0,}}
ãã};
ããdouble scale = 1.3;
ãã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 t = (double)cvGetTickCount();
ããCvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage,
ãã1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/,
ããcvSize(, ) );//æ£æµäººè¸è¿åç©å½¢äººè¸
ããt = (double)cvGetTickCount() - t;
ããprintf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*.) );
ãã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.*scale);
ããcvCircle( img, center, radius, colors[i%8], 3, 8, 0 );
ãã}
ãã}
ããcvShowImage( "result", img );
ããcvReleaseImage( &gray );
ããcvReleaseImage( &small_img );
ãã}