Single-scale Retinex¶
| Fact | Value |
|---|---|
| Also called | Retinex, SSR, Center/surround Retinex |
| Status | Implemented and tested |
| Family | Enhancement, illumination and color |
| Engine | Browser |
| Type | Nonlinear, color, log ratio to a Gaussian surround |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | web/src/core/filters/enhancement/retinex.ts, retinexSsrFilter |
| Operator code | web/src/core/filters/enhancement/retinex.ts, multiscaleRetinex |
Overview¶
Retinex removes slow changes of light. It compares each pixel with a wide Gaussian average of its surroundings, so a shadow that covers a large area cancels out, while edges and texture stay. The single-scale version uses one Gaussian. It is the base of the multi-scale Retinex and of MSRCR.
How it works¶
- Take the log of every value plus 1.
- Blur the image with a Gaussian of scale sigma, with the image mirrored at its edges.
- Subtract the log of the blur from the log of the image.
- Stretch the result to 0 to 255, clipping a small percent of the values at each end.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(I_c\): Channel c of the input, 0 to 255
- \(G_\sigma\): The Gaussian surround with standard deviation sigma
- \(R_c\): The Retinex output, a log ratio
- \(V_{\min}, V_{\max}\): The values clipped to 0 and 255 by the color balance
G_sigma is a Gaussian with standard deviation sigma, and * is convolution. Land's center and surround idea is that the light changes slowly, so dividing by a wide average of the neighborhood removes it and leaves the reflectance. The log turns the division into a difference. Jobson, Rahman and Woodell place the log after the surround, as here, and found sigma = 80 a good choice. The result has no fixed range, so Simplest Color Balance stretches it to 0 to 255. V_min and V_max are the values at positions floor(N s1 / 100) and floor(N (100 - s2) / 100) - 1 of the N sorted values. With Apply to set to intensity, R is computed on I = (R + G + B) / 3 and balanced to I_1. Then each pixel is scaled by A = min(255 / max(R, G, B), I_1 / I), which keeps its color ratios. PixelNexus takes the log of I + 1 so that black pixels have a finite log. The article does not say how it handles zeros. A result with no spread, as from a flat image, turns mid gray.
A pixel's value is roughly the light falling on it times the reflectance of the surface. If the light changes slowly, the Gaussian average holds about the same light, and the ratio I / (G * I) keeps mostly the reflectance. The log of a ratio is a difference of logs. Retinex removes changes of light that are slower than sigma. Changes faster than sigma stay, so a sharp shadow edge remains.
The blur follows Algorithm 3 of Petro, Sbert and Morel. The image is mirrored into a grid of twice its size, which the FFT treats as periodic without jumps at the edges. Its spectrum is multiplied by exp(-2 pi^2 sigma^2 f^2), the Fourier transform of the Gaussian. PixelNexus does this on rows and then on columns. The result is the same, since both the Gaussian and the mirrored grid split into the two axes.
Kernel or operator¶
The Inspector shows the Retinex, the color balance and, with intensity, the color step, as formulas with the current settings.
Algorithm¶
- Add 1 to every value and take the log.
- Blur the image with the Gaussian on the image mirrored at its edges, through the FFT of each row and column.
- Subtract the log of the blurred image.
- Stretch the result to 0 to 255 with Simplest Color Balance, clipping s1 and s2 percent.
- With intensity, scale each pixel so that its intensity matches the result and its color ratios stay.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Scale sigma sigma |
80 | 1 to 1000 px | The standard deviation of the Gaussian surround. |
Apply to apply |
Each channel | Each channel, Intensity, keep colors | Run the Retinex on each color channel, or on the intensity while each pixel keeps its color. |
Clip dark clipLow |
1 | 0 to 20 % | The percent s1 of the darkest values that the color balance sets to 0. |
Clip bright clipHigh |
1 | 0 to 20 % | The percent s2 of the brightest values that the color balance sets to 255. |
Implementation¶
retinexSsrFilter in web/src/core/filters/enhancement/retinex.ts calls multiscaleRetinex with one scale and simplestColorBalance. The Gaussian is mirrorGaussian, which passes two rows or columns through one complex FFT. It is a global color filter, so it always sees the whole image.
Worked example¶
| Image | Left quarter luma | Right quarter luma | Right / left | Saturation (%) |
|---|---|---|---|---|
| Shaded chart | 158.6 | 43.8 | 0.28 | 65 |
| SSR, sigma 80 | 184.4 | 153.6 | 0.83 | 61.5 |
| SSR, sigma 80, intensity | 181.4 | 117.3 | 0.65 | 64.8 |
| MSR, sigma 15, 80, 250 | 197.1 | 146.4 | 0.74 | 57.1 |
| MSR, intensity | 190 | 93.1 | 0.49 | 64.8 |
| MSRCR, alpha 125 | 192.1 | 133.2 | 0.69 | 58.3 |
Each row is a result with its default settings, except where the row says intensity, on the chart of the figure at full size, 720 by 360 pixels, lit from the left. Luma is 0.299 R + 0.587 G + 0.114 B, averaged over the left and right quarters, which hold the same patches. Saturation is (max - min) / max per pixel, averaged over the patches with color. A right / left ratio near 1 means the light is evened out. The row "SSR, sigma 80" is this filter.
On each channel, single-scale Retinex brings the right quarter to 83 percent of the left, and the colors fade a little. On the intensity, the saturation stays close to that of the input, but the light is evened out less.
Complexity¶
- Time: \(O(N log N)\)
- Memory: \(O(N)\)
N is the number of pixels. Every row and column is extended to twice its length for the FFT, so the cost does not depend on sigma.
Artifacts and limitations¶
Limitations
- One scale cannot both keep colors and compress the range. Small scales leave halos, large scales even out less light.
- On each channel, colors turn grayish where one color fills a large area.
Artifacts
- Halos along strong edges at small scales.
- Noise in dark, flat areas is boosted.
Tests¶
The fixture cases come from a NumPy reference that follows the article: the 2D FFT of the mirrored grid for the blur, and the sorting method of Simplest Color Balance. Before the cases are written, the blur is checked against scipy.ndimage.gaussian_filter with the reflect mode. Unit tests check the blur against the spatial Gaussian and that it keeps the image mean. They also check the positions of the clipped values, that a flat image turns mid gray, and that the intensity mode keeps each pixel's color ratios.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/enhancement/retinex.test.ts
References¶
- Jobson, D. J., Rahman, Z. and Woodell, G. A. Properties and performance of a center/surround retinex. IEEE Transactions on Image Processing 6(3), 451 to 462, 1997. DOI 10.1109/83.557356.
- Petro, A. B., Sbert, C. and Morel, J.-M. Multiscale Retinex. Image Processing On Line 4, 71 to 88, 2014. DOI 10.5201/ipol.2014.107.
- Limare, N., Lisani, J.-L., Morel, J.-M., Petro, A. B. and Sbert, C. Simplest Color Balance. Image Processing On Line 1, 297 to 315, 2011. DOI 10.5201/ipol.2011.llmps-scb.