Opencv Version 4.1.0 Drawcontours Error: (-215:Assertion Failed) Npoints > 0 in Function 'Drawcontours'

OpenCV version 4.1.0 drawContours error: (-215:Assertion failed) npoints 0 in function 'drawContours'

Depending on the OpenCV version, cv2.findContours() has varying return signatures.

In OpenCV 3.4.X, cv2.findContours() returns 3 items

image, contours, hierarchy = cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]])

In OpenCV 4.1.X, cv2.findContours() returns 2 items

contours, hierarchy = cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]])

You can easily obtain the contours regardless of the version like this:

cnts = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

Since the last two values are always the same, we can further condense it into a single line using [-2:] to extract the contours from the tuple returned by cv2.findContours()

cnts, _ = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:]

Error: (-215:Assertion failed) npoints 0 while working with contours using OpenCV

According to the documentation for findContours, the method returns (contours, hierarchy), so I think the code should be:

contours, _ = cv2.findContours(edged,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

instead of

_, contours = cv2.findContours(edged,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

OpenCV Drawing Contour Error Assertion Failed

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

OpenCV Assertion failed: (-215:Assertion failed) npoints = 0 && (depth == CV_32F || depth == CV_32S)

This is doing the wrong thing:

contours = contours[0] if imutils.is_cv2() else contours[1]

imutils.is_cv2() is returning False even though it should return True. If you don't mind to remove this dependency, change to:

contours = contours[0]

I found out the reason. Probably, the tutorial you are following was published before OpenCV 4 was released. OpenCV 3 changed cv2.findContours(...) to return image, contours, hierarchy, while OpenCV 2's cv2.findContours(...) and OpenCV 4's cv2.findContours(...) return contours, hierarchy. Therefore, before OpenCV 4, it was correct to say that if you use OpenCV 2 it should be contours[0] else contours[1]. If you still want to have this "compatibility", you can change to:

contours = contours[1] if imutils.is_cv3() else contours[0]


Related Topics



Leave a reply



Submit