我正在使用python和opencv以及自定义距离函数dist()
来计算一个主图像和三个测试图像之间的距离。
返回的最终图片是min(distance1, distance2, distance3)
我现在正在使用for循环遍历图像的两个轴并计算距离。这种方式非常慢,因为for循环很慢。有没有pythonic方法来做到这一点?
每个图像的每个像素都有两个值(mu,sigma)
使用的距离函数:
def w2distance2D(mu1, sig1, mu2, sig2):
"""
Returns the Wasserstein distance between two 2-Dimensional normal distributions
"""
t1 = np.linalg.norm(mu1 - mu2)
#print t1
t1 = t1 ** 2.0
#print t1
t2 = np.trace(sig2) + np.trace(sig1)
p1 = np.trace(np.dot(sig1, sig2))
p2 = (((np.linalg.det(np.dot(sig1, sig2)))))
if p2 < 0.0:
p2 = 0.0
p2 = np.sqrt(p2)
tt = p1 + 2.0*p2
if tt < 0.0:
tt = 0.0
t3 = 2.0 * np.sqrt(tt)
#print t3
if (t1 + t2 - t3) < 0:
result = 0.0
#print "here"
else:
result = np.sqrt(t1 + t2 - t3)
return result