0
0
Computer Visionml~5 mins

Geometric transforms (rotate, flip, crop) in Computer Vision

Choose your learning style9 modes available
Introduction

Geometric transforms help change images by rotating, flipping, or cutting parts. This makes models see images in different ways and learn better.

When you want to make your image data bigger by creating new versions.
When you need to fix image orientation problems.
When you want to focus on a specific part of an image by cropping.
When you want your model to recognize objects no matter how they are turned or flipped.
When preparing images for training to improve model accuracy.
Syntax
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).

Examples
This rotates the image 90 degrees counterclockwise.
Computer Vision
rotated_img = rotate(image, 90)
This flips the image left to right.
Computer Vision
flipped_img = flip(image, 'horizontal')
This cuts a 100x100 pixel square starting at x=10, y=20.
Computer Vision
cropped_img = crop(image, 10, 20, 100, 100)
Sample Model

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.

Computer Vision
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)
OutputSuccess
Important Notes

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.

Summary

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.