Skip to main content

Image Resizing and Interpolation

Compare nearest-neighbor and bilinear image resizing, see aliasing when downsampling, and understand aspect ratio, letterboxing and mask interpolation.

Resizing does more than change an image's dimensions. It places a new sampling grid over the old image and decides what value each new sample should have. The choice affects edges, small objects, and the correspondence between an image and its labels.

Where does an output pixel look?

Consider a one-dimensional row with input width WiW_i and output width WoW_o. A common convention maps the center of output pixel xox_o to the following input coordinate:

xi=(xo+0.5)WiWo−0.5x_i = (x_o + 0.5)\frac{W_i}{W_o} - 0.5

That coordinate is often fractional. Interpolation estimates the value there. The explorer uses this half-pixel convention on both axes and clamps samples at the image boundary. Different coordinate conventions can shift results, so matching the interpolation name alone may not reproduce another pipeline exactly.

Compare the two outputs, then shrink the checkerboard to 4 and 5 pixels. The source stays fixed; only the sampling grid changes.

Resample the same image
Input pattern
12 pixels
Input · 8 × 8

8 rows, 8 columns. Values range from 0 to 255. Open numeric values to inspect each row and column.

Numeric values
Input · 8 × 8: rows and columns start at 0; values rounded to four decimals.
Row / column01234567
00255025502550255
12550255025502550
20255025502550255
32550255025502550
40255025502550255
52550255025502550
60255025502550255
72550255025502550
Nearest · 12 × 12

12 rows, 12 columns. Values range from 0 to 255. Open numeric values to inspect each row and column.

Numeric values
Nearest · 12 × 12: rows and columns start at 0; values rounded to four decimals.
Row / column01234567891011
00255255025525502552550255255
125500255002550025500
225500255002550025500
30255255025525502552550255255
425500255002550025500
525500255002550025500
60255255025525502552550255255
725500255002550025500
825500255002550025500
90255255025525502552550255255
1025500255002550025500
1125500255002550025500
Bilinear · 12 × 12

12 rows, 12 columns. Values range from 0 to 255. Open numeric values to inspect each row and column.

Numeric values
Bilinear · 12 × 12: rows and columns start at 0; values rounded to four decimals.
Row / column01234567891011
00127.5212.542.5127.5212.542.5127.5212.542.5127.5255
1127.5127.5127.5127.5127.5127.5127.5127.5127.5127.5127.5127.5
2212.5127.570.8333184.1667127.570.8333184.1667127.570.8333184.1667127.542.5
342.5127.5184.166770.8333127.5184.166770.8333127.5184.166770.8333127.5212.5
4127.5127.5127.5127.5127.5127.5127.5127.5127.5127.5127.5127.5
5212.5127.570.8333184.1667127.570.8333184.1667127.570.8333184.1667127.542.5
642.5127.5184.166770.8333127.5184.166770.8333127.5184.166770.8333127.5212.5
7127.5127.5127.5127.5127.5127.5127.5127.5127.5127.5127.5127.5
8212.5127.570.8333184.1667127.570.8333184.1667127.570.8333184.1667127.542.5
942.5127.5184.166770.8333127.5184.166770.8333127.5184.166770.8333127.5212.5
10127.5127.5127.5127.5127.5127.5127.5127.5127.5127.5127.5127.5
11255127.542.5212.5127.542.5212.5127.542.5212.5127.50

Calculated with half-pixel centers and clamped edges, without an antialias prefilter. Try 4 pixels, then 5: the changing pattern exposes aliasing. Enlarging creates samples, not recovered detail.

Nearest and bilinear make different promises

Nearest-neighbor interpolation chooses a source sample. It preserves the set of possible values but creates abrupt steps when enlarged. Bilinear interpolation combines the four neighboring samples using distance-based weights. Within one cell, with fractional offsets uu and vv, it computes:

I(u,v)=(1−u)(1−v)I00+u(1−v)I10+(1−u)vI01+uvI11I(u,v) = (1-u)(1-v)I_{00} + u(1-v)I_{10} + (1-u)vI_{01} + uvI_{11}

At the center of samples 0, 100, 100, 200, all weights are one quarter, giving 100. This is a calculated estimate between existing samples. It does not establish that the original scene contained that value or recover detail lost before resizing.

Downsampling needs more than smooth-looking edges

A rapidly alternating checkerboard cannot be faithfully represented on an arbitrarily small grid. Sampling it directly can produce stripes, a false pattern, or a constant image depending on alignment. This is aliasing: different high-frequency inputs become indistinguishable after sampling.

An antialias filter reduces frequencies that the smaller grid cannot represent before sampling. The explorer intentionally omits this prefilter so the failure is visible. For practical resizing, make the antialias setting explicit. Torchvision's resize documentation describes supported interpolation modes, size conventions, and antialias behavior. Torchvision Resize

Preserve the geometry that the task needs

Resizing a 640 × 480 image directly to 320 × 320 uses horizontal scale 0.5 and vertical scale about 0.667. A circle becomes an ellipse. Preserving aspect ratio instead gives 320 × 240; padding 40 pixels above and below produces a square canvas without stretching the contents.

For that letterboxed image, a point maps as follows:

x′=0.5x,y′=0.5y+40x' = 0.5x, \qquad y' = 0.5y + 40

Boxes, landmarks, and masks need the same geometric transformation. When mapping a prediction back, first remove the padding, then divide by the scale. Cropping has a different tradeoff: it preserves scale and shape within the retained region but may remove part of an object.

Images and class masks need different interpolation

A class-index mask might contain 0 for background and 2 for a car. Bilinear interpolation between them can create 1, which could mean an entirely unrelated class. Use a label-preserving operation such as nearest-neighbor for discrete masks. Continuous logits or probability maps are different data and may be interpolated before choosing the final class.

Next, filtering and edge detection make local weighted sums explicit. Later, augmentation applies geometric changes to images and labels together.

If you found this explanation helpful, consider sharing it with others.

Mastodon