首页 > 解决方案 > 如何修复错误:(-215:Assertion failed) npoints > 0 in function 'drawContours'

问题描述

绘制轮廓错误我正在尝试为图像中的对象绘制轮廓

(_, contours) = cv2.findContours(binary, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

# draw contours over original image
length = len(contours)
for c in range(length):
    cv2.drawContours(img,contours,c,(255,0,0),3)

cv2.namedWindow("output", cv2.WINDOW_NORMAL)
cv2.imshow("output", img)
cv2.waitKey(0)

我希望绘制所有轮廓,但我得到的实际结果是错误:

(-215:断言失败)函数“drawContours”中的 npoints > 0

标签: opencvopencv-contour

解决方案


轮廓应该是 numpy 数组。将您的代码更改为:

for c in contours:
    cv2.drawContours(img,[c], 0, (255,0,0),3)

推荐阅读