1 #include "opencv2/objdetect/objdetect_c.h"
2 #include "opencv2/highgui/highgui_c.h"
3 #include "opencv2/core/utility.hpp"
10 printf( "This program demonstrated the use of the latentSVM detector.\n"
11 "It reads in a trained object model and then uses that to detect the object in an image\n"
13 "./latentsvmdetect [<image_filename> <model_filename> [<threads_number>]]\n"
14 " The defaults for image_filename and model_filename are cat.jpg and cat.xml respectively\n"
15 " Press any key to quit.\n");
18 const char* model_filename = "cat.xml";
19 const char* image_filename = "cat.jpg";
20 int tbbNumThreads = -1;
22 static void detect_and_draw_objects( IplImage* image, CvLatentSvmDetector* detector, int numThreads = -1)
24 CvMemStorage* storage = cvCreateMemStorage(0);
25 CvSeq* detections = 0;
27 int64 start = 0, finish = 0;
29 setNumThreads(numThreads);
30 numThreads = getNumThreads();
31 printf("Number of threads %i\n", numThreads);
33 start = cvGetTickCount();
34 detections = cvLatentSvmDetectObjects(image, detector, storage, 0.5f, numThreads);
35 finish = cvGetTickCount();
36 printf("detection time = %.3f\n", (float)(finish - start) / (float)(cvGetTickFrequency() * 1000000.0));
39 for( i = 0; i < detections->total; i++ )
41 CvObjectDetection detection = *(CvObjectDetection*)cvGetSeqElem( detections, i );
42 float score = detection.score;
43 CvRect bounding_box = detection.rect;
44 cvRectangle( image, cvPoint(bounding_box.x, bounding_box.y),
45 cvPoint(bounding_box.x + bounding_box.width,
46 bounding_box.y + bounding_box.height),
47 CV_RGB(cvRound(255.0f*score),0,0), 3 );
49 cvReleaseMemStorage( &storage );
52 int main(int argc, char* argv[])
57 image_filename = argv[1];
58 model_filename = argv[2];
61 tbbNumThreads = atoi(argv[3]);
64 IplImage* image = cvLoadImage(image_filename);
67 printf( "Unable to load the image\n"
68 "Pass it as the first parameter: latentsvmdetect <path to cat.jpg> <path to cat.xml>\n" );
71 CvLatentSvmDetector* detector = cvLoadLatentSvmDetector(model_filename);
74 printf( "Unable to load the model\n"
75 "Pass it as the second parameter: latentsvmdetect <path to cat.jpg> <path to cat.xml>\n" );
76 cvReleaseImage( &image );
79 detect_and_draw_objects( image, detector, tbbNumThreads );
80 cvNamedWindow( "test", 0 );
81 cvShowImage( "test", image );
83 cvReleaseLatentSvmDetector( &detector );
84 cvReleaseImage( &image );
85 cvDestroyAllWindows();