如何计算“EMD” 2个numpy的阵列,即“直方图”使用OpenCV的?(How to compu

2019-08-18 05:38发布

由于我是新来的OpenCV,我不知道如何使用cv.CalcEMD2功能与numpy阵列。
我有两个数组:

a=[1,2,3,4,5]  
b=[1,2,3,4]

我怎样才能转让numpy arrayCVhistogramCvhistogram的功能参数signature

我想任何人谁回答问题,解释任何使用opencv ,通过所提供的解决方案的功能。

“EMD” == 地球先行者的距离 。

更新: -
同时,这将是有益的,如果任何人都可以告诉我如何设置cv.CalcEMD2参数即"signature"使用numpy数组!

注意:-
*对于那些谁可能有兴趣在这个问题上,这个答案需要更多的测试。

Answer 1:

你必须在重量和坐标来定义你的阵列。 如果你有两个数组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的数组转换成CV_32FC1垫你输入之前作为签名的CalcEMD2功能。 代码如下所示:

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。 对于第三个参数的另一个选择是曼哈顿距离CV_DIST_L1。

我也想提一提,我写的代码来计算在Python两个2D直方图的堆土机的距离。 你可以找到这个代码在这里 。



Answer 2:

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])


Answer 3:

我知道OP要衡量堆土机距离使用OpenCV的,但如果你想这样做使用SciPy的,你可以使用以下(Wasserstein的距离也被称为堆土机距离):

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)


文章来源: How to compute “EMD” for 2 numpy arrays i.e “histogram” using opencv?