1

I have been trying to find matched image from sample image using histogram matching. for most of the cases my code is working fine. The range of used method, Bhattacharyya, is 0 <= method <= 1. normally using Bhattacharyya method the output result will close to 0, in case of matched cases. but i have come to a case where both images are almost similar, though there could be some contrast difference. which is why this procedure is giving higher result...

can anyone help me why this comparison is giving so much bigger value?

src image and test image

int main(){
    src_base = imread("images/src.jpg",-1);
    src_test1 = imread("images/test.png",-1);
    double base_test1 = hsvToHist(src_base, src_test1,3);
    cout<< " Bhattacharyya template  Base-Test(1) : "<< base_test1<<endl;

    return 0;  
}


double hsvToHist( Mat src_base, Mat  src_test1, int method){

    Mat hsv_base, hsv_test1;
    cvtColor( src_base, hsv_base, COLOR_BGR2HSV );
    cvtColor( src_test1, hsv_test1, COLOR_BGR2HSV );

    /// initialization to calculate histograms (Using 50 bins for hue, 60 for saturation)
    int h_bins = 50; int s_bins = 60;
    int histSize[] = { h_bins, s_bins };
    float h_ranges[] = { 0, 180 };
    float s_ranges[] = { 0, 256 };
    const float* ranges[] = { h_ranges, s_ranges };
    int channels[] = { 0, 1 };

    /// Histograms
    Mat hist_base, hist_test1;

    /// Calculate the histograms for the HSV images
    calcHist( &hsv_base, 1, channels, Mat(), hist_base, 2, histSize, ranges, true, false );
    normalize( hist_base, hist_base, 0, 1, NORM_MINMAX, -1, Mat() );


    calcHist( &hsv_test1, 1, channels, Mat(), hist_test1, 2, histSize, ranges, true, false );
    normalize( hist_test1, hist_test1, 0, 1, NORM_MINMAX, -1, Mat() );

    ///'3' for Bhattacharyya
    double base_test1 = compareHist( hist_base, hist_test1, method );
    return base_test1;
}
reza5630
  • 21
  • 4

2 Answers2

0

The PNG and JPEG images will have different histograms even though they appear the same, because the JPEG is compressed which means information has been removed and the histogram has been essentially filtered and smoothed. Also, the PNG will have a larger range of values than the JPEG. You may get better results with different bin sizes, but it's hard to tell without testing.

noel
  • 2,257
  • 3
  • 24
  • 39
  • i have already tried that...but though i have taken samples of same format, still the value is high (0.64)... obviously using different format results in much higher result (0.84). – reza5630 Oct 04 '17 at 11:48
0

The Bhattacharyya distance has an N^2 term in the denominator where N is the number of pixels. In general, this allows similar values for different sizes of images. However, for the icons that you are comparing, the divisor is much smaller. You could scale the metric by a factor related to the image size.

Alternately, you could use the HISTCMP_CORREL method, which produces lower absolute values if the differences between pixels are less significant. This method produces larger values if more pixels are compared.

When you want similar results independent of differences in image size you could compute both metrics and consider the images equal if one of them passes a tight threshold for similarity. Actual thresholds will vary depending on whether you are comparing color or grayscale images, and whether you have pre-processed the images using histogram equalization (see cv::equalizeHist).

ashok3t
  • 41
  • 5