histogram function in OpenCV
I am seeking a way to compare 2 images and get the most matching image as output. Using histogram function in OpenCV can I do this?
开发者_JS百科Can anyone please help me?
But I dont know how to do it since I am not very much familiar with OpenCV. Thank you.
The histogram will just ensure that the two images have similar color distributions. The color distributions could be similar in very different images.
As an example, imagine a black and white 8x8 checkboard and an image whose left side is all black and the ride side pure white. These images have the same histogram.
Both of these answers discuss histograms in OpenCV:
Horizontal Histogram in OpenCV
Horizontal Histogram in OpenCV
If your aim is to find the most matching image then OpenCV has a function cvMatchTemplate() which does this. Is does use histogram matching but it is not needed to declare anything else in the code. It is possible to find the portion of the image which corresponds best to the template being matched and other variations available in the documentation.
For every image calculate HSV histogram:
Mat src_mat = imread("./image.jpg");
Mat hsv_mat;
cvtColor( src_mat, hsv_mat, CV_BGR2HSV );
MatND HSV_histogram;
int histSize[] = { 240, 240 };
float h_ranges[] = { 0, 255 };
float s_ranges[] = { 0, 180 };
const float* ranges[] = { h_ranges, s_ranges };
int channels[] = { 0, 1 };
calcHist( &hsv_mat, 1, channels, Mat(), HSV_histogram, 2, histSize, ranges, true, false );
normalize( HSV_histogram, HSV_histogram, 0, 1, NORM_MINMAX, -1, Mat() );
Then make a pairwise comparison and get a similarity score:
double score_ij = compareHist( HSV_histogram_i, HSV_histogram_j, CV_COMP_BHATTACHARYYA );
You can increase your accuracy by dividing image in smaller regions and average the results.
精彩评论