Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version
#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.