university computervision week1 theory

Nearest Neighbor Interpolation

Source:

  • HC1a Images and Interpolation

Rule

Nearest neighbor interpolation chooses the sample whose index is closest to :

This is basically rounding.

Example

If you know samples:

then:

Why it becomes a staircase

For many nearby values, the chosen sample does not change.

Example:

So the output is constant on that whole interval.

Constant interval + sudden jump + constant interval = staircase shape.

The handwritten notes explicitly connect this with the rounding rule:

round(x) == floor(x + 0.5)

So nearest neighbor is really “round the coordinate, then copy that sample”.

Pros

  • very simple
  • very fast
  • preserves exact sample values

Cons

  • blocky
  • jagged edges
  • not smooth

Python

import numpy as np
 
F = np.array([2.0, 5.0, 3.0, 7.0])
 
def nearest_neighbor(F, x):
    k = int(np.floor(x + 0.5))
    k = max(0, min(k, len(F) - 1))
    return F[k]
 
for x in [0.1, 0.5, 1.2, 2.7]:
    print(x, nearest_neighbor(F, x))