Bird
Raised Fist0
Computer Visionml~8 mins

Drawing on images (lines, rectangles, circles, text) in Computer Vision - Model Metrics & Evaluation

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
Metrics & Evaluation - Drawing on images (lines, rectangles, circles, text)
Which metric matters for Drawing on images and WHY

When drawing on images, the main goal is to accurately place shapes and text where intended. Metrics focus on pixel accuracy and alignment correctness. For example, if you draw a rectangle around an object, the drawn shape should closely match the object's true position and size.

Common metrics include Intersection over Union (IoU) for shapes, which measures how well the drawn shape overlaps the target area, and pixel error for text placement, which measures how far the text is from the intended spot.

These metrics matter because they tell us if the drawing is precise and useful for tasks like highlighting objects or adding readable labels.

Confusion matrix or equivalent visualization

For drawing tasks, a confusion matrix is not typical. Instead, we use overlap measures like IoU.

    Example: Drawing a rectangle around a cat in an image

    Ground truth box: (x1=30, y1=40, x2=130, y2=140)
    Drawn box:       (x1=35, y1=45, x2=125, y2=135)

    IoU = Area of Overlap / Area of Union

    Overlap area = (125-35) * (135-45) = 90 * 90 = 8100
    Ground truth area = 100 * 100 = 10000
    Drawn box area = 90 * 90 = 8100

    Union area = 10000 + 8100 - 8100 = 10000

    IoU = 8100 / 10000 = 0.81
    

An IoU of 0.81 means the drawn rectangle matches the target well.

Precision vs Recall tradeoff with examples

In drawing on images, precision means how much of the drawn shape is correct (not outside the target), and recall means how much of the target is covered by the drawing.

Example 1: High precision, low recall
Drawing a small circle inside a large object. The circle is fully inside the object (high precision), but it misses much of the object (low recall).

Example 2: Low precision, high recall
Drawing a large rectangle that covers the whole object and some background. It covers all the object (high recall) but also includes extra area (low precision).

Good drawing balances both: covering the object well without extra parts.

What "good" vs "bad" metric values look like

Good drawing: IoU above 0.75, text placed within 5 pixels of target location, shapes aligned with object edges.

Bad drawing: IoU below 0.4, text overlapping wrong areas or unreadable, shapes misplaced or distorted.

Good metrics mean the drawing clearly marks the intended parts of the image, making it useful for visualization or further analysis.

Common pitfalls in metrics for drawing on images
  • Ignoring scale: Drawing a shape too small or too large can give misleading IoU scores.
  • Misalignment: Slight shifts can reduce IoU even if the shape looks close.
  • Text readability: Metrics may not capture if text is readable or overlaps other elements.
  • Overfitting to training images: Drawing perfectly on training images but failing on new ones.
  • Data leakage: Using ground truth info directly to draw shapes inflates metrics unfairly.
Self-check question

Your model draws rectangles around objects with 98% pixel accuracy but only 12% recall of the object area. Is this good?

Answer: No. High pixel accuracy means the drawn parts are correct, but very low recall means most of the object is missed. The drawing is incomplete and not useful for highlighting the full object.

Key Result
IoU above 0.75 indicates good shape drawing accuracy; balance precision and recall for best results.

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