OpenCV工程图轮廓错误断言失败

时间:2019-07-14 07:36:11

标签: python opencv contour cv2 opencv-drawcontour

因此,我尝试遵循有关如何扫描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)

这就是我得到的:

  

回溯(最近通话最近):文件   “ C:/用户/用户/ PycharmProjects /学习/项目/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:   错误:(-215:断言失败)reader.ptr!=函数中为NULL   'cvDrawContours'

对此有什么解决方案?之前谢谢

2 个答案:

答案 0 :(得分:0)

尝试将所有输入打印到您提供的功能。我想您可能会发现问题。 在下面的评论中让我知道。

答案 1 :(得分:0)

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

(cnts,轮廓,层次结构)= cv2.findContours(edged.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

cnts =轮廓[0] screenCnt = 0

对于轮廓中的轮廓:     #获取矩形边界轮廓     [x,y,w,h] = cv2.boundingRect(轮廓)

# 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