Grayscale conversion¶
| Fact | Value |
|---|---|
| Also called | Desaturate, Convert to gray |
| Status | Implemented and tested |
| Family | Enhancement, illumination and color |
| Engine | Browser |
| Type | Linear, color, weighted channel sum |
| Output | Image |
| Operator shown as | color-transform (exact) |
| Support radius | 0 |
| Code | web/src/core/filters/enhancement/grayscale.ts, grayscaleFilter |
| Operator code | web/src/core/filters/enhancement/grayscale.ts, grayValue |
Overview¶
Grayscale conversion turns each pixel into one gray value, a weighted sum of its red, green and blue values. Green counts most and blue least, as for the human eye.
How it works¶
- Read the red, green and blue values of the pixel.
- Multiply each by its weight and add the three results.
- Write the sum to all three channels.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(R', G', B'\): Stored sRGB values from 0 to 255
- \(Y'\): The gray value written to all three channels
- \(w_R, w_G, w_B\): Channel weights. They add up to 1.
- \(f\): The sRGB encoding function from IEC 61966-2-1. Its inverse decodes to linear light.
The gray value is a weighted sum of the three channels. BT.709 uses the weights 0.2126, 0.7152 and 0.0722. BT.601 uses 0.299, 0.587 and 0.114. The luma methods apply the weights to the stored, gamma-encoded values. The luminance method first decodes them to linear light, sums, and encodes the result again. The Operator tab shows the weights in use.
Luma is a weighted sum of the stored, gamma-encoded values. Luminance is the same weighted sum on linear light values, which measures light more faithfully. For grays the two agree, because all three channels are equal. For saturated colors they differ a lot, as the worked example shows.
Kernel or operator¶
The conversion has no kernel. The Inspector shows the three weights and whether they apply to encoded or linear values. The weights of the two luma standards:
Algorithm¶
- Read the red, green and blue values of the pixel.
- For luminance, decode each value to linear light.
- Multiply each value by its weight and add them up.
- For luminance, encode the sum back to sRGB.
- Write the result to all three channels and round to 8 bits. Keep alpha unchanged.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Method method |
Luma BT.709 (default) | Luma BT.709 (default), Luma BT.601, Luminance (linear light) | Which weights to use, and whether to work on stored or linear values. |
Implementation¶
grayValue computes the gray value of one pixel. The luminance method decodes each channel from sRGB with the IEC 61966-2-1 formula, sums, and encodes the result again. The filter runs in the browser engine, in Web Workers. The studio splits the image into strips. Each strip gets a halo as wide as the support radius, so the strips give exactly the pixels of a whole-image run. The shared filter framework in web/src/core/filters/run.ts handles the border mode, the channel scope, alpha and the final rounding to 8 bits.
Worked example¶
| Color | R, G, B | Luma BT.709 | Luma BT.601 | Luminance |
|---|---|---|---|---|
| Red | 255, 0, 0 | 54 | 76 | 127 |
| Green | 0, 255, 0 | 182 | 150 | 220 |
| Blue | 0, 0, 255 | 18 | 29 | 76 |
| Orange | 255, 128, 0 | 146 | 151 | 163 |
| Gray | 128, 128, 128 | 128 | 128 | 128 |
The three methods agree on gray and disagree on saturated colors. Luminance gives saturated colors a brighter gray than luma, because it sums light, not encoded values.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels.
Artifacts and limitations¶
Limitations
- Different colors with the same gray value become impossible to tell apart.
- The luma methods make saturated blues and reds look darker than they appear.
Artifacts
- Red text on a green background can disappear when both have the same gray value.
Tests¶
All three methods are compared with NumPy versions of the formulas on 24 random colors, within 1e-5. The fixture generator also checks the BT.601 weights against OpenCV's color conversion, allowing one level for OpenCV's fixed-point rounding.
References¶
- ITU-R Recommendation BT.709-6. Parameter values for the HDTV standards for production and international programme exchange, 2015.
- ITU-R Recommendation BT.601-7. Studio encoding parameters of digital television for standard 4:3 and wide-screen 16:9 aspect ratios, 2011.
- IEC 61966-2-1:1999. Multimedia systems and equipment, colour measurement and management, part 2-1, default RGB colour space, sRGB.
- Poynton, C. Digital Video and HD, Algorithms and Interfaces, 2nd edition. Morgan Kaufmann, 2012.
- OpenCV documentation, color conversions. https://docs.opencv.org/4.x/de/d25/imgproc_color_conversions.html