calcBackProject_Demo1.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 |Back_Projection_Theory0.jpg| input image}" );
  24. samples::addSamplesDataSearchSubDirectory("doc/tutorials/imgproc/histograms/back_projection/images");
  25. Mat src = imread(samples::findFile(parser.get<String>( "@input" )) );
  26. if( src.empty() )
  27. {
  28. cout << "Could not open or find the image!\n" << endl;
  29. cout << "Usage: " << argv[0] << " <Input image>" << endl;
  30. return -1;
  31. }
  32. //! [Read the image]
  33. //! [Transform it to HSV]
  34. Mat hsv;
  35. cvtColor( src, hsv, COLOR_BGR2HSV );
  36. //! [Transform it to HSV]
  37. //! [Use only the Hue value]
  38. hue.create(hsv.size(), hsv.depth());
  39. int ch[] = { 0, 0 };
  40. mixChannels( &hsv, 1, &hue, 1, ch, 1 );
  41. //! [Use only the Hue value]
  42. //! [Create Trackbar to enter the number of bins]
  43. const char* window_image = "Source image";
  44. namedWindow( window_image );
  45. createTrackbar("* Hue bins: ", window_image, &bins, 180, Hist_and_Backproj );
  46. Hist_and_Backproj(0, 0);
  47. //! [Create Trackbar to enter the number of bins]
  48. //! [Show the image]
  49. imshow( window_image, src );
  50. // Wait until user exits the program
  51. waitKey();
  52. //! [Show the image]
  53. return 0;
  54. }
  55. /**
  56. * @function Hist_and_Backproj
  57. * @brief Callback to Trackbar
  58. */
  59. void Hist_and_Backproj(int, void* )
  60. {
  61. //! [initialize]
  62. int histSize = MAX( bins, 2 );
  63. float hue_range[] = { 0, 180 };
  64. const float* ranges[] = { hue_range };
  65. //! [initialize]
  66. //! [Get the Histogram and normalize it]
  67. Mat hist;
  68. calcHist( &hue, 1, 0, Mat(), hist, 1, &histSize, ranges, true, false );
  69. normalize( hist, hist, 0, 255, NORM_MINMAX, -1, Mat() );
  70. //! [Get the Histogram and normalize it]
  71. //! [Get Backprojection]
  72. Mat backproj;
  73. calcBackProject( &hue, 1, 0, hist, backproj, ranges, 1, true );
  74. //! [Get Backprojection]
  75. //! [Draw the backproj]
  76. imshow( "BackProj", backproj );
  77. //! [Draw the backproj]
  78. //! [Draw the histogram]
  79. int w = 400, h = 400;
  80. int bin_w = cvRound( (double) w / histSize );
  81. Mat histImg = Mat::zeros( h, w, CV_8UC3 );
  82. for (int i = 0; i < bins; i++)
  83. {
  84. rectangle( histImg, Point( i*bin_w, h ), Point( (i+1)*bin_w, h - cvRound( hist.at<float>(i)*h/255.0 ) ),
  85. Scalar( 0, 0, 255 ), FILLED );
  86. }
  87. imshow( "Histogram", histImg );
  88. //! [Draw the histogram]
  89. }