Dead-pixel correction¶
| Fact | Value |
|---|---|
| Also called | Hot pixel removal, Stuck pixel repair |
| Status | Implemented and tested |
| Family | Restoration and reconstruction |
| Engine | Browser |
| Type | Nonlinear, spatial, outlier detection and repair |
| Output | Image |
| Operator shown as | rank (exact) |
| Support radius | floor(size / 2) |
| Code | web/src/core/filters/restoration/dead-pixel.ts, deadPixelFilter |
| Operator code | web/src/core/filters/restoration/dead-pixel.ts, neighborMedians |
| Channels | RGB |
Overview¶
Dead-pixel correction repairs single pixels that are stuck, dead or hot. Such a pixel differs strongly from its neighbors. The filter compares each pixel with the median of its neighbors, without the pixel itself. When any channel is too far off, the whole pixel gets the neighbor medians. All other pixels keep their exact values. A detection view shows which pixels would change.
How it works¶
- Find the median of the neighbors in each channel, without the pixel itself.
- Find the largest channel difference between the pixel and those medians.
- If it is above the threshold, replace all channels by the medians. Otherwise keep the pixel.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(m_c(x, y)\): Median of channel c over the neighbors of (x, y)
- \(d(x, y)\): The largest channel difference to the neighbor median
- \(T\): The threshold
m_c is the median of channel c over the neighbors, the window without the pixel itself. The pixel is left out, so a defect cannot pull its own reference. The neighbor count is even, so the median is the mean of the two middle values. A pixel whose largest channel difference d is above T is defective, and all its channels get the neighbor medians.
The reference leaves the center out, so a defect cannot pull its own reference. The neighbor count is even, 8 for a 3x3 window, so the median is the mean of the two middle values. Detection uses the largest channel difference, because a defect in one color site of a sensor still spoils the whole pixel after demosaicing.
Kernel or operator¶
The Inspector shows the window without its center and the threshold. The Learn panel shows a line profile through the image, and the worked example below repairs one hot pixel.
Algorithm¶
- Pad the image with the border mode.
- For each pixel and channel, find the median of the neighbors, without the pixel itself.
- Find the largest channel difference between the pixel and those medians.
- Where it is above the threshold, write the medians. Elsewhere keep the pixel, or write the detection mask.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Window size size |
3 | 3 to 7, odd px | The neighbors are this window without its center pixel. |
Threshold threshold |
50 | 1 to 255 | How far a channel may differ from the neighbor median before the pixel counts as defective. |
Show output |
Repaired image | Repaired image, Detected pixels | Show the repaired image, or white dots where defects were found. |
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¶
deadPixelFilter in web/src/core/filters/restoration/dead-pixel.ts sorts the neighbors of each pixel and channel in neighborMedians, then applies the rule. With Show set to Detected pixels it writes white where a defect was found and black elsewhere. It is a color filter: the framework in web/src/core/filters/run.ts hands it the red, green and blue planes together, with the border mode, and keeps alpha. Each strip of the image gets a halo as wide as the support radius, so strips give exactly the pixels of a whole-image run.
Worked example¶
A hot pixel in the red channel. The 8 red neighbors, sorted, are 118, 119, 120, 120, 121, 122, 122, 123. Their median is (120 + 121) / 2 = 120.5. The red difference is 129.5, above T = 50, so the pixel is defective. All three channels get their neighbor medians, and the result is (120.5, 80.5, 60). The green value 84 was close to its neighbors, but it is replaced too, because the whole pixel is taken as faulty.
Complexity¶
- Time: \(O(N\,k^2 \log k)\)
- Memory: \(O(N)\)
N is the number of pixels and k is the window width. Each window of k^2 - 1 neighbors is sorted per channel.
Artifacts and limitations¶
Limitations
- Stars, specular highlights and other real one-pixel details look like defects and may be removed.
- Clusters larger than half the window are not found.
Artifacts
- A small flat spot where a real fine detail was taken as a defect.
Tests¶
The reference takes numpy.median of the neighbors of each NumPy window, on a color input with white, black and one-channel defects. Before it is used, the script checks the neighbor median against scipy.ndimage.generic_filter with a ring footprint. The test covers both views and two window sizes for all five border modes.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/nonlinear/nonlinear5b.test.ts
References¶
- Sun, T. and Neuvo, Y. Detail-preserving median based filters in image processing. Pattern Recognition Letters 15(4), 341 to 347, 1994. DOI 10.1016/0167-8655(94)90082-5.
- NumPy documentation, numpy.median. https://numpy.org/doc/stable/reference/generated/numpy.median.html