5 CvHaarClassifierCascade
* load_object_detector(const char* cascade_path
)
7 return (CvHaarClassifierCascade
*)cvLoad( cascade_path
);
10 void detect_and_draw_objects(IplImage
* image
,
11 CvHaarClassifierCascade
* cascade
,
14 IplImage
* small_image
= image
;
15 CvMemStorage
* storage
= cvCreateMemStorage(0);
19 /* if the flag is specified, down-scale the input image to get a
20 performance boost w/o loosing quality (perhaps) */
23 small_image
= cvCreateImage( cvSize(image
->width
/2,image
->height
/2), IPL_DEPTH_8U
, 3 );
24 cvPyrDown( image
, small_image
, CV_GAUSSIAN_5x5
);
28 /* use the fastest variant */
29 faces
= cvHaarDetectObjects( small_image
, cascade
, storage
, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING
);
31 /* draw all the rectangles */
32 for( i
= 0; i
< faces
->total
; i
++ )
34 /* extract the rectangles only */
35 //CvRect face_rect = *(CvRect*)cvGetSeqElem( faces, i, 0 );
36 CvRect face_rect
= *(CvRect
*)cvGetSeqElem( faces
, i
);
37 cvRectangle( image
, cvPoint(face_rect
.x
*scale
,face_rect
.y
*scale
),
38 cvPoint((face_rect
.x
+face_rect
.width
)*scale
,
39 (face_rect
.y
+face_rect
.height
)*scale
),
43 if( small_image
!= image
)
44 cvReleaseImage( &small_image
);
45 cvReleaseMemStorage( &storage
);
48 // A Simple Camera Capture Framework
49 int main( int argc
, char** argv
)
51 CvHaarClassifierCascade
* cascade
= load_object_detector(argv
[1]);
53 CvCapture
* capture
= cvCaptureFromCAM( CV_CAP_ANY
);
55 fprintf( stderr
, "ERROR: capture is NULL \n" );
60 // Create a window in which the captured images will be presented
61 cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE
);
63 // Show the image captured from the camera in the window and repeat
66 IplImage
* frame
= cvQueryFrame(capture
);
67 IplImage
* flipped
= cvCreateImage(cvSize(frame
->width
, frame
->height
),
68 frame
->depth
, frame
->nChannels
);
70 fprintf( stderr
, "ERROR: frame is null...\n" );
75 cvFlip(frame
, flipped
, 1);
77 detect_and_draw_objects(flipped
, cascade
, 1);
78 cvShowImage( "mywindow", flipped
);
79 // Do not release the frame!
81 //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
82 //remove higher bits using AND operator
83 if( (cvWaitKey(10) & 255) == 27 ) break;
86 // Release the capture device housekeeping
87 cvReleaseCapture( &capture
);
88 cvDestroyWindow( "mywindow" );