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

新手上路,请多包涵

我正在尝试从轮廓创建蒙版,但出现 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

文档 说它应该得到一个数组数组,这似乎是我给它的。那么有什么问题呢?

代码从 OpenCV 2.x 移植。

原文由 Henrik 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 774
1 个回答

如果传入 drwaContours 函数的 numpy 数组的数据类型不是 int64 ,也会出现此错误。如果您对等高线中的点应用变换,从而更改它们的 dtype,则可能会发生此数据类型错误。要更正此错误,请务必将轮廓数据类型转换为 int64。

 new_contour = old_contour_wrong_dtype.dtype('int64')

原文由 alluppercase 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题