8
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?

CDspace
  • 2,639
  • 18
  • 30
  • 36
Dappa jack
  • 145
  • 1
  • 2
  • 12
  • Possible duplicate of http://stackoverflow.com/questions/30013009/opencv-3-0-0-dev-python-bindings-not-working-properly – Mariusz Nov 06 '16 at 16:51

3 Answers3

26

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
Emeeus
  • 5,072
  • 2
  • 25
  • 37
ZdaR
  • 22,343
  • 7
  • 66
  • 87
  • It worked. Please do you know the methods for Bhattacharyya, chi square and intersection – Dappa jack Nov 06 '16 at 17:11
  • Please add the required deprecated APIs' in the question by editing. – ZdaR Nov 06 '16 at 17:33
  • 1
    You have to keep in mind that not all metrics behave the same. For Correlation and Intersection, a bigger value means more similarity. It's the contrary for the others. – Ben Banks Apr 03 '19 at 11:41
12

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 
Benoît Latinier
  • 2,062
  • 2
  • 24
  • 36
lawnchair57821
  • 121
  • 1
  • 3
6

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.

Lanka
  • 357
  • 3
  • 13
  • Thanks for the simple yet very helpful code. I have seen a lot of codes here and elsewhere which tend to complicate things a lot. _Question_: I understand that we are supplying the `[im2]` to calcHist, but what does the rest of the function do: ([0], None,[256],[0,256]) – alpha_989 Feb 02 '18 at 18:19
  • 1
    cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) – Lanka Mar 08 '18 at 08:59
  • Can you describe the use cases for this method? I tried using it to compare images of the same things/areas but at different angles. But the results weren't really good. In what cases could using histograms work well? – x89 Apr 12 '21 at 08:34