calcHist_Demo.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @function calcHist_Demo.cpp
  3. * @brief Demo code to use the function calcHist
  4. * @author
  5. */
  6. #include "opencv2/highgui.hpp"
  7. #include "opencv2/imgcodecs.hpp"
  8. #include "opencv2/imgproc.hpp"
  9. #include <iostream>
  10. using namespace std;
  11. using namespace cv;
  12. /**
  13. * @function main
  14. */
  15. int main(int argc, char** argv)
  16. {
  17. //! [Load image]
  18. CommandLineParser parser( argc, argv, "{@input | lena.jpg | input image}" );
  19. Mat src = imread( samples::findFile( parser.get<String>( "@input" ) ), IMREAD_COLOR );
  20. if( src.empty() )
  21. {
  22. return EXIT_FAILURE;
  23. }
  24. //! [Load image]
  25. //! [Separate the image in 3 places ( B, G and R )]
  26. vector<Mat> bgr_planes;
  27. split( src, bgr_planes );
  28. //! [Separate the image in 3 places ( B, G and R )]
  29. //! [Establish the number of bins]
  30. int histSize = 256;
  31. //! [Establish the number of bins]
  32. //! [Set the ranges ( for B,G,R) )]
  33. float range[] = { 0, 256 }; //the upper boundary is exclusive
  34. const float* histRange[] = { range };
  35. //! [Set the ranges ( for B,G,R) )]
  36. //! [Set histogram param]
  37. bool uniform = true, accumulate = false;
  38. //! [Set histogram param]
  39. //! [Compute the histograms]
  40. Mat b_hist, g_hist, r_hist;
  41. calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, histRange, uniform, accumulate );
  42. calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, histRange, uniform, accumulate );
  43. calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, histRange, uniform, accumulate );
  44. //! [Compute the histograms]
  45. //! [Draw the histograms for B, G and R]
  46. int hist_w = 512, hist_h = 400;
  47. int bin_w = cvRound( (double) hist_w/histSize );
  48. Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
  49. //! [Draw the histograms for B, G and R]
  50. //! [Normalize the result to ( 0, histImage.rows )]
  51. normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
  52. normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
  53. normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
  54. //! [Normalize the result to ( 0, histImage.rows )]
  55. //! [Draw for each channel]
  56. for( int i = 1; i < histSize; i++ )
  57. {
  58. line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ),
  59. Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
  60. Scalar( 255, 0, 0), 2, 8, 0 );
  61. line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ),
  62. Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
  63. Scalar( 0, 255, 0), 2, 8, 0 );
  64. line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ),
  65. Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
  66. Scalar( 0, 0, 255), 2, 8, 0 );
  67. }
  68. //! [Draw for each channel]
  69. //! [Display]
  70. imshow("Source image", src );
  71. imshow("calcHist Demo", histImage );
  72. waitKey();
  73. //! [Display]
  74. return EXIT_SUCCESS;
  75. }