14

I'm trying to create a mask from a contour, but am getting a C++ error.

Using OS X Yosemite, Python 2.7.10, OpenCV 3.1.0.

def create_mask(img, cnt):
    '''Create a mask of the same size as the image
       based on the interior of the contour.'''
    mask = np.zeros((img.shape[0], img.shape[1]), np.uint8)
    print("create_mask, cnt=%s" % cnt)
    cv2.drawContours(mask, [cnt], 0, (0, 255, 0), -1)
    return mask

print("Creating mask from contour %s, on raw shape %s" % (page_contour, raw.shape))
page_mask = create_mask(raw, page_contour)

Output (see bottom for error):

Creating mask from contour [[ 1626.   360.]
 [ 1776.  3108.]
 [  126.  3048.]
 [  330.   486.]], on raw shape (3840, 2160, 3)
create_mask, cnt=[[ 1626.   360.]
 [ 1776.  3108.]
 [  126.  3048.]
 [  330.   486.]]
OpenCV Error: Assertion failed (npoints > 0) in drawContours, file /tmp/opencv320160309-92782-1efch74/opencv-3.1.0/modules/imgproc/src/drawing.cpp, line 2380
Traceback (most recent call last):
  File "./books.py", line 209, in <module>
    page_mask = create_mask(raw, page_contour)
  File "./books.py", line 123, in create_mask
    cv2.drawContours(mask, [cnt], 0, (0, 255, 0), -1)
cv2.error: /tmp/opencv320160309-92782-1efch74/opencv-3.1.0/modules/imgproc/src/drawing.cpp:2380: error: (-215) npoints > 0 in function drawContours

The docs say it should get an array of arrays and this is seemingly what I'm giving it. So what's wrong?

Code is ported from OpenCV 2.x.

0

7 Answers 7

25

I think you are adding extra [] around cnt it should be

cv2.drawContours(mask, cnt, 0, (0, 255, 0), -1)

as cnt is already array of array but [cnt] is array of array of arrays which won't work


Update to the above code

you should convert your contour to numpy array first

ctr = numpy.array(cnt).reshape((-1,1,2)).astype(numpy.int32)
cv2.drawContours(mask, [ctr], 0, (0, 255, 0), -1)

check documentation here

contours is a Python list of all the contours in the image. Each individual contour is a Numpy array of (x,y) coordinates of boundary points of the object.

3
  • 2
    Unfortunately this gives the same error. Turning cnt into None avoids the crash but removes the semantics.
    – Henrik
    Mar 9, 2016 at 21:34
  • Hi I got the above error, when removed brackets from cnts, I now get : <built-in function drawContours> returned NULL without setting an error , do you have any idea how to solve this?
    – El_1988
    Sep 3, 2019 at 15:05
  • Thank you so much! I was getting this because I did not know I had to convert to int. What a cryptic error message. I guess it makes sense in hindsight, but I wasted so much time one this
    – Marcel
    Mar 8, 2022 at 4:19
19

For me this worked. But I'm not sure why.

cv2.drawContours(mask, [cnt.astype(int)], 0, (0, 255, 0), -1)

When you get an array of rounded floats from findContours, drawContours doesn't complain. But when I construct a similar (4,2) array of floats myself, it complains.

2
  • 3
    This is true! But why? Anyone more experienced with numpy who can explain this?
    – Thomas
    Apr 25, 2019 at 16:33
  • uint64 and int32 also works. The latter will take up the least space, compared to the above choice of int as well.
    – makeworld
    Mar 31, 2020 at 18:42
5

You might have commited mistake while finding the contours. Contour is the second value returned by findContours() function as the docs say

im2, contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

So the following code will not work

cnt = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

This might solve your problem.

1
  • 1
    In version 4.4.0 of cv2, the first parameter is the contours. The cv2.findContours and cv2.normalize break backwards compatibility with OpenCV 2.4.X. Oct 14, 2020 at 13:47
1

If you just use this, it will work...

ctr = np.array(cnt).reshape((-1,1,2)).astype(np.int32)
cv2.drawContours(mask, [ctr], -1, 255, -1)
0

Its a numpy array so it will not work this way. Make sure you add this to your code np.array(loop variable).reshape((-1,1,2)).astype(np.int32) This works well.

0

This error will also occur if the data type of the numpy array passed into the drwaContours function is not int64. This data type error might occur if you apply a transformation to the points in the contour, which changes their dtype. To correct for this error, be sure to convert the contour data type to int64.

new_contour = old_contour_wrong_dtype.dtype('int64')
-2

its hierarchy, contours so:

contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.