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
我正在尝试使用上面的代码比较直方图histr
和histg
之间的相关性,当我运行代码时,我得到了错误
'module' object has no attribute 'cv'
似乎CV3各种相关函数的名称已经改变。各种相关函数的名称是什么?
答案 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.