Affine Transformation
Affine transformation maps image coordinates through rotation, scaling, shear, and translation, supporting augmentation, alignment, and preprocessing.
An affine transformation is a coordinate mapping that combines a linear transformation with a translation. In images, it can rotate, resize, reflect, shear, or shift the pixel grid while preserving straight-line structure. Affine transform and affine map are synonyms. In computer vision, these mappings support alignment, data preprocessing, and data augmentation.
Leonhard Euler introduced the term affine for related curves in 1748, in chapter XVIII of Introductio in analysin infinitorum, volume 2.
What Affine Transformations Preserve#
An invertible affine transformation preserves straight lines, parallelism, and spacing ratios along a line. A midpoint remains a midpoint. Lengths and angles can change: shearing and unequal scaling can turn a rectangle into a parallelogram or a circle into an ellipse.
Rigid transformations preserve distances and angles; similarity transformations additionally allow uniform scaling. Affine transformations also allow unequal scaling and shear. A homography includes perspective effects, such as parallel road edges converging toward a vanishing point. An affine mapping preserves parallelism, fitting global skew and scale changes.
How the Affine Matrix Works#
A two-dimensional affine matrix has six parameters, commonly stored in a 2 x 3 matrix. Four control the linear part, and two specify horizontal and vertical translation. Three point pairs determine the matrix: the three source points must be non-collinear, forming a triangle. In OpenCV, cv2.getAffineTransform estimates the matrix from these correspondences.
Adding a constant third coordinate of 1, called a homogeneous coordinate, incorporates translation into matrix multiplication. The resulting 3 x 3 matrix has a final row of 0, 0, 1. Multiple transformations combine through matrix multiplication; rotating then shifting generally differs from shifting then rotating.
An affine transformation is linear exactly when its translation is zero. In a neural network, a fully connected layer combines learned weights with a bias offset. PyTorch's Linear layer applies an affine mapping by default, typically followed by a nonlinear activation function.
Uses in Vision and Model Training#
- Medical Imaging: Affine image registration aligns scans before comparison or deformation correction. SimpleElastix's affine registration example corrects an image that was sheared, scaled 1.2 times, rotated 10 degrees, and translated along both axes.
- Document Processing: Deskewing reduces character slant before optical character recognition. OpenCV's handwritten-digit classifier applies an affine shear before feature extraction and support vector machine classification.
Ultralytics YOLO26 detection and segmentation training supports geometric augmentation through degrees, translate, scale, and shear, updating images and annotations together. With perspective=0.0, the mapping is affine; rotation and shear are disabled by default.
Apply an Affine Transformation in Python#
With pip install opencv-python numpy, this example uses OpenCV to rotate and shrink an image around its center:
from urllib.request import urlopen
import cv2
import numpy as np
with urlopen("https://ultralytics.com/images/bus.jpg") as response:
encoded = np.frombuffer(response.read(), dtype=np.uint8)
image = cv2.imdecode(encoded, cv2.IMREAD_COLOR)
height, width = image.shape[:2]
# Rotation around the center also requires translation.
center = (width / 2, height / 2)
matrix = cv2.getRotationMatrix2D(center, angle=20, scale=0.8)
warped = cv2.warpAffine(image, matrix, (width, height))
cv2.imwrite("affine_result.jpg", warped)The saved image shows a 20-degree rotation and scaling to 80% of the original dimensions inside an unchanged output canvas.
Resampling and Annotation Errors#
Warping samples a discrete pixel grid. Bilinear interpolation blends neighboring pixels; nearest-neighbor interpolation selects an existing value. Rotation exposes border regions with no source pixels, and a fixed canvas clips content outside its boundaries. Repeated bilinear resampling softens fine detail even when the coordinate mapping is invertible.
For supervised vision, images and annotations share the same mapping. Unchanged labels place targets at incorrect locations. Bounding boxes require transforming all four corners before computing an enclosing axis-aligned rectangle, which can include additional background after rotation. Keypoints outside the canvas require visibility updates. Categorical segmentation masks use nearest-neighbor interpolation to retain discrete class IDs.










