What is the python interface for opencv2.fillPoly as input?

I am trying to draw a polygon using the python interface for opencv, cv2. I created an empty image, just a 640x480 array. I have a list of polygons (four-point quadrangles) that I want to draw on the image, however I cannot get the formula to correctly indicate cv2 where the quadrangles should be located, and I continue to get 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 mainly of 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 lines pending has a "border" containing the value of the list of (x, y) points. The code prints:

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

I realized that this is a list of points for the polygon, but apparently this list has invalid points.checkvector , whatever that is. Searching for this error in google did not bring anything useful.

+6
source share
3 answers

AssertionError tells you that OpenCV wants to have a 32-bit integer. An array of polygon points must have that particular data type (e.g. points = numpy.array (A, dtype = 'int32')). You can also just pass it in to a function call (i.e. My_array.astype ('int32')) or as a friend place it once ...

" Change

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

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

+4
source
 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() 

display result

+6
source

I tried in opencv 2.4.2 and python 2.7. From C ++ interface

 void fillPoly(Mat& img, const Point** pts, const int* npts, int ncontours, const Scalar& color, int lineType=8, int shift=0, Point offset=Point() ) 

we know that pts is an array of points array, so you should change this as

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

add [] to rect ['border'] .

+4
source

Source: https://habr.com/ru/post/919366/


All Articles