Bilinear Resizing
Intuition
To enlarge an image, each new pixel must be invented from nearby old pixels. Nearest-neighbour copies the closest pixel (blocky). Bilinear blends the four surrounding pixels, weighted by distance (smooth).
Precise explanation
Coordinate mapping (half-pixel centres, the convention OpenCV's INTER_LINEAR uses): for output
row with scale :
clamped to (same for columns). Let , , (and similarly ). Then
Vectorise by computing row and column coordinate vectors once and using fancy indexing with
np.ix_-style broadcasting.
Worked example
2×2 image [[0, 10], [20, 30]] → 4×4: output (0,0) maps to y = x = −0.25 → clamped 0 → 0; output (1,1) maps to 0.25, 0.25 → 0.75·0.75·0 + 0.75·0.25·10 + 0.25·0.75·20 + 0.25·0.25·30 = 7.5.
Common mistakes
- Using (shifts the image by half a pixel).
- Forgetting to clamp, indexing out of bounds.
- Integer division in scale computation.
Why this matters
Resizing to a fixed input size is the first step of almost every vision model; inconsistent resizing between training and deployment is a known source of accuracy loss.
References
- OpenCV Tutorials (OpenCV 4.13.0) — OpenCV team (
S023) - Scientific Python Lectures — Scientific Python developers (
S006)