如何使用opencv计算2个numpy数组的“EMD”,即“直方图”?

2024-05-11 22:21:52 发布

您现在位置:Python中文网/ 问答频道 /正文

由于我是opencv新手,我不知道如何将^{}函数与numpy数组一起使用。
我有两个数组:

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

如何将numpy array转移到CVhistogram,并从Cvhistogram转移到函数参数signature

我希望任何回答问题的人都能通过提供的解决方案解释任何使用过的opencv函数。

“EMD”=earth mover's distance

更新:-
另外,如果有人能告诉我如何使用numpy数组设置^{}参数,也会很有帮助!!

注:-
*对于那些可能对这个问题感兴趣的人来说,这个答案需要更多的测试。


Tags: 函数numpy数组函数参数解决方案arrayopencvmover
3条回答

必须根据权重和坐标定义数组。如果有两个数组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 fc1 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。第三个参数的另一个选项是曼哈顿距离CV_DIST_L1。

我还想提到的是,我编写代码是为了计算Python中两个2D直方图的地球移动者距离。你可以找到这个代码here

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

我知道操作人员想用OpenCV来测量Earth Mover's Distance,但是如果你想用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)

相关问题 更多 >