haarcascade.cpp
上传用户:banwdc
上传日期:2016-06-25
资源大小:2871k
文件大小:2k
- #include "cv.h"
- #include "highgui.h"
- CvHaarClassifierCascade* load_object_detector( const char* cascade_path )
- {
- return (CvHaarClassifierCascade*)cvLoad( cascade_path );
- }
- void detect_and_draw_objects( IplImage* image,
- CvHaarClassifierCascade* cascade,
- int do_pyramids )
- {
- IplImage* small_image = image;
- CvMemStorage* storage = cvCreateMemStorage(0);
- CvSeq* faces;
- int i, scale = 1;
- /* if the flag is specified, down-scale the 输入图像 to get a
- performance boost w/o loosing quality (perhaps) */
- if( do_pyramids )
- {
- small_image = cvCreateImage( cvSize(image->width/2,image->height/2),
- IPL_DEPTH_8U, 3 );
- cvPyrDown( image, small_image, CV_GAUSSIAN_5x5 );
- scale = 2;
- }
- /* use the fastest variant */
- faces = cvHaarDetectObjects( small_image, cascade, storage, 1.2, 2,
- CV_HAAR_DO_CANNY_PRUNING );
- /* draw all the rectangles */
- for( i = 0; i < faces->total; i++ )
- {
- /* extract the rectanlges only */
- CvRect face_rect = *(CvRect*)cvGetSeqElem( faces, i );
- cvRectangle( image, cvPoint(face_rect.x*scale,face_rect.y*scale),
- cvPoint((face_rect.x+face_rect.width)*scale,
- (face_rect.y+face_rect.height)*scale),
- CV_RGB(255,0,0), 3 );
- }
- if( small_image != image )
- cvReleaseImage( &small_image );
- cvReleaseMemStorage( &storage );
- }
- /* takes image filename and cascade path from the command line */
- int main( int argc, char** argv )
- {
- IplImage* image;
- if( argc==3 && (image = cvLoadImage( argv[1], 1 )) != 0 )
- {
- CvHaarClassifierCascade* cascade = load_object_detector(argv[2]);
- detect_and_draw_objects( image, cascade, 1 );
- cvNamedWindow( "test", 0 );
- cvShowImage( "test", image );
- cvWaitKey(0);
- cvReleaseHaarClassifierCascade( &cascade );
- cvReleaseImage( &image );
- }
- return 0;
- }