16 Opencv 中的 cv2.EMD()

cv2.EMD() 简单解读

参考链接:

函数解读:

https://samvankooten.net/2018/09/25/earth-movers-distance-in-python/

https://gist.github.com/svank/6f3c2d776eea882fd271bba1bd2cc16d

原理:

https://blog.csdn.net/qq_30815237/article/details/86807253?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-0&spm=1001.2101.3001.4242

https://blog.csdn.net/wangdonggg/article/details/32329879

函数定义:

float cv::EMD	(
InputArray	signature1,	# 特征1 size1
InputArray	signature2,	# 特征2 size2
int 		distType,	
# 使用的度量方式:
# cv2.DIST_USER : 用户自定义距离(即下一个参数,cost_matrix)
# cv2.DIST_L1: distance = |x1-x2| + |y1-y2|
# cv2.DIST_L2: 欧式距离
# cv2.DIST_C: distance = max(|x1-x2|,|y1-y2|)
# cv2.DIST_L12: L1-L2 metric: distance = 2(sqrt(1+x*x/2) - 1))
InputArray 	cost_matrix,	# 用户自定义度量方式 size1 * size2
float		lowerBound = 0,	# 一般来说不用,没搞懂
OutputArray	flow	# 流矩阵 size1 * size2
Return		cost	# cost = flow * cost_matrix.sum()
)

注:signature1 和 signature2 在传入 EMD()后会自动被归一化:

import cv2
import numpy
import torch

a = torch.Tensor([1, 2, 3, 4, 5]).view(-1, 1).numpy()
b = torch.Tensor([2, 3, 4, 5, 1]).view(-1, 1).numpy()

# 归一化,有无归一,结果都一样。
# a = a/a.sum(0)
# b = b/b.sum(0)

cost_matrix = torch.ones([5,5]).numpy()
print(a)
print(b)
print(cost_matrix)

cost, _, flow = cv2.EMD(a, b, cv2.DIST_USER, cost_matrix)
print(cost)
print(flow)
posted @ 2021-12-02 10:57  SethDeng  阅读(958)  评论(0编辑  收藏  举报