[英]How to get intersection of two hashmaps with tolerence in Java?
在我的Java代码中,我有两个哈希图,并且我想将交集作为值。 键是颜色的ARGB值(整数),其值是频率(整数)。 基本上每个哈希图都是从图像生成的。
我想确定一个值,该值表示地图之间的距离。 值越高,两个贴图彼此越接近。 当然,它并不是完全严格的,因为在现实生活中,两种颜色看起来可能相同,但具有略微不同的ARGB值,这就是公差部分的所在。
到目前为止,我得到了:
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);
}
有谁知道如何将公差部分合并到其中,因为我不知道...
谢谢
我不确定两个地图的交集是什么,但这听起来好像您想基于两个图像的直方图计算某种距离。 解决此问题的一种经典方法是推土机距离 (EMD)。 暂时假设图像具有相同数量的像素。 这两个图像之间的EMD由第一个图像的像素和第二个图像的像素之间的一一对应关系确定,该对应关系将所有成对像素之间的颜色之间距离的和最小化。 可以使用匈牙利算法在多项式时间内计算EMD。
如果图像大小不同,则必须对频率进行归一化,并将匈牙利算法换成可以解决更一般的最小成本流问题的算法 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.