lot's of changes; nonfree & photo modules added; SIFT & SURF -> nonfree module; Inpai...
[profile/ivi/opencv.git] / samples / c / latentsvmdetect.cpp
1 #include "opencv2/objdetect/objdetect.hpp"\r
2 #include "opencv2/highgui/highgui.hpp"\r
3 #include <stdio.h>\r
4 \r
5 #ifdef HAVE_CVCONFIG_H \r
6 #include <cvconfig.h> \r
7 #endif\r
8 #ifdef HAVE_TBB\r
9 #include "tbb/task_scheduler_init.h"\r
10 #endif\r
11 \r
12 using namespace cv;\r
13 \r
14 void help()\r
15 {\r
16         printf( "This program demonstrated the use of the latentSVM detector.\n"\r
17                         "It reads in a trained object model and then uses that to detect the object in an image\n"\r
18                         "Call:\n"\r
19             "./latentsvmdetect [<image_filename> <model_filename> [<threads_number>]]\n"\r
20                         "  The defaults for image_filename and model_filename are cat.jpg and cat.xml respectively\n"\r
21                         "  Press any key to quit.\n");\r
22 }\r
23 \r
24 const char* model_filename = "cat.xml";\r
25 const char* image_filename = "cat.jpg";\r
26 int   tbbNumThreads = -1;\r
27 \r
28 void detect_and_draw_objects( IplImage* image, CvLatentSvmDetector* detector, int numThreads = -1)\r
29 {\r
30     CvMemStorage* storage = cvCreateMemStorage(0);\r
31     CvSeq* detections = 0;\r
32     int i = 0;\r
33         int64 start = 0, finish = 0;\r
34 #ifdef HAVE_TBB\r
35     tbb::task_scheduler_init init(tbb::task_scheduler_init::deferred);\r
36         if (numThreads > 0)\r
37         {\r
38                 init.initialize(numThreads);\r
39         printf("Number of threads %i\n", numThreads);\r
40         }\r
41         else\r
42         {\r
43                 printf("Number of threads is not correct for TBB version");\r
44                 return;\r
45         }\r
46 #endif\r
47 \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, char* argv[])\r
69 {\r
70         help();\r
71         if (argc > 2)\r
72         {\r
73                 image_filename = argv[1];\r
74                 model_filename = argv[2];\r
75         if (argc > 3)\r
76         {\r
77             tbbNumThreads = atoi(argv[3]);\r
78         }\r
79         }\r
80         IplImage* image = cvLoadImage(image_filename);\r
81         if (!image)\r
82         {\r
83                 printf( "Unable to load the image\n"\r
84                 "Pass it as the first parameter: latentsvmdetect <path to cat.jpg> <path to cat.xml>\n" );\r
85                 return -1;\r
86         }\r
87     CvLatentSvmDetector* detector = cvLoadLatentSvmDetector(model_filename);\r
88         if (!detector)\r
89         {\r
90                 printf( "Unable to load the model\n"\r
91                 "Pass it as the second parameter: latentsvmdetect <path to cat.jpg> <path to cat.xml>\n" );\r
92                 cvReleaseImage( &image );\r
93                 return -1;\r
94         }\r
95     detect_and_draw_objects( image, detector, tbbNumThreads );\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