如何计算"EMD"?对于2 numpy数组,即“直方图"使用opencv? [英] How to compute "EMD" for 2 numpy arrays i.e "histogram" using opencv?

查看:341
本文介绍了如何计算"EMD"?对于2 numpy数组,即“直方图"使用opencv?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我是opencv的新手,所以我不知道如何使用 函数与numpy数组.
我有两个数组:

Since I'm new to opencv, I don't know how to use the cv.CalcEMD2 function with numpy arrays.
I have two arrays:

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

如何将numpy array传输到CVhistogram并从Cvhistogram传输到功能参数signature?

How can I transfer numpy array to CVhistogram and from Cvhistogram to the function parameter signature?

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

I would like anyone who answers the question to explain any used opencv functions through the provided solution.

"EMD" == 地球移动者的距离.

更新:-
同样,如果有人可以告诉我如何设置 cv.CalcEMD2 ,这也会很有帮助.参数"signature"使用numpy数组!

Update:-
also ,It will be helpful if anyone can tell me how to set the cv.CalcEMD2 parameter i.e"signature" using numpy array!!

注意:-
*对于可能对此问题感兴趣的人,此答案需要更多测试.

Note:-
* For those who may be interested in this question ,This answer needs more testing.

推荐答案

您必须根据权重和坐标定义数组.如果您有两个表示一维直方图的数组a = [1,1,0,0,1]和b = [0,1,0,1],则numpy数组应如下所示:

You have to define your arrays in terms of weights and coordinates. If you have two arrays a = [1,1,0,0,1] and b = [0,1,0,1] that represent one dimensional histograms, then the numpy arrays should look like this:

a = [[1 1]
     [1 2]
     [0 3]
     [0 4]
     [1 5]]

b = [[0 1]
     [1 2]
     [0 3]
     [1 4]]

请注意,行数可以不同.列数应为尺寸+1.第一列包含权重,第二列包含坐标.

Notice that the number of rows can be different. The number of columns should be the dimensions + 1. The first column contains the weights, and the second column contains the coordinates.

下一步是在输入numpy数组作为CalcEMD2函数的签名之前,将数组转换为CV_32FC1 Mat.代码如下:

The next step is to convert your arrays to a CV_32FC1 Mat before you input the numpy array as a signature to the CalcEMD2 function. The code would look like this:

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.

Notice that the third parameter of CalcEMD2 is the Euclidean Distance CV_DIST_L2. Another option for the third parameter is the Manhattan Distance CV_DIST_L1.

我还想提到我写的代码来计算Python中两个二维直方图的地球移动器的距离.您可以在此处找到此代码.

I would also like to mention that I wrote the code to calculate the Earth Mover's distance of two 2D histograms in Python. You can find this code here.

这篇关于如何计算"EMD"?对于2 numpy数组,即“直方图"使用opencv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆