DIP+CV · Programming Exercise

EP02_08 — 🔀 Shear Transformation

2.12.8 EP02_08 🔀 Shear Transformation

In this activity, you must implement the shear transformation on an image. Shear is an affine transformation that shifts each point in a fixed direction, by an amount proportional to its distance from a line parallel to that direction, resulting in a tilting effect.

  • Read two integers L and C, representing the matrix dimensions.
  • Read two real values \(sh_x\) (horizontal shear) and \(sh_y\) (vertical shear).
  • Read a string representing the interpolation method (nearest or bilinear).
  • Read the integer values of the original matrix.
  • Apply the transformation while maintaining the original image size (cropping anything that exceeds the boundaries).
  • Print the resulting matrix with dimensions \(L \times C\).
  • See Figure 2.19 for a simulation of this EP.

📌 Important:

  • Inverse Mapping: For each pixel \((x', y')\) of the destination image, compute the corresponding position in the source \((x, y)\) using the inverse shear matrix.
  • Filling: Coordinates that result in positions outside the original matrix must be filled with 0.
  • Coordinates: For the purposes of this implementation, consider \(x\) as the row index and \(y\) as the column index.

2.12.8.1 🧠 Affine Distortion

Shear alters the image geometry by tilting its axes. The relationship between the original coordinates \((x, y)\) and the transformed ones \((x', y')\) is given by:

\[\begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \begin{bmatrix} 1 & sh_x & 0 \\ sh_y & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}\]

This results in the following equations:

  • \(x' = x + sh_x \cdot y\)
  • \(y' = y + sh_y \cdot x\)

2.12.8.2 📋 Task (VPL specification)

Input:

The first line contains L.

The second line contains C.

The third line contains the factors shx and shy.

The fourth line contains the method interp (nearest or bilinear).

The following lines contain the elements of the \(L \times C\) matrix.

Output:

The transformed matrix with the same dimensions \(L \times C\).

2.12.8.3 📌 Examples

Input Output Observation
3
3
0.5 0.0
nearest
10 20 30
40 50 60
70 80 90
10 20 30
0 40 50
0 0 70
Horizontal shear: row \(i\) shifts by \(\lfloor i \cdot 0.5 \rfloor\) pixels. Row \(0→0\)px, row \(1→0\)px, row \(2→1\)px. Pixels shifted out are discarded, and empty positions are filled with \(0\).
2
2
0.0 1.0
nearest
10 20
30 40
10 0
30 20
Vertical shear: column \(j\) shifts down by \(\lfloor j \cdot 1.0 \rfloor\) pixels. Column \(0→0\)px (unchanged), column \(1→1\)px: \(20\) moves down to \((1,1)\) and \((0,1)\) becomes \(0\).
✂️ Simulator EP02_08: Shear 2D x' = x + shx·y | y' = y + shy·x

Adjust the horizontal (shx) and vertical (shy) shear coefficients to observe the angular deformation of the image via reverse coordinate mapping.

0.00
0.00
Original (4×4)
Sheared (Nearest Neighbor)
shx = 0.00, shy = 0.00 → no angular deformation (original image)
Figure 2.19: EP02_08 Simulator: 2D Shear Geometric Transformation (Horizontal and Vertical Shear)
%%writefile EP02_08.py
# Python code
Overwriting EP02_08.py
TestSuite("EP02_08.py").run()
✔️ EP02_08.cases already exists in casos/
📋 5 case(s) loaded from casos/EP02_08.cases

🔍 Testing Python: EP02_08.py
⚠️ EP02_08.py: Empty file (fewer than 3 lines). Tests skipped.