OpenCV绘图轮廓错误断言失败

OpenCV Drawing Contour Error Assertion Failed

所以我正在尝试按照 https://www.pyimagesearch.com/2014/09/01/build-kick-ass-mobile-document-scanner-just-5-minutes/ 中有关如何扫描文档的指南进行操作 恰好在我应该找到轮廓并将其绘制到图像的第 2 步过程中,我在 drawContour 函数

上遇到 "Assertion Failed" 错误

导游没有

screenCnt = None

所以一开始我得到的错误是 screenCnt 不存在

在我添加它之后,却得到了 Assertion Failed 尽管我使用了与指南相同的图片并尝试了另一张图片

#find contour
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]
screenCnt = None

# loop over the contours
for c in cnts:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)
    if len(approx) == 4:
        screenCnt = approx
        break

#draw contour
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)

这是我得到的:

Traceback (most recent call last): File "C:/Users/User/PycharmProjects/learn/project/app.py", line 218, in cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)

cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\drawing.cpp:2606: error: (-215:Assertion failed) reader.ptr != NULL in function 'cvDrawContours'

有什么解决办法吗?之前感谢

尝试打印您提供的函数的所有输入。我想你可能会发现不对劲。 在下面的评论中让我知道。

尝试替换 screenCnt = 0 而不是 None。让我知道。 供您参考,我提供了一小段代码:

(cnts, contours, heirarchy) = cv2.findContours(edged.copy(), 
cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

cnts = contours[0]
screenCnt = 0

for contour in contours:
    # get rectangle bounding contour
    [x,y,w,h] = cv2.boundingRect(contour)

# draw rectangle around contour on original image
#cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,255),2)

##### code added..for thresholding
for c in cnts:
    # approximate the contour
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)

    # if our approximated contour has four points, then
    # we can assume that we have found our screen
    if len(approx) == 4:
        screenCnt = approx
        break