Merge remote-tracking branch 'origin/2.4' into merge-2.4
[profile/ivi/opencv.git] / samples / cpp / tutorial_code / Histograms_Matching / compareHist_Demo.cpp
1 /**
2  * @file compareHist_Demo.cpp
3  * @brief Sample code to use the function compareHist
4  * @author OpenCV team
5  */
6
7 #include "opencv2/highgui/highgui.hpp"
8 #include "opencv2/imgproc/imgproc.hpp"
9 #include <iostream>
10 #include <stdio.h>
11
12 using namespace std;
13 using namespace cv;
14
15 /**
16  * @function main
17  */
18 int main( int argc, char** argv )
19 {
20     Mat src_base, hsv_base;
21     Mat src_test1, hsv_test1;
22     Mat src_test2, hsv_test2;
23     Mat hsv_half_down;
24
25     /// Load three images with different environment settings
26     if( argc < 4 )
27     {
28         printf("** Error. Usage: ./compareHist_Demo <image_settings0> <image_setting1> <image_settings2>\n");
29         return -1;
30     }
31
32     src_base = imread( argv[1], 1 );
33     src_test1 = imread( argv[2], 1 );
34     src_test2 = imread( argv[3], 1 );
35
36     /// Convert to HSV
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
41     hsv_half_down = hsv_base( Range( hsv_base.rows/2, hsv_base.rows - 1 ), Range( 0, hsv_base.cols - 1 ) );
42
43     /// Using 30 bins for hue and 32 for saturation
44     int h_bins = 50; int s_bins = 60;
45     int histSize[] = { h_bins, s_bins };
46
47     // hue varies from 0 to 256, saturation from 0 to 180
48     float s_ranges[] = { 0, 256 };
49     float h_ranges[] = { 0, 180 };
50
51     const float* ranges[] = { h_ranges, s_ranges };
52
53     // Use the o-th and 1-st channels
54     int channels[] = { 0, 1 };
55
56
57     /// Histograms
58     MatND hist_base;
59     MatND hist_half_down;
60     MatND hist_test1;
61     MatND hist_test2;
62
63     /// Calculate the histograms for the HSV images
64     calcHist( &hsv_base, 1, channels, Mat(), hist_base, 2, histSize, ranges, true, false );
65     normalize( hist_base, hist_base, 0, 1, NORM_MINMAX, -1, Mat() );
66
67     calcHist( &hsv_half_down, 1, channels, Mat(), hist_half_down, 2, histSize, ranges, true, false );
68     normalize( hist_half_down, hist_half_down, 0, 1, NORM_MINMAX, -1, Mat() );
69
70     calcHist( &hsv_test1, 1, channels, Mat(), hist_test1, 2, histSize, ranges, true, false );
71     normalize( hist_test1, hist_test1, 0, 1, NORM_MINMAX, -1, Mat() );
72
73     calcHist( &hsv_test2, 1, channels, Mat(), hist_test2, 2, histSize, ranges, true, false );
74     normalize( hist_test2, hist_test2, 0, 1, NORM_MINMAX, -1, Mat() );
75
76     /// Apply the histogram comparison methods
77     for( int i = 0; i < 4; i++ )
78     {
79         int compare_method = i;
80         double base_base = compareHist( hist_base, hist_base, compare_method );
81         double base_half = compareHist( hist_base, hist_half_down, compare_method );
82         double base_test1 = compareHist( hist_base, hist_test1, compare_method );
83         double base_test2 = compareHist( hist_base, hist_test2, compare_method );
84
85         printf( " Method [%d] Perfect, Base-Half, Base-Test(1), Base-Test(2) : %f, %f, %f, %f \n", i, base_base, base_half , base_test1, base_test2 );
86     }
87
88     printf( "Done \n" );
89
90     return 0;
91 }