Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV cv2.fillPoly vs. cv2.fillConvexPoly: expected data type for array of polygon vertices?

Tags:

python

opencv

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(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.fillConvexPoly that does not have dtype numpy.int32. Do cv2.fillPoly and cv2.fillConvexPoly expect different data types for their second argument? If so, what should I be using for cv2.fillPoly?

like image 234
wil Avatar asked Jul 10 '13 23:07

wil


1 Answers

cv2.fillPoly and cv2.fillConvexPoly use different data types for their point arrays, because fillConvexPoly draws only one polygon and fillPoly draws a (python) list of them. Thus,

cv2.fillConvexPoly(ar, triangle, 1)
cv2.fillPoly(ar, [triangle], 1)

are the correct ways to call these two methods. If you had square and hexagon point arrays, you could use

cv2.fillPoly(ar, [triangle, square, hexagon], 1)

to draw all three.

like image 184
wil Avatar answered Sep 21 '22 11:09

wil