Implement a 3×3 homogeneous transformation matrix for 2D scaling, a fundamental concept in 2D/3D Transformations. This transformation is crucial in Computer Vision as it allows images to be resized while maintaining their aspect ratio.
In 2D space, scaling is a linear transformation that enlarges or reduces objects by a certain factor. The scaling factors sx and sy determine how much an object is stretched or shrunk along the x and y axes. To represent this transformation mathematically, we use a matrix equation.
To create the scaling matrix, follow these steps:
Define the scaling factors sx and sy.
Construct a 3×3 matrix with sx and sy on the diagonal.
sx000sy0001
This technique is widely used in image processing applications.
Example
Input
scaling_matrix(2, 3)
Output
[[2,0,0],[0,3,0],[0,0,1]]
Reasoning
The inputs are two scale factors: sx=2 stretches every x-coordinate, sy=3 stretches every y-coordinate. The output is a 3×3 matrix acting on homogeneous points (x,y,1).
Plugging the factors into the description's matrix gives rows (2,0,0), (0,3,0), (0,0,1), i.e. [[2,0,0],[0,3,0],[0,0,1]].
Check it on the point (4,5): row 1 gives 2⋅4=8, row 2 gives 3⋅5=15, row 3 gives 1. So (4,5)↦(8,15).
The off-diagonal zeros in the top-left 2×2 block mean x never leaks into y (no rotation or shear). The first two entries of the third column are 0; that is where a translation tx,ty would sit, so nothing is added to x or y.
Notice the bottom-right 1: it keeps the homogeneous coordinate at 1, so the result is still a valid point. Since sx=sy, this scaling changes the aspect ratio; only sx=sy preserves it.
You are working with 2D homogeneous coordinates and linear transformations. In 2D, a point (x,y) is represented as a 3D vector (x,y,1). This lets you express geometric operations (like scaling, rotation, translation) as matrix multiplications with a 3×3 matrix. The big advantage is that you can compose multiple transforms just by multiplying their matrices (e.g., scale then rotate then translate), which is very convenient in computer vision and graphics.
A scaling transformation changes the size of objects relative to the origin. Scaling by factors sx and sy along the x- and y-axes maps a point (x,y) to (sxx,syy). In matrix form with homogeneous coordinates, this is captured by a diagonal matrix where the scale factors appear on the diagonal entries corresponding to x and y, and the homogeneous coordinate remains 1. The result is the standard 3×3 scaling matrix used throughout CV and graphics.
1. Background Knowledge
A 2D point in homogeneous coordinates:
(x,y)→xy1
A linear/affine transform in homogeneous coordinates is:
p′=Tp
where T is a 3×3 matrix and p,p′ are 3×1 homogeneous vectors.
For pure scaling (no rotation/translation), the transform should:
(x,y)→(sxx,syy)
and leave the homogeneous component as 1.
2. Algorithm / Approach
For “build a 2D scaling matrix” problems, the pattern is:
Start from the identity3×3 matrix.
Replace the diagonal entries that correspond to x and y with the scale factors sx and sy.
Keep the bottom-right entry as 1 (to preserve homogeneous coordinates).
Ensure all off-diagonal elements are 0 for pure scaling.
Conceptually, you are encoding:
x′y′1=sx000sy0001xy1
3. Step-by-Step Strategy
Understand Inputs
Identify the scaling factors sx and sy given by the problem (e.g., as variables or function parameters).
Initialize Matrix
Start with a 3×3 matrix initialized to all zeros or to the identity matrix.
Fill Diagonal Elements
Set entry (0,0) (row 0, col 0) to sx.
Set entry (1,1) to sy.
Set entry (2,2) to 1.
Fill Off-Diagonal Elements
All other entries (off-diagonal) should be 0 for pure scaling.
Return or Print the Matrix
Ensure the matrix is in the format expected by the platform (e.g., nested lists, 2D array, etc.).
Example in pseudocode:
def scaling_matrix_2d(sx, sy):
S = [[0.0 for _ in range(3)] for _ in range(3)]
S = sx
S = sy
S = 1.0
return S
4. Common Pitfalls
Wrong position of scale factors
Accidentally putting sx or sy in off-diagonal positions (e.g., instead of ) will introduce shear, not pure scaling.
Forgetting the homogeneous coordinate
Setting the bottom-right element to 0 or not setting it at all breaks homogeneous behavior; it must be 1.
Using translation slots
The last column (except bottom-right) corresponds to translation in homogeneous matrices. For pure scaling, those entries must be 0.
Mixing up row-major and column-major thinking
Make sure your indexing matches how you think about rows and columns. In most programming languages, M[i][j] means row i, column j.
5. Time & Space Complexity
Time complexity:
Constructing a fixed-size 3×3 matrix and writing constant many values is O(1).
Space complexity:
Storing a 3×3 matrix requires constant space O(1).