compareHist_Demo.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @file compareHist_Demo.cpp
  3. * @brief Sample code to use the function compareHist
  4. * @author OpenCV team
  5. */
  6. #include "opencv2/imgcodecs.hpp"
  7. #include "opencv2/highgui.hpp"
  8. #include "opencv2/imgproc.hpp"
  9. #include <iostream>
  10. using namespace std;
  11. using namespace cv;
  12. const char* keys =
  13. "{ help h| | Print help message. }"
  14. "{ @input1 |Histogram_Comparison_Source_0.jpg | Path to input image 1. }"
  15. "{ @input2 |Histogram_Comparison_Source_1.jpg | Path to input image 2. }"
  16. "{ @input3 |Histogram_Comparison_Source_2.jpg | Path to input image 3. }";
  17. /**
  18. * @function main
  19. */
  20. int main( int argc, char** argv )
  21. {
  22. //! [Load three images with different environment settings]
  23. CommandLineParser parser( argc, argv, keys );
  24. samples::addSamplesDataSearchSubDirectory( "doc/tutorials/imgproc/histograms/histogram_comparison/images" );
  25. Mat src_base = imread(samples::findFile( parser.get<String>( "@input1" ) ) );
  26. Mat src_test1 = imread(samples::findFile( parser.get<String>( "@input2" ) ) );
  27. Mat src_test2 = imread(samples::findFile( parser.get<String>( "@input3" ) ) );
  28. if( src_base.empty() || src_test1.empty() || src_test2.empty() )
  29. {
  30. cout << "Could not open or find the images!\n" << endl;
  31. parser.printMessage();
  32. return -1;
  33. }
  34. //! [Load three images with different environment settings]
  35. //! [Convert to HSV]
  36. Mat hsv_base, hsv_test1, hsv_test2;
  37. cvtColor( src_base, hsv_base, COLOR_BGR2HSV );
  38. cvtColor( src_test1, hsv_test1, COLOR_BGR2HSV );
  39. cvtColor( src_test2, hsv_test2, COLOR_BGR2HSV );
  40. //! [Convert to HSV]
  41. //! [Convert to HSV half]
  42. Mat hsv_half_down = hsv_base( Range( hsv_base.rows/2, hsv_base.rows ), Range( 0, hsv_base.cols ) );
  43. //! [Convert to HSV half]
  44. //! [Using 50 bins for hue and 60 for saturation]
  45. int h_bins = 50, s_bins = 60;
  46. int histSize[] = { h_bins, s_bins };
  47. // hue varies from 0 to 179, saturation from 0 to 255
  48. float h_ranges[] = { 0, 180 };
  49. float s_ranges[] = { 0, 256 };
  50. const float* ranges[] = { h_ranges, s_ranges };
  51. // Use the 0-th and 1-st channels
  52. int channels[] = { 0, 1 };
  53. //! [Using 50 bins for hue and 60 for saturation]
  54. //! [Calculate the histograms for the HSV images]
  55. Mat hist_base, hist_half_down, hist_test1, hist_test2;
  56. calcHist( &hsv_base, 1, channels, Mat(), hist_base, 2, histSize, ranges, true, false );
  57. normalize( hist_base, hist_base, 0, 1, NORM_MINMAX, -1, Mat() );
  58. calcHist( &hsv_half_down, 1, channels, Mat(), hist_half_down, 2, histSize, ranges, true, false );
  59. normalize( hist_half_down, hist_half_down, 0, 1, NORM_MINMAX, -1, Mat() );
  60. calcHist( &hsv_test1, 1, channels, Mat(), hist_test1, 2, histSize, ranges, true, false );
  61. normalize( hist_test1, hist_test1, 0, 1, NORM_MINMAX, -1, Mat() );
  62. calcHist( &hsv_test2, 1, channels, Mat(), hist_test2, 2, histSize, ranges, true, false );
  63. normalize( hist_test2, hist_test2, 0, 1, NORM_MINMAX, -1, Mat() );
  64. //! [Calculate the histograms for the HSV images]
  65. //! [Apply the histogram comparison methods]
  66. for( int compare_method = 0; compare_method < 4; compare_method++ )
  67. {
  68. double base_base = compareHist( hist_base, hist_base, compare_method );
  69. double base_half = compareHist( hist_base, hist_half_down, compare_method );
  70. double base_test1 = compareHist( hist_base, hist_test1, compare_method );
  71. double base_test2 = compareHist( hist_base, hist_test2, compare_method );
  72. cout << "Method " << compare_method << " Perfect, Base-Half, Base-Test(1), Base-Test(2) : "
  73. << base_base << " / " << base_half << " / " << base_test1 << " / " << base_test2 << endl;
  74. }
  75. //! [Apply the histogram comparison methods]
  76. cout << "Done \n";
  77. return 0;
  78. }