1. Home
  2. Docs
  3. Computer Vision
  4. Introduction to OpenCV
  5. Drawing on Images – Basic...
  6. Drawing Text and Polygon

Drawing Text and Polygon

Drawing Text

cv2.putText(image, text, org, font, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])

Parameters:
image: It is the image on which text is to be drawn.
text: Text string to be drawn.
org: It is the coordinates of the bottom-left corner of the text string in the image. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value).
font: It denotes the font type. Some of font types are FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, , etc.
fontScale: Font scale factor that is multiplied by the font-specific base size.
color: It is the color of text string to be drawn. For BGR, we pass a tuple. eg: (255, 0, 0) for blue color.
thickness: It is the thickness of the line in px.
lineType: This is an optional parameter.It gives the type of the line to be used.
bottomLeftOrigin: This is an optional parameter. When it is true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.

Return Value: It returns an image.

font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(blank_img, text="vision", org=(10,500), fontface = font, fontScale = 3, color = (255.255,255), thickness = 3, lineType=cv2.LINE_AA)
plt.imshow(blank_img)
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(blank_img, text="vision", org=(100,400), fontface = font, fontScale = 3, color = (255.255,255), thickness = 3, lineType=cv2.LINE_AA, bottomLeftOrigin=False)
plt.imshow(blank_img)
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(blank_img, text="vision", org=(200,100), fontface = font, fontScale = 3, color = (255.255,255), thickness = 3, lineType=cv2.LINE_AA, bottomLeftOrigin=True)
plt.imshow(blank_img)

cv2.LINE_4

  • The line is drawn by connecting pixels that share an edge (horizontal or vertical).
  • It can result in more visible “steps” or jagged edges when the line is not perfectly horizontal or vertical.

cv2.LINE_8

  • The line is drawn by connecting pixels that share an edge or a corner (horizontal, vertical, or diagonal).
  • This results in smoother lines compared to cv2.LINE_4, but without anti-aliasing.

cv2.LINE_AA

  • The line is drawn using anti-aliasing, which means it blends the line with the background to create a smoother appearance.
  • This results in the smoothest lines, especially noticeable on non-horizontal and non-vertical lines.
import cv2
import numpy as np

#create a blank image
image = np.zeros((400, 400, 3), dtype=np.units)

#Draw lines with different line types
cv2.line(image, (50, 50), (350, 25), (255, 0, 0), 1, cv2.LINE_4)
cv2.line(image, (50, 50), (350, 25), (255, 0, 0), 1, cv2.LINE_8)
cv2.line(image, (50, 50), (350, 25), (255, 0, 0), 1, cv2.LINE_AA)

# Display the image
cv2.imshow('Lines', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Drawing polygon

cv2.polylines(image, [pts], isClosed, color, thickness)
  • img: It is the image on which we are going to draw
  • pts: Coordinates of the vertices of the polygon
  • isClosed: Specifies whether the polygon is closed or not
  • color: Specifies the color of the polygon
  • thickness: It is the thickness of the polyline edges.
  • Return Value: Returns an image with polygon drawn on it.

Creating blank image

blank_img1 = np.zeros(shape=(512,512,3),dtype=np.int32)
plt.imshow(blank_img1)
height, width = blank_img1.shape[:2]
width
changed_height, changed_width = int(height/2), int(width/2)
pts = np.array([[0, changed_height], [changed_width, height], [width, changed_height], [changed_width, 0]])

#creating the polygon using the polylines function
cv2.polylines(blank_img1, [pts], isClosed = True, color=(0,0,255), thickness=10)

#Displaying the image with the polygon drawn on it
plt.imshow(blank_img1)

Loading

Views: 1

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments