Edd's Knowledge Base

Interpolation estimates a value at a position where no sample exists, using the samples around it. Images are grids of samples, so any operation that moves pixels to non-integer positions, such as resizing, rotating or warping, needs it.

Linear Interpolation (1D)

Between two known points and , assume the value changes along a straight line:

The result is a weighted average of the two neighbours. At it is the midpoint. Example: values 10 at and 30 at give at , so .

Bilinear Interpolation (2D)

Interpolate along x on the two surrounding rows, then along y between those two results. The output blends the 4 nearest pixels. With fractional offsets (in x) and (in y) from the top-left neighbour :

def bilinear(img, x, y):
    x0, y0 = int(np.floor(x)), int(np.floor(y))
    x1, y1 = min(x0 + 1, img.shape[1] - 1), min(y0 + 1, img.shape[0] - 1)
    a, b = x - x0, y - y0
    return ((1-a)*(1-b)*img[y0, x0] + a*(1-b)*img[y0, x1]
            + (1-a)*b*img[y1, x0] + a*b*img[y1, x1])

Comparing the Methods

An 8 x 8 image enlarged 8x with three methods.

MethodUsesResultCost
Nearest neighbourThe single closest pixelBlocky, keeps exact original valuesCheapest
Bilinear4 neighboursSmooth, slightly blurredLow
Bicubic16 neighboursSmoother and sharper than bilinear, can overshoot (ringing)Higher

Nearest neighbour is preferred when values must not be mixed, for example label maps where each number is a class.

Resampling in Practice

  • Backward (inverse) mapping: to resize or rotate, loop over the output pixels, compute the source position each one comes from, and interpolate there. Pushing source pixels forward instead leaves holes and overlaps.
  • Enlarging (upsampling) needs interpolation to fill the new pixels. It cannot add real detail, only a smooth guess. The EXPAND step in Image Pyramids does exactly this.
  • Shrinking (downsampling) is different: interpolation alone does not prevent aliasing, so low-pass filter first (Aliasing and Downsampling).
  • Interpolation is itself a form of convolution with a small kernel: a triangle for linear, a cubic spline for bicubic (Cross-Correlation and Convolution). Doubling an image is the clearest case: insert zeros between the samples, then convolve with in each direction, which is bilinear interpolation.

Source: Foundations of Computer Vision, ch. 21: Downsampling and Upsampling Images.


Related: Computer Vision MOC, Image Warping, Image Pyramids, Aliasing and Downsampling, Image Representation and Indexing