Realization method of drawing rectangle and text annotation in opencv under python

  • 2021-07-13 05:30:06
  • OfStack

Draw a rectangle

Function call: cv2.rectangle (img, pt1, pt2, color, thickness, line_type, shift)

img: Images.

pt1: One vertex of a rectangle.

pt2: Another vertex on the diagonal of the rectangle

color: Line color (RGB) or brightness (grayscale image) (grayscale image).

thickness: The thickness of the lines that make up the rectangle. With negative values (such as CV_FILLED), the function draws a color-filled rectangle.

line_type: Type of line. See description in cvLine

shift: The number of decimal points of a coordinate point.


import cv2
colors = (0,0,255)
cv2.rectangle(img, (x, y), (x+row,y+col), colors, 5)

Text annotation

Function call: cv2.putText (img, str, origin, font, size, color, thickness)

The parameters are: picture, added text, upper left corner coordinates (integer), font, font size, color, font thickness


cv2.putText(img, 'lena', (50,150), cv2.FONT_HERSHEY_COMPLEX, 5, (0, 255, 0), 12)

Related articles: