Ask Your Question

Revision history [back]

Python : Giving a custom cost matrix in CalcEMD2?

I am trying to create a custom cost matrix (cyclic array indices) for earth mover's distance calculation between regular / numpy arrays :

#Example input : PointA = [0,0,1,0], PointB = [1,0,0,0]
def earthMoversDistance(pointA, pointB):
    #indices = range(len(pointA))
    dataA = np.array([pointA]).T
    dataB = np.array([pointB]).T
    def cyclicCostMatrix(length):
        halfLength = int(length/2)
        base = np.abs(range(-halfLength, halfLength))
        base = np.roll(base, -halfLength)
        return np.array([np.roll(base, i) for i in xrange(length)])
    costMatrix = cyclicCostMatrix(len(pointA))
    def convertArrayToCVAMat(arr):
        cva64 = cv.fromarray(arr.copy())
        cva32 = cv.CreateMat(cva64.rows, cva64.cols, cv.CV_32FC1)
        cv.Convert(cva64, cva32)
        return cva32
    cvaA = convertArrayToCVAMat(dataA)
    cvaB = convertArrayToCVAMat(dataB)
    cvaCost = convertArrayToCVAMat(costMatrix)
    emd = cv.CalcEMD2(cvaA, cvaB, cv.CV_DIST_USER, cost_matrix = cvaCost)
    return emd

However, I am getting the error:

cv2.error: Only one of cost matrix or distance function should be non-NULL in case of user-defined distance

While my code may not be perfect, I am clearly not giving a distance function to the method call. Is this a bug in the python wrapper or my mistake? I am using OpenCV 2.4.2 I believe (the one that is linked to in python(x,y) ).

Any help will be much appreciated.