首页 > 解决方案 > 在 python 中使用 cv2.drawcontours() 时出错

问题描述

我正在应用一些关于绘制轮廓的代码,但出现以下错误: t = cv2.drawContours(img, contours, -1, (0,255,0), 3)

错误:OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\drawing.cpp:2509: 错误: (-215:Assertion failed) npoints > 0 in function 'cv::drawContours '

有人能告诉我问题是什么吗??

标签: python-3.x

解决方案


很难这么说,因为你没有使用代码编辑器......但我猜它来自

thresh = cv2.adaptiveThreshold(img,8,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY,11,2)
t = cv2.drawContours(img, contours, -1, (0,255,0), 3)

缺少 findContours 函数。到目前为止,轮廓对象是空的。在绘图之前,您需要找到它们。 https://docs.opencv.org/4.1.0/d4/d73/tutorial_py_contours_begin.html


推荐阅读