PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/source/3rd_party/opencv/sources/samples/c/latentsvmdetect.cpp

https://gitlab.com/Ruggero/SparkEngine_Desktop
C++ | 83 lines | 75 code | 8 blank | 0 comment | 5 complexity | ff6410d38700869350cfe2fc2a78033a MD5 | raw file
  1. #include "opencv2/objdetect/objdetect.hpp"
  2. #include "opencv2/highgui/highgui.hpp"
  3. #include <stdio.h>
  4. using namespace cv;
  5. static void help()
  6. {
  7. printf( "This program demonstrated the use of the latentSVM detector.\n"
  8. "It reads in a trained object model and then uses that to detect the object in an image\n"
  9. "Call:\n"
  10. "./latentsvmdetect [<image_filename> <model_filename> [<threads_number>]]\n"
  11. " The defaults for image_filename and model_filename are cat.jpg and cat.xml respectively\n"
  12. " Press any key to quit.\n");
  13. }
  14. const char* model_filename = "cat.xml";
  15. const char* image_filename = "cat.jpg";
  16. int tbbNumThreads = -1;
  17. static void detect_and_draw_objects( IplImage* image, CvLatentSvmDetector* detector, int numThreads = -1)
  18. {
  19. CvMemStorage* storage = cvCreateMemStorage(0);
  20. CvSeq* detections = 0;
  21. int i = 0;
  22. int64 start = 0, finish = 0;
  23. cv::setNumThreads(numThreads);
  24. start = cvGetTickCount();
  25. detections = cvLatentSvmDetectObjects(image, detector, storage, 0.5f, numThreads);
  26. finish = cvGetTickCount();
  27. printf("detection time = %.3f\n", (float)(finish - start) / (float)(cvGetTickFrequency() * 1000000.0));
  28. for( i = 0; i < detections->total; i++ )
  29. {
  30. CvObjectDetection detection = *(CvObjectDetection*)cvGetSeqElem( detections, i );
  31. float score = detection.score;
  32. CvRect bounding_box = detection.rect;
  33. cvRectangle( image, cvPoint(bounding_box.x, bounding_box.y),
  34. cvPoint(bounding_box.x + bounding_box.width,
  35. bounding_box.y + bounding_box.height),
  36. CV_RGB(cvRound(255.0f*score),0,0), 3 );
  37. }
  38. cvReleaseMemStorage( &storage );
  39. }
  40. int main(int argc, char* argv[])
  41. {
  42. help();
  43. if (argc > 2)
  44. {
  45. image_filename = argv[1];
  46. model_filename = argv[2];
  47. if (argc > 3)
  48. {
  49. tbbNumThreads = atoi(argv[3]);
  50. }
  51. }
  52. IplImage* image = cvLoadImage(image_filename);
  53. if (!image)
  54. {
  55. printf( "Unable to load the image\n"
  56. "Pass it as the first parameter: latentsvmdetect <path to cat.jpg> <path to cat.xml>\n" );
  57. return -1;
  58. }
  59. CvLatentSvmDetector* detector = cvLoadLatentSvmDetector(model_filename);
  60. if (!detector)
  61. {
  62. printf( "Unable to load the model\n"
  63. "Pass it as the second parameter: latentsvmdetect <path to cat.jpg> <path to cat.xml>\n" );
  64. cvReleaseImage( &image );
  65. return -1;
  66. }
  67. detect_and_draw_objects( image, detector, tbbNumThreads );
  68. cvNamedWindow( "test", 0 );
  69. cvShowImage( "test", image );
  70. cvWaitKey(0);
  71. cvReleaseLatentSvmDetector( &detector );
  72. cvReleaseImage( &image );
  73. cvDestroyAllWindows();
  74. return 0;
  75. }