1

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?

Can anyone please help me?

But I dont know how to do it since I am not very much familiar with OpenCV. Thank you.

4

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Both of these answers discuss histograms in OpenCV:

Horizontal Histogram in OpenCV

Horizontal Histogram in OpenCV

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.