TwilighT
TwilighT

Reputation: 45

How to compare the images not only with histogram?

I'm programming about comparing with two images. Thanks to stackoverflow and google, I could get information about comparing ways.

Most of them are the ways with comparing histogram included in OpenCV. They are cool usually.

However, in some cases, the problem happens.

Let me give some examples.

enter image description here enter image description here

Two images are the same except the color. (actually, it is inversed.) In this case, the outlines of images are the same, but with histogram comparing, it says there's no concern between the two images.

From that time, I wondered whether there is an algorithm or method which compares not only histogram but also outlines. I also think 'outline' is very ambiguous, but I don't know how I explain with other words.

Thanks for reading. :)

Upvotes: 2

Views: 817

Answers (2)

Cookie Monster
Cookie Monster

Reputation: 390

You could use the Hu moments.

// Find contours
std::vector<std::vector<cv::Point> > contours;
cv::findContours(image, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
cv::Moments mom = cv::moments(contours[0]); 
double hu[7];
cv::HuMoments(mom, hu); // now in hu are your 7 Hu-Moments

The hu moments are a vector of 7 components. If you extract the vectors for both images then you can compare them using a quadratic difference. It will look like this:

float Distance::distEuclidean(const std::vector<float>& query, const std::vector<float>& item_db)
{
float sum = 0;
for (int i = 0; i < query.size();i++)
{
    float v1 = query.at(i);
    float v2 = item_db.at(i);
    sum += (v1-v2)*(v1-v2);
}
float distance = sqrt(sum);
return distance;
}

Please note that hu[7] is a c vector and the input of the function is a c++ std::vector

Upvotes: 0

skm
skm

Reputation: 5649

There is not one method which can be applied to all the situation. Whatever image comparison method you want to choose, you must think about your requirement and the possible image set first.

If you don't know anything about the two images which you want to compare then, you must use a combination of different techniques like histogram matching, template matching, feature matching and several other.

For this particular case where the colors are different but the structure is same, you can use the following strategy:

  1. Find the contours in each image.
  2. You will get two contour images....one for each image
  3. apply template matching on it.

Upvotes: 1

Related Questions