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 isX[::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 .
Compute and the block average. Then apply nearest-neighbor to each. Which result better represents the image? Every kept pixel has an even coordinate sum, so it is black. Hence is a image of zeros. If the grid started one column to the right (
C[::2, 1::2]), every kept pixel would be white. A one-pixel shift of the scene flips the result from black to white. Either way the mean is wrong ( or instead of ). This is an alias at frequency zero.Each block holds two black and two white pixels, so the block average is everywhere. The pattern is gone, but the brightness is right.
Nearest-neighbor turns either result into a constant image (, , or ). No interpolation brings the checkerboard back. A grid cannot hold a pattern that changes every pixel, so losing it is unavoidable. Showing a false solid color is avoidable, by averaging (blurring) first.
(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.
Compute and . Where is the second one wrong, and why there? Expanding first gives . Keeping the even positions returns exactly.
Shrinking first keeps . Expanding gives . The error is
The kept positions are exact, and all the error sits at the discarded positions. Linear interpolation assumes the row moves in a straight line between kept samples. The row rose from to unevenly, so the guess at position is off by . It dipped to between and , so the guess there is off by . The final has no kept sample to its right, so the end rule copies and misses by . Any wiggle shorter than two pixels is invisible after the shrink.
See Also
- 2.4. Anti-Aliasing, for why the blur must come before .
- 2.5. Image Pyramids, which repeat blur-and-shrink to build multiscale representations.
- 3.2. Image Warping, where resampling happens on a grid that is rotated or sheared instead of scaled.
- 2.10a Sampling, the signals view of sampling a continuous function.