Difference of Gaussians¶
| Fact | Value |
|---|---|
| Also called | DoG |
| Status | Implemented and tested |
| Family | Linear spatial filters (also Edge detection and boundary analysis) |
| Engine | Browser |
| Type | Linear, spatial, band-pass convolution |
| Output | Image |
| Operator shown as | dynamic-kernel (exact) |
| Support radius | floor(3 sigma ratio + 0.5) |
| Code | web/src/core/filters/linear/dog-log.ts, dogFilter |
| Operator code | web/src/core/filters/linear/dog-log.ts, dogFilter |
| Channels | Brightness only, RGB |
Overview¶
The Difference of Gaussians, DoG, subtracts a wide Gaussian blur from a narrow one. The narrow blur keeps details that the wide blur removes, so the difference keeps only details between the two sizes. It is a band-pass filter, a cheap stand-in for the Laplacian of Gaussian, and the basis of many blob and feature detectors.
How it works¶
- Blur the image with a narrow Gaussian.
- Blur it with a wide Gaussian, k times wider.
- Subtract the wide blur from the narrow one.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(G_{\sigma}\): A normalized Gaussian with standard deviation sigma
- \(k\): The ratio of the two sigmas
G is a normalized Gaussian. Both blurs keep the mean, so their difference is 0 in flat areas. The narrow blur keeps details that the wide one removes, so the difference holds the details between the two scales. For k near 1 it is close to (k - 1) sigma squared times the Laplacian of Gaussian, which is why it serves as a cheap blob and edge detector. Both Gaussians are truncated at 3 sigma.
Both Gaussians are normalized, so the difference kernel adds up to 0 and flat areas give 0. In the frequency domain the DoG is the difference of two low-pass filters, a band-pass that peaks at a frequency set by sigma and k. For k near 1, G(k sigma) - G(sigma) is close to (k - 1) sigma^2 times the Laplacian of Gaussian. Marr and Hildreth found that k = 1.6 gives a close match to the LoG.
Kernel or operator¶
The Inspector shows the difference kernel, built from the two Gaussian kernels of the code. The Impulse response demo in the Learn panel shows its center-surround shape.
Algorithm¶
- Pad the image with the border mode.
- Blur with the narrow Gaussian and with the wide Gaussian, each as two 1D passes.
- Subtract the wide blur from the narrow blur.
- Map the signed result to 8 bits with the output mapping.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Sigma sigma |
1.5 | 0.3 to 20 px | The sigma of the narrow Gaussian. |
Ratio k ratio |
1.6 | 1.05 to 5 | The wide Gaussian has sigma k times the narrow one. 1.6 is the ratio of Marr and Hildreth. |
Output mapping outputMapping |
Offset by 128 | Absolute value, Offset by 128, Stretch to full range, Clip to 0 to 255 | How signed results, such as gradients, become 8-bit pixel values. |
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. |
Channels channelScope |
Brightness only | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
dogFilter in web/src/core/filters/linear/dog-log.ts runs two separable Gaussian blurs, truncated at 3 sigma like the Gaussian filter, and subtracts them. The filter runs in the browser engine, in Web Workers, through the shared filter framework in web/src/core/filters/run.ts, which handles the border mode, the channel scope, alpha and the output mapping. By default it works on luma and gives a gray result.
Worked example¶
| x | 28 | 30 | 31 | 32 | 33 | 35 |
|---|---|---|---|---|---|---|
| Input | 40 | 40 | 40 | 215 | 215 | 215 |
| DoG + 128 | 117 | 109 | 119 | 137 | 147 | 139 |
Sigma 1.5 and ratio 1.6 on a step edge, shown with the offset mapping, so 128 means 0. The result dips below 128 on the dark side and rises above it on the bright side, and it is 128 far from the edge.
Complexity¶
- Time: \(O(N\,k\sigma)\)
- Memory: \(O(N)\)
N is the number of pixels per channel. Each blur costs two passes of about 6 sigma taps.
Artifacts and limitations¶
Limitations
- Only an approximation of the Laplacian of Gaussian.
- The result is signed and needs an output mapping.
Artifacts
- Light and dark halos on both sides of edges.
Tests¶
The reference subtracts two scipy.ndimage.gaussian_filter results with truncate 3. Before it is used, the script checks it against skimage.filters.difference_of_gaussians. The test covers two sigma and ratio settings for all five border modes.
References¶
- Marr, D. and Hildreth, E. Theory of edge detection. Proceedings of the Royal Society of London, Series B 207(1167), 187 to 217, 1980. DOI 10.1098/rspb.1980.0020.
- scikit-image documentation, skimage.filters.difference_of_gaussians. https://scikit-image.org/docs/stable/api/skimage.filters.html#skimage.filters.difference_of_gaussians