0
0
Computer Visionml~20 mins

Drawing on images (lines, rectangles, circles, text) in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Drawing on images (lines, rectangles, circles, text)
Problem:You want to learn how to draw basic shapes and text on images using computer vision tools. Currently, you can load and display images but do not know how to add drawings like lines, rectangles, circles, or text on them.
Current Metrics:No drawings are present on images; only original images are shown.
Issue:Lack of knowledge on how to use drawing functions to annotate images for visualization or data augmentation.
Your Task
Learn to draw a blue line, a green rectangle, a red circle, and add white text on a sample image. The output image should clearly show all these drawings.
Use OpenCV (cv2) library in Python.
Drawings must be visible and correctly positioned.
Use simple, clear colors and thickness for shapes and text.
Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Solution
Computer Vision
import cv2
import numpy as np

# Create a blank black image
image = np.zeros((400, 600, 3), dtype=np.uint8)

# Draw a blue line from top-left to bottom-right
cv2.line(image, (50, 50), (550, 350), (255, 0, 0), thickness=3)

# Draw a green rectangle
cv2.rectangle(image, (100, 100), (300, 250), (0, 255, 0), thickness=4)

# Draw a red circle
cv2.circle(image, (400, 300), 50, (0, 0, 255), thickness=5)

# Add white text
cv2.putText(image, 'Hello OpenCV', (150, 380), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)

# Show the image
cv2.imshow('Drawing on Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Created a blank image to draw on.
Added a blue line using cv2.line().
Added a green rectangle using cv2.rectangle().
Added a red circle using cv2.circle().
Added white text using cv2.putText().
Displayed the final image with all drawings.
Results Interpretation

Before: Only a blank black image was shown with no drawings.

After: The image now contains a blue line, green rectangle, red circle, and white text, demonstrating how to annotate images.

Drawing shapes and text on images is essential for visualization and annotation in computer vision. OpenCV provides simple functions to add these elements clearly and efficiently.
Bonus Experiment
Try drawing multiple shapes with different colors and thicknesses on a real photo loaded from disk.
💡 Hint
Load an image using cv2.imread(), then apply drawing functions at different positions. Experiment with font styles and sizes for text.