Story 8: Geometric Transformations – Shape & Degree, Images Have It Easy!
Hello again, image manipulators! Today, we bend, flip, rotate, and stretch images as if they were made of rubber!
Ever wanted to rotate a selfie, flip an image, or zoom in on a cat's face like in CSI? Well, today we make geometric transformations easy!
1️⃣ Resizing – Make It Bigger or Smaller!
Sometimes, you want to shrink an image-to save space-or enlarge it-without losing too much quality.
import cv2
# Load the image
image = cv2.imread("image.jpg")
# Resize to 50% of original size
smaller = cv2.resize(image, None, fx=0.5, fy=0.5)
# Resize to double the size
bigger = cv2.resize(image, None, fx=2, fy=2)
cv2.imshow("Smaller Image", smaller)
cv2.imshow("Bigger Image", bigger)
cv2.waitKey(0)
cv2.destroyAllWindows()
🔴 What’s Happening?
fx and fy control width and height scaling.
0.5 means half size, 2 means double size.
✅Pro Tip: Want exact dimensions? Use:
resized = cv2.resize(image, (800, 600))
This resizes to 800x600 pixels!!
2️⃣ Flipping – Mirror Magic!
Want to create a mirror image? The image can be flipped vertically, horizontally, or even both!
# Flip horizontally (left-right)
flip_h = cv2.flip(image, 1)
# Flip vertically (upside-down)
flip_v = cv2.flip(image, 0)
# Flip both ways
flip_both = cv2.flip(image, -1)
cv2.imshow("Flipped Horizontally", flip_h)
cv2.imshow("Flipped Vertically", flip_v)
cv2.imshow("Flipped Both Ways", flip_both)
cv2.waitKey(0)
cv2.destroyAllWindows()
🔴What’s Happening?
1 → Flips left-right.
0 → Flips upside-down.
-1 → Flips both ways!
✅Pro Tip: Use horizontal flip for selfies that look weird!
3️⃣ Rotating – Give It a Spin!
Want to rotate an image 90° or 45°? No problem!
# Flip horizontally (left-right)
flip_h = cv2.flip(image, 1)
# Flip vertically (upside-down)
flip_v = cv2.flip(image, 0)
# Flip both ways
flip_both = cv2.flip(image, -1)
cv2.imshow("Flipped Horizontally", flip_h)
cv2.imshow("Flipped Vertically", flip_v)
cv2.imshow("Flipped Both Ways", flip_both)
cv2.waitKey(0)
cv2.destroyAllWindows()
🔴 What’s Happening?
We rotate around the center (w//2, h//2).
45 → The rotation angle.
1.0 → The scale factor (1 = original size).
Pro Tip: Change 45 to -90 for counter-clockwise rotation!
4️⃣ Translation – Move It Around!
Want to move an image left, right, up, or down?
import numpy as np
# Define translation matrix (move 100px right, 50px down)
matrix = np.float32([[1, 0, 100], [0, 1, 50]])
# Apply translation
translated = cv2.warpAffine(image, matrix, (w, h))
cv2.imshow("Translated Image", translated)
cv2.waitKey(0)
cv2.destroyAllWindows()
🔴What’s Happening?
100 → Moves right by 100 pixels.
50 → Moves down by 50 pixels.
✅ Pro Tip: Use negative values to move left or up!
5️⃣ Perspective Transformation – Warping Reality!
Want to change perspective, like tilting a building or fixing a skewed document?
# Define 4 corner points (source and destination)
pts1 = np.float32([[50, 50], [200, 50], [50, 200], [200, 200]])
pts2 = np.float32([[10, 100], [180, 50], [50, 250], [220, 200]])
# Get perspective transformation matrix
matrix = cv2.getPerspectiveTransform(pts1, pts2)
# Apply transformation
warped = cv2.warpPerspective(image, matrix, (w, h))
cv2.imshow("Warped Image", warped)
cv2.waitKey(0)
cv2.destroyAllWindows()
🔴 What’s Happening?
We define 4 points to warp the image into a new shape!
Used for correcting distortions, like scanning documents.
✅ Pro Tip: This is great for 3D-like effects!