Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data type error with drawContours unless I pickle/unpickle first

Tags:

python

opencv

I'm running into an interesting error with opencv 2.4.3 on python 2.7.3 on Windows. When attempting to use drawContours I get a "TypeError: contours data type = 5 is not supported" error unless I pickle/unpickle the contours first.

This doesn't work (I get the "TypeError: contours data type = 5 is not supported"):

noBg = cv2.blur(src, (5,5))
noBg = cv2.inRange(noBg, np.array([80, 0, 200], np.uint8), np.array([255, 50, 255], np.uint8)) 
noBg = np.invert(noBg)
contours, hierarchy = cv2.findContours(noBg, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(src, contours, -1, (0,255,0), 3) 

But this does work:

noBg = cv2.blur(src, (5,5))
noBg = cv2.inRange(noBg, np.array([80, 0, 200], np.uint8), np.array([255, 50, 255], np.uint8)) 
noBg = np.invert(noBg)
contours, hierarchy = cv2.findContours(noBg, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

tmp = cPickle.dumps(contours)    
contours = cPickle.loads(tmp)

cv2.drawContours(src, contours, -1, (0,255,0), 3) 

Has anyone else seen this behavior or am I missing something obvious? I'm new to python/opencv so that may very well be the case.

Edit: Just tested this on my Mac, and both cases work fine. Maybe just a Windows problem?

like image 291
A_Rupert Avatar asked Nov 12 '12 00:11

A_Rupert


2 Answers

If you downgrade to opencv 2.4.2 this will work. It appears to be a bug with 2.4.3 which was just released two weeks ago.

like image 148
Ian Avatar answered Oct 11 '22 02:10

Ian


I have just experienced the same problem after getting OpenCV 2.4.3 when using contours from findContours and convexHull. As I did not want to downgrade, casting the contours array elements to int temporarily solves the problem.

contours, _ = cv2.findContours(noBg,cv2.RETR_LIST ,cv2.CHAIN_APPROX_SIMPLE,offset = (0,0))
hull_contour = cv2.convexHull(contours[0].astype('int'))
like image 33
Jan Avatar answered Oct 11 '22 04:10

Jan