【问题标题】:Calculating Distances Between 2 Histograms by Using Euclidean Distance使用欧几里得距离计算 2 个直方图之间的距离
【发布时间】:2021-06-16 22:51:10
【问题描述】:

我正在尝试计算两个色调图像直方图的欧几里德距离,我找到了 cv2.compareHist 方法,但它没有提供欧几里德距离的选项。非常感谢任何帮助。

【问题讨论】:

  • en.wikipedia.org/wiki/Euclidean_distance 上查看更高的维度。每个 bin 的维度不同
  • 我的数学很差,所以我必须问结果是一个数字,还是一个数字数组
  • 单个数字:dist=sqrt( ( (bin1a-bin1b)^2 ... (bin256a-bin256b) ) / 256),其中 bin1a 在 imageA 的第一个 bin 计数中,bin1b 是 imageB 的第一个 bin 计数等。即距离是平方平均值的平方根对应的 bin 之间的差异)

标签: python opencv image-processing histogram opencv-python


【解决方案1】:

这里是如何在 Python/OpenCV 中做到这一点。

输入:

import cv2
import math

# Load the images
img1 = cv2.imread('lena.jpg')
img2 = cv2.imread('zelda1.jpg')

# convert to gray
gray1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)

# Calculate the histograms, and normalize them
hist1 = cv2.calcHist([gray1], [0], None, [256], [0, 256])
#cv2.normalize(hist1, hist1, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)

hist2 = cv2.calcHist([gray2], [0], None, [256], [0, 256])
#cv2.normalize(hist2, hist2, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)

# compute euclidean distance
sum = 0
for i in range (0,256):
    sum = sum + (hist1[i][0]-hist2[i][0])**2
dist = math.sqrt(sum)
print('euclidean distance:', dist)

# above is equivalent to cv2.norm()
dist2 = cv2.norm(hist1, hist2, normType=cv2.NORM_L2)
print('euclidean distance2:', dist2)

结果:

euclidean distance : 2319.6163475885405
euclidean distance2: 2319.6163475885405

如果图像的尺寸不同(总像素=宽度*高度),则可能应该通过将每个 bin 除以图像中的总像素来标准化直方图。

【讨论】:

  • 不应该等于dist = cv2.norm(hist1, hist2)吗?为什么要将总和除以 256?
  • 正确。对不起,我在想 rmse。我不知道 cv2.norm()。
  • 我已根据@rotem 的评论更正了我的帖子。
猜你喜欢
  • 1970-01-01
  • 2020-09-25
  • 2016-05-11
  • 2013-04-07
  • 1970-01-01
  • 2021-01-31
  • 2015-09-23
  • 1970-01-01
相关资源
最近更新 更多