Image resizing explained: aliasing, downsampling, and interpolation methods
What shrinking an image throws away, why fine patterns turn into false ones without a blur first, how nearest-neighbor, bilinear and bicubic fill in new pixels, and which OpenCV flag to use for photos and for masks.
Resizing an image changes the number of pixels in its grid. Making it smaller means each new pixel has to summarize several old ones, and making it larger means new pixels have to be filled in between the old ones. Resizing comes up often in computer vision. Photos from different cameras get brought to one size before training, and large photos get shrunk so a model runs faster. Done carelessly, it creates patterns that were not in the scene or breaks the labels attached to the image.
What downsampling changes
Downsampling makes a smaller grid from a larger one, such as 512 by 512 down to 256 by 256. Each output pixel summarizes a region of the input, here a 2 by 2 block.
Three things change. Fine detail gets averaged away. The image has 4 times fewer pixels, so it takes less memory and less compute. And objects get smaller in pixels: an object 100 pixels wide becomes 50 pixels wide.

Aliasing
The fastest way to downsample by 2 is to keep every other pixel. That works on smooth images and fails on fine patterns. Take a 1D signal that alternates every pixel, like thin black and white stripes:
$$[0, 255, 0, 255, 0, 255, 0, 255]$$
Keep indices 0, 2, 4 and 6:
$$[0, 0, 0, 0]$$
The stripes vanished into flat black. Keeping the odd indices instead gives flat white. Neither result is a smaller version of the stripes. The stripes repeat faster than the new grid can record, so they turn into a pattern that was not there, in this case a constant. This is aliasing: detail finer than the new grid shows up as a false, coarser pattern.

Blur before shrinking
Anti-aliasing means removing the detail that is finer than the target grid before reducing the resolution. The usual way is a Gaussian blur sized to the shrink factor. The detail that would turn into false patterns is the same detail the blur removes, so what is left can be sampled safely.
Good resize functions do this automatically when shrinking, and some have a flag to turn it off. Turning it off to save time produces aliased training images.

Aliased vs anti-aliased resizing in code
PIL is a Python imaging library, and its resize takes a target size and a resampling method. Image.NEAREST picks one input pixel for each output pixel. Image.LANCZOS filters each region before sampling it:
import numpy as np
from PIL import Image
# a fine checkerboard, 2-pixel squares
i = np.indices((256, 256))
base = (i[0] // 2 + i[1] // 2) % 2
img = Image.fromarray((base * 255).astype(np.uint8))
# just picks samples
naive = img.resize((60, 60), Image.NEAREST)
# filters, then samples
clean = img.resize((60, 60), Image.LANCZOS)
print(np.array(naive).std(), np.array(clean).std())
# 127.5 vs 0.51The standard deviation measures how much the pixel values vary. The nearest-neighbor resize picks individual black or white squares, and they line up into wide blocks that were not in the pattern, so the values still swing between 0 and 255. The filtered resize averages each region first, so the checkerboard settles to an even mid-gray, which is what a 60 by 60 grid can hold of 2-pixel squares.

Upsampling
Upsampling makes a larger grid from a smaller one, such as 256 by 256 up to 512 by 512. Every new position between the original pixels has to be filled, and its value is estimated from the pixels that exist.
Upsampling cannot bring back detail that an earlier downsample removed. Enlarging a blurry image four times gives a larger blurry image.

A downsample-then-upsample round trip
PyTorch resizes image tensors with F.interpolate. The tensor here has shape (batch, channels, height, width):
import torch
import torch.nn.functional as F
# (1, 1, 4, 4)
x = torch.arange(16.).view(1, 1, 4, 4)
small = F.interpolate(
x, size=(2, 2), mode='bilinear',
align_corners=False)
# (1, 1, 2, 2)
print(small.shape)
back = F.interpolate(
small, size=(4, 4), mode='bilinear',
align_corners=False)
# (1, 1, 4, 4)
print(back.shape)
# False
print(torch.allclose(back, x))The shape comes back to 4 by 4, but the values do not. The downsample averaged each 2 by 2 block of four numbers into one. That gives 2.5, 4.5, 10.5 and 12.5. The upsample can only estimate values between those averages, so its first row is $[2.5, 3.0, 4.0, 4.5]$ instead of $[0, 1, 2, 3]$.

Labels have to be resized too
When an image is resized, the labels that describe it have to change with it. Bounding boxes and keypoints, which are pixel coordinates, have to be scaled by the same factor as the image. Masks have to be resized with nearest-neighbor so their class numbers stay valid, for reasons the interpolation section below shows. An image resized without its labels gives the model boxes that point at the wrong pixels.

Interpolation
When resizing or rotating, a position in the output grid usually falls between pixels of the input. Interpolation is the rule that estimates the value there.
Nearest-neighbor
Nearest-neighbor interpolation copies the value of the closest input pixel. For a target 0.7 of the way from a pixel with value 10 to a pixel with value 20, the output is 20, because that pixel is closer.
It never creates a new value, so a mask of class numbers stays valid after resizing. On photos it gives blocky, stair-stepped results, since each input pixel turns into a solid block.

Bilinear
Bilinear interpolation blends the nearest pixels, weighted by how close the target is to each. On one axis it is a straight-line blend. For the same target 0.7 of the way from 10 to 20:
$$10 + 0.7 \times (20 - 10) = 17$$
In 2D it blends the four pixels around the target. With the target at fractional position $f_x$ across and $f_y$ down, it blends along $x$ on the top row and on the bottom row, then blends those two results along $y$:
def bilinear(tl, tr, bl, br, fx, fy):
top = tl + fx * (tr - tl)
bot = bl + fx * (br - bl)
return top + fy * (bot - top)tl and tr are the top-left and top-right values, and bl and br are the bottom-left and bottom-right values. Bilinear is smooth and cheap, and it is the usual choice for resizing photos. It also creates in-between values like that 17, which is wrong for a mask of class numbers.

Bilinear interpolation on one square
Four pixels sit at the corners of a unit square. The top row holds 10 and 20, and the bottom row holds 30 and 40. The target is at $f_x = 0.25$, $f_y = 0.75$ from the top-left corner.
Top edge: $10 + 0.25 \cdot (20 - 10) = 12.5$
Bottom edge: $30 + 0.25 \cdot (40 - 30) = 32.5$
Between those in $y$: $12.5 + 0.75 \cdot (32.5 - 12.5) = 27.5$
bilinear(10, 20, 30, 40, 0.25, 0.75) returns the same 27.5. The result blends all four corners, weighted by how close the target is to each, and none of the four inputs held that value.

Bicubic and the tradeoffs
Bicubic interpolation blends the 16 nearest pixels, a 4 by 4 block, with a smoother weighting curve. It is sharper than bilinear when enlarging photos and a little slower. Near hard edges it can overshoot, which shows up as a thin light or dark halo called ringing.
Nearest keeps exact values but looks blocky. Bilinear is smooth with a mild blur, and bicubic is the sharpest of the three. When shrinking, blurring first has a bigger effect on the result than which of these methods you pick.

Choosing the interpolation flag in OpenCV
cv2.resize picks the method from its interpolation flag, so the flag is where the choice gets made. The size argument is (width, height):
import cv2
import numpy as np
img = np.random.randint(0, 256, (100, 150, 3), dtype=np.uint8)
seg = np.zeros((100, 150), dtype=np.uint8)
seg[:, 75:] = 2
h, w = seg.shape
photo = cv2.resize(
img, (w * 2, h * 2),
interpolation=cv2.INTER_LINEAR)
mask = cv2.resize(
seg, (w * 2, h * 2),
interpolation=cv2.INTER_NEAREST)
print(np.unique(mask)) # [0 2]INTER_LINEAR blends four neighbors and is the usual choice for photos. INTER_NEAREST copies the single closest pixel and is the safe choice for a label map: the enlarged mask still holds only the classes 0 and 2. INTER_CUBIC is sharper than linear and a little slower. INTER_AREA averages the pixels that fall into each output cell, which makes it the one to use when shrinking. Resizing the same seg with INTER_LINEAR returns the values 0, 1 and 2: pixels on the border between class 0 and class 2 get blended into class 1.

Nearest for masks, smooth methods for photos
Label images such as segmentation masks have to use nearest-neighbor, because any blend can produce a class number that does not belong there. Photos use bilinear or bicubic. Getting this backwards raises no error. It corrupts masks with wrong class numbers along every border, and it makes photos blocky.

When to resize, and common mistakes
Shrink with a filtered method, such as INTER_AREA in OpenCV or Image.LANCZOS in PIL. Enlarge photos with bilinear or bicubic. Resize masks, and any other image of class numbers, with nearest-neighbor. Then scale the boxes and keypoints by the same factors.
- Shrinking by picking pixels. Nearest-neighbor or every-other-pixel downsampling turns fine texture into false patterns. Use a method that averages or blurs first.
- Resizing a mask with bilinear. The border between two classes fills with class numbers that do not belong there.
- Resizing the image and forgetting the labels. Boxes and keypoints stay in the old coordinates and point at the wrong pixels.
- Expecting an enlarged image to gain detail. Upsampling fills in estimates between existing pixels and cannot restore what a downsample removed.
- Mixing up width and height.
cv2.resizetakes(width, height), while NumPy arrays have shape(height, width).
Related concepts
The blur that prevents aliasing is a Gaussian blur, which Image blurring and denoising covers along with the box and median filters. Stride in a convolution also shrinks an image, and Image convolution explained covers how stride and padding set the output shape. Masks resized with nearest-neighbor are often cleaned up afterward with opening and closing, which Morphological operations covers along with erosion and dilation.
QuiddityML teaches image resizing, aliasing and interpolation in the classical image processing unit of its Computer Vision track. The exercises there include writing bilinear from scratch and tracing tensor shapes through a downsample and an upsample (QuiddityML).