v9.1.1
Menu

2D Scaling Matrix

easyFREE2D/3D TransformationsGeometryCV: Image Formation

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 sxs_x and sys_y determine how much an object is stretched or shrunk along the xx and yy axes. To represent this transformation mathematically, we use a matrix equation.

To create the scaling matrix, follow these steps:

  1. Define the scaling factors sxs_x and sys_y.
  2. Construct a 3×3 matrix with sxs_x and sys_y on the diagonal.
(sx000sy0001)\begin{pmatrix} s_x & 0 & 0 \\ 0 & s_y & 0 \\ 0 & 0 & 1 \end{pmatrix}

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=2s_x = 2 stretches every xx-coordinate, sy=3s_y = 3 stretches every yy-coordinate. The output is a 3×33 \times 3 matrix acting on homogeneous points (x,y,1)(x, y, 1).
  • Plugging the factors into the description's matrix gives rows (2,0,0)(2, 0, 0), (0,3,0)(0, 3, 0), (0,0,1)(0, 0, 1), i.e. [[2,0,0],[0,3,0],[0,0,1]].
  • Check it on the point (4,5)(4, 5): row 1 gives 2⋅4=82 \cdot 4 = 8, row 2 gives 3⋅5=153 \cdot 5 = 15, row 3 gives 11. So (4,5)↦(8,15)(4, 5) \mapsto (8, 15).
  • The off-diagonal zeros in the top-left 2×22 \times 2 block mean xx never leaks into yy (no rotation or shear). The first two entries of the third column are 00; that is where a translation tx,tyt_x, t_y would sit, so nothing is added to xx or yy.
  • Notice the bottom-right 11: it keeps the homogeneous coordinate at 11, so the result is still a valid point. Since sx≠sys_x \ne s_y, this scaling changes the aspect ratio; only sx=sys_x = s_y preserves it.

Constraints

  • sx and sy are positive floating-point numbers

Learn this in

More from CV: Image Formation

solution.py

Test results

Sign in to run your code against the test cases.