I am trying to use the fillpoly function to fill two polygons at once (with a triangle and a trapezium). Please find the below code.
# Import necessary libraries import cv2 import numpy as np # Read an image img = np.zeros((500,500)) # Define an array of endpoints of triangle points = np.array([[[160, 130], [350, 130], [250, 300]],[[160, 30], [50, 30], [80, 130],[100, 130]]],dtype=object) # Use fillPoly() function and give input as # image, end points,color of polygon # Here color of polygon will blue for i in range(len(points)): cv2.fillPoly(img, pts=[points[i]], color=(255, 0, 0)) #cv2.fillPoly(img, pts=[points[1]], color=(255, 0, 0)) # Displaying the image cv2.imshow("Triangle", img) # wait for the user to press any key to # exit window cv2.waitKey(0) # Closing all open windows cv2.destroyAllWindows()
However, the output gives me an error. Please find it below.
/var/folders/hh/phtw2ldx7gj88krjgwdbz9680000gn/T/ipykernel_1755/1373141779.py:9: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray. points = np.array([[[160, 130], [350, 130], [250, 300]],[[160, 30], [50, 30], [80, 130],[100, 130]]]) --------------------------------------------------------------------------- error Traceback (most recent call last) Input In [35], in <cell line: 14>() 11 # Use fillPoly() function and give input as 12 # image, end points,color of polygon 13 # Here color of polygon will blue 14 for i in range(len(points)): ---> 15 cv2.fillPoly(img, pts=[points[i]], color=(255, 0, 0)) 16 #cv2.fillPoly(img, pts=[points[1]], color=(255, 0, 0)) 17 18 19 # Displaying the image 20 cv2.imshow("Triangle", img) error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'fillPoly' > Overload resolution failed: > - Can't parse 'pts'. Sequence item with index 0 has a wrong type > - Can't parse 'pts'. Sequence item with index 0 has a wrong type
It gives an error due to the different number in coordinates of the polygones (3 and 4 respectively).
Would anyone be able to help me in this regards please.
Thanks & Best Regards
Schroter Michael
Advertisement
Answer
- Your image needs to have 3 channels (
np.zeros((500, 500, 3))
) instead of 1. - Your coordinates need to be
np.int32
.
import cv2 import numpy as np img = np.zeros((500, 500, 3)) # Define an array of points. points = [ [[160, 130], [350, 130], [250, 300]], [[160, 30], [50, 30], [80, 130], [100, 130]], ] for poly in points: cv2.fillPoly(img, np.array([poly], dtype=np.int32), (255, 0, 0)) # Displaying the image cv2.imshow("Triangle", img) # wait for the user to press any key to # exit window cv2.waitKey(0) # Closing all open windows cv2.destroyAllWindows()