Image Transforms

Types of Transforms

Geometric transforms change pixel positions without changing content:

Common transforms:

  • Translation: Shift image position
  • Rotation: Rotate around a point
  • Scaling: Resize image
  • Flipping: Mirror horizontally/vertically
  • Affine: Combination of above (preserves parallelism)
  • Perspective: 3D-like transformation

Translation

Translation: Shift image by (tx, ty) pixels

Transformation matrix:

Formula: new_x = x + tx, new_y = y + ty

Use case: Positioning, augmentation, registration

Translation in OpenCV

  1. import cv2
  2. import numpy as np
  3. # translation matrix (shift right 100, down 50)
  4. #ans: M = np.float32([[1, 0, 100],
  5. #ans: [0, 1, 50]])
  6. # apply translation
  7. height, width = img.shape[:2]
  8. #ans: translated = cv2.warpAffine(img, M, (width, height))
  9. # shift left (negative tx)
  10. M = np.float32([[1, 0, -50],
  11. [0, 1, 0]])
  12. #ans: shifts 50 pixels left

Rotation

Rotation: Rotate image around center by angle θ

Transformation matrix:

OpenCV function: cv2.getRotationMatrix2D(center, angle, scale)

Rotation in OpenCV

  1. # get image center
  2. height, width = img.shape[:2]
  3. center = (width // 2, height // 2)
  4. # rotation matrix (45 degrees, no scaling)
  5. #ans: M = cv2.getRotationMatrix2D(center, 45, 1.0)
  6. #ans: angle=45°, scale=1.0 (no resize)
  7. # apply rotation
  8. #ans: rotated = cv2.warpAffine(img, M, (width, height))
  9. # rotate 90 degrees
  10. M = cv2.getRotationMatrix2D(center, 90, 1.0)
  11. rotated = cv2.warpAffine(img, M, (width, height))
  12. #ans: 90° clockwise rotation

Scaling (Resizing)

Scaling: Change image size

Methods:

  • Nearest neighbor: Fast, blocky
  • Bilinear: Smooth, fast
  • Bicubic: Smoother, slower
  • Lanczos: Best quality, slowest

OpenCV flags: INTER_NEAREST, INTER_LINEAR, INTER_CUBIC, INTER_LANCZOS4

Resizing in OpenCV

  1. # resize to specific dimensions
  2. #ans: resized = cv2.resize(img, (640, 480))
  3. #ans: width=640, height=480 (exact size)
  4. # resize by scale factor
  5. #ans: scaled = cv2.resize(img, None, fx=0.5, fy=0.5)
  6. #ans: fx=fy=0.5 means 50% of original size
  7. # resize with interpolation
  8. #ans: resized = cv2.resize(img, (320, 240), interpolation=cv2.INTER_CUBIC)
  9. #ans: INTER_CUBIC for smooth downscaling
  10. # upscale 2x
  11. scaled = cv2.resize(img, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
  12. #ans: doubles size with bilinear interpolation

Flipping

Flipping: Mirror image across axis

Types:

  • Horizontal flip: flipCode = 1 (left ↔ right)
  • Vertical flip: flipCode = 0 (top ↔ bottom)
  • Both: flipCode = -1 (180° rotation)

Use case: Data augmentation, mirror effects

Flipping in OpenCV

  1. # horizontal flip (left-right)
  2. #ans: flipped_h = cv2.flip(img, 1)
  3. #ans: flipCode=1 for horizontal
  4. # vertical flip (up-down)
  5. #ans: flipped_v = cv2.flip(img, 0)
  6. #ans: flipCode=0 for vertical
  7. # both axes (180° rotation)
  8. #ans: flipped_both = cv2.flip(img, -1)
  9. #ans: flipCode=-1 for both axes

Transpose

Transpose: Swap rows and columns (reflect across diagonal)

Effect: Width becomes height, height becomes width

Difference from rotation: 90° rotation + flip

Transpose in OpenCV

  1. # transpose (swap width and height)
  2. #ans: transposed = cv2.transpose(img)
  3. height, width = img.shape[:2]
  4. #ans: new shape = (width, height, channels)
  5. # transpose vs rotate 90°
  6. transposed = cv2.transpose(img)
  7. rotated = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
  8. #ans: different results! transpose reflects across diagonal
  9. # convenient rotation functions
  10. #ans: rot_90 = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
  11. #ans: rot_180 = cv2.rotate(img, cv2.ROTATE_180)
  12. #ans: rot_270 = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)

Cropping

Cropping: Extract region of interest (ROI)

NumPy slicing: img[y1:y2, x1:x2]

Note: No special OpenCV function needed!

Cropping in OpenCV

  1. # crop region [y1:y2, x1:x2]
  2. #ans: cropped = img[100:300, 50:250]
  3. #ans: rows 100-300, columns 50-250
  4. # crop center region
  5. height, width = img.shape[:2]
  6. x = width // 4
  7. y = height // 4
  8. w = width // 2
  9. h = height // 2
  10. #ans: cropped = img[y:y+h, x:x+w]
  11. #ans: center half of image

Exercises - Part 1 (Concepts)

  1. # what does translation do?
  2. #ans: shifts image position by (tx, ty)
  3. # what is rotation center?
  4. #ans: point around which image rotates
  5. # what interpolation for downscaling?
  6. #ans: INTER_AREA or INTER_CUBIC (best quality)
  7. # what is flipCode=1?
  8. #ans: horizontal flip (left ↔ right)
  9. # difference between transpose and rotate?
  10. #ans: transpose reflects across diagonal

Exercises - Part 2 (Concepts)

  1. # what happens to size during rotation?
  2. #ans: content may be clipped at edges
  3. # what is fx and fy in resize?
  4. #ans: scale factors for x and y directions
  5. # fastest interpolation method?
  6. #ans: INTER_NEAREST (nearest neighbor)
  7. # best quality interpolation?
  8. #ans: INTER_LANCZOS4 (slowest but best)
  9. # what is warpAffine used for?
  10. #ans: apply affine transformations (translation, rotation, etc.)

Exercises - Part 3 (Coding)

  1. # translate right 50, down 30
  2. #ans: M = np.float32([[1, 0, 50],
  3. #ans: [0, 1, 30]])
  4. #ans: h, w = img.shape[:2]
  5. #ans: translated = cv2.warpAffine(img, M, (w, h))
  6. # rotate 30 degrees around center
  7. h, w = img.shape[:2]
  8. center = (w // 2, h // 2)
  9. #ans: M = cv2.getRotationMatrix2D(center, 30, 1.0)
  10. #ans: rotated = cv2.warpAffine(img, M, (w, h))

Exercises - Part 4 (Coding)

  1. # resize to 800x600
  2. #ans: resized = cv2.resize(img, (800, 600))
  3. # scale down to 25%
  4. #ans: scaled = cv2.resize(img, None, fx=0.25, fy=0.25)
  5. # flip horizontally
  6. #ans: flipped = cv2.flip(img, 1)
  7. # flip vertically
  8. #ans: flipped = cv2.flip(img, 0)

Exercises - Part 5 (Coding)

  1. # rotate 90 degrees clockwise
  2. #ans: rotated = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
  3. # rotate 180 degrees
  4. #ans: rotated = cv2.rotate(img, cv2.ROTATE_180)
  5. # transpose image
  6. #ans: transposed = cv2.transpose(img)
  7. # crop top-left 100x100 region
  8. #ans: cropped = img[0:100, 0:100]

Exercises - Part 6 (Mixed)

  1. # upscale 3x with cubic interpolation
  2. #ans: scaled = cv2.resize(img, None, fx=3, fy=3, interpolation=cv2.INTER_CUBIC)
  3. # rotate with scale
  4. h, w = img.shape[:2]
  5. M = cv2.getRotationMatrix2D((w//2, h//2), 45, 0.8)
  6. #ans: 45° rotation + 80% scale
  7. #ans: rotated = cv2.warpAffine(img, M, (w, h))
  8. # combine flip and rotate
  9. flipped = cv2.flip(img, 1)
  10. #ans: rotated = cv2.rotate(flipped, cv2.ROTATE_90_CLOCKWISE)

Exercises - Part 7 (Mixed)

  1. # resize maintaining aspect ratio
  2. h, w = img.shape[:2]
  3. new_w = 640
  4. new_h = int(h * (new_w / w))
  5. #ans: resized = cv2.resize(img, (new_w, new_h))
  6. # crop center square
  7. h, w = img.shape[:2]
  8. size = min(h, w)
  9. start_y = (h - size) // 2
  10. start_x = (w - size) // 2
  11. #ans: cropped = img[start_y:start_y+size, start_x:start_x+size]

Exercises - Part 8 (Advanced)

  1. # translate with border handling
  2. M = np.float32([[1, 0, 100],
  3. [0, 1, 50]])
  4. h, w = img.shape[:2]
  5. #ans: translated = cv2.warpAffine(img, M, (w, h), borderMode=cv2.BORDER_CONSTANT, borderValue=(0,0,0))
  6. # rotate and expand canvas
  7. angle = 45
  8. h, w = img.shape[:2]
  9. center = (w//2, h//2)
  10. M = cv2.getRotationMatrix2D(center, angle, 1.0)
  11. cos = np.abs(M[0, 0])
  12. sin = np.abs(M[0, 1])
  13. new_w = int(h * sin + w * cos)
  14. new_h = int(h * cos + w * sin)
  15. M[0, 2] += (new_w - w) / 2
  16. M[1, 2] += (new_h - h) / 2
  17. #ans: rotated = cv2.warpAffine(img, M, (new_w, new_h))
  18. #ans: expands canvas to fit rotated image

Google tag (gtag.js)