Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triangle Filling in opencv

We have Rectangle fillings , circle and ellipse filling in opencv, but can anyone say how to fill a triangle in an image using opencv ,python.

like image 960
Hariprasad Avatar asked Aug 16 '18 10:08

Hariprasad


People also ask

How do you make a triangle in OpenCV?

Approach: Create a black window with three color channels with resolution 400 x 300. Draw three lines which are passing through the given points using the inbuilt line function of the OpenCV. It will create a triangle on the black window.

How do I 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 identify an OpenCV triangle?

Python - OpenCV & PyQT5 together To detect a triangle in an image, we first detect all the contours in the image. Then we loop over all the contours. Find the approximate contour for each of the contours. If the number of vertex points in the approximate contour is 3, then draw the contour and set it as a triangle.

What does cv2 Line_aa do?

cv. LINE_AA gives anti-aliased line which looks great for curves.


2 Answers

You can draw a polygon and then fill it, like @ZdaR said.

# draw a triangle
vertices = np.array([[480, 400], [250, 650], [600, 650]], np.int32)
pts = vertices.reshape((-1, 1, 2))
cv2.polylines(img_rgb, [pts], isClosed=True, color=(0, 0, 255), thickness=20)

# fill it
cv2.fillPoly(img_rgb, [pts], color=(0, 0, 255))
# show it
plt.imshow(img_rgb)
like image 90
mirkancal Avatar answered Nov 15 '22 19:11

mirkancal


The simplest solution of filling a triangle shape is using draw contour function in OpenCV. Assuming we know the three points of the triangle as "pt1", "pt2" and "pt3":

import cv2
import numpy as np

image = np.ones((300, 300, 3), np.uint8) * 255

pt1 = (150, 100)
pt2 = (100, 200)
pt3 = (200, 200)

cv2.circle(image, pt1, 2, (0,0,255), -1)
cv2.circle(image, pt2, 2, (0,0,255), -1)
cv2.circle(image, pt3, 2, (0,0,255), -1)

We can put the three points into an array and draw as a contour:

triangle_cnt = np.array( [pt1, pt2, pt3] )

cv2.drawContours(image, [triangle_cnt], 0, (0,255,0), -1)

cv2.imshow("image", image)
cv2.waitKey()

Here is the output image. Cheers. Triangle Filling

like image 41
Howard GENG Avatar answered Nov 15 '22 20:11

Howard GENG