我在python中寻找Earth Mover的距离(或快速EMD)实现。 关于在何处找到它的任何线索,我已经在网上看得足够多了。 我想在我正在做的图像检索项目中使用它。 感谢。
编辑: 我使用pulp libararies找到了一个非常好的解决方案。 此页面还包含设置所需的说明。
答案 0 :(得分:22)
在OpenCv中有一个很好的Python实现。函数的名称是CalcEMD2,用于比较两个图像的直方图的简单代码如下所示:
#Import OpenCv library
from cv2 import *
### HISTOGRAM FUNCTION #########################################################
def calcHistogram(src):
# Convert to HSV
hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
cv.CvtColor(src, hsv, cv.CV_BGR2HSV)
# Extract the H and S planes
size = cv.GetSize(src)
h_plane = cv.CreateMat(size[1], size[0], cv.CV_8UC1)
s_plane = cv.CreateMat(size[1], size[0], cv.CV_8UC1)
cv.Split(hsv, h_plane, s_plane, None, None)
planes = [h_plane, s_plane]
#Define numer of bins
h_bins = 30
s_bins = 32
#Define histogram size
hist_size = [h_bins, s_bins]
# hue varies from 0 (~0 deg red) to 180 (~360 deg red again */
h_ranges = [0, 180]
# saturation varies from 0 (black-gray-white) to 255 (pure spectrum color)
s_ranges = [0, 255]
ranges = [h_ranges, s_ranges]
#Create histogram
hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1)
#Calc histogram
cv.CalcHist([cv.GetImage(i) for i in planes], hist)
cv.NormalizeHist(hist, 1.0)
#Return histogram
return hist
### EARTH MOVERS ############################################################
def calcEM(hist1,hist2,h_bins,s_bins):
#Define number of rows
numRows = h_bins*s_bins
sig1 = cv.CreateMat(numRows, 3, cv.CV_32FC1)
sig2 = cv.CreateMat(numRows, 3, cv.CV_32FC1)
for h in range(h_bins):
for s in range(s_bins):
bin_val = cv.QueryHistValue_2D(hist1, h, s)
cv.Set2D(sig1, h*s_bins+s, 0, cv.Scalar(bin_val))
cv.Set2D(sig1, h*s_bins+s, 1, cv.Scalar(h))
cv.Set2D(sig1, h*s_bins+s, 2, cv.Scalar(s))
bin_val = cv.QueryHistValue_2D(hist2, h, s)
cv.Set2D(sig2, h*s_bins+s, 0, cv.Scalar(bin_val))
cv.Set2D(sig2, h*s_bins+s, 1, cv.Scalar(h))
cv.Set2D(sig2, h*s_bins+s, 2, cv.Scalar(s))
#This is the important line were the OpenCV EM algorithm is called
return cv.CalcEMD2(sig1,sig2,cv.CV_DIST_L2)
### MAIN ########################################################################
if __name__=="__main__":
#Load image 1
src1 = cv.LoadImage("image1.jpg")
#Load image 1
src2 = cv.LoadImage("image2.jpg")
# Get histograms
histSrc1= calcHistogram(src1)
histSrc2= calcHistogram(src2)
# Compare histograms using earth mover's
histComp = calcEM(histSrc1,histSrc2,30,32)
#Print solution
print(histComp)
我使用Python 2.7和Python(x,y)测试了与之前代码非常相似的代码。如果你想了解更多关于Earth Mover的信息,你想看看使用OpenCV和C ++的实现,你可以阅读Gary Bradski和他的“学习OpenCV”一书中的“第7章:直方图匹配”。 Adrain Kaebler。
答案 1 :(得分:2)
谷歌搜索它显示了这个链接 - http://www.cs.huji.ac.il/~ofirpele/FastEMD/code/。也许你可以在这里做更多的研究,并将C / Matlab代码移植到Python中
答案 2 :(得分:1)
这是用于计算两个等长一维分布之间的地球移动距离的python代码
def emd (a,b):
earth = 0
earth1 = 0
diff = 0
s= len(a)
su = []
diff_array = []
for i in range (0,s):
diff = a[i]-b[i]
diff_array.append(diff)
diff = 0
for j in range (0,s):
earth = (earth + diff_array[j])
earth1= abs(earth)
su.append(earth1)
emd_output = sum(su)/(s-1)
print(emd_output)
答案 3 :(得分:0)
地球移动者的距离( EMD )也称为 Wasserstein指标
您可以从scipy.stats
获得该实现的Python实现:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.wasserstein_distance.html