Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

OpenCV Python Draw Shapes and Text: Lines, Circles, Rectangles & Text Tutorial

OpenCV Python – Draw Shapes and Text

OpenCV is not only used for image processing and computer vision but also for drawing shapes and adding text to images. This is very useful for creating annotations, UI overlays, and visual indicators in images and videos.

In this tutorial, you will learn how to draw different shapes and text using OpenCV Python.


1. Why Draw Shapes and Text in OpenCV?

Drawing shapes and text helps in:

  • Image annotation
  • Object detection visualization
  • UI overlays in video streams
  • Highlighting regions in images
  • Creating graphics for AI applications

2. Import OpenCV

import cv2
import numpy as np

3. Create a Blank Image

We first create a blank canvas:

img = np.zeros((500, 500, 3), dtype=np.uint8)

4. Draw a Line

Syntax:

cv2.line(image, start_point, end_point, color, thickness)

Example:

cv2.line(img, (50, 50), (450, 50), (0, 255, 0), 3)

cv2.imshow("Line", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

5. Draw a Rectangle

Syntax:

cv2.rectangle(image, pt1, pt2, color, thickness)

Example:

cv2.rectangle(img, (100, 100), (400, 300), (255, 0, 0), 3)

cv2.imshow("Rectangle", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

6. Draw a Circle

Syntax:

cv2.circle(image, center, radius, color, thickness)

Example:

cv2.circle(img, (250, 250), 80, (0, 0, 255), -1)

cv2.imshow("Circle", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

7. Draw an Ellipse

cv2.ellipse(img, (250, 250), (100, 50), 0, 0, 360, (255, 255, 0), 2)

cv2.imshow("Ellipse", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

8. Add Text to Image

Syntax:

cv2.putText(image, text, position, font, font_scale, color, thickness)

Example:

cv2.putText(img, "OpenCV Drawing", (50, 450),
cv2.FONT_HERSHEY_SIMPLEX, 1,
(255, 255, 255), 2)

cv2.imshow("Text", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

9. Font Styles in OpenCV

OpenCV supports different fonts:

  • cv2.FONT_HERSHEY_SIMPLEX
  • cv2.FONT_HERSHEY_PLAIN
  • cv2.FONT_HERSHEY_DUPLEX
  • cv2.FONT_HERSHEY_COMPLEX

10. Combine All Shapes Example

img = np.zeros((500, 500, 3), dtype=np.uint8)

cv2.line(img, (0, 0), (500, 500), (0, 255, 0), 2)
cv2.rectangle(img, (50, 50), (450, 300), (255, 0, 0), 2)
cv2.circle(img, (250, 250), 60, (0, 0, 255), -1)
cv2.putText(img, "OpenCV Shapes", (50, 450),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)

cv2.imshow("Combined Shapes", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

11. Real-World Applications

Drawing shapes and text is used in:

  • Object detection bounding boxes
  • Face recognition systems
  • Video surveillance overlays
  • Augmented reality apps
  • UI design for computer vision tools

12. Common Mistakes

❌ Nothing appears on screen

✔ Solution:

  • Check cv2.waitKey(0)
  • Ensure correct image window functions

❌ Wrong color output

✔ OpenCV uses BGR format:

(255, 0, 0)  Blue

13. Conclusion

Drawing shapes and text in OpenCV Python is essential for building visual computer vision applications. It allows you to annotate images, highlight objects, and create interactive graphics.

Once you master this, you can build advanced systems like object tracking dashboards and AI-powered visual tools.




Post a Comment

0 Comments