我正在寻找计算两个直方图之间earth mover's distance (EMD)的java代码(或库)。这可以是直接或间接的(例如使用匈牙利算法)。我在c / c ++中找到了几个这样的实现(例如"Fast and Robust Earth Mover's Distances",但我想知道是否有现成的Java版本。
我将使用EMD计算来评估this paper在我正在研究的科学项目中所给出的方法。
更新
使用各种资源我估计下面的代码应该可以解决问题。 determineMinCostAssignment 是匈牙利算法确定的最佳分配的计算。为此,我将使用http://konstantinosnedas.com/dev/soft/munkres.htm中的代码 我主要担心的是计算出的 flow :我不确定这是否正确。是否有人可以验证这是否正确?
/**
* Determines the Earth Mover's Distance between two histogram assuming an equal distance between two buckets of a histogram. The distance between
* two buckets is equal to the differences in the indexes of the buckets.
*
* @param threshold
* The maximum distance to use between two buckets.
*/
public static double determineEarthMoversDistance(double[] histogram1, double[] histogram2, int threshold) {
if (histogram1.length != histogram2.length)
throw new InvalidParameterException("Each histogram must have the same number of elements");
double[][] groundDistances = new double[histogram1.length][histogram2.length];
for (int i = 0; i < histogram1.length; ++i) {
for (int j = 0; j < histogram2.length; ++j) {
int abs_diff = Math.abs(i - j);
groundDistances[i][j] = Math.min(abs_diff, threshold);
}
}
int[][] assignment = determineMinCostAssignment(groundDistances);
double costSum = 0, flowSum = 0;
for (int i = 0; i < assignment.length; i++) {
double cost = groundDistances[assignment[i][0]][assignment[i][1]];
double flow = histogram2[assignment[i][1]];
costSum += cost * flow;
flowSum += flow;
}
return costSum / flowSum;
}
答案 0 :(得分:6)
这是我刚刚发布的FastEMD算法的纯Java端口: https://github.com/telmomenezes/JFastEMD
答案 1 :(得分:1)
网站"Fast and Robust Earth Mover's Distances"有一个用于C / C ++代码的Java包装器,带有用于Linux和Windows的已编译二进制文件。
答案 2 :(得分:1)
这是我用于Java / Scala的内容:
import org.apache.commons.math3.ml.distance.EarthMoversDistance
new EarthMoversDistance().compute(observed, expected)
答案 3 :(得分:0)
答案 4 :(得分:0)
https://github.com/wihoho/VideoRecognition
我很确定你可以用Java做同样的事情。只需添加一个文件接口即可将EMD的C实现与Java代码连接起来。