Image Processing 4. Desaturation
2 min read June 14, 2025 #image processing #javascriptImage Processing 1. Introduction
Image Processing 2. Simple transformation
Image Processing 3. Negate filter, channel extraction and inversion
Image Processing 4. Desaturation
Image Processing 5. Color Models
Image Processing 6. Brightness, saturation, contrast,, gamma correction
Next up is the Desaturation filter, which converts a color image to grayscale.
Homework#
The previous homework assignment was to output each individual image channel as shades of gray. I also provided a hint: gray is a combination of all three colors in equal proportions. Thus, the solution was simply to duplicate the brightness of one channel to the other two:
// Red
for ; i < dst.length; i++
// Green
for ; i < dst.length; i++
// Blue
for ; i < dst.length; i++
Solution example: CodePen
Desaturation#
The essence of this filter is to convert a color image to grayscale while preserving its original brightness. An immediate intuitive solution is to sum the color components and divide by 3:
for ; i < dst.length; i++
This is probably the fastest method, but it isn't very suitable for human perception. Humans don't perceive all colors uniformly (I'm not talking about vision impairments). Our eyes are most sensitive to green and least sensitive to blue. This means that to accurately preserve brightness in a desaturated image, we must consider perceptual sensitivity to each color component.
For the NTSC television standard:
Gray = 0.3R + 0.59G + 0.11B
for ; i < dst.length; i++
For the sRGB color profile (profiles are a topic for another article):
Gray = 0.2126R + 0.7152G + 0.0722B
for ; i < dst.length; i++
The first option with the arithmetic mean is noticeably darker than the others. Differences between NTSC and sRGB coefficients are subtle at first glance: in sRGB, Pikachu's cheeks and tongue appear darker, which, in my opinion, better matches the brightness of the original color image.
Few more examples:
Which algorithm to use depends on the task:
- For speed, use the arithmetic mean.
- For quality, use the sRGB, as it's better suited for digital environments.