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

When I run this code:

import cv2

image = cv2.imread('screenshoot10.jpg')
cv2.imshow('input image', image)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

edged = cv2.Canny(gray, 30, 200)
cv2.imshow('canny edges', edged)

_, contours = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.imshow('canny edges after contouring', edged)

print(contours)
print('Numbers of contours found=', len(contours))

cv2.drawContours(image, contours, -1, (0, 255, 0), 3)
cv2.imshow('contours', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

I am getting this error:

OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\imgproc\src\drawing.cpp:2509: error: (-215:Assertion failed) npoints > 0 in function 'cv::drawContours'

What am I doing wrong?



Solution 1:[1]

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)

Solution 2:[2]

Depending on the OpenCV version, cv2.findContours() has varying return signatures. In v3.4.X, three items are returned.

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

In v2.X and v4.1.X, two items are returned.

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]
for c in cnts:
    ...

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:]

Solution 3:[3]

Because this question shows up on top when looking for Error: (-215:Assertion failed) npoints > 0 while working with contours using OpenCV, I want to point out another possible reason as to why one can get this error:

I was using the BoxPoints function after doing minAreaRect to get a rotated rectangle around a contour. I hadn't done np.int0 to its output (to convert the returned array to integer values) before passing it to drawContours. This fixed it:

rect = cv2.minAreaRect(cnt)
box = cv2.cv.BoxPoints(rect) 
box = np.int0(box) # convert to integer values
cv2.drawContours(im,[box],0,(0,0,255),2)

Solution 4:[4]

All the solutions provided here are either fixed for a particular version or they do not store all the values returned from cv2.findContours(). You can store all the values returned from every tuple of the function independent of the version of cv2.

The following snippet will work irrespective of the OpenCV version installed in your system/environment and will also store all the tuples in individual variables.

First, get the version of OpenCV installed (we don't want the entire version just the major number either 3 or 4) :

import cv2
version = cv2.__version__[0]

Based on the version either of the following two statements will be executed and the corresponding variables will be populated:

if version == '4':
    contours, hierarchy = cv2.findContours(binary_image, cv2.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

elif version == '3':
    img, contours, hierarchy = cv2.findContours(binary_image, cv2.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

The contours returned from the function in either scenarios will be stored in contours.

Note: the above snippet is written assuming either version 3 or 4 of OpenCV is installed. For older versions, please refer to the documentation or update to the latest.

If you are using OpenCV version 4, and wondering what the first variable returned by cv2.findContours() of version 3 is; its just the same as the input image (in this case binary_image).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3
Solution 4