Adjust the scale factor (s) to compare nearest-neighbor interpolation (discrete replica) with bilinear interpolation (weighted average of the 4 neighbors).
2.12.7 EP02_07 🔍 Resizing (Scaling)
In this activity, you must implement image resizing using scale factors. Unlike simple subsampling, here we will use interpolation techniques to allow both image enlargement and reduction.
- Read two integers L and C, representing the dimensions of the original matrix.
- Read two real values \(s_x\) (scale along rows) and \(s_y\) (scale along columns).
- Read a string representing the interpolation method (
nearestorbilinear). - Read the integer values of the original matrix.
- Compute the new dimensions: \(L' = \text{round}(L \times s_x)\) and \(C' = \text{round}(C \times s_y)\).
- Print the resulting matrix with the new dimensions.
- See Figure 2.18 for a simulation of this EP.
📌 Important:
Inverse Mapping: For each pixel \((x', y')\) of the destination image, find the corresponding position in the source using \((x, y) = (x'/s_x, y'/s_y)\).
Interpolation:
nearest: Selects the value of the nearest pixel (rounding the coordinates).bilinear: Performs double linear interpolation among the four nearest neighboring pixels in the original image.Boundaries: Ensure that the mapping does not attempt to access indices outside the range \([0, L-1]\) and \([0, C-1]\).
2.12.7.1 🧠 Interpolation for Enlargement/Reduction
Resizing an image by factors \((s_x, s_y)\) requires filling gaps (in enlargement) or merging information (in reduction). The interpolation method defines the visual quality of the result:
| Method | Operation | Visual Effect |
|---|---|---|
| Nearest | Takes the value of the nearest neighbor. | Fast, but produces a “pixelated” or blocky effect. |
| Bilinear | Weighted average of the 4 neighbors (\(2 \times 2\)). | Smooths the image, reducing jaggedness. |
2.12.7.2 📋 Task (specification for VPL)
Input:
The first line contains L.
The second line contains C.
The third line contains the factors sx and sy.
The fourth line contains the method interp (nearest or bilinear).
The following lines contain the elements of the \(L \times C\) matrix.
Output:
The resized matrix with dimensions \(L' \times C'\).
2.12.7.3 📌 Examples
| Input | Output | Observation |
|---|---|---|
| 2 2 2.0 2.0 nearest 1 2 3 4 |
1 1 2 2 1 1 2 2 3 3 4 4 3 3 4 4 |
2× enlargement: each original pixel is replicated in a 2×2 block. The \(2\times2\) image becomes \(4\times4\). |
| 2 2 0.5 0.5 nearest 10 20 30 40 |
10 | 0.5× reduction: the \(2\times2\) image becomes \(1\times1\). With nearest, the only output pixel samples position \((0,0)=10\). |
%%writefile EP02_07.py
# Python code
import numpy as np
from morph import mm
# 1. Reading dimensions, factors, and method
l = int(input())
c = int(input())
sx, sy = map(float, input().split())
interp = input().strip()
# 2. Reading the original image
img = mm.readImg(l, c)
# 3. New dimensions
l_new = round(l * sx)
c_new = round(c * sy)
# 4. Resizing using mm.resize
# cv2.resize uses (width, height) = (columns, rows)
resultado = mm.resize(img, (c_new, l_new), method=interp)
# 5. Display
print(mm.drawImg(resultado))Overwriting EP02_07.py
TestSuite("EP02_07.py").run()✔️ EP02_07.cases already exists in casos/ 📋 5 case(s) loaded from casos/EP02_07.cases 🔍 Testing Python: EP02_07.py ✔️ Case1_Ampliacao_2x_Nearest: OK ✔️ Case2_Reducao_05x_Nearest: OK ✔️ Case3_Sem_Escala: OK ✔️ Case4_Ampliacao_Bilinear: OK ✔️ Case5_Escala_Assimetrica: OK 📊 Result: 5/5 (100.0%) 🎉 Congratulations! All tests passed.