The Python code given below draws a triangle, how can I fill it with a color inside? Or another easier way to draw a triangle in OpenCV?
pts = np.array([[100,350],[165,350],[165,240]], np.int32)
cv2.polylines(img,[pts],True,(0,255,255),2)
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.
I think what you are looking for is cv2. fillPoly , which fills the area bounded by one or more polygons. This is a simple snippet, I generate a contour of four points representing vertices of a square, then I fill the polygon with a white color.
Python - OpenCV & PyQT5 together You can draw Polylines on an image using the method polylines() of the imgproc class. Following is the syntax of this method. mat − A Mat object representing the image on which the Polylines are to be drawn.
polylines() method is used to draw a polygon on any image. Parameters: image: It is the image on which circle is to be drawn. pts: Array of polygonal curves.
You have to use cv2.fillPoly()
.
Change the second line to:
cv2.fillPoly(img, [pts], 255)
Illustration:
img = np.zeros([400, 400],dtype=np.uint8)
pts = np.array([[100,350],[165,350],[165,240]], np.int32)
cv2.fillPoly(img, [pts], 255)
cv2.imshow('Original', img)
Result:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With