Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV 3.1 drawContours '(-215) npoints > 0'

Tags:

c++

python

opencv

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.

like image 598
Henrik Avatar asked Mar 09 '16 20:03

Henrik


People also ask

What does cv2 drawContours do?

The drawContours function: After finding the contours and storing the coordinate points (x, y)of the contour line in an array, we can use these points to draw the contour lines on the image. We use the drawContours function of OpenCV to do the same. image- Destination image.

What is cv2 findcontours?

OpenCV has findContour() function that helps in extracting the contours from the image. It works best on binary images, so we should first apply thresholding techniques, Sobel edges, etc. Below is the code for finding contours – import cv2. import numpy as np.

How do you draw contours in OpenCV?

To draw the contours, cv. drawContours function is used. It can also be used to draw any shape provided you have its boundary points. Its first argument is source image, second argument is the contours which should be passed as a Python list, third argument is index of contours (useful when drawing individual contour.


2 Answers

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.

like image 148
Thesane Avatar answered Oct 01 '22 03:10

Thesane


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.

like image 40
Antonvh Avatar answered Oct 01 '22 01:10

Antonvh