So I am trying to follow guide on how to scan a document in https://www.pyimagesearch.com/2014/09/01/build-kick-ass-mobile-document-scanner-just-5-minutes/ exactly on the step 2 process where i am supposed to find the contour and draw it to the image, i got an “Assertion Failed” error on drawContour function
The guide did not have
screenCnt = None
so at first i got Error like screenCnt did not exist
after i add it, got Assertion Failed instead even though i used the same image as the guide and tried another image too
#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)
Here is what i got :
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:projectsopencv-pythonopencvmodulesimgprocsrcdrawing.cpp:2606: error: (-215:Assertion failed) reader.ptr != NULL in function ‘cvDrawContours’
Any solution for this ? Thanks before
Advertisement
Answer
Try replacing screenCnt = 0
instead of None
. and let me know.
For your reference, I’m providing small code snippet:
(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