Image Interpolation

Interpolation estimates pixel values at non-integer coordinates when transforming or resizing images. It is essential in inverse warping, where the transformed coordinate usually does not align with integer pixel positions. Let be the stored value at integer column and row . Interpolation defines a function of continuous coordinates that uses the discrete samples as its parameters.

Nearest neighbor interpolation assigns to the new location the value of the pixel at the nearest integer position, found by rounding:

It is the fastest method, but it often produces blocky or jagged edges due to intensity jumps.

Bilinear interpolation adds smoothing by using the four pixels around . Let , , and , so that . Then

The four weights are non-negative and sum to , so the result is a weighted average of the four neighbors. It equals linear interpolation along on both rows, followed by linear interpolation along (or the other order). The result is continuous, but its slope jumps at pixel borders.

The lecture also lists Gaussian interpolation, a Gaussian-weighted average of nearby pixels that blurs slightly. Bicubic interpolation fits cubics through a neighborhood. It is smoother and sharper than bilinear, at a higher cost. Library versions are MATLAB’s interp2, scipy.ndimage.map_coordinates (order=0 nearest, order=1 bilinear, order=3 cubic) and cv2.remap.

Versus img[y, x]

The formulas above put the column first. NumPy arrays put the row first, so is img[y, x], and map_coordinates expects coordinates in (row, column) order. Swapping the two gives a plausible but wrong number with no error message. The first example below shows the size of the mistake.

Intuition

A digital image stores values only at grid points. Any value between them is a guess about what the underlying scene did between samples, which is the reconstruction problem of resampling. Nearest neighbor guesses that the image is constant around each sample, so it copies the closest one. Bilinear guesses that the image changes linearly between neighbors. Its weights have a simple picture. Draw the point inside the cell of four pixels, and split the cell into four rectangles through the point. Each corner gets the area of the rectangle on the opposite side of the point. As the point moves toward a corner, the rectangle opposite that corner grows, and so does its weight.

\usepackage{tikz}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{mid}{HTML}{8A8A8A}
\begin{tikzpicture}[x=1cm,y=1cm,>=latex]
\fill[cold!12] (0,0) rectangle (2,3);
\fill[hot!12] (2,0) rectangle (5,3);
\fill[hot!25] (0,3) rectangle (2,5);
\fill[cold!25] (2,3) rectangle (5,5);
\draw[thick] (0,0) rectangle (5,5);
\draw[dashed,mid] (2,0) -- (2,5);
\draw[dashed,mid] (0,3) -- (5,3);
\node[align=center] at (3.5,4) {\small $(1-\Delta x)(1-\Delta y)$\\ \small for $f(x_0, y_0)$};
\node[align=center] at (1,4) {\small $\Delta x(1-\Delta y)$\\ \small for $f(x_0{+}1, y_0)$};
\node[align=center] at (3.5,1.5) {\small $(1-\Delta x)\Delta y$\\ \small for $f(x_0, y_0{+}1)$};
\node[align=center] at (1,1.5) {\small $\Delta x\,\Delta y$\\ \small for\\ \small $f(x_0{+}1, y_0{+}1)$};
\fill (2,3) circle (2.5pt) node[above right] {$(x, y)$};
\foreach \p in {(0,0),(5,0),(0,5),(5,5)} \fill \p circle (3pt);
\node[below left] at (0,0) {$f(x_0, y_0)$};
\node[below right] at (5,0) {$f(x_0{+}1, y_0)$};
\node[above left] at (0,5) {$f(x_0, y_0{+}1)$};
\node[above right] at (5,5) {$f(x_0{+}1, y_0{+}1)$};
\draw[<->] (0,-0.6) -- (2,-0.6) node[midway,below] {$\Delta x$};
\draw[<->] (-0.6,0) -- (-0.6,3) node[midway,left] {$\Delta y$};
\end{tikzpicture}
\end{document}

The picture is drawn with increasing upward. In an image array row lies below row , but the formula and the area rule are the same.

Properties

(Exact on Ramps) Bilinear interpolation reproduces any image of the form exactly, and it never leaves the range of its four neighbors.

The weights form an average that is linear in along each row and linear in along each column. A linear ramp therefore comes back unchanged, which the second example below checks. Since all four weights are non-negative and sum to , the output lies between the smallest and largest of the four corner values. The term makes the surface curved along diagonals. With corners and , the value along the diagonal is . The method is linear in each coordinate separately, which is what “bilinear” means.

(Interpolation as Filtering) Nearest neighbor and bilinear interpolation are convolutions of the samples with a box kernel and a tent kernel.

The nearest-neighbor kernel is a box of width one pixel, so each sample spreads into a flat square. The bilinear kernel is the tent , so each sample spreads into a pyramid that reaches zero at the neighboring pixels. The tent is the convolution of two boxes. This ties interpolation to linear filters and to convolution in signal processing. For example, upsampling with bilinear interpolation equals inserting zeros between samples and then filtering with the kernel along each axis. Bicubic kernels have small negative lobes. They sharpen edges but can overshoot, for example above next to a bright edge. Clip the result before casting back to uint8, as explained in 1.1. Digital Images.

(Choosing a Method) Use nearest neighbor for labels and masks, bilinear as the default for photos, and bicubic when enlarging.

Nearest neighbor never creates new values. A binary mask stays binary, and a segmentation map keeps valid class labels. Bilinear averaging of labels and would invent label . For photos, bilinear is the usual default. It acts as a small blur, so repeated warps soften an image a little more each time. Composing all transformations into one matrix and resampling once avoids this. Bicubic keeps more sharpness when enlarging, at about four times the number of neighbors.

(Borders) Near the last row or column, the neighbor or falls outside the image.

Even a point exactly on the last column, with , reads index in a direct implementation. One fix clamps the index to the last valid value (copy edge). Another pads the image by one pixel. A third treats any point outside as outside and returns a fill value, where and are the image width and height. Warping code usually takes the last option and records it in an alpha mask.

Examples

(Easy) Bilinear Interpolation on a Patch

A patch has , , and . As a NumPy array with rows first, it is img = np.array([[10, 20], [30, 60]]). We want the value at .

(Medium) Sampling a Rotated Gradient

An image is a horizontal gradient with . It is rotated by about the origin with the lecture’s , using inverse warping. Consider the output pixel .