5

I want to calculate 3D histogram of my Cielab image in python. I am using openCV to calculate my histogram. I want to compare images using compareHist function of openCV, thats why I am using openCV to compute 3D histogram of my image.

I tried with the following variables:

i_lab = image.copy()
i_lab = i_lab.astype(np.uint8)
Range_hist = [[0, 100], [-100, 100], [-100, 100]]    
hist_1 = cv2.calcHist([i_lab], [[0], [1], [2]], None, [[20], [20], [20]], Range_hist)

But it gives error SystemError: error return without exception set Please tell me what am I doing wrong and if it is possible to compute 3D histogram using openCV in python

1
  • I used the compareHist function on the histogram generated by using numpy library, so I don't need the above function to calculate my histogram anymore. Commented Apr 10, 2013 at 12:21

2 Answers 2

7

I came across this question while trying to make a 3D histogram of an HSV image, and encountered the same error. It turns out that the OpenCV documentation is leading us astray here. The docs are written for the C++ API and as such can only be used as a vague guide to the Python cv2 API (although I have found that the docs are misleading for C++ as well at times).

The function signature is as follows:

cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) -> hist

The key point is that the channels, histSize and ranges parameters should be flat lists, not nested lists as in your example. Try the following, assuming i_lab is a three-channel image:

range_hist = [0, 100, -100, 100, -100, 100]
hist_1 = cv2.calcHist([i_lab], [0, 1, 2], None, [20, 20, 20], range_hist)

For a more complete example, try this code listing from the opencvpython blog.

Sign up to request clarification or add additional context in comments.

Comments

0

For computing a histogram in cielab, this worked for me:

function signature of calcHist: images, channels, mask, number of bins, array of the dims arrays of the histogram bin boundaries in each dimension.

img = cv2.imread(file) # in bgr 
hist = cv2.calcHist([img],[0, 1, 2],None,[256, 256, 256],[0, 255, 0, 255, 0, 255]) # in bgr
img = cv2.cvtColor(img, cv2.COLOR_BGR2LAB) # in lab 
hist = cv2.calcHist([img],[0, 1, 2],None,[100, 2*128, 2*128],[0, 100, -128, 127, -128, 127])`# in lab 

correlation = cv2.HISTCMP_CORREL # compare histograms using correlation
corr = cv2.compareHist(img, img2, correlation)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.