1 #include "opencv2/core/core.hpp"
\r
2 #include "opencv2/objdetect/objdetect.hpp"
\r
3 #include "opencv2/highgui/highgui.hpp"
\r
7 #ifdef HAVE_CONFIG_H
\r
8 #include <cvconfig.h>
\r
11 #include "tbb/task_scheduler_init.h"
\r
18 printf( "This program demonstrated the use of the latentSVM detector.\n"
\r
19 "It reads in a trained object model and then uses that to detect the object in an image\n"
\r
21 "./latentsvmdetect [--image_filename]=<image_filename, cat.jpg as default> \n"
\r
22 " [--model_filename] = <model_filename, cat.xml as default> \n"
\r
23 " [--threads_number] = <number of threads, -1 as default>\n"
\r
24 " The defaults for image_filename and model_filename are cat.jpg and cat.xml respectively\n"
\r
25 " Press any key to quit.\n");
\r
29 void detect_and_draw_objects( IplImage* image, CvLatentSvmDetector* detector, int numThreads = -1)
\r
31 CvMemStorage* storage = cvCreateMemStorage(0);
\r
32 CvSeq* detections = 0;
\r
34 int64 start = 0, finish = 0;
\r
36 tbb::task_scheduler_init init(tbb::task_scheduler_init::deferred);
\r
39 init.initialize(numThreads);
\r
40 printf("Number of threads %i\n", numThreads);
\r
44 printf("Number of threads is not correct for TBB version");
\r
48 start = cvGetTickCount();
\r
49 detections = cvLatentSvmDetectObjects(image, detector, storage, 0.5f, numThreads);
\r
50 finish = cvGetTickCount();
\r
51 printf("detection time = %.3f\n", (float)(finish - start) / (float)(cvGetTickFrequency() * 1000000.0));
\r
56 for( i = 0; i < detections->total; i++ )
\r
58 CvObjectDetection detection = *(CvObjectDetection*)cvGetSeqElem( detections, i );
\r
59 CvRect bounding_box = detection.rect;
\r
60 cvRectangle( image, cvPoint(bounding_box.x, bounding_box.y),
\r
61 cvPoint(bounding_box.x + bounding_box.width,
\r
62 bounding_box.y + bounding_box.height),
\r
63 CV_RGB(255,0,0), 3 );
\r
65 cvReleaseMemStorage( &storage );
\r
68 int main(int argc, const char* argv[])
\r
72 CommandLineParser parser(argc, argv);
\r
74 string imageFileName = parser.get<string>("image_filename", "cat.jpg");
\r
75 string modelFileName = parser.get<string>("model_filename", "cat.xml");
\r
76 int tbbNumThreads = parser.get<int>("threads_number", -1);
\r
78 IplImage* image = cvLoadImage(imageFileName.c_str());
\r
81 printf( "Unable to load the image\n"
\r
82 "Pass it as the first parameter: latentsvmdetect <path to cat.jpg> <path to cat.xml>\n" );
\r
85 CvLatentSvmDetector* detector = cvLoadLatentSvmDetector(modelFileName.c_str());
\r
88 printf( "Unable to load the model\n"
\r
89 "Pass it as the second parameter: latentsvmdetect <path to cat.jpg> <path to cat.xml>\n" );
\r
90 cvReleaseImage( &image );
\r
94 detect_and_draw_objects( image, detector, tbbNumThreads );
\r
96 cvNamedWindow( "test", 0 );
\r
97 cvShowImage( "test", image );
\r
99 cvReleaseLatentSvmDetector( &detector );
\r
100 cvReleaseImage( &image );
\r
101 cvDestroyAllWindows();
\r