Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
716 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...