1

因此,我正在尝试遵循有关如何在 https://www.pyimagesearch.com/2014/09/01/build-kick-ass-mobile-document-scanner-just-5-minutes/ 中扫描文档 的指南在我应该找到轮廓并将其绘制到图像的第 2 步过程中,在 drawContour 函数上出现“断言失败”错误

导游没有

screenCnt = 无

所以起初我得到了错误,比如 screenCnt 不存在

在我添加它之后,即使我使用了与指南相同的图像并尝试了另一个图像,也得到了断言失败

#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(最近一次调用最后):文件“C:/Users/User/PycharmProjects/learn/project/app.py”,第 218 行,在 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'

有什么解决方案吗?之前谢谢

4

2 回答 2

0

尝试打印您提供的功能的所有输入。我想你可能会发现一些不对劲的地方。请在下面的评论中告诉我。

于 2019-07-14T07:39:39.683 回答
0

尝试替换 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
于 2019-07-14T07:52:50.393 回答