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.