break down big functions. Closes #12
authorozzloy <ozzloy@zero7.(none)>
Tue, 9 Mar 2010 09:13:03 +0000 (01:13 -0800)
committerozzloy <ozzloy@zero7.(none)>
Tue, 9 Mar 2010 09:13:03 +0000 (01:13 -0800)
oble.cpp
oble.h

index 5de3abe301275852c999d03c00c15458c108a1a6..3f351ae574eb9af94e5ba1cdc37be0d08d1e586f 100644 (file)
--- 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 3e65b6e1a1b569cd4529408cf6fb0a99c1ca5b90..cb4cd243ef30265c9241d8c53265671a49ba5e36 100644 (file)
--- 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);