Ask Your Question

How to calculate Euclidean Distance d(h,g)

asked 2016-12-18 04:03:59 -0600

azdoud.y gravatar image

updated 2016-12-18 04:05:18 -0600

Dear community,

I’d like to apply this equation of euclidean distance between two histogram for every Channel A, B and C

image description

any help i'd be glad

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2016-12-18 04:59:02 -0600

berak gravatar image

updated 2016-12-18 05:24:33 -0600

above formula would be cv::norm(g,h, NORM_L2SQR).

still note, that to compare histograms, you'd rather use a CHI_SQR or HELLINGER distance instead of L2, that's why there is compareHist()

edit flag offensive delete link more


thank you @berak as always in time

azdoud.y gravatar imageazdoud.y ( 2016-12-18 05:49:17 -0600 )edit

answered 2016-12-19 05:14:21 -0600

KirtiSwagat gravatar image
#include <opencv2/highgui/highgui.hpp>

#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>

#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
Mat Image1= imread("first.jpg",1);// 1 for default color space assume here B,G, R color space
Mat Image2=imread("second.jpg",1);
vector<Mat> planes_for_first_image;//Vector of Matrix created to hold the channels present in the input image
vector<Mat> planes_for_second_image;
split(Image1, planes_for_first_image);//Channels are separated here and stored in the vector of Matrix.
split(Image2, planes_for_second_image);
int histSize= 256;
int histHeight= 400;
int histWidth= 512;
int binWidth= cvRound((double)histWidth/histHeight);
Mat histImage= (histHeight, histWidth, CV_8UC3, scalar(0,0,0));
float range[]={0, 256};
const float* histRange= { range };
bool uniform= true;
bool accumulate= false;
Mat BlueHist1; Mat BlueHist2; // Matrix Declared to hold plane wise histogram
Mat GreenHist1; Mat GreenHist2;
Mat RedHist1; Mat RedHist2;
calHist(&planes_for_first_image)[0],1,0, Mat(), BlueHist1,1, &histSize, &histRange, uniform, accumulate);//histogram calculation for first image:- Blue Plane
calHist(&planes_for_first_image)[1],1,0, Mat(), GreenHist1,1, &histSize, &histRange, uniform, accumulate);//histogram calculation for first image:- Green Plane
calHist(&planes_for_first_image)[2],1,0, Mat(), RedHist1,1, &histSize, &histRange, uniform, accumulate);//histogram calculation for first image:- Red Plane

calHist(&planes_for_second_image)[0],1,0, Mat(), BlueHist2,1, &histSize, &histRange, uniform, accumulate);//histogram calculation for second image:- Blue Plane
calHist(&planes_for_second_image)[1],1,0, Mat(), GreenHist2,1, &histSize, &histRange, uniform, accumulate);//histogram calculation for second image:- Green Plane
calHist(&planes_for_second_image)[2],1,0, Mat(), RedHist1,1, &histSize, &histRange, uniform, accumulate);//histogram calculation for second image:- Red Plane

normalize(BlueHist1, BlusHist1, 0,histImage.rows, NORM_MINIMAX, -1, Mat() );
normalize(GreenHist1, GreenHist1, 0,histImage.rows, NORM_MINIMAX, -1, Mat() );
normalize(RedHist1, RedHist1, 0,histImage.rows, NORM_MINIMAX, -1, Mat() );

normalize(BlueHist2, BlusHist2, 0,histImage.rows, NORM_MINIMAX, -1, Mat() );
normalize(GreenHist2, GreenHist2, 0,histImage.rows, NORM_MINIMAX, -1, Mat() );
normalize(RedHist2, RedHist2, 0,histImage.rows, NORM_MINIMAX, -1, Mat() );

float distance_blue_plane= compareHist(BlueHist1, BlueHist2, CV_COMP_BHATTACHARYYA);// Histogram can be compared with Bhattacharyya distance or Chisqure or Correl or Hellinger or intersect use can use any one of these
float distance_green_plane= compareHist(GreenHist1, GreenHist2, CV_COMP_CHISQR);//I am just giving examples u can use any one .
float distance_red_place= compareHist(RedHist1, RedHist2, CV_COMP_CORREL);


return 0;

I think for histogram comparison these method for distance calculations are better than Euclidean distance. If I am wrong please update me.

edit flag offensive delete link more


after making a set of experience it seems that the built-in methods give better result than euclidean distance, however this does not mean that euclidean distance is a bad way to make comparisons, every thing can be improved

azdoud.y gravatar imageazdoud.y ( 2016-12-19 11:00:20 -0600 )edit

Question Tools

1 follower


Asked: 2016-12-18 04:03:59 -0600

Seen: 2,289 times

Last updated: Dec 19 '16