Multi-scale Retinex¶
| Fact | Value |
|---|---|
| Also called | MSR, MSRCP, Multi-scale Retinex with chromaticity preservation |
| Status | Implemented and tested |
| Family | Enhancement, illumination and color |
| Engine | Browser |
| Type | Nonlinear, color, mean of log ratios to Gaussian surrounds |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | web/src/core/filters/enhancement/retinex.ts, retinexMsrFilter |
| Operator code | web/src/core/filters/enhancement/retinex.ts, multiscaleRetinex |
Overview¶
One Retinex scale cannot do everything. A small scale brings out detail but leaves halos and gray colors. A large scale keeps colors but evens out less light. Multi-scale Retinex averages three scales, 15, 80 and 250 pixels, as Jobson, Rahman and Woodell suggest.
With Apply to set to intensity, this is the MSRCP of Petro, Sbert and Morel. The Retinex runs on the intensity, and each pixel keeps its color ratios.
How it works¶
- Take the log of every value plus 1.
- For each of the three scales, blur the image with that Gaussian and subtract the log of the blur.
- Average the three results.
- Stretch the average 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_n}\): The Gaussian surround at scale n
- \(R_c\): The Retinex output before the color balance
Each term is a single-scale Retinex with the Gaussian surround G_sigma_n. Jobson, Rahman and Woodell found three scales enough for most images, with equal weights, and fixed the scales at 15, 80 and 250 pixels. The small scale brings out detail, the large one keeps the colors and the tone. As Petro, Sbert and Morel do, Simplest Color Balance stretches the result to 0 to 255. It clips s1 percent of the values at the bottom and s2 percent at the top. 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). PixelNexus takes the log of I + 1 so that black pixels have a finite log, and a result with no spread turns mid gray.
Each scale removes the changes of light that are slower than it. The small scale removes the most, so it brings out the most detail, but it also removes the color of large areas. The large scale removes little, so it keeps the tone and the colors. The average is a trade-off between the two.
On each channel, the result shows a known weakness: where one color fills a large area, its channel is compared with its own average, and the color fades toward gray. The intensity mode avoids this. The Retinex of the intensity I = (R + G + B) / 3 is balanced to I_1. Then every channel of the pixel is scaled by A = min(255 / max(R, G, B), I_1 / I). The cap keeps every channel within 255.
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.
- For each scale, blur the image with the Gaussian on the image mirrored at its edges, and subtract the log of the blur.
- Average the three results.
- Stretch the average to 0 to 255 with Simplest Color Balance.
- With intensity, scale each pixel so that its intensity matches the result and its color ratios stay.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Small scale sigma1 |
15 | 1 to 1000 px | The standard deviation of the first Gaussian surround. |
Middle scale sigma2 |
80 | 1 to 1000 px | The standard deviation of the second Gaussian surround. |
Large scale sigma3 |
250 | 1 to 1000 px | The standard deviation of the third 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 (MSRCP). |
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¶
retinexMsrFilter in web/src/core/filters/enhancement/retinex.ts calls multiscaleRetinex with three scales and simplestColorBalance. The Gaussian is mirrorGaussian, the exact Gaussian on the mirrored image through the FFT of each row and column. 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 "MSR, sigma 15, 80, 250" is this filter.
On each channel, multi-scale Retinex lifts the right quarter to 74 percent of the left and loses some saturation. On the intensity, the saturation stays close to that of the input, and the light is evened out less.
Complexity¶
- Time: \(O(N log N)\)
- Memory: \(O(N)\)
N is the number of pixels. Three Gaussian blurs through the FFT of each row and column, whatever the scales.
Artifacts and limitations¶
Limitations
- On each channel, colors turn grayish where one color fills a large area. Use intensity or MSRCR.
- The scales are in pixels. On a small image the large scale averages almost the whole image, so scale them down with the image.
Artifacts
- Mild halos along strong edges.
- Noise in dark, flat areas is boosted.
Tests¶
The fixture cases come from a NumPy reference that follows the article, with the default scales and with smaller ones, on each channel and on the intensity. Before the cases are written, the blur is checked against scipy.ndimage.gaussian_filter with the reflect mode. Unit tests check that a flat image has no detail at all, and that a gain on I + 1 does not change the Retinex.
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. A multiscale retinex for bridging the gap between color images and the human observation of scenes. IEEE Transactions on Image Processing 6(7), 965 to 976, 1997. DOI 10.1109/83.597272.
- 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.