Python OpenCV cv2.fillPoly 与 cv2.fillConvexPoly:多边形顶点数组的预期数据类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17582849/
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
OpenCV cv2.fillPoly vs. cv2.fillConvexPoly: expected data type for array of polygon vertices?
提问by wil
I have the following code:
我有以下代码:
import cv2
import numpy
ar = numpy.zeros((10,10))
triangle = numpy.array([ [1,3], [4,8], [1,9] ], numpy.int32)
If I use cv2.fillConvexPoly like so:
如果我像这样使用 cv2.fillConvexPoly:
cv2.fillConvexPoly(ar, triangle, 1)
then the results are as expected. If, however, I try:
那么结果正如预期的那样。但是,如果我尝试:
cv2.fillPoly(ar, triangle, 1)
then I get a failed assertion. This seems to be identical to the assertion that fails if I use a numpy array for cv2.fillConvexPolythat does not have dtype numpy.int32. Do cv2.fillPolyand cv2.fillConvexPolyexpect different data types for their second argument? If so, what should I be using for cv2.fillPoly?
然后我得到一个失败的断言。如果我使用cv2.fillConvexPoly没有 dtype的 numpy 数组,这似乎与失败的断言相同numpy.int32。他们的第二个参数是否cv2.fillPoly并cv2.fillConvexPoly期望不同的数据类型?如果是这样,我应该使用cv2.fillPoly什么?
采纳答案by wil
cv2.fillPolyand cv2.fillConvexPolyuse different data types for their point arrays, because fillConvexPolydraws only one polygon and fillPolydraws a (python) list of them. Thus,
cv2.fillPoly并cv2.fillConvexPoly为其点数组使用不同的数据类型,因为fillConvexPoly只绘制一个多边形并fillPoly绘制它们的(python)列表。因此,
cv2.fillConvexPoly(ar, triangle, 1)
cv2.fillPoly(ar, [triangle], 1)
are the correct ways to call these two methods. If you had squareand hexagonpoint arrays, you could use
是调用这两个方法的正确方法。如果你有square和hexagon点数组,你可以使用
cv2.fillPoly(ar, [triangle, square, hexagon], 1)
to draw all three.
绘制所有三个。

