Python 3.x cv2.error:OpenCV(4.0.0)/io/OpenCV/modules/imgproc/src/shapedscr.cpp:272

Python 3.x cv2.error:OpenCV(4.0.0)/io/OpenCV/modules/imgproc/src/shapedscr.cpp:272,python-3.x,Python 3.x,这是我的python脚本: while True: text = "" img = cam.read()[1] img = cv2.flip(img, 1) imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) dst = cv2.calcBackProject([imgHSV], [0, 1], hist, [0, 180, 0, 256], 1) disc = cv2.getStructuringEleme

这是我的python脚本:

while True:
    text = ""
    img = cam.read()[1]
    img = cv2.flip(img, 1)
    imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    dst = cv2.calcBackProject([imgHSV], [0, 1], hist, [0, 180, 0, 256], 1)
    disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(10,10))
    cv2.filter2D(dst,-1,disc,dst)
    blur = cv2.GaussianBlur(dst, (11,11), 0)
    blur = cv2.medianBlur(blur, 15)
    thresh = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
    thresh = cv2.merge((thresh,thresh,thresh))
    thresh = cv2.cvtColor(thresh, cv2.COLOR_BGR2GRAY)
    thresh = thresh[y:y+h, x:x+w]
    contours = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1]
    if len(contours) > 0:
        contour = max(contours, key = cv2.contourArea)
        if cv2.contourArea(contour) > 10000:
            x1, y1, w1, h1 = cv2.boundingRect(contour)
            save_img = thresh[y1:y1+h1, x1:x1+w1]
这段代码在另一个系统上正常工作,但当我在我的系统中运行它时, 它显示以下错误:

cv2.error:OpenCV(4.0.0)/io/OpenCV/modules/imgproc/src/shapedscr.cpp:272:error:(-215:断言失败)函数“contourArea”中的npoints>=0&(depth==CV|32F | depth==cv32s)

这可能是由以下脚本引起的:

contour = max(contours, key = cv2.contourArea)
我正在使用ubuntu 18.02和opencv 4.0。。。
这是我们项目的一部分,请帮助。

发生此问题的原因是
cv2.findContours
在opencv中已从V3.X更改为V4.0。 所以在V3.X中它曾经是

findContours(图像、模式、方法[、轮廓[、层次[、偏移]])->图像、轮廓、层次

返回了三个对象

和V4.0
findContours(图像、模式、方法[、轮廓[、层次[、偏移]])->轮廓、层次

返回两个对象

所以你的代码是

contours=cv2.findContours(thresh.copy()、cv2.RETR\u TREE、cv2.CHAIN\u APPROX\u NONE)[0]

如果你想得到轮廓