From b7e484d5baf2f9173ce53595b4e06eabeb30e036 Mon Sep 17 00:00:00 2001 From: ozzloy Date: Sat, 7 Feb 2009 21:45:34 -0800 Subject: [PATCH] Initial commit --- Makefile | 3 ++ hello-world.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 Makefile create mode 100644 hello-world.cpp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3e0fc3a --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ + +all: + gcc `pkg-config --cflags opencv` -o hello-world hello-world.cpp `pkg-config --libs opencv` diff --git a/hello-world.cpp b/hello-world.cpp new file mode 100644 index 0000000..e9bb476 --- /dev/null +++ b/hello-world.cpp @@ -0,0 +1,86 @@ +#include +#include +#include + +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 input image 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 rectangles only */ + //CvRect face_rect = *(CvRect*)cvGetSeqElem( faces, i, 0 ); + 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 ); +} + +// A Simple Camera Capture Framework +int main( int argc, char** argv ) +{ + CvHaarClassifierCascade* cascade = load_object_detector(argv[1]); + + CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY ); + if( !capture ) { + fprintf( stderr, "ERROR: capture is NULL \n" ); + getchar(); + return -1; + } + + // Create a window in which the captured images will be presented + cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE ); + + // Show the image captured from the camera in the window and repeat + while( 1 ) { + // Get one frame + IplImage* frame = cvQueryFrame( capture ); + if( !frame ) { + fprintf( stderr, "ERROR: frame is null...\n" ); + getchar(); + break; + } + + detect_and_draw_objects( frame, cascade, 1 ); + cvShowImage( "mywindow", frame ); + // Do not release the frame! + + //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), + //remove higher bits using AND operator + if( (cvWaitKey(10) & 255) == 27 ) break; + } + + // Release the capture device housekeeping + cvReleaseCapture( &capture ); + cvDestroyWindow( "mywindow" ); + return 0; +} -- 2.30.2