Resampling

A digital image stores samples, the values of a continuous image function at the points of a grid. Reconstruction turns the samples back into a continuous function, which amounts to guessing what did between them. Resampling moves the image to a new grid. We reconstruct and sample it again, more sparsely (downsampling) or more densely (upsampling).

Downsampling by a factor of keeps every other row and every other column. For an image indexed from , the operation is

The output has pixels, about one quarter as many. In NumPy it is X[::2, ::2]. A image becomes , then .

Upsampling by , written , places the old samples at the even positions of a grid with twice the height and width. It fills the new positions by interpolation. Nearest-neighbor interpolation copies each pixel into a block. Linear interpolation (bilinear in 2D) sets each new sample to the average of its nearest old samples.

The two operations are not inverses. With this alignment , because the old samples survive at the even positions. In general . Downsampling discards three quarters of the samples, and interpolation cannot recover detail that fell between the kept ones. Downsampling without a blur also causes aliasing, so the correct shrink is , as explained in 2.4. Anti-Aliasing. Repeating blur-and-shrink builds the Gaussian levels of an image pyramid.

Downsample Versus Subsample

The lecture says “filter the image, then subsample”, so there subsample means keeping every other pixel with no filtering. Discussion 4 defines the same way, as a pure subsample. Some libraries and texts use “downsample” for the filtered version. In these notes never blurs, and every anti-aliasing blur is written out explicitly.

Intuition

Many tasks need the same picture at another size. A thumbnail needs fewer pixels. Coarse-to-fine alignment in Project 1 searches a small copy first. Blending works on a whole ladder of sizes. The name describes the operation. We sample the same underlying scene function a second time, on a different grid. Digital audio shows the 1D version. Recording writes down samples of a sound wave, and playback must fill in the gaps between them before a speaker can play it.

\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\definecolor{keep}{HTML}{2F80ED}
\definecolor{guess}{HTML}{C9DDF8}
\begin{tikzpicture}[x=0.6cm,y=0.6cm,>=latex,font=\small]
\foreach \c in {(0,0),(2,0),(0,-2),(2,-2)} {\fill[keep] \c rectangle ++(1,-1);}
\draw[step=1] (0,-4) grid (4,0);
\node[below] at (2,-4.2) {$X$, $4 \times 4$};
\draw[->,thick] (4.5,-2) -- (6.5,-2) node[midway,above] {$\operatorname{down}_2$};
\fill[keep] (7,-1) rectangle (9,-3);
\draw[step=1] (7,-3) grid (9,-1);
\node[below] at (8,-4.2) {$\operatorname{down}_2(X)$, $2 \times 2$};
\draw[->,thick] (9.5,-2) -- (11.5,-2) node[midway,above] {$\operatorname{expand}_2$};
\fill[guess] (12,0) rectangle (16,-4);
\foreach \c in {(12,0),(14,0),(12,-2),(14,-2)} {\fill[keep] \c rectangle ++(1,-1);}
\draw[step=1] (12,-4) grid (16,0);
\node[below] at (14,-4.2) {$\operatorname{expand}_2(\operatorname{down}_2(X))$, $4 \times 4$};
\fill[keep] (0,-5.7) rectangle (0.5,-6.2);
\node[right] at (0.6,-5.95) {sample that survives};
\fill[guess] (7,-5.7) rectangle (7.5,-6.2);
\node[right] at (7.6,-5.95) {value guessed by interpolation};
\end{tikzpicture}
\end{document}

The diagram shows the asymmetry. Shrinking throws away samples. Whatever lived only in the white cells is gone, or worse, it shows up later as a false pattern. Growing adds samples but no information. Every light cell on the right is a fixed mix of the dark cells, so an upsampled image looks soft. A grid can always be made coarser safely after a blur. It can never be made finer without guessing.

Properties

(Averaging Is Blur Plus Subsample) Averaging each block is a box filter followed by .

Discussion 1 writes the vectorized version as (X[0::2, 0::2] + X[0::2, 1::2] + X[1::2, 0::2] + X[1::2, 1::2]) / 4. It is the cheapest downsample that blurs before it subsamples, and it removes the finest checkerboard pattern exactly. The box is a weak low-pass filter, since its spectrum is a sinc with side lobes (2.1.3. Blurring and Sharpening). A Gaussian blur before suppresses aliasing more evenly, which is why pyramids use it.

(Upsampling Adds No Detail) An upsampled image has four times the pixels and no new information.

Each new pixel of is a fixed weighted sum of old pixels, so the image carries the same information in more numbers. Nearest-neighbor copies create sharp block edges. Those edges are artifacts set by the old samples, and they hold no scene detail. With smooth interpolation, almost all of the content stays below the highest frequency the small grid could hold. That is why looks blurry next to . The difference is therefore the fine detail, and it becomes a level of the Laplacian pyramid in 2.5. Image Pyramids.

(Odd Sizes) Slicing rounds sizes up, so expanding does not always return the original shape.

X[::2] on rows keeps rows, and turns those into rows. Pyramid code that subtracts from must crop or pad to match shapes. Cropping the input to a multiple of before building levels avoids the problem.

(Float Before Arithmetic) Resampling arithmetic on uint8 arrays wraps around and truncates.

NumPy adds uint8 arrays modulo . With pixels and , (a + b) / 2 returns , since wraps to . Converting first with img.astype(np.float32) / 255 gives the intended . Casting back with astype(np.uint8) truncates, so becomes and becomes . Discussion 1 shows that an out-of-range can wrap to . Clip to the valid range before any cast.

Examples

(Easy) The Finest Checkerboard

A image is a checkerboard of black () and white () pixels, with . Its mean brightness is .

(Medium) A Round Trip on One Row

Take the row and use linear . Old samples go to the even positions. Each new sample in between is the average of its two neighbors, and the last new sample copies its left neighbor.

See Also