OpenCV 3.1 drawContours'(-215)npoints> 0' [英] OpenCV 3.1 drawContours '(-215) npoints > 0'

查看:236
本文介绍了OpenCV 3.1 drawContours'(-215)npoints> 0'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从轮廓创建遮罩,但出现C ++错误.

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

使用OS X Yosemite,Python 2.7.10,OpenCV 3.1.0.

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?

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

Code is ported from OpenCV 2.x.

推荐答案

我认为您在cnt周围添加了额外的[] 应该是

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

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

因为cnt已经是array的数组,但是[cnt]是无法工作的array的数组

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

更新为上述代码

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

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)

检查文档此处

contours是图像中所有轮廓的Python列表.每个 单个轮廓是边界(x,y)坐标的Numpy数组 物体的点.

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.

这篇关于OpenCV 3.1 drawContours'(-215)npoints&gt; 0'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆