Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous Exception with calcHist in OpenCV

Tags:

python

opencv

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!

like image 799
duckworthd Avatar asked Dec 21 '22 03:12

duckworthd


1 Answers

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
like image 86
mathematical.coffee Avatar answered Jan 07 '23 20:01

mathematical.coffee