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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35902139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:07:33  来源:igfitidea点击:

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

pythonc++opencv

提问by Henrik

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

我正在尝试从轮廓创建蒙版,但遇到 C++ 错误。

Using OS X Yosemite, Python 2.7.10, OpenCV 3.1.0.

使用 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 docssay 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.

代码是从 OpenCV 2.x 移植的。

回答by Thesane

I think you are adding extra []around cntit should be

我认为你 应该[]cnt它周围添加额外的

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

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

cnt已经是阵列的阵列,但[cnt]是数组的数组的数组,它不会工作



Update to the above code

更新上面的代码

you should convert your contour to numpy array first

您应该先将轮廓转换为 numpy 数组

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.

轮廓是图像中所有轮廓的 Python 列表。每个单独的轮廓是对象边界点的 (x,y) 坐标的 Numpy 数组。

回答by Antonvh

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

对我来说这有效。但我不确定为什么。

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

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.

当您从 findContours 获得一组圆形浮点数时,drawContours 不会抱怨。但是当我自己构建一个类似的 (4,2) 浮点数组时,它会抱怨。

回答by Prateek Gupta

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

您可能在查找轮廓时犯了错误。Contour 是findContours()函数返回的第二个值,如文档所说

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.

这可能会解决您的问题。

回答by Par bas

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)

回答by Mondara Thotage

its hierarchy, contours so:

它的层次结构,轮廓如下:

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

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