【问题标题】:OpenCV 3.1 drawContours '(-215) npoints > 0'OpenCV 3.1 drawContours '(-215) npoints > 0'
【发布时间】:2016-06-24 10:48:37
【问题描述】:

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

使用 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)

输出(错误见底部):

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

docs 说它应该得到一个数组数组,这似乎就是我给它的。那怎么了?

代码从 OpenCV 2.x 移植。

【问题讨论】:

    标签: python c++ opencv


    【解决方案1】:

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

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

    因为cnt 已经是数组的数组,但[cnt] 是数组的数组,它不起作用


    更新上述代码

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

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

    查看文档here

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

    【讨论】:

    • 不幸的是,这给出了同样的错误。将 cnt 变为 None 可以避免崩溃,但会删除语义。
    • 您好,我收到了上述错误,当从cnts中删除括号时,我现在得到:返回NULL而没有设置错误,您知道如何解决这个问题吗?
    【解决方案2】:

    对我来说,这很有效。但我不知道为什么。

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

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

    【讨论】:

    • 这是真的!但为什么?任何对 numpy 更有经验的人可以解释一下吗?
    • uint64int32 也可以。与上述int 的选择相比,后者占用的空间也最少。
    【解决方案3】:

    您可能在查找轮廓时犯了错误。轮廓是findContours() 函数返回的第二个值,就像docs 说的那样

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

    所以下面的代码是行不通的

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

    这可能会解决您的问题。

    【讨论】:

    • 在 cv2 4.4.0 版本中,第一个参数是轮廓。 cv2.findContours 和 cv2.normalize 打破了与 OpenCV 2.4.X 的向后兼容性。
    【解决方案4】:

    如果你只是使用它,它会工作......

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

    【讨论】:

      【解决方案5】:

      它是一个 numpy 数组,所以它不会以这种方式工作。 确保将其添加到您的代码中 np.array(loop variable).reshape((-1,1,2)).astype(np.int32) 这很好用。

      【讨论】:

        【解决方案6】:

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

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

        【讨论】:

          猜你喜欢
          • 2017-08-20
          • 1970-01-01
          • 1970-01-01
          • 2019-11-11
          • 2019-07-11
          • 2020-06-16
          • 2022-10-17
          • 2015-03-13
          相关资源
          最近更新 更多