opencv2.fillPoly的python接口需要什么作为输入? [英] What does the python interface to opencv2.fillPoly want as input?

查看:293
本文介绍了opencv2.fillPoly的python接口需要什么作为输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python界面到opencv,cv2绘制多边形。我创建了一个空图像,只是一个640x480 numpy数组。我有一个要在图像上绘制的多边形(四点四边形)列表,但是,我似乎无法获得指令cv2四边形应该在哪里的正确权利,而且我不断遇到此错误:

I'm trying to draw a polygon using the python interface to opencv, cv2. I've created an empty image, just a 640x480 numpy array. I have a list of polygons (four point quadrilaterals) that I want to draw on the image, however, I can't seem to get the formate right to instruct cv2 where the quadrilaterals should be, and I keep getting this error:

OpenCV Error: Assertion failed (points.checkVector(2, CV_32S) >= 0) in fillConvexPoly, file .../OpenCV-2.4.0/modules/core/src/drawing.cpp, line 2017

我的代码包括基本上是以下内容:

My code consists of essentially the following:

binary_image = np.zeros(image.shape,dtype='int8')
for rect in expected:
    print(np.array(rect['boundary']))
    cv2.fillConvexPoly(binary_image, np.array(rect['boundary']), 255)
fig = pyplot.figure(figsize=(16, 14))
ax = fig.add_subplot(111)
ax.imshow(binary_image)
pyplot.show()

其中我期望的rect列表具有包含(x,y)点列表值的边界。代码显示:

where my list of rects in expected has the 'boundary' containing the value of a list of (x,y) points. The code prints:

[[ 91 233]
 [419 227]
 [410 324]
 [ 94 349]]

我认为这是多边形的点列表,但显然,该列表具有无效的 points.checkvector ,无论如何。谷歌搜索该错误没有发现任何用处。

I figured that this is the list of points for a polygon, but apparently that list has an invalid points.checkvector, whatever that is. A google search for that error turned up nothing useful.

推荐答案

AssertionError告诉您OpenCV需要签名的32位整数。多边形点数组应具有特定的数据类型(例如 points = numpy.array(A,dtype ='int32'))。您也可以将其强制转换为函数调用(即 my_array.astype('int32'))或作为朋友放置一次...

The AssertionError is telling you that OpenCV wants a signed, 32-bit integer. The array of polygon points should have that particular data type (e.g. points = numpy.array(A,dtype='int32') ). You could also just cast it for the function call (i.e. my_array.astype('int32') ) or as a friend put it once...


更改

<< c $ c> cv2.fillConvexPoly(binary_image,np.array(rect ['boundary ']),255)

cv2.fillConvexPoly(binary_image,np.array(rect ['boundary'],'int32'),255)

这篇关于opencv2.fillPoly的python接口需要什么作为输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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