calcBackProject_Demo1.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @file BackProject_Demo1.cpp
  3. * @brief Sample code for backproject function usage
  4. * @author OpenCV team
  5. */
  6. #include "opencv2/imgproc.hpp"
  7. #include "opencv2/imgcodecs.hpp"
  8. #include "opencv2/highgui.hpp"
  9. #include <iostream>
  10. using namespace cv;
  11. using namespace std;
  12. /// Global Variables
  13. Mat hue;
  14. int bins = 25;
  15. /// Function Headers
  16. void Hist_and_Backproj(int, void* );
  17. /**
  18. * @function main
  19. */
  20. int main( int argc, char* argv[] )
  21. {
  22. //! [Read the image]
  23. CommandLineParser parser( argc, argv, "{@input | | input image}" );
  24. Mat src = imread( parser.get<String>( "@input" ) );
  25. if( src.empty() )
  26. {
  27. cout << "Could not open or find the image!\n" << endl;
  28. cout << "Usage: " << argv[0] << " <Input image>" << endl;
  29. return -1;
  30. }
  31. //! [Read the image]
  32. //! [Transform it to HSV]
  33. Mat hsv;
  34. cvtColor( src, hsv, COLOR_BGR2HSV );
  35. //! [Transform it to HSV]
  36. //! [Use only the Hue value]
  37. hue.create(hsv.size(), hsv.depth());
  38. int ch[] = { 0, 0 };
  39. mixChannels( &hsv, 1, &hue, 1, ch, 1 );
  40. //! [Use only the Hue value]
  41. //! [Create Trackbar to enter the number of bins]
  42. const char* window_image = "Source image";
  43. namedWindow( window_image );
  44. createTrackbar("* Hue bins: ", window_image, &bins, 180, Hist_and_Backproj );
  45. Hist_and_Backproj(0, 0);
  46. //! [Create Trackbar to enter the number of bins]
  47. //! [Show the image]
  48. imshow( window_image, src );
  49. // Wait until user exits the program
  50. waitKey();
  51. //! [Show the image]
  52. return 0;
  53. }
  54. /**
  55. * @function Hist_and_Backproj
  56. * @brief Callback to Trackbar
  57. */
  58. void Hist_and_Backproj(int, void* )
  59. {
  60. //! [initialize]
  61. int histSize = MAX( bins, 2 );
  62. float hue_range[] = { 0, 180 };
  63. const float* ranges[] = { hue_range };
  64. //! [initialize]
  65. //! [Get the Histogram and normalize it]
  66. Mat hist;
  67. calcHist( &hue, 1, 0, Mat(), hist, 1, &histSize, ranges, true, false );
  68. normalize( hist, hist, 0, 255, NORM_MINMAX, -1, Mat() );
  69. //! [Get the Histogram and normalize it]
  70. //! [Get Backprojection]
  71. Mat backproj;
  72. calcBackProject( &hue, 1, 0, hist, backproj, ranges, 1, true );
  73. //! [Get Backprojection]
  74. //! [Draw the backproj]
  75. imshow( "BackProj", backproj );
  76. //! [Draw the backproj]
  77. //! [Draw the histogram]
  78. int w = 400, h = 400;
  79. int bin_w = cvRound( (double) w / histSize );
  80. Mat histImg = Mat::zeros( h, w, CV_8UC3 );
  81. for (int i = 0; i < bins; i++)
  82. {
  83. rectangle( histImg, Point( i*bin_w, h ), Point( (i+1)*bin_w, h - cvRound( hist.at<float>(i)*h/255.0 ) ),
  84. Scalar( 0, 0, 255 ), FILLED );
  85. }
  86. imshow( "Histogram", histImg );
  87. //! [Draw the histogram]
  88. }