由于我是opencv的新手,我不知道如何将cv.CalcEMD2
函数与numpy
数组一起使用。
我有两个数组:
a=[1,2,3,4,5]
b=[1,2,3,4]
如何将numpy array
转移到CVhistogram
并从Cvhistogram
转移到功能参数signature
?
我希望有人回答这个问题,通过提供的解决方案解释任何使用过的opencv
函数。
“EMD”== earth mover's distance。
更新: -
另外,如果有人可以告诉我如何使用"signature"
数组设置cv.CalcEMD2
参数,即numpy
,将会很有帮助。
注意: -
*对于那些可能对这个问题感兴趣的人,这个答案需要更多的测试。
答案 0 :(得分:16)
您必须根据权重和坐标来定义数组。如果你有两个数组a = [1,1,0,0,1]和b = [0,1,0,1]代表一维直方图,那么numpy数组应如下所示:
a = [[1 1]
[1 2]
[0 3]
[0 4]
[1 5]]
b = [[0 1]
[1 2]
[0 3]
[1 4]]
请注意,行数可以不同。列数应为维+ 1.第一列包含权重,第二列包含坐标。
下一步是在将numpy数组作为签名输入到CalcEMD2函数之前,将数组转换为CV_32FC1 Mat。代码如下所示:
from cv2 import *
import numpy as np
# Initialize a and b numpy arrays with coordinates and weights
a = np.zeros((5,2))
for i in range(0,5):
a[i][1] = i+1
a[0][0] = 1
a[1][0] = 1
a[2][0] = 0
a[3][0] = 0
a[4][0] = 1
b = np.zeros((4,2))
for i in range(0,4):
b[i][1] = i+1
b[0][0] = 0
b[1][0] = 1
b[2][0] = 0
b[3][0] = 1
# Convert from numpy array to CV_32FC1 Mat
a64 = cv.fromarray(a)
a32 = cv.CreateMat(a64.rows, a64.cols, cv.CV_32FC1)
cv.Convert(a64, a32)
b64 = cv.fromarray(b)
b32 = cv.CreateMat(b64.rows, b64.cols, cv.CV_32FC1)
cv.Convert(b64, b32)
# Calculate Earth Mover's
print cv.CalcEMD2(a32,b32,cv.CV_DIST_L2)
# Wait for key
cv.WaitKey(0)
请注意,CalcEMD2的第三个参数是欧几里德距离CV_DIST_L2。第三个参数的另一个选项是Manhattan Distance CV_DIST_L1。
我还想提一下,我编写了代码来计算地球移动器在Python中两个2D直方图的距离。您可以找到此代码here。
答案 1 :(得分:3)
CV.CalcEMD2要求数组根据文档也包含每个信号的权重。
我建议使用权重1来定义数组,如下所示:
a=array([1,1],[2,1],[3,1],[4,1],[5,1])
b=array([1,1],[2,1],[3,1],[4,1])
答案 2 :(得分:0)
我知道OP希望使用OpenCV来衡量Earth Mover's Distance,但如果你想使用Scipy这样做,你可以使用以下内容(Wasserstein Distance也被称为Earth Mover' s距离):
from scipy.stats import wasserstein_distance
from scipy.ndimage import imread
import numpy as np
def get_histogram(img):
'''
Get the histogram of an image. For an 8-bit, grayscale image, the
histogram will be a 256 unit vector in which the nth value indicates
the percent of the pixels in the image with the given darkness level.
The histogram's values sum to 1.
'''
h, w = img.shape
hist = [0.0] * 256
for i in range(h):
for j in range(w):
hist[img[i, j]] += 1
return np.array(hist) / (h * w)
a = imread('a.jpg')
b = imread('b.jpg')
a_hist = get_histogram(a)
b_hist = get_histogram(b)
dist = wasserstein_distance(a_hist, b_hist)
print(dist)