OpenCV countour/convexHull 错误

sno*_*ake 5 python windows opencv contour

我正在尝试重现教程。但是我在调​​用时收到错误cv2.convexHull(cnt,returnPoints = False)

OpenCV 错误:在 cv::convexHull,文件 C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\imgproc\ 中,断言失败(total >= 0 && (depth == CV_32F || depth == CV_32S)) src\convhull.cpp,第 134 行 Traceback(最近一次调用最后一次):文件“Z:/Image processing/HypheArea/test.py”,第 10 行,在 hull = cv2.convexHull(cnt,returnPoints = False) cv2.error : C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\imgproc\src\convhull.cpp:134: 错误: (-215) total >= 0 && (depth == CV_32F || depth == CV_32S ) 在函数 cv::convexHull 中

示例代码:

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()
Run Code Online (Sandbox Code Playgroud)

我已经尝试将我的数组转换为 int32 或 float32 但它没有帮助。

Windows 7、Python 2.7、OpenCV 3.0.0

任何帮助将不胜感激