Created
July 1, 2013 14:31
-
-
Save oostendo/5901331 to your computer and use it in GitHub Desktop.
Earth mover distance for 2 numpy histograms (uses OpenCV's calcEMD2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#extended from http://stackoverflow.com/questions/15706339/how-to-compute-emd-for-2-numpy-arrays-i-e-histogram-using-opencv | |
#Earth Mover Distance = histogram similarity metric. Weights are optional. | |
def histEMD(hist1, hist2, hist1weights = [], hist2weights = []): | |
if not len(hist1weights): | |
hist1weights = np.ones(len(hist1)) | |
if not len(hist2weights): | |
hist2weights = np.ones(len(hist1)) | |
a64 = cv.fromarray(np.dstack((hist1weights, hist1))[0].copy()) | |
a32 = cv.CreateMat(a64.rows, a64.cols, cv.CV_32FC1) | |
cv.Convert(a64, a32) | |
b64 = cv.fromarray(np.dstack((hist2weights, hist2))[0].copy()) | |
b32 = cv.CreateMat(b64.rows, b64.cols, cv.CV_32FC1) | |
cv.Convert(b64, b32) | |
return cv.CalcEMD2(a32,b32,cv.CV_DIST_L2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment