how to use compareHist function opencv

青春壹個敷衍的年華 提交于 2019-12-06 06:02:34

问题


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

I am trying to use the code above to compare the correlation between histograms histr and histg when I run the code the I get the error

'module' object has no attribute 'cv'

It seems that CV3 the names of the various correlation functions have changed. What are the names of the various correlation functions?


回答1:


The opencv version you are using has cv2.cv.CV_COMP_CORREL renamed to cv2.HISTCMP_CORREL

The function name changes are as follows (left hand side shows the names for opencv2, right hand side shows the name for the latest version of 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



回答2:


As Zdar mentioned it looks like the constants have been renamed in opencv3.0 to:

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



回答3:


sample code for compare histogram in 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

return value show how close to your test image with compared one. example: cv2.HISTCMP_BHATTACHARYYA method gives zero(0.0) for the same image. other methods are cv2.HISTCMP_CHISQR,cv2.HISTCMP_CHISQR_ALT,cv2.HISTCMP_CORREL cv2.HISTCMP_HELLINGER,cv2.HISTCMP_INTERSECT,cv2.HISTCMP_KL_DIV.



来源:https://stackoverflow.com/questions/40451706/how-to-use-comparehist-function-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!