Bird
Raised Fist0
Computer Visionml~5 mins

Drawing on images (lines, rectangles, circles, text) in Computer Vision

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Drawing on images helps highlight or mark important parts, making it easier to understand or explain what the image shows.
You want to show where an object is in a photo by drawing a box around it.
You need to add labels or notes directly on an image for better explanation.
You want to connect points or show paths by drawing lines on a map or diagram.
You want to highlight a specific area with a circle to draw attention.
You want to create simple graphics or annotations on images for reports or presentations.
Syntax
Computer Vision
cv2.line(image, start_point, end_point, color, thickness)
cv2.rectangle(image, top_left_corner, bottom_right_corner, color, thickness)
cv2.circle(image, center, radius, color, thickness)
cv2.putText(image, text, position, font, font_scale, color, thickness, lineType=cv2.LINE_AA)
All drawing functions change the image directly (in-place).
Color is usually in BGR format (Blue, Green, Red) when using OpenCV.
Examples
Draws a blue line from point (10,10) to (100,10) with thickness 2.
Computer Vision
cv2.line(img, (10, 10), (100, 10), (255, 0, 0), 2)
Draws a green rectangle with top-left corner at (50,50) and bottom-right corner at (150,150).
Computer Vision
cv2.rectangle(img, (50, 50), (150, 150), (0, 255, 0), 3)
Draws a filled red circle centered at (200,200) with radius 40.
Computer Vision
cv2.circle(img, (200, 200), 40, (0, 0, 255), -1)
Writes white text 'Hello' at position (10,250) with font size 1 and thickness 2.
Computer Vision
cv2.putText(img, 'Hello', (10, 250), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
Sample Model
This program creates a black image and draws a blue line, green rectangle, red filled circle, and white text on it. Then it saves the image as 'output_image.png'.
Computer Vision
import cv2
import numpy as np

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

# Draw a blue line
cv2.line(img, (50, 50), (350, 50), (255, 0, 0), 3)

# Draw a green rectangle
cv2.rectangle(img, (50, 100), (350, 200), (0, 255, 0), 4)

# Draw a filled red circle
cv2.circle(img, (200, 250), 40, (0, 0, 255), -1)

# Put white text
cv2.putText(img, 'Test Image', (100, 290), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)

# Save the image to file
cv2.imwrite('output_image.png', img)

# Print confirmation
print('Drawing complete and image saved as output_image.png')
OutputSuccess
Important Notes
Coordinates are in (x, y) format, where x is horizontal and y is vertical position.
Thickness -1 means the shape is filled completely.
Use cv2.imwrite() to save the image after drawing.
Summary
Drawing on images helps visually highlight or label parts of the image.
Use cv2.line, cv2.rectangle, cv2.circle, and cv2.putText to draw shapes and text.
Colors are in BGR order and thickness controls line or text boldness.

Practice

(1/5)
1. Which OpenCV function is used to draw a rectangle on an image?
easy
A. cv2.line
B. cv2.rectangle
C. cv2.circle
D. cv2.putText

Solution

  1. 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.
  2. Step 2: Identify the function for rectangles

    The function named cv2.rectangle is designed to draw rectangles on images.
  3. Final Answer:

    cv2.rectangle -> Option B
  4. Quick Check:

    Rectangle drawing = cv2.rectangle [OK]
Hint: Rectangle drawing uses cv2.rectangle function [OK]
Common Mistakes:
  • Confusing cv2.line with rectangle drawing
  • Using cv2.circle for rectangles
  • Trying to draw text with cv2.rectangle
2. Which parameter in cv2.putText controls the thickness of the text?
easy
A. thickness
B. fontScale
C. fontFace
D. color

Solution

  1. 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).
  2. Step 2: Identify thickness parameter

    The thickness parameter controls how bold or thick the text lines appear.
  3. Final Answer:

    thickness -> Option A
  4. Quick Check:

    Text thickness = thickness parameter [OK]
Hint: Thickness of text is set by 'thickness' parameter [OK]
Common Mistakes:
  • Confusing fontScale with thickness
  • Using color to control thickness
  • Mistaking fontFace for thickness
3. What will be the color of the line drawn by this code snippet?
cv2.line(img, (10, 10), (100, 10), (0, 0, 255), 2)
medium
A. Red
B. Green
C. Blue
D. Black

Solution

  1. 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.
  2. Step 2: Identify the color from the tuple

    Since only the last value (Red) is 255, the line color will be bright red.
  3. Final Answer:

    Red -> Option A
  4. Quick Check:

    BGR (0,0,255) = Red [OK]
Hint: Remember OpenCV colors are BGR, last value 255 means Red [OK]
Common Mistakes:
  • Assuming (0,0,255) is blue (RGB confusion)
  • Mixing up color order
  • Ignoring OpenCV's BGR format
4. Identify the error in this code that tries to draw a circle:
cv2.circle(img, (50, 50), -10, (255, 0, 0), 3)
medium
A. Center coordinates must be floats
B. Color tuple is wrong format
C. Thickness cannot be 3
D. Negative radius is invalid

Solution

  1. Step 1: Check circle parameters

    The radius parameter must be a positive integer representing the circle size.
  2. Step 2: Identify invalid radius

    The radius given is -10, which is invalid and will cause an error.
  3. Final Answer:

    Negative radius is invalid -> Option D
  4. Quick Check:

    Radius must be positive integer [OK]
Hint: Radius must be positive; negative values cause errors [OK]
Common Mistakes:
  • Using negative radius values
  • Thinking thickness 3 is invalid
  • Assuming center coordinates must be floats
5. You want to draw a blue rectangle with thickness 4 and label it "Object" in white text above it. Which code snippet correctly does this?
img = cv2.imread('image.jpg')
start = (30, 30)
end = (150, 150)
# Options below
hard
A. cv2.rectangle(img, start, end, (255, 0, 0), 2) cv2.putText(img, 'Object', (30, 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 4)
B. cv2.rectangle(img, start, end, (0, 0, 255), 4) cv2.putText(img, 'Object', (30, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2)
C. cv2.rectangle(img, start, end, (255, 0, 0), 4) cv2.putText(img, 'Object', (30, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
D. cv2.rectangle(img, start, end, (0, 255, 0), 4) cv2.putText(img, 'Object', (30, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Blue rectangle with thickness 4 and white "Object" text above -> Option C
  4. Quick Check:

    Blue = (255,0,0), white text, thickness 4 [OK]
Hint: Blue is (255,0,0); white text is (255,255,255) [OK]
Common Mistakes:
  • Mixing up BGR color order
  • Using wrong thickness values
  • Placing text inside rectangle instead of above