Amit Khandelwal
Amit Khandelwal

Reputation: 185

How to get similarity percentage in images using openCv?

I used openCV library to get the similarity percentage in images . I used compareHist function of openCv library which returns double value, there different method name (int value) are passed in this function and got different- different result for every Mehod .Now how to take decision on these double values????

Mat src_base, hsv_base;
Mat src_test1, hsv_test1;
//  Mat src_test2, hsv_test2;
Mat hsv_half_down;

 String baseImgPath = [baseImagePath UTF8String];
String firstCmpImgPath = [firstCmpImagePath UTF8String];//compare image path



src_base = imread( baseImgPath, 1 ); read source image
src_test1 = imread(firstCmpImgPath, 1 ); read compared image
//  src_test2 = imread(secondCmpImgPath, 1 );
 if( !src_base.data ||  !src_test1.data  /*||!src_test2.data*/)
{
 return nil;
 }

 cvtColor( src_base, hsv_base, COLOR_BGR2HSV );
cvtColor( src_test1, hsv_test1, COLOR_BGR2HSV );
//cvtColor( src_test2, hsv_test2, COLOR_BGR2HSV );

hsv_half_down = hsv_base( Range( hsv_base.rows/2, hsv_base.rows - 1 ), Range( 0,       hsv_base.cols - 1 ) );

/// Using 50 bins for hue and 60 for saturation
int h_bins = 50; int s_bins = 60;
int histSize[] = { h_bins, s_bins };

// hue varies from 0 to 179, saturation from 0 to 255
 float h_ranges[] = { 0, 180 };
 float s_ranges[] = { 0, 256 };

 const float* ranges[] = { h_ranges, s_ranges };

 // Use the o-th and 1-st channels
 int channels[] = { 0, 1 };


 /// Histograms
  MatND hist_base;
 MatND hist_half_down;
 MatND hist_test1;
 MatND hist_test2;

 /// 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_half_down, 1, channels, Mat(), hist_half_down, 2, histSize, ranges,  true, false );
 normalize( hist_half_down, hist_half_down, 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() );


 for( int i = 0; i < 4; i++ )
 {
  int compare_method = i;

 double base_test1 = compareHist( hist_base, hist_test1, compare_method );
 }

       compare method are CV_COMP_CORREL, CV_COMP_CHISQR , CV_COMP_INTERSECT ,  CV_COMP_BHATTACHARYYA 
  Reference link http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_comparison/histogram_comparison.html

enter image description here

enter image description here

enter image description here

enter image description here

Upvotes: 1

Views: 2499

Answers (1)

VAndrei
VAndrei

Reputation: 5580

For a bitmap it makes sense to define a similarity metric that computes the percentage of pixels in an image that are different from a target image.

However when you are using the histogram of a bitmap/image this metric looses sense because you already made a statistic on that image (or extracted a feature). From this point, to compute the similarity, you compare the features of the 2 images, in your case with compareHist.

A higher distance means a more different image and 0 distance could mean that the images are 100% identical. Now it depends if the algorithm can actually output 0. However a 0.5 distance does not mean that the images are identical 50%.

However you can artificially create a similarity degree measured in percentage. You can consider the following:

  • The 2 images with the lowest similarity degree between them (maximum distance) have 0% similarity; You can even compute this distance using one pure black image and one pure white image :)
  • Distance 0 is similarity 100%.

Based on these assumptions, you can extract the similarity measured in percentage, based on your computeHist distance.

Upvotes: 2

Related Questions