Search

Search papers and lessons

Bilinear Interpolation

Level 2 · 13 min read

Bilinear interpolation estimates a value between four known grid points by blending them in two straight-line steps, one across and one up or down, which is how images are smoothly resized and textures are sampled.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Signal processing
  • Linear algebra
  • ReadReady to readNot yet

TL;DR

Bilinear interpolation estimates a value at a point that falls between four known grid values, a Function sampled on a grid. It blends the four values in two simple steps: first along one direction, then along the other. It is the standard way to resize an image smoothly and to look up values in a texture between stored pixels.

Builds on: Digital Image, Linear Function · Leads to: RoIAlign, Image Pyramid

Where it's used

Resizing photos and screenshots. When you zoom a photo in an image editor or a browser scales a picture to fit your screen, the new pixels usually fall between the old ones. Bilinear interpolation gives each new pixel a brightness that mixes its four nearest old pixels, weighted by how close the new pixel is to each. The result looks smooth instead of blocky. (Nearest-neighbour copying, which takes the closest old pixel as is, is faster but produces hard blocks.)

Texture sampling in 3D graphics. A game stores a wall texture as a small grid of colours. When the texture is drawn on a surface that is closer to the camera or at an angle, one screen pixel maps to a point between texels. Graphics hardware has performed bilinear filtering in its texture units for decades, so each sample costs only a few multiplications.

Mask R-CNN's RoIAlign. The 2017 Mask R-CNN paper for locating and outlining objects in photos needs to pull a fixed-size feature patch out of a feature map at positions that are not whole numbers. The older RoIPool step rounded those positions to the nearest cell, which shifted the features slightly. RoIAlign instead samples the feature map with bilinear interpolation, which the paper reports improved mask accuracy. See RoIAlign.

How it works

Take the smallest possible case: a 2 by 2 grid of values, and one query point inside the square. Let the grid points sit at the corners of the unit square, with the values

  • top-left (0, 0): 10
  • top-right (1, 0): 20
  • bottom-left (0, 1): 30
  • bottom-right (1, 1): 40

We want the value at the point (0.5, 0.25). Here x runs from left to right, y runs from top to bottom, and 0.5 is halfway across while 0.25 is a quarter of the way down.

  1. Find the position inside the cell. The query is 0.5 across and 0.25 down. These two fractions are the only things the method needs; they say how far the point is from each side. Check: both fractions lie between 0 and 1, so the point is inside the square.

  2. Blend across the top edge. Along the top row (y = 0), the point is halfway between 10 and 20. A straight-line blend at fraction 0.5 gives 10 + 0.5 × (20 − 10) = 15. Check: at fraction 0 the blend returns 10, and at fraction 1 it returns 20. It is halfway, so 15 is right.

  3. Blend across the bottom edge. Along the bottom row (y = 1), the same horizontal fraction 0.5 sits between 30 and 40. The blend is 30 + 0.5 × (40 − 30) = 35. Check: the bottom values are both 10 larger than the top values, so 35 is 20 larger than 15 as expected.

  4. Blend the two results up or down. We now have two values on the same vertical line, 15 at the top and 35 at the bottom. The query is a quarter of the way down, so the blend is 15 + 0.25 × (35 − 15) = 20. Check: the answer lies between 15 and 35, closer to 15 because the point is nearer the top.

  5. Output. The estimated value at (0.5, 0.25) is 20. Two straight-line blends produced it, and the four original values were the only input.

The visual below shows the same flow.

flowchart LR
  A["4 corner values"] --> B["blend across the top row"]
  A --> C["blend across the bottom row"]
  B --> D["blend between the two results, using the vertical fraction"]
  C --> D
  D --> E["estimated value"]

The same idea works on a line, which is the simplest version. A reader who knows Linear Function already has it: between two values, the output moves in a straight line as the input moves across the gap.

00.20.40.60.81101520fraction acrossvalue
  • blend of 10 and 20
Between the two grid values the blend is a straight line. At a fraction of 0.3 the value is 13.

The math

Definition: Bilinear interpolation

Let a rectangle have corners at x₀ < x₁ and y₀ < y₁, with known values f₀₀ = f(x₀, y₀), f₁₀ = f(x₁, y₀), f₀₁ = f(x₀, y₁) and f₁₁ = f(x₁, y₁). For a query point (x, y) inside the rectangle, define the fractions

tx=x−x0x1−x0,ty=y−y0y1−y0.t_x = \frac{x - x_0}{x_1 - x_0}, \qquad t_y = \frac{y - y_0}{y_1 - y_0}.

The bilinear estimate is

f(x,y)=(1−tx)(1−ty) f00+tx(1−ty) f10+(1−tx) ty f01+tx ty f11.f(x, y) = (1 - t_x)(1 - t_y)\, f_{00} + t_x (1 - t_y)\, f_{10} + (1 - t_x)\, t_y\, f_{01} + t_x\, t_y\, f_{11}.

Read the symbols in words: t_x is how far across the query sits, from 0 at the left edge to 1 at the right edge; t_y is the same going down. Each of the four products is a weight. The four weights add up to 1, so the output is a weighted average of the corner values.

Step 1 in symbols

For the example query, x₀ = 0, x₁ = 1, y₀ = 0, y₁ = 1, so t_x = 0.5 and t_y = 0.25. Nothing else changes.

Step 2 in symbols

Blend across the top edge (y = y₀). The straight-line blend along x is

f(x,y0)=(1−tx) f00+tx f10.f(x, y_0) = (1 - t_x)\, f_{00} + t_x\, f_{10}.

Use the same formula on the bottom edge with f₀₁ and f₁₁ in place of f₀₀ and f₁₀. This is the Linear Function form: a starting value plus a fraction times the change.

For the example: f(0.5, 0) = 0.5 × 10 + 0.5 × 20 = 15, and f(0.5, 1) = 0.5 × 30 + 0.5 × 40 = 35.

Step 3 in symbols

Blend those two results along y:

f(x,y)=(1−ty) f(x,y0)+ty f(x,y1).f(x, y) = (1 - t_y)\, f(x, y_0) + t_y\, f(x, y_1).

For the example: 0.75 × 15 + 0.25 × 35 = 11.25 + 8.75 = 20.

Why the two-stage form equals the four-weight form

Substitute Step 2 into Step 3 and expand. The four products in the Definition appear exactly, so the two routes give the same number. Note the product t_x t_y. It makes the whole function non-linear in (x, y) taken together, even though it is linear in x when y is fixed and linear in y when x is fixed. That is why the method is called bilinear rather than linear. For a general discussion of functions of two inputs see Multivariable Function.

Checking the example by the four weights

Weights: (1 − 0.5)(1 − 0.25) = 0.375, 0.5 × 0.75 = 0.375, 0.5 × 0.25 = 0.125, 0.5 × 0.25 = 0.125. They add to 1. The estimate is 0.375 × 10 + 0.375 × 20 + 0.125 × 30 + 0.125 × 40 = 3.75 + 7.5 + 3.75 + 5 = 20. Both routes agree.

Worked examples

Example
1. A line between two values

Two grid values are 10 at x = 0 and 20 at x = 1. Estimate the value at x = 0.3.

  1. The fraction across is t = 0.3 − 0 = 0.3 over a gap of 1, so t = 0.3.
  2. The blend is 10 + 0.3 × (20 − 10).
  3. The gap is 10, and 0.3 of it is 3.
  4. The estimate is 10 + 3 = 13.

This is the one-dimensional case. Bilinear interpolation is this blend applied twice.

Example
2. The standard case: four corners at the centre

The corners of a unit square hold f₀₀ = 2, f₁₀ = 6, f₀₁ = 4 and f₁₁ = 8. Estimate the value at the centre (0.5, 0.5).

  1. Across the top row: 2 + 0.5 × (6 − 2) = 2 + 2 = 4.
  2. Across the bottom row: 4 + 0.5 × (8 − 4) = 4 + 2 = 6.
  3. Up the middle: 4 + 0.5 × (6 − 4) = 4 + 1 = 5.
  4. Check with the four weights. Each weight is 0.5 × 0.5 = 0.25, so the estimate is (2 + 6 + 4 + 8) / 4 = 20 / 4 = 5.

At the centre, bilinear interpolation returns the plain average of the four corners.

Example
3. A realistic patch: enlarging a grayscale image

A 2 by 2 patch of a grayscale photo has brightness values 50 (top-left), 100 (top-right), 150 (bottom-left) and 200 (bottom-right), on a 0 to 255 scale. When the photo is enlarged, a new pixel lands at position (0.75, 0.5) inside this patch. Estimate its brightness.

  1. Across the top row: 50 + 0.75 × (100 − 50) = 50 + 37.5 = 87.5.
  2. Across the bottom row: 150 + 0.75 × (200 − 150) = 150 + 37.5 = 187.5.
  3. Up the middle: 87.5 + 0.5 × (187.5 − 87.5) = 87.5 + 50 = 137.5.
  4. The new pixel is about 137.5, a value between the four originals, so the enlarged image has no hard edge across this patch.

Try it: the same patch, a new pixel at (0.2, 0.6). Use the two-stage method and give the brightness. Answer: across the top gives 50 + 0.2 × 50 = 60; across the bottom gives 150 + 0.2 × 50 = 160; then 60 + 0.6 × (160 − 60) = 60 + 60 = 120. The four-weight check gives 0.32 × 50 + 0.08 × 100 + 0.48 × 150 + 0.12 × 200 = 16 + 8 + 72 + 24 = 120.

Common mistakes and tradeoffs

It is tempting to think that bilinear interpolation adds detail to an image. In fact it only invents values that lie between the existing ones, because the output is a weighted average of the four neighbours. An enlarged photo is smoother, not sharper, and it can look soft at large zoom factors. Sharpness needs a different idea, which is outside this lesson.

It is tempting to think that the method is linear. In fact it is linear along each axis separately, but the product term t_x t_y makes the whole function curved. If you assume straight-line behaviour across a whole cell you will get the wrong values near the corners.

It is tempting to think that the four corners are always the right inputs. In fact the method assumes the values change smoothly between grid points. Across a sharp edge, such as a black line on white paper, bilinear interpolation blurs the edge over one cell. For the sharpest edges, nearest-neighbour copying keeps the edge sharp but produces blocks. Shrinking an image is a different problem: sampling too sparsely folds fine detail into false patterns, which is the subject of the Nyquist–Shannon Sampling Theorem, and the usual cure is to smooth the image first, often with a Convolution.

When to use it: resizing images, looking up textures, reading a gridded field (for example temperatures on a map grid) at arbitrary places, and sampling feature maps at fractional positions.

When not to use it: when values jump between neighbours (pixel art, category labels, classification masks), where nearest-neighbour is correct; and when you need the best possible quality on large enlargements, where bicubic or higher-order methods look better. Those use more neighbours and more weights.

History

Linear interpolation is much older than computers. Navigators, astronomers and engineers used printed tables and filled in the values between entries by proportional estimates for centuries. The two-dimensional version followed naturally once tables were laid out as grids.

The method became central to computer graphics when images and textures had to be drawn at sizes and angles that did not match their stored pixels. Graphics hardware has carried out bilinear filtering in fixed circuitry for decades, which is why it is so cheap in games. In 2017 Mask R-CNN made it part of a deep learning detector: RoIAlign replaced rounding with bilinear sampling so that the features stayed aligned with the pixels they came from.

Recap

  • A grid stores values at fixed points. A query between points needs an estimate.
  • Bilinear interpolation takes the four corners of the cell around the query.
  • It blends across each pair of edges with the 1D straight-line rule, then blends the two results in the other direction.
  • The four weights add to 1, so the answer is a weighted average and never outside the corner values.
  • It is the basis of smooth image resizing, texture sampling and RoIAlign. Pyramids of resized images, covered in Image Pyramid, use the same resampling idea.

What to read next: RoIAlign applies this method inside a detector, and Image Pyramid stores images at several resolutions.

Check yourself

QuestionThe four corners of a unit cell are 0, 4, 8 and 12 (top-left, top-right, bottom-left, bottom-right). What does bilinear interpolation return at the centre (0.5, 0.5)?Show answer

Across the top: 0 + 0.5 × 4 = 2. Across the bottom: 8 + 0.5 × 4 = 10. Up the middle: 2 + 0.5 × (10 − 2) = 6. The centre value is the average of the four corners, (0 + 4 + 8 + 12) / 4 = 6, as expected.

QuestionA value is 10 at x = 0 and 30 at x = 1. What is the straight-line blend at x = 0.25?Show answer

The gap is 20, and 0.25 of it is 5. The estimate is 10 + 5 = 15.

QuestionWhy does bilinear interpolation not produce values outside the range of the four corner values, even though it can produce values in between?Show answer

Its four weights are all between 0 and 1 and add up to 1. So the output is a weighted average of the four corner values. An average of numbers lies between the smallest and largest of them, so the result cannot go below the smallest corner or above the largest.

QuestionYou enlarge a map of a binary mask, where each pixel is either 0 (background) or 1 (object). Why might bilinear interpolation be the wrong choice here, and what would you use instead?Show answer

Bilinear interpolation averages neighbouring values, so at the boundary it creates fractional values such as 0.5, which are not valid mask labels. Nearest-neighbour copying keeps each pixel as one of the original labels, so the object boundary stays a clean 0 or 1 edge.

QuestionA query point lies at t_x = 0.2 and t_y = 0.6 in a cell with corner values f₀₀ = 5, f₁₀ = 15, f₀₁ = 25 and f₁₁ = 35. Using the two-stage method, what is the estimate, and what would a value of t_x = 0.8 do to the answer?Show answer

Across the top: 5 + 0.2 × 10 = 7. Across the bottom: 25 + 0.2 × 10 = 27. Up: 7 + 0.6 × (27 − 7) = 7 + 12 = 19. Moving the query to t_x = 0.8 moves it closer to the right edge, so the answer rises toward the right-hand values: across the top becomes 13, across the bottom 33, and the result 13 + 0.6 × 20 = 25.

Sources