Image Interpolation — From Nearest Neighbor to Bicubic

Why Interpolation Matters

Imagine you have a low-resolution photo and want to print it larger. Between every two pixels in the original image is now a “blank space” — where do the new pixels come from?

Interpolation is the solution: using known pixel values to estimate values at unknown positions. Image upscaling is essentially an interpolation problem.

$$ \text{Upscaled Image} = \text{Interpolation Algorithm}(\text{Original Pixels}) $$

The quality of interpolation directly affects the upscaled image. Too simple methods produce mosaic artifacts, while too complex methods may introduce ringing artifacts. Let’s progress step by step, from the simplest nearest neighbor interpolation to the most commonly used bicubic interpolation.

Nearest Neighbor Interpolation

Principle

Nearest neighbor interpolation is the most straightforward method: for each new pixel in the target image, find the pixel in the original image closest to it and directly copy its value.

Mathematical Expression

Given target coordinates $(x, y)$, the corresponding original pixel coordinate is:

$$ f(x, y) = I(\lfloor x + 0.5 \rfloor, \lfloor y + 0.5 \rfloor) $$

where $I$ is the original image, $\lfloor \cdot \rfloor$ is the floor function, and $+0.5$ implements rounding.

Pros and Cons

  • Pros: Extremely fast — requires only one lookup operation
  • Cons: Produces obvious jagged edges and blocky artifacts, worst quality
mermaid
flowchart TD
    TARGET["New pixel location<br/>(x, y)"] --> SEARCH["Find nearest original pixel"]
    SEARCH --> COPY["Directly copy pixel value"]
    COPY --> RESULT["Blocky effect<br/>Jagged edges"]

    classDef blue fill:#2196F3,color:#fff
    classDef orange fill:#FF9800,color:#fff
    classDef red fill:#f44336,color:#fff
    class TARGET,SEARCH blue
    class COPY orange
    class RESULT red

This method suits scenarios requiring extreme speed with low quality requirements (e.g., quick previews in games).

Bilinear Interpolation

Principle

Bilinear interpolation is smarter than nearest neighbor: it uses the values of $2 \times 2 = 4$ pixels around the target position and calculates the new pixel through weighted averaging.

Why “bilinear”? Because it first performs linear interpolation in one direction (e.g., x-direction), then performs linear interpolation in the other direction (y-direction). Two directions, each once — hence “bi-linear.”

Mathematical Derivation

Let the four pixels around target point $(x, y)$ be $Q_{11}, Q_{12}, Q_{21}, Q_{22}$, with coordinates $(x_1, y_1), (x_1, y_2), (x_2, y_1), (x_2, y_2)$ respectively.

Step 1: Perform linear interpolation twice in the x-direction

First calculate the top edge $R_1$ (between $Q_{11}$ and $Q_{21}$):

$$ R_1 = \frac{x_2 - x}{x_2 - x_1} Q_{11} + \frac{x - x_1}{x_2 - x_1} Q_{21} $$

Then calculate the bottom edge $R_2$ (between $Q_{12}$ and $Q_{22}$):

$$ R_2 = \frac{x_2 - x}{x_2 - x_1} Q_{12} + \frac{x - x_1}{x_2 - x_1} Q_{22} $$

Step 2: Perform linear interpolation once in the y-direction

$$ f(x, y) = \frac{y_2 - y}{y_2 - y_1} R_1 + \frac{y - y_1}{y_2 - y_1} R_2 $$

Combined, this gives the final bilinear interpolation formula.

Intuitive Understanding

How are weights distributed? The closer an original pixel is to the target pixel, the higher its weight. If $(x, y)$ is exactly in the center of four pixels, each pixel has a weight of 0.25; if it’s near the top-left corner, the top-left pixel $Q_{11}$ has the highest weight.

mermaid
flowchart TD
    TOP["Top pixel pair<br/>Q11, Q21"]
    BOTTOM["Bottom pixel pair<br/>Q12, Q22"]
    TOP -->|x-direction linear interp| R1["R1<br/>Top edge interp"]
    BOTTOM -->|x-direction linear interp| R2["R2<br/>Bottom edge interp"]
    R1 -->|y-direction linear interp| RESULT["Final result<br/>f(x, y)"]
    R2 --> RESULT

    classDef px fill:#2196F3,color:#fff
    classDef proc fill:#FF9800,color:#fff
    classDef res fill:#4CAF50,color:#fff
    class TOP,BOTTOM px
    class R1,R2 proc
    class RESULT res

Pros and Cons

  • Pros: Smooth results, moderate computational cost, much better quality than nearest neighbor
  • Cons: Blurs image edges and details — the smoothing nature of linear interpolation softens sharp edges

Bilinear interpolation is one of the most commonly used methods in image processing (the default in OpenCV’s resize), suitable for most daily scenarios.

Bicubic Interpolation

Principle

Bicubic interpolation further expands the neighborhood: it uses the values of $4 \times 4 = 16$ pixels around the target position and calculates the new pixel through cubic polynomial interpolation. Weights are calculated using a cubic interpolation kernel function.

“Bi-cubic” similarly means performing cubic interpolation in both directions (x and y).

Keys Kernel Function

The most commonly used cubic interpolation kernel is the Keys kernel (Keys’ cubic kernel), defined piecewise as:

$$ W(x) = \begin{cases} (a+2)|x|^3 - (a+3)|x|^2 + 1 & |x| \leq 1 \ a|x|^3 - 5a|x|^2 + 8a|x| - 4a & 1 < |x| < 2 \ 0 & |x| \geq 2 \end{cases} $$

where the parameter $a$ is typically -0.5 or -0.75. When $a = -0.5$, this kernel function approximates the ideal sinc function:

$$ \text{sinc}(x) = \frac{\sin(\pi x)}{\pi x} $$

The sinc function is the ideal filter for image reconstruction, but it only decays to zero at infinity, making it impractical. The Keys kernel truncates the sinc function with finite support, preserving good frequency characteristics while ensuring computational efficiency.

2D Interpolation Formula

For target position $(x, y)$, the final bicubic interpolation formula is:

$$ f(x, y) = \sum_{i=-1}^{2} \sum_{j=-1}^{2} I(x_i, y_j) \cdot W(x - x_i) \cdot W(y - y_j) $$

where $I(x_i, y_j)$ are the values of the 16 surrounding pixels, and $W$ is the Keys kernel function.

Intuitive Understanding

Compared to bilinear, bicubic uses more neighboring pixels (16 vs 4) and has more complex weight distribution — the Keys kernel transitions smoothly near the center and decays rapidly at larger distances. This allows bicubic interpolation to better preserve edges and details.

mermaid
flowchart TD
    NEIGHBOR["4x4 neighborhood<br/>16 pixels"]
    KERNEL["Keys kernel function<br/>Calculate weights"]
    WEIGHT["Each pixel's weight"]
    SUM["Weighted sum"]

    NEIGHBOR --> KERNEL
    KERNEL --> WEIGHT
    WEIGHT --> SUM

    classDef px fill:#2196F3,color:#fff
    classDef proc fill:#FF9800,color:#fff
    classDef res fill:#4CAF50,color:#fff
    class NEIGHBOR px
    class KERNEL,WEIGHT proc
    class SUM res

Pros and Cons

  • Pros: Sharper results than bilinear, better edge preservation, higher quality
  • Cons: Larger computational cost (16 pixels vs 4), may produce slight ringing artifacts — wavelike artifacts near strong edges

Bicubic interpolation is the preferred method for high-quality image upscaling, widely used in image editing software like Photoshop and GIMP.

Comparison of Three Methods

The following table summarizes the characteristics of the three interpolation methods:

MethodNeighborhood PixelsSpeedQualityEdge PreservationArtifact Type
Nearest Neighbor1⭐⭐⭐⭐⭐❌ PoorBlocky, jagged
Bilinear4⭐⭐⭐⭐⭐⭐⚖️ ModerateBlur
Bicubic16⭐⭐⭐⭐✅ GoodRinging (slight)
mermaid
flowchart TD
    SPEED["Need speed<br/>Real-time preview"]
    BALANCE["Balance speed and quality<br/>Daily use"]
    QUALITY["Need highest quality<br/>Professional use"]

    NN["Nearest Neighbor<br/>Interpolation"]
    BI["Bilinear<br/>Interpolation"]
    BICUBIC["Bicubic<br/>Interpolation"]

    SPEED --> NN
    BALANCE --> BI
    QUALITY --> BICUBIC

    classDef orange fill:#FF9800,color:#fff
    classDef green fill:#4CAF50,color:#fff
    classDef red fill:#f44336,color:#fff
    class SPEED,BALANCE,QUALITY orange
    class NN red
    class BI orange
    class BICUBIC green

Summary

The three interpolation methods represent different design philosophies:

  • Nearest Neighbor: Minimalism — direct copy, no questions asked
  • Bilinear: Pragmatism — four neighbors, linear weighting
  • Bicubic: Perfectionism — sixteen neighbors, cubic weighting, approaching the ideal

In practice, bilinear interpolation already satisfies most requirements. Bicubic interpolation is used for professional image processing or print output. Nearest neighbor interpolation suits quick previews or special scenarios that need to preserve pixel-sharp boundaries (e.g., pixel art).

In the next article, we’ll introduce more modern deep learning methods — how super-resolution neural networks surpass these traditional interpolation algorithms.