Search code examples
imagecomparehistogramhsv

How to compare two color histogram


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.


Solution

  • 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.