简体   繁体   English

如何在Java中获得具有容忍度的两个哈希图的交集?

[英]How to get intersection of two hashmaps with tolerence in Java?

In my java code, I have two hashmaps, and I want to get the intersection as a value. 在我的Java代码中,我有两个哈希图,并且我想将交集作为值。 The keys are ARGB values of a color (integer) and its value is the frequency (integer). 键是颜色的ARGB值(整数),其值是频率(整数)。 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. 当然,它并不是完全严格的,因为在现实生活中,两种颜色看起来可能相同,但具有略微不同的ARGB值,这就是公差部分的所在。

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 谢谢

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). 解决此问题的一种经典方法是推土机距离 (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. 这两个图像之间的EMD由第一个图像的像素和第二个图像的像素之间的一一对应关系确定,该对应关系将所有成对像素之间的颜色之间距离的和最小化。 The EMD can be computed in polynomial time using the Hungarian algorithm . 可以使用匈牙利算法在多项式时间内计算EMD。

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 . 如果图像大小不同,则必须对频率进行归一化,并将匈牙利算法换成可以解决更一般的最小成本流问题的算法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM