gpt4 book ai didi

python - 错误 : (-215:Assertion failed) npoints > 0 while working with contours using OpenCV

转载 作者:太空宇宙 更新时间:2023-11-03 21:39:12 26 4
gpt4 key购买 nike

当我运行这段代码时:

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:error: (-215:Assertion failed) npoints > 0 in function'cv::drawContours'

我做错了什么?

最佳答案

根据documentation对于 findContours,该方法返回 (contours, hierarchy),所以我认为代码应该是:

contours, _ = cv2.findContours(edged,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

代替

_, contours = cv2.findContours(edged,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

关于python - 错误 : (-215:Assertion failed) npoints > 0 while working with contours using OpenCV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58290182/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com