Image Scaling
Bi-Cubic Interpolation
Image Processing
Graphics Algorithms
Image Enhancement

Bi-Cubic Interpolation Algorithm for Image Scaling

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Bi-cubic interpolation is a common choice for resizing images when you want smoother results than nearest-neighbor or bilinear sampling. It estimates each new pixel from a neighborhood of nearby pixels, which usually preserves gradients and edges better during enlargement.

Why Bi-Cubic Looks Better

Nearest-neighbor scaling copies the closest source pixel. That is fast, but it produces jagged edges. Bilinear interpolation blends four neighbors and removes some blockiness, yet it can still look soft.

Bi-cubic interpolation uses a 4 by 4 neighborhood around the sampling point. Instead of fitting a straight line between values, it uses cubic curves in the horizontal and vertical directions. That extra context helps the algorithm produce smoother transitions while keeping more local detail.

In practice, bi-cubic interpolation is widely used for:

  • enlarging photos for display
  • resizing thumbnails with fewer artifacts
  • image preparation before computer-vision pipelines
  • graphics tools where quality matters more than minimal CPU cost

How the Sampling Step Works

When scaling, every destination pixel maps back to a floating-point position in the source image. Suppose a new pixel lands at source coordinate x = 12.4, y = 8.7. The integer part tells you the nearby source cell, and the fractional part tells you how far you are between known pixels.

Bi-cubic interpolation evaluates weighted contributions from the surrounding 16 pixels. The weights come from a cubic kernel. Pixels closer to the sampling position get larger weights, and pixels farther away get smaller weights. Because the kernel is smooth, the result changes smoothly as the sampling position moves.

Many implementations use the Catmull-Rom style parameter a = -0.5, although other values can sharpen or soften the result.

Python Example

The example below rescales a grayscale image represented as a list of lists. It shows the core algorithm without depending on an image library.

python
1def cubic_weight(t, a=-0.5):
2    t = abs(t)
3    if t <= 1:
4        return (a + 2) * t**3 - (a + 3) * t**2 + 1
5    if t < 2:
6        return a * t**3 - 5 * a * t**2 + 8 * a * t - 4 * a
7    return 0.0
8
9
10def clamp(value, low, high):
11    return max(low, min(high, value))
12
13
14def bicubic_resize(image, new_width, new_height):
15    src_height = len(image)
16    src_width = len(image[0])
17    result = [[0 for _ in range(new_width)] for _ in range(new_height)]
18
19    x_scale = src_width / new_width
20    y_scale = src_height / new_height
21
22    for out_y in range(new_height):
23        for out_x in range(new_width):
24            src_x = (out_x + 0.5) * x_scale - 0.5
25            src_y = (out_y + 0.5) * y_scale - 0.5
26            base_x = int(src_x)
27            base_y = int(src_y)
28
29            total = 0.0
30            weight_sum = 0.0
31
32            for m in range(-1, 3):
33                for n in range(-1, 3):
34                    px = clamp(base_x + n, 0, src_width - 1)
35                    py = clamp(base_y + m, 0, src_height - 1)
36                    wx = cubic_weight(src_x - (base_x + n))
37                    wy = cubic_weight(src_y - (base_y + m))
38                    weight = wx * wy
39                    total += image[py][px] * weight
40                    weight_sum += weight
41
42            result[out_y][out_x] = round(total / weight_sum)
43
44    return result
45
46
47source = [
48    [10, 20, 30, 40],
49    [20, 40, 60, 80],
50    [30, 60, 90, 120],
51    [40, 80, 120, 160],
52]
53
54scaled = bicubic_resize(source, 8, 8)
55for row in scaled[:3]:
56    print(row)

This code clamps border lookups so the algorithm does not read beyond the image edges. Production image libraries often optimize the same idea with vectorized math or low-level loops.

Quality and Tradeoffs

Bi-cubic interpolation is not magic. It cannot invent real detail that was never in the original image. What it does well is estimate a visually smoother surface between known pixels. That is why enlarged text screenshots may still look soft, while natural photos often look noticeably better than with simpler methods.

There is also a cost. Bi-cubic interpolation uses 16 source pixels per output pixel, so it is heavier than bilinear interpolation. On modern hardware that is usually acceptable for offline processing, but it can matter in real-time graphics or large batch jobs.

If you are working with sharp edges such as pixel art or UI icons, nearest-neighbor or a dedicated edge-aware scaler may be the better choice. Bi-cubic is best when you want natural smoothness rather than crisp block boundaries.

Common Pitfalls

  • Using bi-cubic interpolation for pixel art usually blurs deliberate hard edges. Use nearest-neighbor when you want exact block shapes.
  • Ignoring border handling leads to index errors or dark seams near the image boundary. Clamp, mirror, or pad edge coordinates consistently.
  • Assuming the method restores lost detail causes unrealistic expectations. Interpolation smooths estimates between pixels; it does not recover original high-resolution content.
  • Mixing color spaces can shift brightness or color. For color images, resize channels consistently and be aware of gamma-related differences in advanced pipelines.
  • Choosing the wrong kernel parameter can make the result too soft or too sharp. Test with representative images before fixing one setting across a project.

Summary

  • Bi-cubic interpolation resizes an image by combining a 4 by 4 neighborhood around each sample point.
  • It usually produces smoother and more natural enlargements than nearest-neighbor or bilinear interpolation.
  • The cubic kernel determines how much each surrounding pixel contributes to the final value.
  • The method costs more computation, but the visual gain is often worth it for photo-like content.
  • Correct edge handling and realistic expectations are as important as the interpolation formula itself.

Course illustration
Course illustration

All Rights Reserved.