Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill a polyline in OpenCV?

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)
like image 999
user70 Avatar asked Jul 01 '18 09:07

user70


People also ask

How do you fill 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.

How do you fill contours in OpenCV?

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.

How do you create a polyline in OpenCV Python?

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.

How do you draw a polygon with OpenCV?

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.


1 Answers

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:

enter image description here

like image 137
Jeru Luke Avatar answered Sep 26 '22 01:09

Jeru Luke