Matched filter¶
| Fact | Value |
|---|---|
| Also called | Matched frequency filter, Template matching, Normalized cross-correlation |
| Status | Implemented and tested |
| Family | Linear spatial filters (also Texture, corners, blobs and features) |
| Engine | Science engine |
| Type | Nonlinear, frequency, correlation with a template |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | engine/src/pixelnexus_engine/filters/matched.py, matched_filter |
| Operator code | engine/src/pixelnexus_engine/filters/matched.py, matched_filter_representation |
| Packages | scikit-image, SciPy |
Overview¶
Select a pattern, and the matched filter searches the whole image for it. It slides the template over every position and compares it with the image under it. The plain matched filter multiplies and adds, which is a correlation. The normalized cross-correlation also divides by the spread of the window and of the template. It then scores how alike the shapes are, from -1 to 1, whatever their brightness and contrast. The filter shows either map, or lists the best matches in the Features panel and draws them on the canvas. It runs in the PixelNexus engine, with scikit-image and SciPy.
The selection is only the template here. The filter runs on the whole image, and the result covers the whole image.
How it works¶
- Cut the template from the luma, inside the rectangle around the selection.
- Correlate the image with the template less its mean, with the FFT.
- For the normalized form, divide by the spread of each window and of the template.
- Show the map, or find its peaks above the threshold as matches.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(T, \bar T\): The template and its mean
- \(I, \bar I_{u,v}\): The image and the mean of the window at (u, v)
- \(\gamma\): The normalized cross-correlation, from -1 to 1
T is the template, the luma inside the rectangle around the selection, with its mean T bar. For every position (u, v), the window of the image with the same size has the mean I bar. The plain matched filter is the numerator alone, the correlation of the image with the template less its mean. Turin showed that this filter is best for finding a known signal in white noise. But it grows with the contrast of the window, so a faint copy of the pattern scores low and the edge of a bright area can score high. The normalized cross-correlation gamma divides by the spread of the window and of the template. It goes from -1 to 1. It is 1 where the window is the template up to brightness and contrast, and -1 where it is the template with dark and light swapped. A window with no spread gives 0.
The matched filter comes from signal processing. To find a known signal in white noise, correlate with the signal itself. Turin showed that this gives the best ratio of signal to noise at the place of the signal. In an image, the signal is the template, and the correlation is computed at every position.
The plain correlation has a weakness. It grows with the contrast of the window. A copy of the pattern at a quarter of the contrast scores a quarter as much. Strong edges that only partly look like the pattern can score more than a real but faint copy. Subtracting the template's mean keeps flat areas at 0, but it does not fix this.
The normalized cross-correlation removes the mean of each window and divides by the spread of the window and of the template. By the Cauchy-Schwarz inequality, the result lies between -1 and 1. It is 1 exactly when the window equals the template times a positive number plus a constant, so brightness and contrast drop out. It is -1 for the template with dark and light swapped.
Computed directly, each position needs a sum over the whole template. Lewis showed how to make it fast. The numerator is a correlation, which the FFT computes for all positions at once. The sums of the window and of its squares come from integral images.
Kernel or operator¶
The kernel is the template itself, less its mean, cut from the image. The Inspector asks the engine for the stages with the current settings: the template, the plain matched filter, the normalization and the matches.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Output output |
Normalized cross-correlation | Normalized cross-correlation, Plain matched filter, Matches | The normalized cross-correlation as an image, the plain correlation with the template as an image, or a table of the best matches. |
Match threshold threshold |
0.8 | 0 to 1 | A match needs at least this normalized cross-correlation. 1 is a perfect match. |
Most matches matches |
20 | 1 to 500 | At most this many matches, the best first. |
Border mode border |
Reflect 101 (default) | Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap | How pixels outside the image are filled when the window reaches past the edge. |
Implementation¶
matched_filter in engine/src/pixelnexus_engine/filters/matched.py cuts the template from the luma in pixel levels, inside the rectangle around the selected pixels. A flat template, or an empty selection, stops the job with a message.
The normalized map is skimage.feature.match_template with pad_input. It pads the image by the template size with the border mode, computes the numerator with scipy.signal.fftconvolve and the window sums with integral images. A window with no spread gives 0. For a template of h x w pixels, the value of the window with its top-left pixel at (top, left) lands on pixel (top + h // 2, left + w // 2). The map shows -1 as black, 0 as mid gray and 1 as white.
The plain map is the correlation with the template less its mean, padded and aligned the same way. It is shown with 0 as mid gray, and stretched so its largest absolute value is black or white.
The matches are the peaks of the normalized map found by skimage.feature.peak_local_max, at or above the threshold. They lie at least half the smaller side of the template apart, which is our choice, and at most the given number are kept. Each row gives the center of the matching window, the score, and the size of the template. It also gives a radius of half the smaller side of the template, so the canvas draws a ring that fits.
Worked example¶
| Noise sigma | Place | Normalized | Plain, relative |
|---|---|---|---|
| 0 | Template | 1.000 | 1.00 |
| 0 | Faint copy | 1.000 | 0.25 |
| 0 | Inverted copy | -1.000 | -1.00 |
| 0 | Full copy | 1.000 | 1.00 |
| 0 | Bright block, best | 0.329 | 0.33 |
| 8 | Template | 1.000 | 1.00 |
| 8 | Faint copy | 0.919 | 0.25 |
| 8 | Inverted copy | -0.989 | -0.98 |
| 8 | Full copy | 0.990 | 0.99 |
| 8 | Bright block, best | 0.330 | 0.32 |
The test image of the figure, with the template cut from the first cross. The normalized value and the plain value are taken at the center of each object. The plain value is divided by its value at the template itself. For the bright block, the row gives the largest values anywhere on it or near it.
The normalized map gives the faint copy the same score as the template. The plain map gives it a quarter, less than the edge of the bright block. Noise of sigma 8 lowers the faint copy to 0.92 and leaves the strong copies near 1. The inverted copy scores -1 in both maps, so it is never a match.
Complexity¶
- Time: \(O(N log N)\)
- Memory: \(O(N)\)
N is the number of pixels of the padded image. The FFT makes the cost almost independent of the template size, and the window sums come from integral images.
Artifacts and limitations¶
Limitations
- The template must have the same size and orientation as the copies. Turned or scaled copies score low.
- The template is a rectangle, so any background inside it counts too.
- A flat template has no spread and matches nothing. The filter then stops with a message.
Artifacts
- Broad peaks around each match, about as wide as the template's details.
Tests¶
The template must be the rectangle around the selected pixels, and an empty or flat template must stop with a message. The normalized map must equal scikit-image's match_template for two border modes. A template must score 1 at its own center, for odd and even sizes. The plain map must equal a direct sum over the padded window at sample pixels. On the test scene, the faint and the full copy must score 1, the inverted copy -1 and the inside of the block 0. The plain map must score the faint copy at a quarter, below a corner of the block. The matches must be the three copies, with centers between pixels for an even template. With noise and a threshold of 0.95, the faint copy must drop out first. The engine API tests send the template as a mask, and refuse a job without one.
References¶
- Turin, G. L. An introduction to matched filters. IRE Transactions on Information Theory 6(3), 311 to 329, 1960. DOI 10.1109/TIT.1960.1057571.
- Lewis, J. P. Fast normalized cross-correlation. Industrial Light and Magic, 1995.
- Briechle, K., and Hanebeck, U. D. Template matching using fast normalized cross correlation. Proceedings of SPIE 4387, Optical Pattern Recognition XII, 95 to 102, 2001. DOI 10.1117/12.421129.
- scikit-image documentation, skimage.feature.match_template.