How to calculate Euclidean Distance d(h,g)
Dear community,
I’d like to apply this equation of euclidean distance between two histogram for every Channel A, B and C
any help i'd be glad
Dear community,
I’d like to apply this equation of euclidean distance between two histogram for every Channel A, B and C
any help i'd be glad
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()
#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);
cout<<distnace_blue_plane<<endl;
cout<<distance_green_plane<<endl;
cout<<distance_red_plane<<endl;
return 0;
}
I think for histogram comparison these method for distance calculations are better than Euclidean distance. If I am wrong please update me.
Asked: 2016-12-18 04:03:59 -0600
Seen: 2,345 times
Last updated: Dec 19 '16
Area of a single pixel object in OpenCV
build problems for android_binary_package - Eclipse Indigo, Ubuntu 12.04
OpenCV DescriptorMatcher matches
Can't compile .cu file when including opencv.hpp
Using OpenCV's stitching module, strange error when compositing images
compile error in opencv2/flann/lsh_table.h when compiling bgslibrary