some samples updated according to new CommandLineParser class
[profile/ivi/opencv.git] / samples / c / latentsvmdetect.cpp
1 #include "opencv2/core/core.hpp"\r
2 #include "opencv2/objdetect/objdetect.hpp"\r
3 #include "opencv2/highgui/highgui.hpp"\r
4 \r
5 #include <stdio.h>\r
6 \r
7 #ifdef HAVE_CONFIG_H \r
8 #include <cvconfig.h> \r
9 #endif\r
10 #ifdef HAVE_TBB\r
11 #include "tbb/task_scheduler_init.h"\r
12 #endif\r
13 \r
14 using namespace cv;\r
15 \r
16 void help()\r
17 {\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
20             "Call:\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
26 }\r
27 \r
28 \r
29 void detect_and_draw_objects( IplImage* image, CvLatentSvmDetector* detector, int numThreads = -1)\r
30 {\r
31     CvMemStorage* storage = cvCreateMemStorage(0);\r
32     CvSeq* detections = 0;\r
33     int i = 0;\r
34     int64 start = 0, finish = 0;\r
35 #ifdef HAVE_TBB\r
36     tbb::task_scheduler_init init(tbb::task_scheduler_init::deferred);\r
37     if (numThreads > 0)\r
38     {\r
39         init.initialize(numThreads);\r
40         printf("Number of threads %i\n", numThreads);\r
41     }\r
42     else\r
43     {\r
44         printf("Number of threads is not correct for TBB version");\r
45         return;\r
46     }\r
47 #endif\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
52 \r
53 #ifdef HAVE_TBB\r
54     init.terminate();\r
55 #endif\r
56     for( i = 0; i < detections->total; i++ )\r
57     {\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
64     }\r
65     cvReleaseMemStorage( &storage );\r
66 }\r
67 \r
68 int main(int argc, const char* argv[])\r
69 {\r
70     help();\r
71 \r
72     CommandLineParser parser(argc, argv);\r
73 \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
77 \r
78     IplImage* image = cvLoadImage(imageFileName.c_str());\r
79     if (!image)\r
80     {\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
83         return -1;\r
84     }\r
85     CvLatentSvmDetector* detector = cvLoadLatentSvmDetector(modelFileName.c_str());\r
86     if (!detector)\r
87     {\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
91         return -1;\r
92     }\r
93 \r
94     detect_and_draw_objects( image, detector, tbbNumThreads );\r
95 \r
96     cvNamedWindow( "test", 0 );\r
97     cvShowImage( "test", image );\r
98     cvWaitKey(0);\r
99     cvReleaseLatentSvmDetector( &detector );\r
100     cvReleaseImage( &image );\r
101     cvDestroyAllWindows();\r
102     \r
103     return 0;\r
104 }\r