python - 错误:(-215:Assertion failed) npoints > 0 在使用 OpenCV 处理轮廓时
问题描述
当我运行此代码时:
import cv2
image = cv2.imread('screenshoot10.jpg')
cv2.imshow('input image', image)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edged = cv2.Canny(gray, 30, 200)
cv2.imshow('canny edges', edged)
_, contours = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.imshow('canny edges after contouring', edged)
print(contours)
print('Numbers of contours found=', len(contours))
cv2.drawContours(image, contours, -1, (0, 255, 0), 3)
cv2.imshow('contours', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
我收到此错误:
OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\imgproc\src\drawing.cpp:2509: 错误: (-215:Assertion failed) npoints > 0 in function 'cv::drawContours'
我究竟做错了什么?
解决方案
根据findContours的文档,该方法返回(轮廓,层次结构),所以我认为代码应该是:
contours, _ = cv2.findContours(edged,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
代替
_, contours = cv2.findContours(edged,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
推荐阅读
- javascript - 以响应方式使图像适合划分
- json - 添加文件的Git奇怪的合并冲突
- python-3.x - 我需要关于图书馆 pyttsx3 的帮助
- powerbi - 如何将矩阵小计添加到 Power BI 中的第一列
- java - 从枚举构造函数为静态映射制作枚举映射
- python - 如何使用pandas根据分组数据减去一列的行?
- android - 如何从画廊中挑选短视频并在android中使用相机录制短视频
- python - 如何从 Python 3 生成器中检索随机元素?
- javascript - 如何从 javascript/angular 触发 html5 视频标签下载事件
- php - PHP如何根据日期排列数组键值