Geometric transforms help change images by rotating, flipping, or cutting parts. This makes models see images in different ways and learn better.
Geometric transforms (rotate, flip, crop) in Computer Vision
rotate(image, angle) flip(image, mode) crop(image, x_start, y_start, width, height)
rotate turns the image by the angle in degrees (positive for counterclockwise).
flip can be horizontal or vertical depending on mode.
crop cuts out a rectangle from the image starting at (x_start, y_start).
rotated_img = rotate(image, 90)flipped_img = flip(image, 'horizontal')cropped_img = crop(image, 10, 20, 100, 100)
This code creates a simple image, then rotates it 45 degrees, flips it horizontally, and crops a 100x100 area from the top-left. It prints pixel values and cropped shape to show the changes.
import cv2 import numpy as np # Create a simple image: white square on black background image = np.zeros((200, 200, 3), dtype=np.uint8) cv2.rectangle(image, (50, 50), (150, 150), (255, 255, 255), -1) # Rotate image 45 degrees (h, w) = image.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, 45, 1.0) rotated = cv2.warpAffine(image, M, (w, h)) # Flip image horizontally flipped = cv2.flip(image, 1) # Crop image: 100x100 from top-left corner cropped = image[0:100, 0:100] # Check some pixel values to confirm changes print('Original center pixel:', image[100, 100]) print('Rotated center pixel:', rotated[100, 100]) print('Flipped center pixel:', flipped[100, 100]) print('Cropped shape:', cropped.shape)
Rotation may cause parts of the image to go outside the frame or create black corners.
Flipping horizontally means left-right swap; vertically means top-bottom swap.
Cropping must stay inside image bounds to avoid errors.
Geometric transforms change images by rotating, flipping, or cropping.
They help models learn better by showing images in different views.
Use simple functions to apply these transforms safely within image size.
Practice
Solution
Step 1: Understand horizontal flip
Horizontal flip reverses the image left to right, like a mirror reflection.Step 2: Compare with other transforms
Rotate changes orientation, crop cuts part of image, vertical flip flips top to bottom.Final Answer:
Horizontal flip -> Option AQuick Check:
Mirror image = Horizontal flip [OK]
- Confusing vertical flip with horizontal flip
- Thinking rotate is flipping
- Mixing crop with flip
Solution
Step 1: Identify OpenCV rotate syntax
cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE) rotates image 90 degrees clockwise.Step 2: Check other options
Flip flips image, crop is not a cv2 function, ROTATE_180 rotates 180 degrees, not 90.Final Answer:
rotated = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE) -> Option CQuick Check:
Rotate 90 clockwise = cv2.ROTATE_90_CLOCKWISE [OK]
- Using flip instead of rotate
- Wrong rotate constant
- Trying crop as a cv2 function
import cv2
image = cv2.imread('img.jpg')
cropped = image[50:150, 30:130]Solution
Step 1: Calculate crop height
Height = 150 - 50 = 100 pixels.Step 2: Calculate crop width
Width = 130 - 30 = 100 pixels.Step 3: Confirm channels
Image has 3 color channels (RGB/BGR).Final Answer:
(100, 100, 3) -> Option AQuick Check:
Crop size = (height, width, channels) = (100, 100, 3) [OK]
- Mixing width and height order
- Forgetting zero-based indexing
- Ignoring color channels
import cv2
image = cv2.imread('img.jpg')
flipped = cv2.flip(image, 2)Solution
Step 1: Check cv2.flip flipCode values
flipCode=0 flips vertically, 1 flips horizontally, -1 flips both axes.Step 2: Identify invalid flipCode
flipCode=2 is invalid and causes error.Final Answer:
Flip code 2 is invalid; use 0 for vertical flip -> Option BQuick Check:
Vertical flip = flipCode 0 [OK]
- Using wrong flipCode values
- Thinking flip needs grayscale
- Assuming cv2.flip needs extra args
Solution
Step 1: Rotate image 90 degrees clockwise
Use cv2.rotate with ROTATE_90_CLOCKWISE on original image.Step 2: Crop center 100x100 pixels
Calculate center indices and slice 50 pixels before and after center in both dimensions.Step 3: Verify order and correctness
Rotation first, then crop center; other options rotate wrong angle or crop before rotate.Final Answer:
Rotate then crop center 100x100 -> Option DQuick 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]
- Cropping before rotating changes crop area
- Using wrong rotate angle
- Using flip instead of rotate
Ready to prove it?
Pass the test and this pattern moves unknown
