Drawing Shapes and Text in OpenCV
Drawing Shapes and Text in OpenCV
Drawing shapes and text in OpenCV is an essential technique used to visualize results in computer vision applications. Whether you are building a face detection system or an object tracking model, drawing shapes and labels helps in understanding and presenting outputs clearly.
In this lesson, you will learn how to draw lines, rectangles, circles, and add text using OpenCV.
What is Drawing Shapes and Text in OpenCV?
Drawing shapes and text in OpenCV involves:
- Highlighting objects in images
- Adding labels to detected objects
- Visualizing predictions
- Annotating images for analysis
These operations are widely used in AI and computer vision systems.
Drawing Basic Shapes in OpenCV
Drawing a Line
import cv2
image = cv2.imread("image.jpg")
cv2.line(image, (0, 0), (200, 200), (255, 0, 0), 2)
Drawing a Rectangle
cv2.rectangle(image, (50, 50), (200, 200), (0, 255, 0), 2)
Drawing a Circle
cv2.circle(image, (150, 150), 50, (0, 0, 255), 2)
These shapes are commonly used to mark detected objects.
Adding Text to Image
Drawing shapes and text in OpenCV also includes labeling:
cv2.putText(image, "Object Detected", (50, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1,
(255, 255, 255), 2)
This is useful in real-time AI systems.
Drawing on Video Frames
You can apply the same drawing functions on video frames:
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.rectangle(frame, (50, 50), (200, 200), (0, 255, 0), 2)
cv2.imshow("Video", frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
Customizing Drawing Properties
You can customize:
- Color (BGR format)
- Thickness
- Font style
- Position
Example:
cv2.putText(image, "AI Vision", (100, 100),
cv2.FONT_HERSHEY_COMPLEX, 1,
(0, 255, 255), 3)
Why Drawing Shapes and Text in OpenCV is Important
Drawing shapes and text in OpenCV helps you:
- Visualize detection results
- Debug computer vision models
- Build user-friendly applications
- Highlight important regions in images
It is widely used in applications like surveillance, object detection, and augmented reality.
Real-World Applications
- Drawing bounding boxes in object detection
- Labeling faces in recognition systems
- Highlighting regions in medical imaging
- Adding overlays in AR/VR systems
Internal Resource
Click here for more free courses
FAQs
What is drawing in OpenCV?
Drawing in OpenCV means creating shapes and text on images for visualization.
Why is drawing important in computer vision?
It helps display results clearly and improves understanding.
Can I draw on videos using OpenCV?
Yes, you can draw shapes and text on video frames in real time.
What is cv2.putText used for?
It is used to add text labels to images.
Is drawing used in AI models?
Yes, it is used to visualize predictions and outputs.



