What if you could instantly add perfect notes and shapes to any image with just a few lines of code?
Why Drawing on images (lines, rectangles, circles, text) in Computer Vision? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you want to highlight important parts of a photo by drawing shapes or adding notes directly on it. Doing this by hand with a paintbrush or marker is slow and messy, especially if you have hundreds of images.
Manually drawing on images is tiring and error-prone. You might smudge the photo, lose precision, or spend hours repeating the same task. It's hard to keep the drawings consistent and neat across many pictures.
Using code to draw lines, rectangles, circles, or text on images lets you do this quickly and perfectly every time. You can automate the process, customize colors and sizes, and add clear labels without any mess.
Open image in paint, select brush, draw shape, save imagecv2.line(img, start_point, end_point, color, thickness)
This lets you easily mark, annotate, and explain images automatically, making your work clearer and faster.
Doctors can highlight tumors on X-ray images with circles and labels automatically, helping them explain findings to patients quickly.
Manual drawing on images is slow and inconsistent.
Code lets you draw shapes and text precisely and repeatedly.
This speeds up tasks like annotation and explanation on images.
Practice
Solution
Step 1: Understand drawing functions in OpenCV
OpenCV provides specific functions for different shapes: cv2.line for lines, cv2.circle for circles, cv2.rectangle for rectangles, and cv2.putText for text.Step 2: Identify the function for rectangles
The function named cv2.rectangle is designed to draw rectangles on images.Final Answer:
cv2.rectangle -> Option BQuick Check:
Rectangle drawing = cv2.rectangle [OK]
- Confusing cv2.line with rectangle drawing
- Using cv2.circle for rectangles
- Trying to draw text with cv2.rectangle
cv2.putText controls the thickness of the text?Solution
Step 1: Review cv2.putText parameters
The function cv2.putText has parameters: fontFace (font style), fontScale (size), color (text color), and thickness (line thickness of text).Step 2: Identify thickness parameter
The thickness parameter controls how bold or thick the text lines appear.Final Answer:
thickness -> Option AQuick Check:
Text thickness = thickness parameter [OK]
- Confusing fontScale with thickness
- Using color to control thickness
- Mistaking fontFace for thickness
cv2.line(img, (10, 10), (100, 10), (0, 0, 255), 2)
Solution
Step 1: Understand BGR color format in OpenCV
OpenCV uses BGR order for colors, so (0, 0, 255) means Blue=0, Green=0, Red=255.Step 2: Identify the color from the tuple
Since only the last value (Red) is 255, the line color will be bright red.Final Answer:
Red -> Option AQuick Check:
BGR (0,0,255) = Red [OK]
- Assuming (0,0,255) is blue (RGB confusion)
- Mixing up color order
- Ignoring OpenCV's BGR format
cv2.circle(img, (50, 50), -10, (255, 0, 0), 3)
Solution
Step 1: Check circle parameters
The radius parameter must be a positive integer representing the circle size.Step 2: Identify invalid radius
The radius given is -10, which is invalid and will cause an error.Final Answer:
Negative radius is invalid -> Option DQuick Check:
Radius must be positive integer [OK]
- Using negative radius values
- Thinking thickness 3 is invalid
- Assuming center coordinates must be floats
img = cv2.imread('image.jpg')
start = (30, 30)
end = (150, 150)
# Options belowSolution
Step 1: Identify blue color in BGR
Blue in BGR is (255, 0, 0), so rectangle color must be (255, 0, 0) with thickness 4.Step 2: Check text color and position
Text "Object" should be white (255, 255, 255) and placed above rectangle at (30, 20) with reasonable font scale and thickness.Final Answer:
Blue rectangle with thickness 4 and white "Object" text above -> Option CQuick Check:
Blue = (255,0,0), white text, thickness 4 [OK]
- Mixing up BGR color order
- Using wrong thickness values
- Placing text inside rectangle instead of above
