如何使用compareHist函数opencv

时间:2016-11-06 16:31:29

标签: python opencv histogram

img = cv2.imread('mandrill.png')
histg = cv2.calcHist([img],[0],None,[256],[0,256])

if len (sys.argv) < 2:
    print >>sys.stderr, "Usage:", sys.argv[0], "<image>..."
    sys.exit (1)

for fn in sys.argv[1:]:
    im = cv2.imread (fn)

histr = cv2.calcHist([im],[0],None,[256],[0,256])
a = cv2.compareHist(histr,histg,cv2.cv.CV_COMP_CORREL)
print a

我正在尝试使用上面的代码比较直方图histrhistg之间的相关性,当我运行代码时,我得到了错误

'module' object has no attribute 'cv'

似乎CV3各种相关函数的名称已经改变。各种相关函数的名称是什么?

3 个答案:

答案 0 :(得分:21)

您使用的opencv版本已将cv2.cv.CV_COMP_CORREL重命名为cv2.HISTCMP_CORREL

函数名称更改如下(左侧显示opencv2的名称,右侧显示最新版本opencv(opencv3)的名称):

cv2.cv.CV_COMP_CORREL:: cv2.HISTCMP_CORREL
cv2.cv.CV_COMP_CHISQR :: cv2.HISTCMP_CHISQR/ cv2.HISTCMP_CHISQR_ALT
cv2.cv.CV_COMP_INTERSECT :: cv2.HISTCMP_INTERSECT
cv2.cv.CV_COMP_BHATTACHARYYA :: cv2.HISTCMP_BHATTACHARYYA

答案 1 :(得分:11)

正如Zdar所说,看起来常量已在opencv3.0中重命名为:

cv2.HISTCMP_CORREL
cv2.HISTCMP_CHISQR
cv2.HISTCMP_INTERSECT 
cv2.HISTCMP_BHATTACHARYYA
a = cv2.compareHist(histr,histg,cv2.HISTCMP_CORREL) should work 

答案 2 :(得分:3)

OpenCV 3.2中比较直方图的示例代码

import cv2

path='location_of_images'
im1 = cv2.imread(path+'/'+'first.jpg',0)
hist1 = cv2.calcHist([im1],[0],None,[256],[0,256])

im2 = cv2.imread(path+'/'+'second.jpg',0)
hist2 = cv2.calcHist([im2],[0],None,[256],[0,256])

a=cv2.compareHist(hist1,hist2,cv2.HISTCMP_BHATTACHARYYA)

print a

返回值显示比较测试图像的接近程度。 示例:cv2.HISTCMP_BHATTACHARYYA方法为同一图像提供零(0.0)。 其他方法是cv2.HISTCMP_CHISQR,cv2.HISTCMP_CHISQR_ALT,cv2.HISTCMP_CORREL cv2.HISTCMP_HELLINGER,cv2.HISTCMP_INTERSECT,cv2.HISTCMP_KL_DIV.