Homogeneous Coordinates and Transformation Matrices

Homogeneous Coordinates and Transformation Matrices

What are homogeneous coordinates?

Homogeneous coordinates are a coordinate system used in projective geometry, obtained by augmenting ordinary (Cartesian) coordinates with one extra component (the geometric dimension does not increase). Strictly speaking they form an equivalence class in which proportional coordinates $(kx, ky, kw) \sim (x, y, w)$ (for $k \neq 0$) denote the same point (the zero vector does not represent a point), which is exactly what lets us express affine and projective transformations, including translation, as a single matrix product (nonlinear maps such as lens distortion cannot be expressed this way).

2D point (x, y) Cartesian → Homogeneous (x, y, 1) 3-component vector 3D: (X, Y, Z) → homog.: (X, Y, Z, 1) same point, another representative: (kx, ky, k) ∼ (x, y, 1)
Figure 1. Correspondence between Cartesian and homogeneous coordinates.

Definition of homogeneous coordinates

  • Homogeneous coordinates of a 2D point $(x, y)$: $(x, y, 1)$, or more generally $(kx, ky, k)$ for $k \neq 0$
  • Homogeneous coordinates of a 3D point $(X, Y, Z)$: $(X, Y, Z, 1)$, or $(kX, kY, kZ, k)$ ($k \neq 0$)
  • Back to Cartesian coordinates: $(x, y, w) \to (x/w, y/w)$ (for $w \neq 0$; $w = 0$ represents a point at infinity)

Why use homogeneous coordinates?

  • Translation can be written as a matrix product
  • Several transforms can be composed into a single matrix
  • Projective transforms (perspective projection) can be handled as matrix products (dividing by the last component when returning to Cartesian coordinates)
  • Points at infinity can be represented with finite coordinates ($w = 0$)

Why translation is special

In Cartesian coordinates, translation is an addition $(x', y') = (x, y) + (t_x, t_y)$ and cannot be written as a single matrix product (a linear map always fixes the origin). Adding one extra dimension via homogeneous coordinates lets us write translation as a single matrix product $(x', y', 1) = T\,(x, y, 1)$, so rotation, scaling, and translation can all be composed within the same framework — this is the key benefit.

2D transformation matrices

Vector convention: vectors are treated as column vectors, and transforms multiply on the left, $p' = T\,p$. Writing $(x, y, 1)$ as a comma-separated row is just shorthand for the column vector $(x, y, 1)^\top$, written horizontally to save space instead of stacking it vertically.

Translation

original translated (tx, ty) Translation matrix: T = 10tx 01ty 001 p′ = T p (x′, y′, 1) = (x+tx, y+ty, 1)
Figure 2. Translation transformation matrix.

Rotation

θ rotate about origin Rotation matrix: R = cos θ−sin θ0 sin θcos θ0 001 CCW is taken as positive
Figure 3. Rotation about the origin. (Counterclockwise is positive when $x$ points right and $y$ points up; in image coordinates with $y$ pointing down, a positive angle with the same matrix looks clockwise.)

Scaling

sx = 2, sy = 3 Scale matrix: S = sx00 0sy0 001 sx ≠ sy: anisotropic scale
Figure 4. Effect of the scaling transformation. (The matrix scales about the origin; in the figure the lower-left corner is the origin.)

Composing transformations

Several transforms can be composed into a single matrix product. Note that they are applied from right to left.

Composition of transforms

Applying scale → rotation → translation in that order:

$$M = T \cdot R \cdot S$$ $$\mathbf{p}' = M \mathbf{p} = T(R(S \mathbf{p}))$$
original → S scaled → R rotated → T translated
Figure 5. Composition of geometric transforms (scale → rotation → translation).

Important: matrix multiplication is not commutative in general ($AB \neq BA$ is possible). Changing the order of the transforms can change the result (some pairs, such as two translations, do commute).

3D transformation matrices

Transforms in 3D space are represented by $4 \times 4$ matrices.

3D translation

$$T = \begin{pmatrix} 1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1 \end{pmatrix}$$

3D rotation matrices

About the X-axis (roll):

$$R_x(\theta) = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos\theta & -\sin\theta & 0 \\ 0 & \sin\theta & \cos\theta & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}$$

About the Y-axis (pitch):

$$R_y(\theta) = \begin{pmatrix} \cos\theta & 0 & \sin\theta & 0 \\ 0 & 1 & 0 & 0 \\ -\sin\theta & 0 & \cos\theta & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}$$

About the Z-axis (yaw):

$$R_z(\theta) = \begin{pmatrix} \cos\theta & -\sin\theta & 0 & 0 \\ \sin\theta & \cos\theta & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}$$
A single 3D diagram of rotations about the X, Y, and Z axes, with a right-hand-rule rotation arrow around each axis Y-axis X-axis Z-axis
Figure 6. Rotations about the X, Y, and Z axes — each orange arrow follows the right-hand rule (counterclockwise when viewed from the positive end of the axis looking toward the origin).

Note (limitations of Euler angles): Roll, Pitch, and Yaw are terms used for the attitude of aircraft and ships; the correspondence above (X = roll, Y = pitch, Z = yaw) is the convention of a body frame with X forward, Y right, and Z down (vertical). With different axes the correspondence changes (e.g., in CG with Y up, yaw is the rotation about the vertical Y axis). In CV/CG these are often simply called rotation about the X-, Y-, and Z-axes. The order in which the three rotations are composed (e.g. $R = R_z(\text{yaw})\,R_y(\text{pitch})\,R_x(\text{roll})$) is convention-dependent. Euler-angle representations also suffer from gimbal lock (with the rotation order above, when the pitch is ±90° the effects of roll and yaw become indistinguishable; what is lost is a degree of freedom of the angle representation, while the orientation itself still has 3 degrees of freedom); when smooth interpolation or singularity avoidance matters, quaternions or rotation matrices are usually used directly.

Rigid and similarity transformations

Rigid transformation (Euclidean)

Rotation + translation. Preserves shape, size, and orientation (no reflection) (6 degrees of freedom: 3 rotations + 3 translations). A camera's extrinsic parameters $\mathbf{X}_c = R\,\mathbf{X}_w + \mathbf{t}$ take this form (next chapter, Camera Matrix).

$$\begin{pmatrix} \mathbf{R} & \mathbf{t} \\ \mathbf{0}^T & 1 \end{pmatrix}, \quad \mathbf{R} \in SO(3), \quad \mathbf{t} \in \mathbb{R}^3$$

Similarity transformation

Rigid transform + isotropic scaling. Preserves angles (7 DOF in 3D: 6 rigid + 1 scale).

$$\begin{pmatrix} s\mathbf{R} & \mathbf{t} \\ \mathbf{0}^T & 1 \end{pmatrix}, \quad s > 0$$

Affine transformation

Preserves parallelism (12 DOF in 3D: 9 from the linear part $\mathbf{A}$ + 3 from the translation $\mathbf{t}$).

$$\begin{pmatrix} \mathbf{A} & \mathbf{t} \\ \mathbf{0}^T & 1 \end{pmatrix}, \quad \mathbf{A} \in \mathbb{R}^{3 \times 3},\ \det\mathbf{A} \neq 0$$

Code example (Python)

import numpy as np

def translation_matrix_2d(tx, ty):
    """2D translation matrix"""
    return np.array([
        [1, 0, tx],
        [0, 1, ty],
        [0, 0, 1]
    ])

def rotation_matrix_2d(theta):
    """2D rotation matrix (radians)"""
    c, s = np.cos(theta), np.sin(theta)
    return np.array([
        [c, -s, 0],
        [s,  c, 0],
        [0,  0, 1]
    ])

def scale_matrix_2d(sx, sy):
    """2D scale matrix"""
    return np.array([
        [sx, 0, 0],
        [0, sy, 0],
        [0,  0, 1]
    ])

# Composition: scale -> rotation -> translation
S = scale_matrix_2d(2, 2)
R = rotation_matrix_2d(np.radians(45))
T = translation_matrix_2d(100, 50)
M = T @ R @ S  # applied from right to left

# Transform a point
p = np.array([10, 20, 1])  # homogeneous coordinates
p_transformed = M @ p  # affine, so the third component stays 1
print(f"Transformed: ({p_transformed[0]:.1f}, {p_transformed[1]:.1f})")  # Transformed: (85.9, 92.4)

# Use with OpenCV
import cv2
img = cv2.imread("input.png")  # input image (the file name is an example)
height, width = img.shape[:2]
# The last row of an affine matrix is (0, 0, 1), so the first two rows (2x3) suffice
M_cv = M[:2, :]
img_transformed = cv2.warpAffine(img, M_cv, (width, height))

Summary

  • Homogeneous coordinates let us express transforms, including translation, as matrix products.
  • 2D transforms use $3 \times 3$ matrices; 3D transforms use $4 \times 4$ matrices.
  • Composition of transforms is a matrix product (applied from right to left).
  • A rigid transform is rotation + translation (shape-preserving).
  • A camera's extrinsic parameters are expressed as a rigid transform.

Frequently Asked Questions

What does $w = 0$ mean in homogeneous coordinates?

When $w = 0$, the Cartesian coordinates $(x/w, y/w)$ are undefined (they do not "become infinite"). If $(x, y) \neq (0, 0)$, the homogeneous coordinates $(x, y, 0)$ represent a point at infinity, where parallel lines meet in projective geometry. For example, $(1, 0, 0)$ is the point at infinity shared by all lines parallel to the $x$-axis, and a projective transform can handle it with finite arithmetic.

Why do 2D and 3D transformation matrices have different sizes?

A 2D transform acts on the 3-component homogeneous vector $(x, y, 1)$, so it uses a $3 \times 3$ matrix; a 3D transform acts on the 4-component homogeneous vector $(X, Y, Z, 1)$, so it uses a $4 \times 4$ matrix. In general, an affine or projective transform of $n$-dimensional space is represented by an $(n+1) \times (n+1)$ matrix.