Draw Polygons On An Image using OpenCV Python

In Python OpenCV Tutorial, Explained How to put text and Polylines over the image using python OpenCV cv2.polylines() function?

Syntax: cv2.polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]])
Return: Image with Polygon

Parameters:

.   @param img Image.
.   @param pts Array of polygonal curves.
.   @param isClosed Flag indicating whether the drawn polylines are closed or not. If they are closed,
.   the function draws a line from the last vertex of each curve to its first vertex.
.   @param color Polyline color.
.   @param thickness Thickness of the polyline edges.
.   @param lineType Type of the line segments. See #LineTypes
.   @param shift Number of fractional bits in the vertex coordinates.

Draw Polygon, Triangle, Line, Rectangle, Hexagon and Print Text on Image

Draw Octagon using Polyline

import cv2
import numpy as np

img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\adult-blockchain-blond-1037913.jpg"

img = cv2.imread(img_path)

img_resize = cv2.resize(img, (1280, 720))


#pts = np.array([[12,45], [34,56], [23,76], [67,98], [93,56]])

pts = np.array([[25, 70], [25, 145], 
                [75, 190], [150, 190], 
                [200, 145], [200, 70],  
                [150, 25], [75, 25]], 
               np.int32)

isClosed = True
color = (34,65,200)
thickness = 5

#polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]]) -> img
cv2.polylines(img_resize, [pts], isClosed, color, thickness)

cv2.imshow("Model Image", img_resize)
cv2.waitKey(0)
cv2.destroyAllWindows()

Draw Polygon

import cv2
import numpy as np

img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\adult-blockchain-blond-1037913.jpg"

img = cv2.imread(img_path)

img_resize = cv2.resize(img, (1280, 720))


pts = np.array([[12,45], [34,56], [23,76], [67,98], [93,56]])
isClosed = True
color = (34,65,200)
thickness = 5

#polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]]) -> img
cv2.polylines(img_resize, [pts], isClosed, color, thickness)

cv2.imshow("Model Image", img_resize)
cv2.waitKey(0)
cv2.destroyAllWindows()

Draw Line, rectangle, triangle using Polylines

import cv2
import numpy as np


img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\adult-blockchain-blond-1037913.jpg"

img = cv2.imread(img_path)

img_resize = cv2.resize(img, (1280, 720))

#pts = np.array([[100,200], [200,300], [300,400], [400,500], [600, 700]], np.int32)


rectangle = np.array([[10,5],[10,225],[50,225],[50,5]], np.int32)
triangle = np.array([[60,10],[60,200],[150,100]], np.int32)

line = np.array([[10,11],[50,225]], np.int32)


print(pts)

isClosed = False
color = (34,23,45)
thickness = 2


#polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]]) -> img
cv2.polylines(img_resize, [triangle], isClosed, color, thickness)

cv2.imshow("Polyline on Model", img_resize)

cv2.waitKey(0)
cv2.destroyAllWindows()

Draw Polylines and Print Text

import cv2
import numpy as np


img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\adult-blockchain-blond-1037913.jpg"

img = cv2.imread(img_path)

img_resize = cv2.resize(img, (1280, 720))

#pts = np.array([[100,200], [200,300], [300,400], [400,500], [600, 700]], np.int32)

pts = np.array([[25, 70], [25, 145], 
                [75, 190], [150, 190], 
                [200, 145], [200, 70],  
                [150, 25], [75, 25]], 
               np.int32)
#pts = pts.reshape((-1,1,2))

print(pts)

isClosed = True
color = (34,23,45)
thickness = 2

#polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]]) -> img
cv2.polylines(img_resize, [pts], isClosed, color, thickness)

######Print text

#putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) -> img
cv2.putText(img_resize, "Octagon", (50, 100), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 1, (200,20,200), 2)

#Show image
cv2.imshow("Polyline on Model", img_resize)

cv2.waitKey(0)
cv2.destroyAllWindows()

Leave a Reply