Compose Transformations
Implement a function to compose multiple 2D transformations by multiplying their matrices, which is essential in computer vision for tasks like image formation and object recognition. This process involves combining various transformations such as rotation, scaling, and translation.
The concept of transformation matrices is crucial here, where each matrix Tiβ represents a specific transformation. The combined transformation is obtained by multiplying these matrices in a specific order, which is important due to the non-commutative nature of matrix multiplication.
Here are the steps to achieve this composition:
- Start with the identity matrix as the initial combined transformation matrix.
- Multiply each given transformation matrix with the current combined transformation matrix from right to left. The key to this process is understanding how matrix multiplication works and how it applies to 2D transformations.
This technique is widely used in image processing and robotics.
Example:
compose([[[1,0,5],[0,1,0],[0,0,1]], [[2,0,0],[0,2,0],[0,0,1]]])
[[2,0,10],[0,2,0],[0,0,1]]
-
We have two matrices: a translation T1β=β100β010β501ββ and a scaling T2β=β200β020β001ββ.
-
The combined transform is Tcombinedβ=T2ββ T1β:
- First row: [2β 1+0β 0+0β 0,2β 0+0β 1+0β 0,2β 5+0β 0+0β 1]=[2,0,10]
- Second row: [0β 1+2β 0+0β 0,0β 0+2β 1+0β 0,0β 5+2β 0+0β 1]=[0,2,0]
- Third row: [0,0,1] (identity row stays the same)
-
So the final output matrix is β200β020β1001ββ, which matches
[[2,0,10],[0,2,0],[0,0,1]].
Constraints:
- Input is a list of 3Γ3 matrices
- Return the composed transformation matrix
Youβre composing several 2D transformations by multiplying their matrices into a single combined matrix. The key ideas are: (1) each geometric transform (rotation, translation, scaling, etc.) can be written as a matrix (usually in homogeneous coordinates), and (2) applying multiple transforms in sequence is equivalent to multiplying their matrices in the correct order.
1. Background Knowledge
In 2D computer vision and graphics, points are often represented in homogeneous coordinates as 3D vectors [x,y,1]T. This allows all common transformsβtranslation, rotation, scaling, shearβto be represented as 3Γ3 matrices. A transformation T maps a point p to pβ²=Tβ p.
When you apply multiple transformations in sequence, say first T1β, then T2β, then T3β to a point p, the result is:
pβ²=T3ββ (T2ββ (T1ββ p))=(T3ββ T2ββ T1β)β p.So you can precompute a combined transformation matrix T_{\text{combined}} = T_n \cdot \dots \cdot T_2 \cdot T_1, and then apply it once to all points. The important detail is that matrix multiplication is not commutative: T2βT1βξ =T1βT2β in general, so order matters.
2. Algorithm / General Approach
The general pattern for these problems:
- Start from an identity matrix representing βdo nothingβ.
- Iterate over the given transforms in the order they are applied (right-to-left in terms of application to points).
- Multiply matrices to accumulate into a single combined matrix.
- Use the combined matrix for any subsequent point transformations (if required by the problem).
Conceptually: βcompose transformations by repeated matrix multiplication in the correct orderβ.
3. Step-by-Step Strategy
-
Choose matrix size For 2D transforms with translation, use homogeneous coordinates and 3Γ3 matrices.
-
Initialize the combined matrix
import numpy as np
T_combined = np.eye(3) # identity matrix
- Multiply transforms in the correct order
- If the problem defines:
and you have a list Ts = [T1, T2,..., Tn], then:
T_combined = np.eye(3)
for T in Ts:
# apply T after current T_combined: T_combined = T * T_combined
T_combined = T @ T_combined
- This produces Tnββ¦T2βT1β if Ts is in [T1, T2,..., Tn] order.
- (If needed) Apply to points For a point p=[x,y,1]T:
p_prime = T_combined @ p
- Return or output the final T_combined matrix as the solution.
4. Common Pitfalls
-
Order of multiplication
-
Applying transforms is right-to-left on vectors, but when accumulating you often loop left-to-right over the list and multiply as T_combined = T_i @ T_combined, not the other way around.
-
Reversing the multiplication order will give the wrong combined transform.
-
Using the wrong coordinate representation
-
Trying to represent translation with a 2Γ2 matrix does not work; translations require homogeneous coordinates and 3Γ3 matrices.
-
Mixing row-major vs column-major conventions
-
Most math libraries use column vectors with transforms on the left: pβ²=Tp.
-
Some graphics APIs use row vectors with transforms on the right. Be consistent with the convention assumed by your problem.
-
Floating-point inaccuracies
-
Repeated multiplication may introduce small numerical errors. For this problem itβs usually fine, but be aware of tiny deviations from exact values.
5. Time & Space Complexity
Assume each transformation matrix is kΓk (for 2D homogeneous transforms, k=3) and there are n transformations:
-
Time complexity
-
Each matrix multiplication is O(k3) with the standard algorithm.
-
Doing this for nβ1 compositions: O(nβ k3).
-
For fixed k=3, this is effectively O(n).
-
Space complexity
-
You only need to keep the current combined matrix and the matrix youβre multiplying in: O(k2) space.
-
For fixed k=3, this is constant space relative to n.