0

I used a map<CString, vector<double>> structure to store the mapping of file name to its HSV color histogram.And there are 100 elements in this map as a image DB.If now comes a image,and I have get the input image's histogram,how can I do the compare?

I know a method called "quadratic distance", but I do not understand it.

zxi
  • 195
  • 4
  • 17

1 Answers1

1

One simple method would be using a distance calculator like this:

double dist(vector<double> *histogram1, vector<double> *histogram2) {
    double result = 0.0;
    for (vector<double>::iterator val1=histogram1->begin(), val2=histogram2->begin();
         val1<histogram1->end();
         val1++, val2++) {
        result += (*val1 - *val2) * (*val1 - *val2);
    }
    result = sqrt(result);
    return result;
}

And then determine which histogram has the smallest distance. Please note that this is for demonstration purposes only, you must add vector size checks etc.

Ilmo Euro
  • 4,925
  • 1
  • 27
  • 29
  • Simple and useful,I will have a try...Thanks.Why the sqrt func is used here ?@llmoEuro – zxi Jul 04 '12 at 06:17
  • If you treat the two histograms as n-dimensional points (in the mathematical sense), this function gives their Euclidean distance using the generalized Pythagorean theorem, and `sqrt()` is part of it. If you only need to find the one with the smallest distance, you don't need it, because if `sqrt(a) < sqrt(b)` then `a < b`. – Ilmo Euro Jul 04 '12 at 08:10