compareHist_Demo.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 | | Path to input image 1. }"
  15. "{ @input2 | | Path to input image 2. }"
  16. "{ @input3 | | 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. Mat src_base = imread( parser.get<String>("input1") );
  25. Mat src_test1 = imread( parser.get<String>("input2") );
  26. Mat src_test2 = imread( parser.get<String>("input3") );
  27. if( src_base.empty() || src_test1.empty() || src_test2.empty() )
  28. {
  29. cout << "Could not open or find the images!\n" << endl;
  30. parser.printMessage();
  31. return -1;
  32. }
  33. //! [Load three images with different environment settings]
  34. //! [Convert to HSV]
  35. Mat hsv_base, hsv_test1, hsv_test2;
  36. cvtColor( src_base, hsv_base, COLOR_BGR2HSV );
  37. cvtColor( src_test1, hsv_test1, COLOR_BGR2HSV );
  38. cvtColor( src_test2, hsv_test2, COLOR_BGR2HSV );
  39. //! [Convert to HSV]
  40. //! [Convert to HSV half]
  41. Mat hsv_half_down = hsv_base( Range( hsv_base.rows/2, hsv_base.rows ), Range( 0, hsv_base.cols ) );
  42. //! [Convert to HSV half]
  43. //! [Using 50 bins for hue and 60 for saturation]
  44. int h_bins = 50, s_bins = 60;
  45. int histSize[] = { h_bins, s_bins };
  46. // hue varies from 0 to 179, saturation from 0 to 255
  47. float h_ranges[] = { 0, 180 };
  48. float s_ranges[] = { 0, 256 };
  49. const float* ranges[] = { h_ranges, s_ranges };
  50. // Use the 0-th and 1-st channels
  51. int channels[] = { 0, 1 };
  52. //! [Using 50 bins for hue and 60 for saturation]
  53. //! [Calculate the histograms for the HSV images]
  54. Mat hist_base, hist_half_down, hist_test1, hist_test2;
  55. calcHist( &hsv_base, 1, channels, Mat(), hist_base, 2, histSize, ranges, true, false );
  56. normalize( hist_base, hist_base, 0, 1, NORM_MINMAX, -1, Mat() );
  57. calcHist( &hsv_half_down, 1, channels, Mat(), hist_half_down, 2, histSize, ranges, true, false );
  58. normalize( hist_half_down, hist_half_down, 0, 1, NORM_MINMAX, -1, Mat() );
  59. calcHist( &hsv_test1, 1, channels, Mat(), hist_test1, 2, histSize, ranges, true, false );
  60. normalize( hist_test1, hist_test1, 0, 1, NORM_MINMAX, -1, Mat() );
  61. calcHist( &hsv_test2, 1, channels, Mat(), hist_test2, 2, histSize, ranges, true, false );
  62. normalize( hist_test2, hist_test2, 0, 1, NORM_MINMAX, -1, Mat() );
  63. //! [Calculate the histograms for the HSV images]
  64. //! [Apply the histogram comparison methods]
  65. for( int compare_method = 0; compare_method < 4; compare_method++ )
  66. {
  67. double base_base = compareHist( hist_base, hist_base, compare_method );
  68. double base_half = compareHist( hist_base, hist_half_down, compare_method );
  69. double base_test1 = compareHist( hist_base, hist_test1, compare_method );
  70. double base_test2 = compareHist( hist_base, hist_test2, compare_method );
  71. cout << "Method " << compare_method << " Perfect, Base-Half, Base-Test(1), Base-Test(2) : "
  72. << base_base << " / " << base_half << " / " << base_test1 << " / " << base_test2 << endl;
  73. }
  74. //! [Apply the histogram comparison methods]
  75. cout << "Done \n";
  76. return 0;
  77. }