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.

Demosaicing: Interpolation Inside the Camera

Interpolation also happens before you ever see a photograph. A sensor pixel measures only one brightness, so a colour camera puts a colour filter array over it, most commonly the Bayer pattern, a repeating tile of one red, two green and one blue filter (RGGB). Green gets half the pixels because the eye is most sensitive to it. At every pixel two of the three colour values are therefore missing.

Demosaicing fills them in by interpolating each colour channel separately from the neighbours that did measure it. In the simple (bilinear) version:

  • green at a red or blue pixel is the average of its 4 green neighbours;
  • red at a green pixel is the average of its 2 red neighbours (left and right, or above and below, depending on the row);
  • red at a blue pixel is the average of its 4 diagonal red neighbours.

Interpolating each channel from its own samples keeps colours correct. Naive averaging across an edge produces colour fringes and a zipper pattern, so better methods interpolate along edges rather than across them.

Sources: Foundations of Computer Vision, ch. 21: Downsampling and Upsampling Images; Demosaicing (Wikipedia).


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