0

In my java code, I have two hashmaps, and I want to get the intersection as a value. The keys are ARGB values of a color (integer) and its value is the frequency (integer). Basically each hashmap was generated from an image.

I want to determine a value that represents how close the maps are to each other. The higher the value the more close the two maps are to each other. Of course it can't be perfectly strict because in real life two colors can look the same but have slightly different ARGB values, which is where the tolerance part comes in.

So far I got this:

private int colorCompare(Result otherResult) {
    HashMap<Integer, Integer> colorMap1 = getColorMap();
    HashMap<Integer, Integer> colorMap2 = otherResult.getColorMap();

    int sum = 0;
    for (Map.Entry<Integer, Integer> entry : colorMap1.entrySet()) {
        Integer key = entry.getKey();
        Integer value = entry.getValue();

        if (colorMap2.containsKey(key)) {
            sum += value + colorMap2.get(key);
        }   
    }

    return sum;
}


public double CloseTo(Pixel otherpixel) {
    Color mycolor = getColor();
    Color othercolor = otherpixel.getColor();
    double rmean = ( mycolor.getRed() + othercolor.getRed() )/2;
    int r = mycolor.getRed() - othercolor.getRed();
    int g = mycolor.getGreen() - othercolor.getGreen();
    int b = mycolor.getBlue() - othercolor.getBlue();
    double weightR = 2 + rmean/256;
    double weightG = 4.0;
    double weightB = 2 + (255-rmean)/256;
    return Math.sqrt(weightR*r*r + weightG*g*g + weightB*b*b);
} 

Does anyone know how to incorporate the tolerance part into it as I have no idea...

Thanks

omega
  • 40,311
  • 81
  • 251
  • 474

1 Answers1

1

I was unsure what the intersection of two maps would be, but it sounds as though you want to compute a distance of some sort based on the histograms of two images. One classic approach to this problem is Earth mover's distance (EMD). Assume for the moment that the images have the same number of pixels. The EMD between these two images is determined by the one-to-one correspondence between the pixels of the first image and the pixels of the second that minimizes the sum over all paired pixels of the distance between their colors. The EMD can be computed in polynomial time using the Hungarian algorithm.

If the images are of different sizes, then we have to normalize the frequencies and swap out the Hungarian algorithm for one that can solve a more general minimum-cost flow problem.

David Eisenstat
  • 64,237
  • 7
  • 60
  • 120
  • This is the basic hashmap intersection http://stackoverflow.com/questions/13180488/intersection-of-java-util-map – omega Aug 25 '14 at 01:09