5

I am trying to reproduce this tutorial. However I get an error when calling cv2.convexHull(cnt,returnPoints = False)

OpenCV Error: Assertion failed (total >= 0 && (depth == CV_32F || depth == CV_32S)) in cv::convexHull, file C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\imgproc\src\convhull.cpp, line 134 Traceback (most recent call last): File "Z:/Image processing/HypheArea/test.py", line 10, in hull = cv2.convexHull(cnt,returnPoints = False) cv2.error: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\imgproc\src\convhull.cpp:134: error: (-215) total >= 0 && (depth == CV_32F || depth == CV_32S) in function cv::convexHull

Example Code:

import cv2
import numpy as np

img = cv2.imread('star.jpg')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 127, 255,0)
contours = cv2.findContours(thresh,2,1)
cnt = contours[0]

hull = cv2.convexHull(cnt,returnPoints = False)
defects = cv2.convexityDefects(cnt,hull)

for i in range(defects.shape[0]):
    s,e,f,d = defects[i,0]
    start = tuple(cnt[s][0])
    end = tuple(cnt[e][0])
    far = tuple(cnt[f][0])
    cv2.line(img,start,end,[0,255,0],2)
    cv2.circle(img,far,5,[0,0,255],-1)

cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

I tried already to convert my array into int32 or float32 but it did not help.

Windows 7, Python 2.7, OpenCV 3.0.0

Any help would be very appreciated

honeymoon
  • 2,400
  • 5
  • 34
  • 43
  • Can you upload the image of the star you are using? – Jeru Luke Jan 06 '17 at 18:52
  • Another suggestion, try changing the code in line 5 to `ret, thresh = cv2.threshold(img_gray, 127, 255,1)`. This is because contours are found only based on the white regions in the binary image. – Jeru Luke Jan 06 '17 at 18:54
  • Do have a look at [THIS ANSWER](http://stackoverflow.com/questions/41508775/drawing-convexhull-in-opencv2-python/41512353#41512353) i posted. Do leave a comment if it helps – Jeru Luke Jan 06 '17 at 18:59
  • I think it cause by contours[0] array length is too much bigger than convexHull can handle or the value isn't what convexHull expected. sorry for my bad language – Jan sebastian Feb 14 '18 at 08:43

0 Answers0