83a078e5d04c588c02e5e4a14a37187222f1208f
[platform/upstream/opencv.git] / samples / c / latentsvmdetect.cpp
1 #include "opencv2/objdetect/objdetect_c.h"
2 #include "opencv2/highgui/highgui_c.h"
3 #include "opencv2/core/utility.hpp"
4 #include <stdio.h>
5
6 using namespace cv;
7
8 static void help()
9 {
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"
12             "Call:\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");
16 }
17
18 const char* model_filename = "cat.xml";
19 const char* image_filename = "cat.jpg";
20 int   tbbNumThreads = -1;
21
22 static void detect_and_draw_objects( IplImage* image, CvLatentSvmDetector* detector, int numThreads = -1)
23 {
24     CvMemStorage* storage = cvCreateMemStorage(0);
25     CvSeq* detections = 0;
26     int i = 0;
27     int64 start = 0, finish = 0;
28
29     setNumThreads(numThreads);
30     numThreads = getNumThreads();
31     printf("Number of threads %i\n", numThreads);
32
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));
37     setNumThreads(-1);
38
39     for( i = 0; i < detections->total; i++ )
40     {
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 );
48     }
49     cvReleaseMemStorage( &storage );
50 }
51
52 int main(int argc, char* argv[])
53 {
54     help();
55     if (argc > 2)
56     {
57         image_filename = argv[1];
58         model_filename = argv[2];
59         if (argc > 3)
60         {
61             tbbNumThreads = atoi(argv[3]);
62         }
63     }
64     IplImage* image = cvLoadImage(image_filename);
65     if (!image)
66     {
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" );
69         return -1;
70     }
71     CvLatentSvmDetector* detector = cvLoadLatentSvmDetector(model_filename);
72     if (!detector)
73     {
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 );
77         return -1;
78     }
79     detect_and_draw_objects( image, detector, tbbNumThreads );
80     cvNamedWindow( "test", 0 );
81     cvShowImage( "test", image );
82     cvWaitKey(0);
83     cvReleaseLatentSvmDetector( &detector );
84     cvReleaseImage( &image );
85     cvDestroyAllWindows();
86
87     return 0;
88 }