DSP: image processing
The earlier DSP chapters worked on one-dimensional signals. Images are two-dimensional signals, and most of the same ideas carry over: convolution, smoothing, differentiation and optimal linear filtering. Images also raise questions with no one-dimensional counterpart. Which pixels belong to which object? Where are the edges, lines and circles? Which points in one picture are the same as points in another? This chapter follows that path, from filtering through segmentation to shapes and local features.
The central questions
- Which scale? Every operation here looks at a neighbourhood, and its size decides what counts as noise and what as structure. Gaussian scale space makes the choice explicit, and blob and feature detectors make it automatic.
- Local or global? A global threshold or equalisation treats every pixel alike; adaptive filters, edge detectors and features look only at a neighbourhood.
- Pixels or shapes? Filters and segmentation produce images; the Hough transform and feature detectors produce lists of shapes and keypoints.
- What should be invariant? Brightness, contrast, rotation and scale all change between photographs of the same scene. Each method is designed to ignore some of them.
Conventions
Images are ndarray::Array2 arrays indexed (row, column), with rows increasing downwards: f64 for filtering and features, u8 for histogram methods. Where a method speaks of and , is the column and the row.
Kernels have odd dimensions and are centred on the output pixel. Outside the image, pixels are zero, replicated or reflected, as the Border mode says.
Testing against SciPy and scikit-image
The test images (a noisy scene of a disc and a square, drawn lines and circles, and Gaussian spots) and the reference results come from SciPy 1.17.1 and scikit-image 0.26.0, by book/generator/data/make_image_refs.py. The
filtered images were stored to 12 significant digits, which limits the differences seen:
Table 17.1. Image routines against SciPy and scikit-image.
| SciPy or scikit-image function and case | compared | result |
|---|---|---|
ndimage.correlate, mode constant | largest difference | 4.9e-13 |
ndimage.convolve, mode constant | largest difference | 4.9e-13 |
ndimage.correlate, mode nearest | largest difference | 4.9e-13 |
ndimage.convolve, mode nearest | largest difference | 4.9e-13 |
ndimage.correlate, mode reflect | largest difference | 4.9e-13 |
ndimage.convolve, mode reflect | largest difference | 4.9e-13 |
ndimage.gaussian_filter, σ = 1.5, mode nearest | largest difference | 5.0e-13 |
ndimage.gaussian_filter, σ = 2.3, mode nearest | largest difference | 8.7e-6 |
ndimage.gaussian_filter, σ = 1.5, mode reflect | largest difference | 5.0e-13 |
ndimage.sobel, along columns | largest difference | 4.9e-13 |
ndimage.sobel, along rows | largest difference | 4.9e-13 |
signal.wiener, 5 × 5, given noise | largest difference away from the edges (SciPy pads with zeros) | 5.0e-13 |
filters.threshold_otsu | threshold | 101 (scikit-image 101) |
feature.canny, σ = 2 | edge pixels; fraction of each within one pixel of the other’s | 230 and 232; 100% and 100% |
feature.canny, σ = 1 | edge pixels; fraction of each within one pixel of the other’s | 229 and 230; 100% and 100% |
segmentation.watershed, 3 markers | pixels labelled the same | 9216 of 9216 (100.00%) |
transform.hough_line and hough_line_peaks | the two strongest lines (θ, ρ) | (0°, 70.2), (117°, 7.2); scikit-image (0°, 70.0), (117°, 7.0) |
transform.hough_circle and hough_circle_peaks | centres (row, column) and radii | (62, 60, r 20), (30, 30, r 12); scikit-image (62, 60, r 20), (30, 30, r 12) |
feature.blob_log, σ from 1 to 10 in 10 log steps | blobs (row, column, σ) | (20, 20, σ 2.15), (30, 65, σ 3.59), (70, 30, σ 5.99), (72, 75, σ 2.78); scikit-image (20, 20, σ 2.15), (30, 65, σ 3.59), (70, 30, σ 5.99), (72, 75, σ 2.78) |
Everything agrees except the Gaussian blur at σ = 2.3, where the library’s kernel radius () is one longer than SciPy’s (), and the Hough lines’ , whose bins are placed differently. Canny’s edges match to within a pixel; scikit-image interpolates the gradient in non-maximum suppression, where the library compares with the nearest neighbours.
The morphology, distance-transform, labelling, rank-filter, derivative-filter and geometric-transform routines are compared with
scipy.ndimage on a separate test image, for every structuring element, boundary mode and spline order, by
book/generator/data/make_ndimage_refs.py:
Table 17.2. Morphology, distance transforms, labelling, rank and derivative filters and geometric transforms against scipy.ndimage.
| scipy.ndimage function and case | largest difference ÷ largest value |
|---|---|
| binary_erosion cross | identical |
| binary_dilation cross | identical |
| binary_opening cross | identical |
| binary_closing cross | identical |
| binary_fill_holes cross | identical |
| binary_erosion square | identical |
| binary_dilation square | identical |
| binary_opening square | identical |
| binary_closing square | identical |
| binary_fill_holes square | identical |
| binary_erosion cross 2 iterations | identical |
| binary_dilation disk5 | identical |
| grey_erosion square reflect | identical |
| grey_dilation disk5 reflect | identical |
| grey_erosion square nearest | identical |
| grey_dilation disk5 nearest | identical |
| grey_erosion square mirror | identical |
| grey_dilation disk5 mirror | identical |
| grey_erosion square wrap | identical |
| grey_dilation disk5 wrap | identical |
| grey_erosion square constant | identical |
| grey_dilation disk5 constant | identical |
| grey_opening square | identical |
| grey_closing square | identical |
| white_tophat disk5 | 1.4e-16 |
| black_tophat disk5 | 1.4e-16 |
| morphological_gradient square | 1.3e-16 |
| distance_transform_edt | 1.6e-15 |
| distance_transform_edt sampling 2 0.5 | 1.8e-16 |
| distance_transform_cdt taxicab | identical |
| distance_transform_cdt chessboard | identical |
| label cross | identical |
| label cross sizes | identical |
| label cross sum | 1.2e-16 |
| label cross mean | 2.4e-15 |
| label cross minimum | identical |
| label cross maximum | identical |
| label cross center_of_mass | 2.0e-15 |
| label cross find_objects | identical |
| label square | identical |
| label square sizes | identical |
| label square sum | 1.2e-16 |
| label square mean | 2.4e-15 |
| label square minimum | identical |
| label square maximum | identical |
| label square center_of_mass | 2.0e-15 |
| label square find_objects | identical |
| median_filter 3x3 reflect | identical |
| median_filter 3x3 nearest | identical |
| median_filter 3x3 mirror | identical |
| median_filter 3x3 wrap | identical |
| median_filter 3x3 constant | identical |
| median_filter 4x4 | identical |
| median_filter 2x5 | identical |
| percentile_filter 25 5x5 | identical |
| percentile_filter 100 3x3 | identical |
| minimum_filter 3x2 | identical |
| maximum_filter 2x3 | identical |
| rank_filter -2 disk5 | identical |
| gaussian_filter 1.5 reflect | 1.4e-15 |
| gaussian_filter 1.5 nearest | 1.4e-15 |
| gaussian_filter 1.5 mirror | 1.4e-15 |
| gaussian_filter 1.5 wrap | 1.7e-15 |
| gaussian_filter 1.5 constant | 1.8e-15 |
| gaussian_filter (1, 2.5) order (0, 1) | 4.1e-15 |
| gaussian_filter 2 order (2, 0) | 1.3e-15 |
| gaussian_filter 1.2 order (1, 1) | 8.8e-16 |
| gaussian_laplace 1.5 | 2.9e-15 |
| gaussian_gradient_magnitude 1.5 | 3.0e-15 |
| laplace | 4.3e-16 |
| map_coordinates order 0 constant | identical |
| map_coordinates order 1 constant | 4.2e-17 |
| map_coordinates order 2 constant | 3.3e-16 |
| map_coordinates order 3 constant | 5.9e-16 |
| map_coordinates order 4 constant | 1.5e-15 |
| map_coordinates order 5 constant | 9.2e-16 |
| map_coordinates order 0 nearest | identical |
| map_coordinates order 1 nearest | 1.7e-16 |
| map_coordinates order 2 nearest | 7.2e-16 |
| map_coordinates order 3 nearest | 5.3e-15 |
| map_coordinates order 4 nearest | 4.3e-12 |
| map_coordinates order 5 nearest | 2.1e-10 |
| map_coordinates order 0 mirror | identical |
| map_coordinates order 1 mirror | 5.0e-16 |
| map_coordinates order 2 mirror | 6.7e-16 |
| map_coordinates order 3 mirror | 8.4e-16 |
| map_coordinates order 4 mirror | 1.5e-15 |
| map_coordinates order 5 mirror | 9.2e-16 |
| map_coordinates order 0 reflect | identical |
| map_coordinates order 1 reflect | 1.7e-16 |
| map_coordinates order 2 reflect | 7.5e-16 |
| map_coordinates order 3 reflect | 1.2e-15 |
| map_coordinates order 4 reflect | 7.8e-16 |
| map_coordinates order 5 reflect | 1.0e-15 |
| map_coordinates order 0 wrap | identical |
| map_coordinates order 1 wrap | 7.5e-16 |
| map_coordinates order 2 wrap | 8.4e-16 |
| map_coordinates order 3 wrap | 1.2e-15 |
| map_coordinates order 4 wrap | 7.5e-15 |
| map_coordinates order 5 wrap | 3.2e-15 |
| affine_transform shear order 3 | 1.5e-15 |
| shift (2.5, -1.25) order 1 nearest | 1.7e-16 |
| shift (2.5, -1.25) order 3 | 1.8e-15 |
| zoom (1.5, 0.75) order 3 | 1.4e-15 |
| zoom (0.5, 2) order 1 | 8.5e-16 |
| rotate 30 reshape order 3 | 1.4e-15 |
| rotate -20 order 1 nearest | 1.9e-15 |
| rotate 30 reshape order 3 shape | identical |
| fourier_gaussian (1.5, 0.7) | 1.8e-15 |
| fourier_uniform (3, 4) | 1.6e-15 |
| fourier_ellipsoid (5, 3) | 1.7e-15 |
| fourier_shift (2.5, -1.25) | 1.2e-15 |
| fourier_gaussian real rows | 1.0e-15 |
| fourier_uniform real rows | 1.1e-15 |
| fourier_ellipsoid real rows | 1.1e-15 |
| fourier_shift real rows | 1.3e-15 |
Binary results, labels, chamfer distances and rank filters are identical. Everything else agrees to rounding error, except quintic
splines in Nearest mode, which agree to (both libraries pad the image by 12 pixels before prefiltering, which the
slowly decaying poles of order 5 do not quite forget).
In the library
All of these are in lib_jsl_dsp (API reference), under discrete::image.
| You need | Use | Entry |
|---|---|---|
| A convolution with border handling; a blur, gradient or sharpening | correlate2d, convolve2d, separable_convolve, gaussian_blur, sobel, sharpening | Image filtering |
| More contrast; less noise; less blur | histogram_equalization, wiener_filter, wiener_deconvolution | Contrast and restoration |
| A foreground mask, an edge map or a set of regions | otsu, canny_edge_detection, watershed, label_boundaries | Segmentation |
| Straight lines or circles | hough_lines, hough_transform, hough_line_peaks, hough_circles | The Hough transform |
| Blobs, keypoints and descriptors; matches between images | blob_detection, sift, surf, match_descriptors | Local features |
| To clean a binary mask; to measure depth inside objects; to count and measure objects | binary_opening, binary_closing, binary_fill_holes, grey_erosion, white_tophat, distance_transform_edt, label, regions | Morphology, distance transforms and labelling |
| To remove impulse noise; derivatives at a scale; to shift, rotate, zoom or warp an image | median_filter, rank_filter, gaussian_filter, gaussian_laplace, map_coordinates, affine_transform, rotate, zoom | Rank filters, derivative filters and geometric transforms |
| To smooth with a large Gaussian, box or disc at a fixed cost; to shift by a fraction of a pixel | fourier_gaussian, fourier_uniform, fourier_ellipsoid, fourier_shift, fourier_filter | Fourier-domain filters |