Skip to main content
This guide covers essential geometric transformations including resizing, rotation, flipping, cropping, and more advanced affine and perspective transformations.

Overview

Image transformations allow you to modify the geometry of images. Common operations include:
  • Resizing images to different dimensions
  • Rotating images by any angle
  • Flipping images horizontally or vertically
  • Cropping regions of interest
  • Applying affine and perspective transformations

Resizing Images

Resize images to specific dimensions or by a scaling factor.
import cv2 as cv
import numpy as np

# Load image
img = cv.imread("image.jpg")
height, width = img.shape[:2]

# Resize to specific dimensions
resized = cv.resize(img, (800, 600))

# Resize by scaling factor
scaled = cv.resize(img, None, fx=0.5, fy=0.5, interpolation=cv.INTER_LINEAR)

# Resize maintaining aspect ratio
new_width = 640
aspect_ratio = new_width / width
new_height = int(height * aspect_ratio)
resized_aspect = cv.resize(img, (new_width, new_height))

cv.imshow("Original", img)
cv.imshow("Resized", resized)
cv.imshow("Scaled", scaled)
cv.waitKey(0)
cv.destroyAllWindows()

Interpolation Methods

MethodDescriptionUse Case
INTER_NEARESTNearest neighborFastest, lowest quality
INTER_LINEARBilinear interpolationGood balance (default)
INTER_CUBICBicubic interpolationSlower, higher quality
INTER_AREAResampling using pixel areaBest for downsampling
INTER_LANCZOS4Lanczos interpolationHighest quality, slowest

Rotating Images

Rotate images by 90-degree increments or arbitrary angles.

Simple 90-Degree Rotations

import cv2 as cv

img = cv.imread("image.jpg")

# Rotate 90 degrees clockwise
rotated_90_cw = cv.rotate(img, cv.ROTATE_90_CLOCKWISE)

# Rotate 90 degrees counter-clockwise
rotated_90_ccw = cv.rotate(img, cv.ROTATE_90_COUNTERCLOCKWISE)

# Rotate 180 degrees
rotated_180 = cv.rotate(img, cv.ROTATE_180)

cv.imshow("Original", img)
cv.imshow("90° CW", rotated_90_cw)
cv.imshow("180°", rotated_180)
cv.waitKey(0)

Arbitrary Angle Rotation

import cv2 as cv
import numpy as np

img = cv.imread("image.jpg")
height, width = img.shape[:2]

# Define rotation center (image center)
center = (width // 2, height // 2)

# Rotation angle in degrees (positive = counter-clockwise)
angle = 45

# Scale factor (1.0 = no scaling)
scale = 1.0

# Get rotation matrix
rotation_matrix = cv.getRotationMatrix2D(center, angle, scale)

# Apply rotation
rotated = cv.warpAffine(img, rotation_matrix, (width, height))

cv.imshow("Original", img)
cv.imshow("Rotated 45°", rotated)
cv.waitKey(0)
When rotating by arbitrary angles, parts of the image may be cropped. To preserve the entire rotated image, calculate new dimensions and adjust the rotation matrix accordingly.

Flipping Images

Flip images horizontally, vertically, or both.
import cv2 as cv

img = cv.imread("image.jpg")

# Flip horizontally (mirror)
flipped_h = cv.flip(img, 1)

# Flip vertically
flipped_v = cv.flip(img, 0)

# Flip both horizontally and vertically
flipped_both = cv.flip(img, -1)

cv.imshow("Original", img)
cv.imshow("Horizontal Flip", flipped_h)
cv.imshow("Vertical Flip", flipped_v)
cv.imshow("Both", flipped_both)
cv.waitKey(0)

Cropping Images

Crop a region of interest from an image using array slicing.
import cv2 as cv

img = cv.imread("image.jpg")

# Define crop region (y1:y2, x1:x2)
y1, y2 = 100, 400
x1, x2 = 200, 600

# Crop the image
cropped = img[y1:y2, x1:x2]

cv.imshow("Original", img)
cv.imshow("Cropped", cropped)
cv.waitKey(0)

Affine Transformations

Affine transformations preserve parallel lines and include translation, rotation, scaling, and shearing.
import cv2 as cv
import numpy as np

img = cv.imread("image.jpg")
rows, cols = img.shape[:2]

# Define source points (3 points from original image)
src_points = np.float32([[50, 50], [200, 50], [50, 200]])

# Define destination points (where those points should move to)
dst_points = np.float32([[10, 100], [200, 50], [100, 250]])

# Get affine transformation matrix
affine_matrix = cv.getAffineTransform(src_points, dst_points)

# Apply affine transformation
transformed = cv.warpAffine(img, affine_matrix, (cols, rows))

cv.imshow("Original", img)
cv.imshow("Affine Transform", transformed)
cv.waitKey(0)

Perspective Transformations

Perspective transformations correct for camera angle and viewpoint changes.
import cv2 as cv
import numpy as np

img = cv.imread("image.jpg")
rows, cols = img.shape[:2]

# Define source points (4 corners from original image)
src_points = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])

# Define destination points (rectangle)
dst_points = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])

# Get perspective transformation matrix
perspective_matrix = cv.getPerspectiveTransform(src_points, dst_points)

# Apply perspective transformation
warped = cv.warpPerspective(img, perspective_matrix, (300, 300))

cv.imshow("Original", img)
cv.imshow("Perspective Transform", warped)
cv.waitKey(0)
Perspective transformations are commonly used for document scanning, where you select the four corners of a document in a photo and transform it to a flat, rectangular view.

Key Functions

FunctionDescription
resize()Resize image to specific dimensions or scale
rotate()Rotate image by 90, 180, or 270 degrees
flip()Flip image horizontally, vertically, or both
getRotationMatrix2D()Get rotation matrix for arbitrary angles
getAffineTransform()Get affine transformation matrix from 3 point pairs
getPerspectiveTransform()Get perspective transformation matrix from 4 point pairs
warpAffine()Apply affine transformation
warpPerspective()Apply perspective transformation
When applying transformations, pixels that fall outside the destination image are cropped. Use appropriate border modes or adjust output dimensions to preserve all data.