commit for github
[ozzloy@gmail.com/oble] / hello-world.cpp
1 #include <cv.h>
2 #include <highgui.h>
3 #include <stdio.h>
4
5 CvHaarClassifierCascade* load_object_detector(const char* cascade_path)
6 {
7 return (CvHaarClassifierCascade*)cvLoad( cascade_path );
8 }
9
10 void detect_and_draw_objects(IplImage* image,
11 CvHaarClassifierCascade* cascade,
12 int do_pyramids)
13 {
14 IplImage* small_image = image;
15 CvMemStorage* storage = cvCreateMemStorage(0);
16 CvSeq* faces;
17 int i, scale = 1;
18
19 /* if the flag is specified, down-scale the input image to get a
20 performance boost w/o loosing quality (perhaps) */
21 if( do_pyramids )
22 {
23 small_image = cvCreateImage( cvSize(image->width/2,image->height/2), IPL_DEPTH_8U, 3 );
24 cvPyrDown( image, small_image, CV_GAUSSIAN_5x5 );
25 scale = 2;
26 }
27
28 /* use the fastest variant */
29 faces = cvHaarDetectObjects( small_image, cascade, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING );
30
31 /* draw all the rectangles */
32 for( i = 0; i < faces->total; i++ )
33 {
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),
40 CV_RGB(255,0,0), 3 );
41 }
42
43 if( small_image != image )
44 cvReleaseImage( &small_image );
45 cvReleaseMemStorage( &storage );
46 }
47
48 // A Simple Camera Capture Framework.
49 >>>>>>> experimental:hello-world.cpp
50 int main( int argc, char** argv )
51 {
52 CvHaarClassifierCascade* cascade = load_object_detector(argv[1]);
53
54 CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
55 if( !capture ) {
56 fprintf( stderr, "ERROR: capture is NULL \n" );
57 getchar();
58 return -1;
59 }
60
61 // Create a window in which the captured images will be presented
62 cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
63
64 // Show the image captured from the camera in the window and repeat
65 while( 1 ) {
66 // Get one frame
67 IplImage* frame = cvQueryFrame(capture);
68 IplImage* flipped = cvCreateImage(cvSize(frame->width, frame->height),
69 frame->depth, frame->nChannels);
70 if( !frame ) {
71 fprintf( stderr, "ERROR: frame is null...\n" );
72 getchar();
73 break;
74 }
75
76 cvFlip(frame, flipped, 1);
77
78 detect_and_draw_objects(flipped, cascade, 1);
79 cvShowImage( "mywindow", flipped);
80 // Do not release the frame!
81
82 //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
83 //remove higher bits using AND operator
84 if( (cvWaitKey(10) & 255) == 27 ) break;
85 }
86
87 // Release the capture device housekeeping
88 cvReleaseCapture( &capture );
89 cvDestroyWindow( "mywindow" );
90 return 0;
91 }