Adjust the rotation angle (θ) via slider or quick shortcuts to observe the trigonometric transformation of coordinates around the image center.
● Green square with orange marker (top-right corner) – rotation around the center.
In this activity, you must implement the rotation of an image around its geometric center. This operation requires coordinate mapping and the use of interpolation techniques to determine the new pixel values.
nearest or bilinear).📌 Important:
Inverse Mapping: To avoid “holes” in the final image, iterate over each pixel \((x', y')\) of the destination image and compute its corresponding position \((x, y)\) in the original image using the inverse rotation matrix.
Interpolation:
nearest: Assigns the value of the pixel closest to the computed coordinate.
bilinear: Computes a weighted average based on the 4 nearest neighbors.
Borders: Pixels whose origin \((x, y)\) falls outside the bounds of the original image must be filled with 0.
The rotation of a point \((x, y)\) relative to the origin by an angle \(\theta\) is given by the transformation matrix. To rotate around a center \((x_c, y_c)\), we first translate the center to the origin, rotate, and translate back:
\[\begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \begin{bmatrix} \cos\theta & -\sin\theta & x_c \\ \sin\theta & \cos\theta & y_c \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x-x_c \\ y-y_c \\ 1 \end{bmatrix}\]
Tip: Use inverse mapping to ensure that all pixels of the output image are correctly filled.
Input:
The first line contains L.
The second line contains C.
The third line contains the angle theta (in degrees) and the method interp (nearest or bilinear).
The following lines contain the elements of the \(L \times C\) matrix.
Output:
The rotated matrix with L rows and C columns.
| Input | Output | Observation |
|---|---|---|
| 2 2 90 nearest 1 2 3 4 |
3 1 4 2 |
90° clockwise rotation: column 0 becomes row 0 (from bottom to top). \((0,0)=1→(1,0)\), \((1,0)=3→(0,0)\), \((0,1)=2→(1,1)\), \((1,1)=4→(0,1)\). |
| 3 3 45 bilinear 0 0 0 0 255 0 0 0 0 |
0 180 0 180 255 180 0 180 0 |
45° rotation: the central pixel remains \(255\); the direct neighbors receive an interpolated value \(\approx 180\) via bilinear; the corners remain \(0\). |
Adjust the rotation angle (θ) via slider or quick shortcuts to observe the trigonometric transformation of coordinates around the image center.
● Green square with orange marker (top-right corner) – rotation around the center.
%%writefile EP02_06.py
# Python codeOverwriting EP02_06.py
TestSuite("EP02_06.py").run()✔️ EP02_06.cases already exists in casos/
📋 5 case(s) loaded from casos/EP02_06.cases
🔍 Testing Python: EP02_06.py
⚠️ EP02_06.py: Empty file (fewer than 3 lines). Tests skipped.