4

I've been getting the following ambiguous error when trying to use cv2.calcHist()

>>> img
array([ 1.,  2.,  3.,  4.,  5.])
>>> cv2.calcHist( [img], channels = [0], mask = np.ones(img.size), histSize = [6], ranges = [(0,6)])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: error return without exception set

The error is so generic that I have absolutely no way of knowing what could be going wrong. You can find the function documented here. I am currently running OpenCV v2.3.1 as installed through MacPorts.

Thanks!

1 Answer 1

11

I find the online documentation a bit sparse for the Python interface. One resource I find invaluable are the samples (OpenCV-2.3.x/samples/python2), in which you can find example use of almost all the functions in the Python interface.

However looking at the documentation:

  • the mask argument must be 8-bit (mask.astype('uint8'))
  • the input img should be CV_8U or CV_32F (so img.astype('uint8') or img.astype('float32'))

And from looking at the python2 samples (camshift.py,color_histogram.py):

  • the ranges argument doesn't need to be a tuple, just a list (although the documentation suggests it should be a list of lists or array of arrays...)

So:

cv2.calcHist( [img.astype('float32')],                 # <-- convert to float32
              channels=[0], 
              mask=np.ones(img.size).astype('uint8'),  # <-- convert to uint8
              histSize=[6], 
              ranges=[0,6] )                           # <-- flat list
Sign up to request clarification or add additional context in comments.

2 Comments

I didn't realize there were examples! MacPorts is pretty quiet about what it installs. In the end, I realized that it would be faster and easier to use numpy.histogram instead of OpenCV's solution.
I had the same issue. The ranges argument not being a flat list was a problem for me.

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.