Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 54 ยท Geometric Transformations

Interpolation and Image Quality

Compare INTER_NEAREST, LINEAR, CUBIC, LANCZOS4 and INTER_AREA, learn why shrinking causes aliasing, build image pyramids with pyrDown and pyrUp, and code a bilinear sampler.

Whenever you resize, rotate or warp an image, the output pixels land between the pixels of the input. Output pixel (10, 10) might correspond to input position (6.3, 7.8), and there is no pixel at 6.3. The rule you use to invent a value at such a position is called interpolation, and it decides whether your result is blocky, smooth, sharp or full of ugly artifacts.

This lesson compares OpenCV's interpolation flags, explains the one situation (shrinking) where plain interpolation goes wrong, and introduces image pyramids, the standard way to work at several resolutions. You will code a bilinear sampler for arbitrary float positions, watch bicubic overshoot on a sharp edge, and reproduce aliasing and the INTER_AREA fix in NumPy. The cv2 calls are shown read-only; every idea runs live in a NumPy stand-in. The earlier lessons Resizing, Cropping and Flipping and Translation, Rotation and Affine Transforms both use these flags.

The flags at a glance

Five ways to fill in a pixel

NEAREST Copy the closest pixel. Blocky but exact.
LINEAR Blend the 4 nearest. Smooth, fast, default.
CUBIC Use 16 neighbors. Sharper, can overshoot.
LANCZOS4 Use 64 neighbors. Sharpest, slowest.

More neighbors means better detail and more work.

1 / 2

The flags are used in cv2.resize, cv2.warpAffine, cv2.warpPerspective and cv2.remap:

Python
import cv2

small = cv2.resize(img, None, fx=0.25, fy=0.25, interpolation=cv2.INTER_AREA)
big   = cv2.resize(small, None, fx=4, fy=4, interpolation=cv2.INTER_CUBIC)
tilt  = cv2.warpAffine(img, M, (w, h), flags=cv2.INTER_LANCZOS4)

Note that warpAffine and warpPerspective take the flag through flags. INTER_AREA is designed for resize, so use it there.

Bilinear sampling by hand

Bilinear interpolation takes a position (x, y) with fractional parts, finds the four surrounding pixels, and blends them. First blend horizontally along the top pair and the bottom pair, then blend those two results vertically. The weights are just how close you are to each neighbor. A sampler that accepts any float position is also the building block of warps: a warp computes a source position for every output pixel and then calls the sampler.

Python A bilinear sampler

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

At (0.5, 0.5) the four weights are each one quarter, so the result is the plain average, (10 + 20 + 30 + 80) / 4 = 35. At a whole-number position the sampler returns the stored value, which is why bilinear never changes pixels it lands exactly on. In real code the clamping is the border handling that borderMode controls.

Cubic: sharper, with a catch

Bicubic interpolation looks at a 4 by 4 neighborhood (16 pixels) and fits a smooth cubic curve through them in each direction. Because the curve can swing past the data, a sharp edge produces overshoot: values slightly darker than the dark side and slightly brighter than the bright side, which you see as thin halos. In an 8-bit image the results are clipped to 0 to 255, but the halos remain. In exchange, edges look crisper than with bilinear.

OpenCV uses a cubic convolution kernel with the parameter a = -0.75. The 1D version below uses the same formula to show the effect on a step edge.

Python Linear versus cubic on a sharp edge

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at position 1.5, on the dark side of the edge: linear gives 0, but cubic gives -23.9, darker than any pixel in the row. On the bright side, at 3.5, linear gives 255 and cubic gives 278.9. Real OpenCV clamps those values to the 0 to 255 range when saving the uint8 result (Data Types, Ranges and Saturation explains how), but the neighboring pixels that stayed in range still carry a faint halo. INTER_LANCZOS4 is similar with an 8 by 8 neighborhood: sharper again, slower again, same family of ringing.

Shrinking and aliasing

Shrinking breaks the picture of "blend the few pixels around the sample position". If you shrink by a factor of four, each output pixel stands for a 4 by 4 block of input pixels, but bilinear or cubic interpolation only looks at the nearest 2 by 2 or 4 by 4 around one position. The rest of the block is ignored, which means fine detail can be skipped entirely or misrepresented. This is aliasing: thin stripes become fat stripes or turn into moire patterns, and a fence in the distance flickers when the video moves.

INTER_AREA fixes this by treating the output pixel as a small area: it averages all the input pixels that fall inside it. The result is slightly softer but honest.

Python Aliasing versus area averaging

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Picking every second pixel from the stripe pattern returns all zeros: the pattern vanished and the picture says "this region is black", which is wrong. Averaging returns 127.5 everywhere, which is the right answer for fine black-and-white detail seen from far away: mid-gray. The checkerboard shows the same effect in two dimensions. (In real OpenCV, INTER_AREA handles non-integer scale factors too, by weighting partial pixels; the reshape trick above is the integer-factor special case.)

Image pyramids

Sometimes you want not one resized image but a whole stack of them: the original, then half the size, then a quarter, and so on. That stack is an image pyramid. Pyramids let an algorithm find big things cheaply on a small level and refine them on a larger one, or search for an object of unknown size by scanning every level.

Python
level1 = cv2.pyrDown(img)       # blur with a 5x5 Gaussian, then drop every second row and column
level0_again = cv2.pyrUp(level1)  # insert zero rows/columns, then blur and scale by 4

pyrDown first blurs (to avoid the aliasing described above) and then keeps every second row and column. The output size is ((width + 1) // 2, (height + 1) // 2). pyrUp doubles the size, so it cannot recover the lost detail: the result is a blurry copy of the original. The difference between the original and its blurred-and-restored version is called a Laplacian pyramid level and is used in blending images smoothly.

Python pyrDown and pyrUp in NumPy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This stand-in keeps the structure of OpenCV's pair (blur then subsample; zero-insert then blur times four) but works in floating point and does not reproduce OpenCV's integer rounding exactly. On this smooth ramp the restored image is close to the original but not identical (the largest difference is 25.5 levels), and on an image with fine detail the loss would be larger, which is what a Laplacian pyramid stores. You will use pyramids again when matching templates at different scales.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which interpolation flag is the right default when you shrink a photo to a quarter of its size?

    INTER_AREA averages all the source pixels that fall into each output pixel, which avoids the aliasing you get when only a few neighbors are sampled.
  2. 2. You resize a segmentation mask where each pixel holds a class id (0, 1, 2). Which flag should you use?

    Blending the ids 0 and 2 would create 1, a different class. Nearest neighbor copies existing values only.
  3. 3. What is a visible side effect of bicubic interpolation on a sharp edge?

    The cubic curve can swing past the data points, producing slightly darker and brighter bands along sharp edges.
  4. 4. What size does cv2.pyrDown produce for a 101 by 75 (width by height) image?

    The new size is ((width + 1) // 2, (height + 1) // 2) = (51, 38).

Summary

  • Interpolation decides the value at positions that fall between input pixels; the flag trades speed for smoothness and sharpness: NEAREST, LINEAR (default), CUBIC, LANCZOS4.
  • Bilinear blends the four nearest pixels using fractional-distance weights; a float-position sampler is the core of every warp.
  • Cubic and Lanczos are sharper but overshoot near edges, producing faint halos after clipping.
  • When shrinking, use INTER_AREA: other modes skip pixels and alias. Use NEAREST for masks and label images.
  • pyrDown blurs then drops every other row and column; pyrUp zero-inserts and blurs. Pyramids support multi-scale search and smooth blending.

Next up: turn grayscale images into black-and-white decisions in Thresholding.