Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the python interface to opencv2.fillPoly want as input?

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() 

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]] 

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.

like image 444
DaveA Avatar asked Jun 29 '12 23:06

DaveA


People also ask

What does cv2 fillPoly do?

fillPoly() function of OpenCV is used to draw filled polygons like rectangle, triangle, pentagon over an image. This function takes inputs an image and endpoints of Polygon and color.

How do you draw a polygon in OpenCV?

Step 1: Import cv2 and numpy. Step 2: Define the endpoints. Step 3: Define the image using zeros. Step 4: Draw the polygon using the fillpoly() function.


2 Answers

import numpy as np import cv2 import matplotlib.pyplot as plt  a3 = np.array( [[[10,10],[100,10],[100,100],[10,100]]], dtype=np.int32 ) im = np.zeros([240,320],dtype=np.uint8) cv2.fillPoly( im, a3, 255 )  plt.imshow(im) plt.show() 

result display

Check on colab.research.google.com

like image 140
themadmax Avatar answered Sep 28 '22 00:09

themadmax


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...

" Changing

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

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

like image 38
adrien g Avatar answered Sep 28 '22 02:09

adrien g