Bird
Raised Fist0
Computer Visionml~8 mins

Geometric transforms (rotate, flip, crop) in Computer Vision - Model Metrics & Evaluation

Choose your learning style10 modes available
Metrics & Evaluation - Geometric transforms (rotate, flip, crop)
Which metric matters for Geometric Transforms and WHY

When using geometric transforms like rotate, flip, and crop in computer vision, the key metric to watch is model accuracy or performance on validation data. This is because these transforms change the input images to help the model learn better. We want to see if these changes improve the model's ability to recognize objects correctly.

Also, robustness is important. This means the model should still perform well even if images are rotated or flipped in real life. So, metrics like accuracy, precision, recall, or F1 score on transformed images tell us if the model learned well from these geometric changes.

Confusion Matrix Example

Suppose we have a model classifying images after applying flips and rotations. Here is a confusion matrix showing results on a test set:

      | Predicted Cat | Predicted Dog |
      |---------------|---------------|
      | True Cat: 45  | 5             |
      | True Dog: 7   | 43            |
    

From this matrix:

  • True Positives (TP) for Cat = 45
  • False Positives (FP) for Cat = 5
  • False Negatives (FN) for Cat = 7
  • True Negatives (TN) for Cat = 43

We can calculate precision and recall to check if the model learned well with geometric transforms.

Precision vs Recall Tradeoff with Geometric Transforms

Imagine a model trained with rotated and flipped images to detect cats. If the model is very strict, it might only label very clear cat images as cats, leading to:

  • High precision: Most predicted cats are really cats.
  • Low recall: It misses some cats that look different due to rotation or flip.

On the other hand, if the model tries to catch every possible cat, it might label some dogs as cats, leading to:

  • High recall: Most cats are found.
  • Low precision: More wrong cat labels.

Geometric transforms help balance this by teaching the model to recognize cats in many positions, improving both precision and recall.

What Good vs Bad Metric Values Look Like

Good values:

  • Accuracy above 85% on transformed images.
  • Precision and recall both above 80%, showing balanced detection.
  • F1 score close to precision and recall, indicating no big tradeoff.

Bad values:

  • Accuracy below 70%, meaning transforms confused the model.
  • Precision very high but recall very low, or vice versa, showing imbalance.
  • F1 score much lower than precision or recall, indicating poor overall performance.
Common Pitfalls with Metrics and Geometric Transforms
  • Ignoring data leakage: Using the same transformed images in training and testing can falsely boost accuracy.
  • Overfitting: Model learns to recognize only specific rotations or flips seen in training, not generalizing well.
  • Accuracy paradox: High accuracy might hide poor recall if classes are imbalanced.
  • Not validating on original images: Model might perform well on transformed images but poorly on real-world data.
Self Check

Your model trained with rotated and flipped images has 98% accuracy but only 12% recall on the cat class. Is it good for production?

Answer: No, it is not good. The very low recall means the model misses most cats, which is a big problem if you want to detect cats reliably. High accuracy can be misleading if most images are not cats. You should improve recall by adjusting training or using more diverse transforms.

Key Result
For geometric transforms, balanced precision and recall on transformed images show the model learned robust features.

Practice

(1/5)
1. Which geometric transform flips an image horizontally, like looking in a mirror?
easy
A. Horizontal flip
B. Rotate 90 degrees
C. Crop center
D. Vertical flip

Solution

  1. Step 1: Understand horizontal flip

    Horizontal flip reverses the image left to right, like a mirror reflection.
  2. Step 2: Compare with other transforms

    Rotate changes orientation, crop cuts part of image, vertical flip flips top to bottom.
  3. Final Answer:

    Horizontal flip -> Option A
  4. Quick Check:

    Mirror image = Horizontal flip [OK]
Hint: Mirror effect means horizontal flip [OK]
Common Mistakes:
  • Confusing vertical flip with horizontal flip
  • Thinking rotate is flipping
  • Mixing crop with flip
2. Which Python code correctly rotates an image by 90 degrees clockwise using OpenCV?
easy
A. rotated = cv2.crop(image, (0,0,100,100))
B. rotated = cv2.flip(image, 1)
C. rotated = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
D. rotated = cv2.rotate(image, cv2.ROTATE_180)

Solution

  1. Step 1: Identify OpenCV rotate syntax

    cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE) rotates image 90 degrees clockwise.
  2. Step 2: Check other options

    Flip flips image, crop is not a cv2 function, ROTATE_180 rotates 180 degrees, not 90.
  3. Final Answer:

    rotated = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE) -> Option C
  4. Quick Check:

    Rotate 90 clockwise = cv2.ROTATE_90_CLOCKWISE [OK]
Hint: Use cv2.rotate with ROTATE_90_CLOCKWISE for 90° turn [OK]
Common Mistakes:
  • Using flip instead of rotate
  • Wrong rotate constant
  • Trying crop as a cv2 function
3. What will be the shape of the image after applying this code?
import cv2
image = cv2.imread('img.jpg')
cropped = image[50:150, 30:130]
medium
A. (100, 100, 3)
B. (150, 130, 3)
C. (80, 100, 3)
D. (100, 80, 3)

Solution

  1. Step 1: Calculate crop height

    Height = 150 - 50 = 100 pixels.
  2. Step 2: Calculate crop width

    Width = 130 - 30 = 100 pixels.
  3. Step 3: Confirm channels

    Image has 3 color channels (RGB/BGR).
  4. Final Answer:

    (100, 100, 3) -> Option A
  5. Quick Check:

    Crop size = (height, width, channels) = (100, 100, 3) [OK]
Hint: Subtract start from end index for height and width [OK]
Common Mistakes:
  • Mixing width and height order
  • Forgetting zero-based indexing
  • Ignoring color channels
4. This code tries to flip an image vertically but causes an error. What is wrong?
import cv2
image = cv2.imread('img.jpg')
flipped = cv2.flip(image, 2)
medium
A. image must be grayscale for flip
B. Flip code 2 is invalid; use 0 for vertical flip
C. cv2.flip requires three arguments
D. cv2.flip does not flip images

Solution

  1. Step 1: Check cv2.flip flipCode values

    flipCode=0 flips vertically, 1 flips horizontally, -1 flips both axes.
  2. Step 2: Identify invalid flipCode

    flipCode=2 is invalid and causes error.
  3. Final Answer:

    Flip code 2 is invalid; use 0 for vertical flip -> Option B
  4. Quick Check:

    Vertical flip = flipCode 0 [OK]
Hint: Use flipCode 0 for vertical flip in cv2.flip [OK]
Common Mistakes:
  • Using wrong flipCode values
  • Thinking flip needs grayscale
  • Assuming cv2.flip needs extra args
5. You want to rotate an image 90 degrees clockwise and then crop the central 100x100 pixels. Which code correctly does this?
hard
A. crop = img[50:150, 50:150] rot = cv2.rotate(crop, cv2.ROTATE_90_CLOCKWISE)
B. rot = cv2.flip(img, 1) crop = rot[rot.shape[0]//2-50:rot.shape[0]//2+50, rot.shape[1]//2-50:rot.shape[1]//2+50]
C. rot = cv2.rotate(img, cv2.ROTATE_180) crop = rot[0:100, 0:100]
D. rot = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE) crop = rot[rot.shape[0]//2-50:rot.shape[0]//2+50, rot.shape[1]//2-50:rot.shape[1]//2+50]

Solution

  1. Step 1: Rotate image 90 degrees clockwise

    Use cv2.rotate with ROTATE_90_CLOCKWISE on original image.
  2. Step 2: Crop center 100x100 pixels

    Calculate center indices and slice 50 pixels before and after center in both dimensions.
  3. Step 3: Verify order and correctness

    Rotation first, then crop center; other options rotate wrong angle or crop before rotate.
  4. Final Answer:

    Rotate then crop center 100x100 -> Option D
  5. Quick Check:

    Rotate 90° clockwise then center crop = rot = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE) crop = rot[rot.shape[0]//2-50:rot.shape[0]//2+50, rot.shape[1]//2-50:rot.shape[1]//2+50] [OK]
Hint: Rotate first, then crop center using shape indices [OK]
Common Mistakes:
  • Cropping before rotating changes crop area
  • Using wrong rotate angle
  • Using flip instead of rotate
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard