/opencv/samples/c/latentsvmdetect.cpp

https://github.com/malcolmreynolds/OpenCV · C++ · 104 lines · 95 code · 9 blank · 0 comment · 6 complexity · 432f62974a245c502074647b929f6263 MD5 · raw file

  1. #include "opencv2/objdetect/objdetect.hpp"
  2. #include "opencv2/highgui/highgui.hpp"
  3. #include <stdio.h>
  4. #ifdef HAVE_CVCONFIG_H
  5. #include <cvconfig.h>
  6. #endif
  7. #ifdef HAVE_TBB
  8. #include "tbb/task_scheduler_init.h"
  9. #endif
  10. using namespace cv;
  11. void help()
  12. {
  13. printf( "This program demonstrated the use of the latentSVM detector.\n"
  14. "It reads in a trained object model and then uses that to detect the object in an image\n"
  15. "Call:\n"
  16. "./latentsvmdetect [<image_filename> <model_filename> [<threads_number>]]\n"
  17. " The defaults for image_filename and model_filename are cat.jpg and cat.xml respectively\n"
  18. " Press any key to quit.\n");
  19. }
  20. const char* model_filename = "cat.xml";
  21. const char* image_filename = "cat.jpg";
  22. int tbbNumThreads = -1;
  23. void detect_and_draw_objects( IplImage* image, CvLatentSvmDetector* detector, int numThreads = -1)
  24. {
  25. CvMemStorage* storage = cvCreateMemStorage(0);
  26. CvSeq* detections = 0;
  27. int i = 0;
  28. int64 start = 0, finish = 0;
  29. #ifdef HAVE_TBB
  30. tbb::task_scheduler_init init(tbb::task_scheduler_init::deferred);
  31. if (numThreads > 0)
  32. {
  33. init.initialize(numThreads);
  34. printf("Number of threads %i\n", numThreads);
  35. }
  36. else
  37. {
  38. printf("Number of threads is not correct for TBB version");
  39. return;
  40. }
  41. #endif
  42. start = cvGetTickCount();
  43. detections = cvLatentSvmDetectObjects(image, detector, storage, 0.5f, numThreads);
  44. finish = cvGetTickCount();
  45. printf("detection time = %.3f\n", (float)(finish - start) / (float)(cvGetTickFrequency() * 1000000.0));
  46. #ifdef HAVE_TBB
  47. init.terminate();
  48. #endif
  49. for( i = 0; i < detections->total; i++ )
  50. {
  51. CvObjectDetection detection = *(CvObjectDetection*)cvGetSeqElem( detections, i );
  52. CvRect bounding_box = detection.rect;
  53. cvRectangle( image, cvPoint(bounding_box.x, bounding_box.y),
  54. cvPoint(bounding_box.x + bounding_box.width,
  55. bounding_box.y + bounding_box.height),
  56. CV_RGB(255,0,0), 3 );
  57. }
  58. cvReleaseMemStorage( &storage );
  59. }
  60. int main(int argc, char* argv[])
  61. {
  62. help();
  63. if (argc > 2)
  64. {
  65. image_filename = argv[1];
  66. model_filename = argv[2];
  67. if (argc > 3)
  68. {
  69. tbbNumThreads = atoi(argv[3]);
  70. }
  71. }
  72. IplImage* image = cvLoadImage(image_filename);
  73. if (!image)
  74. {
  75. printf( "Unable to load the image\n"
  76. "Pass it as the first parameter: latentsvmdetect <path to cat.jpg> <path to cat.xml>\n" );
  77. return -1;
  78. }
  79. CvLatentSvmDetector* detector = cvLoadLatentSvmDetector(model_filename);
  80. if (!detector)
  81. {
  82. printf( "Unable to load the model\n"
  83. "Pass it as the second parameter: latentsvmdetect <path to cat.jpg> <path to cat.xml>\n" );
  84. cvReleaseImage( &image );
  85. return -1;
  86. }
  87. detect_and_draw_objects( image, detector, tbbNumThreads );
  88. cvNamedWindow( "test", 0 );
  89. cvShowImage( "test", image );
  90. cvWaitKey(0);
  91. cvReleaseLatentSvmDetector( &detector );
  92. cvReleaseImage( &image );
  93. cvDestroyAllWindows();
  94. return 0;
  95. }