You can draw a triangle in OpenCV Python by following the given steps. I highly recommend you get the “Computer Vision: Models, Learning, and Inference Book” to learn Computer Vision.
Step 1
Import the OpenCV library. If OpenCV is not installed in your system then first install it using This Method.
import cv2 #cv2 is used for OpenCV library
Step 2
Now read the image from the location. In my case “C:\\AiHints” is the location and “AI.jpg” is the name of the image. Change it according to your image location and name.
image = cv2.imread("C:\\AiHints\\AI.jpg") #imread is use to read an image from a location
Step 3
Now I will draw a triangle on the image using the following code. In the code, there are three lines that connect each other with three points and make a triangle. After that, I will specify a color for the triangle (0,255,0) that is green color. In the end, 6 is the thickness of the line. You can change any value according to your requirement.
point1 = (50, 50) point2 = (50, 150) point3 = (100, 150) cv2.line(image,point1,point2,(0,255,0),6) cv2.line(image,point2,point3,(0,255,0),6) cv2.line(image,point3,point1,(0,255,0),6)
Step 4
To display the image in a specified window use ”imshow” function.
cv2.imshow("Green triangle on Image",image2)
Step 5
“waitKey(0)” will display a window until any key is pressed. “destroyAllWindows()” will destroy all the windows that we created.
cv2.waitKey(0) cv2.destroyAllWindows()