Python 断言错误(-215)npoints>=0,从验证码图像提取字母时出错

Python 断言错误(-215)npoints>=0,从验证码图像提取字母时出错,python,opencv,Python,Opencv,在制作从验证码图像中提取字母的程序时,我遇到了一个错误: cv2.error: OpenCV(4.0.0) /io/opencv/modules/imgproc/src/shapedescr.cpp:741: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'pointSetBoundingRect' 在尝试了很多之后,我知道错误

在制作从验证码图像中提取字母的程序时,我遇到了一个错误:

cv2.error: OpenCV(4.0.0) /io/opencv/modules/imgproc/src/shapedescr.cpp:741: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'pointSetBoundingRect'
在尝试了很多之后,我知道错误是因为在我的代码中,我使用的图像生成了一个包含负数的矩阵,但实际上在pointSetBoundingRect函数中,它断言npoints>=0。我尝试使用不同的图像,但都产生了相同的结果

这是我的密码:

import cv2
import imutils

img = cv2.imread('cap2.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.copyMakeBorder(gray, 8, 8, 8, 8, cv2.BORDER_REPLICATE)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if imutils.is_cv2() else contours[1]

for contour in contours:
    (x,y,w,h) = cv2.boundingRect(contour)
    print ((x,y,w,h))
请帮忙!如何使从图像自动生成的矩阵具有正值。 或者请告诉我错误是否是其他原因

这是我在上面代码中使用的验证码图像

在opencv 4.0中,cv2.findContours返回2个值。所以可能是这样

 contours,hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)