How to use compareHist opencv function

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 the histograms histrand histgwhen I run the code I get an error

'module' object has no attribute 'cv'

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

+4
source share
3 answers

The used version of opencv has cv2.cv.CV_COMP_CORREL, renamedcv2.HISTCMP_CORREL

The changes to the function name are as follows (on the left side are the names for opencv2, the right 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/ HISTCMP_CHISQR_ALT
cv2.cv.CV_COMP_INTERSECT :: cv2.HISTCMP_INTERSECT
cv2.cv.CV_COMP_BHATTACHARYYA :: cv2.HISTCMP_BHATTACHARYYA
+17
source

, , opencv3.0:

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

sample code for comparing histograms 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

the return value shows how close one is to your test image. Example: the method cv2.HISTCMP_BHATTACHARYYAgives zero (0.0) for the same image. other methods:cv2.HISTCMP_CHISQR,cv2.HISTCMP_CHISQR_ALT,cv2.HISTCMP_CORREL cv2.HISTCMP_HELLINGER,cv2.HISTCMP_INTERSECT,cv2.HISTCMP_KL_DIV.

+3
source

Source: https://habr.com/ru/post/1659994/


All Articles