Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 casecomparedresult
ndimage.correlate, mode constantlargest difference4.9e-13
ndimage.convolve, mode constantlargest difference4.9e-13
ndimage.correlate, mode nearestlargest difference4.9e-13
ndimage.convolve, mode nearestlargest difference4.9e-13
ndimage.correlate, mode reflectlargest difference4.9e-13
ndimage.convolve, mode reflectlargest difference4.9e-13
ndimage.gaussian_filter, σ = 1.5, mode nearestlargest difference5.0e-13
ndimage.gaussian_filter, σ = 2.3, mode nearestlargest difference8.7e-6
ndimage.gaussian_filter, σ = 1.5, mode reflectlargest difference5.0e-13
ndimage.sobel, along columnslargest difference4.9e-13
ndimage.sobel, along rowslargest difference4.9e-13
signal.wiener, 5 × 5, given noiselargest difference away from the edges (SciPy pads with zeros)5.0e-13
filters.threshold_otsuthreshold101 (scikit-image 101)
feature.canny, σ = 2edge pixels; fraction of each within one pixel of the other’s230 and 232; 100% and 100%
feature.canny, σ = 1edge pixels; fraction of each within one pixel of the other’s229 and 230; 100% and 100%
segmentation.watershed, 3 markerspixels labelled the same9216 of 9216 (100.00%)
transform.hough_line and hough_line_peaksthe two strongest lines (θ, ρ)(0°, 70.2), (117°, 7.2); scikit-image (0°, 70.0), (117°, 7.0)
transform.hough_circle and hough_circle_peakscentres (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 stepsblobs (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 caselargest difference ÷ largest value
binary_erosion crossidentical
binary_dilation crossidentical
binary_opening crossidentical
binary_closing crossidentical
binary_fill_holes crossidentical
binary_erosion squareidentical
binary_dilation squareidentical
binary_opening squareidentical
binary_closing squareidentical
binary_fill_holes squareidentical
binary_erosion cross 2 iterationsidentical
binary_dilation disk5identical
grey_erosion square reflectidentical
grey_dilation disk5 reflectidentical
grey_erosion square nearestidentical
grey_dilation disk5 nearestidentical
grey_erosion square mirroridentical
grey_dilation disk5 mirroridentical
grey_erosion square wrapidentical
grey_dilation disk5 wrapidentical
grey_erosion square constantidentical
grey_dilation disk5 constantidentical
grey_opening squareidentical
grey_closing squareidentical
white_tophat disk51.4e-16
black_tophat disk51.4e-16
morphological_gradient square1.3e-16
distance_transform_edt1.6e-15
distance_transform_edt sampling 2 0.51.8e-16
distance_transform_cdt taxicabidentical
distance_transform_cdt chessboardidentical
label crossidentical
label cross sizesidentical
label cross sum1.2e-16
label cross mean2.4e-15
label cross minimumidentical
label cross maximumidentical
label cross center_of_mass2.0e-15
label cross find_objectsidentical
label squareidentical
label square sizesidentical
label square sum1.2e-16
label square mean2.4e-15
label square minimumidentical
label square maximumidentical
label square center_of_mass2.0e-15
label square find_objectsidentical
median_filter 3x3 reflectidentical
median_filter 3x3 nearestidentical
median_filter 3x3 mirroridentical
median_filter 3x3 wrapidentical
median_filter 3x3 constantidentical
median_filter 4x4identical
median_filter 2x5identical
percentile_filter 25 5x5identical
percentile_filter 100 3x3identical
minimum_filter 3x2identical
maximum_filter 2x3identical
rank_filter -2 disk5identical
gaussian_filter 1.5 reflect1.4e-15
gaussian_filter 1.5 nearest1.4e-15
gaussian_filter 1.5 mirror1.4e-15
gaussian_filter 1.5 wrap1.7e-15
gaussian_filter 1.5 constant1.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.52.9e-15
gaussian_gradient_magnitude 1.53.0e-15
laplace4.3e-16
map_coordinates order 0 constantidentical
map_coordinates order 1 constant4.2e-17
map_coordinates order 2 constant3.3e-16
map_coordinates order 3 constant5.9e-16
map_coordinates order 4 constant1.5e-15
map_coordinates order 5 constant9.2e-16
map_coordinates order 0 nearestidentical
map_coordinates order 1 nearest1.7e-16
map_coordinates order 2 nearest7.2e-16
map_coordinates order 3 nearest5.3e-15
map_coordinates order 4 nearest4.3e-12
map_coordinates order 5 nearest2.1e-10
map_coordinates order 0 mirroridentical
map_coordinates order 1 mirror5.0e-16
map_coordinates order 2 mirror6.7e-16
map_coordinates order 3 mirror8.4e-16
map_coordinates order 4 mirror1.5e-15
map_coordinates order 5 mirror9.2e-16
map_coordinates order 0 reflectidentical
map_coordinates order 1 reflect1.7e-16
map_coordinates order 2 reflect7.5e-16
map_coordinates order 3 reflect1.2e-15
map_coordinates order 4 reflect7.8e-16
map_coordinates order 5 reflect1.0e-15
map_coordinates order 0 wrapidentical
map_coordinates order 1 wrap7.5e-16
map_coordinates order 2 wrap8.4e-16
map_coordinates order 3 wrap1.2e-15
map_coordinates order 4 wrap7.5e-15
map_coordinates order 5 wrap3.2e-15
affine_transform shear order 31.5e-15
shift (2.5, -1.25) order 1 nearest1.7e-16
shift (2.5, -1.25) order 31.8e-15
zoom (1.5, 0.75) order 31.4e-15
zoom (0.5, 2) order 18.5e-16
rotate 30 reshape order 31.4e-15
rotate -20 order 1 nearest1.9e-15
rotate 30 reshape order 3 shapeidentical
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 rows1.0e-15
fourier_uniform real rows1.1e-15
fourier_ellipsoid real rows1.1e-15
fourier_shift real rows1.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 needUseEntry
A convolution with border handling; a blur, gradient or sharpeningcorrelate2d, convolve2d, separable_convolve, gaussian_blur, sobel, sharpeningImage filtering
More contrast; less noise; less blurhistogram_equalization, wiener_filter, wiener_deconvolutionContrast and restoration
A foreground mask, an edge map or a set of regionsotsu, canny_edge_detection, watershed, label_boundariesSegmentation
Straight lines or circleshough_lines, hough_transform, hough_line_peaks, hough_circlesThe Hough transform
Blobs, keypoints and descriptors; matches between imagesblob_detection, sift, surf, match_descriptorsLocal features
To clean a binary mask; to measure depth inside objects; to count and measure objectsbinary_opening, binary_closing, binary_fill_holes, grey_erosion, white_tophat, distance_transform_edt, label, regionsMorphology, distance transforms and labelling
To remove impulse noise; derivatives at a scale; to shift, rotate, zoom or warp an imagemedian_filter, rank_filter, gaussian_filter, gaussian_laplace, map_coordinates, affine_transform, rotate, zoomRank 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 pixelfourier_gaussian, fourier_uniform, fourier_ellipsoid, fourier_shift, fourier_filterFourier-domain filters