From: ozzloy Date: Tue, 9 Mar 2010 09:13:03 +0000 (-0800) Subject: break down big functions. Closes #12 X-Git-Url: http://challenge-bot.com/repos/?p=ozzloy%40gmail.com%2Foble;a=commitdiff_plain;h=12af27da4c505ca3a52d28063cadd6b69996212d break down big functions. Closes #12 --- diff --git a/oble.cpp b/oble.cpp index 5de3abe..3f351ae 100644 --- a/oble.cpp +++ b/oble.cpp @@ -64,7 +64,6 @@ void detect_and_draw_objects(IplImage* image, CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* faces; int i, scale = 1; - static int saw_last_time = 0; /* if the flag is specified, down-scale the input image to get a performance boost w/o loosing quality (perhaps) */ @@ -80,7 +79,29 @@ void detect_and_draw_objects(IplImage* image, faces = cvHaarDetectObjects(small_image, cascade, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING); - if(0 < faces->total) + update_idle(faces->total); + + /* draw all the rectangles */ + for(i = 0; i < faces->total; i++) + { + /* extract the rectangles only */ + CvRect face = *(CvRect*)cvGetSeqElem(faces, i); + CvPoint upperLeft = cvPoint(face.x * scale, face.y * scale); + CvPoint bottomRight = cvPoint((face.x + face.width) * scale, + (face.y + face.height) * scale); + cvRectangle(image, upperLeft, bottomRight, CV_RGB(255,0,0), 3); + } + + if(small_image != image) + cvReleaseImage(&small_image); + cvReleaseMemStorage(&storage); +} + +void update_idle(int faces_total) +{ + static int saw_last_time = 0; + + if(0 < faces_total) { t_current = time(NULL); if(verbose) @@ -104,20 +125,6 @@ void detect_and_draw_objects(IplImage* image, } } - /* draw all the rectangles */ - for(i = 0; i < faces->total; i++) - { - /* extract the rectangles only */ - CvRect face = *(CvRect*)cvGetSeqElem(faces, i); - CvPoint upperLeft = cvPoint(face.x * scale, face.y * scale); - CvPoint bottomRight = cvPoint((face.x + face.width) * scale, - (face.y + face.height) * scale); - cvRectangle(image, upperLeft, bottomRight, CV_RGB(255,0,0), 3); - } - - if(small_image != image) - cvReleaseImage(&small_image); - cvReleaseMemStorage(&storage); } void screensave(time_t t_current) @@ -195,42 +202,16 @@ int parse_opts(int argc, char **argv) return 0; } -// A Simple Camera Capture Framework. -int main(int argc, char** argv) +void get_frames(CvCapture* capture, CvHaarClassifierCascade* cascade) { - if(parse_opts(argc, argv)) return 0; - - CvHaarClassifierCascade* cascade = - load_object_detector(cascade_filename.c_str()); + IplImage* frame = NULL; + get_one_frame(capture, frame); - CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY); - if(!capture) { - cerr << "ERROR: capture is NULL " << endl; - getchar(); - return -1; - } - - // Create a window in which the captured images will be presented - cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE); - - IplImage* frame = cvQueryFrame(capture); - if(!frame) { - cerr << "ERROR: frame is null..." << endl; - getchar(); - return -1; - } IplImage* mirrored = cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels); - t_current = time(NULL); // Show the image captured from the camera in the window and repeat while(1) { - // Get one frame - frame = cvQueryFrame(capture); - if(!frame) { - cerr << "ERROR: frame is null..." << endl; - getchar(); - break; - } + if(get_one_frame(capture, frame)) break; //flip the image so displayed right/left corresponds to physical right/left cvFlip(frame, mirrored, MIRROR); @@ -244,10 +225,47 @@ int main(int argc, char** argv) //remove higher bits using AND operator if((cvWaitKey(100) & 255) == 27) break; } + cvReleaseImage(&mirrored); +} + +int do_capture() +{ + CvHaarClassifierCascade* cascade = + load_object_detector(cascade_filename.c_str()); + + CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY); + if(!capture) { + cerr << "ERROR: capture is NULL " << endl; + getchar(); + return -1; + } + + // Create a window in which the captured images will be presented + cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE); + t_current = time(NULL); + get_frames(capture, cascade); // Release the capture device housekeeping cvReleaseCapture(&capture); cvDestroyWindow("mywindow"); - cvReleaseImage(&mirrored); return 0; } + +int get_one_frame(CvCapture* capture, IplImage* &frame) +{ + frame = cvQueryFrame(capture); + if(!frame) { + cerr << "ERROR: frame is null..." << endl; + getchar(); + return 1; + } + return 0; +} + +// A Simple Camera Capture Framework. +int main(int argc, char** argv) +{ + if(parse_opts(argc, argv)) return 0; + + return do_capture(); +} diff --git a/oble.h b/oble.h index 3e65b6e..cb4cd24 100644 --- a/oble.h +++ b/oble.h @@ -49,3 +49,11 @@ void detect_and_draw_objects(IplImage* image, void screensave(time_t t_current); int parse_opts(int argc, char **argv); + +int do_capture(); + +int get_one_frame(CvCapture* capture, IplImage* &frame); + +void get_frames(CvCapture* capture, CvHaarClassifierCascade* cascade); + +void update_idle(int faces_total);