In this article I’d like to discuss the basics of grayscaling, as it is in my opinion the most basic way to edit an image, which makes it a great way to start learning about image editing. I realize this topic has been done to death, but if I’m going to do a whole series on image editing, I should probably have a baseline right? Anyway, without any further ado, let’s just jump into this topic.
What is grayscale?
Well I’m glad you asked. If you think back to the old days of photography, you probably think of black and white cameras. Here’s the thing, technically speaking, those images weren’t black and white, they were shades of gray with black and white interspersed. Grayscale refers to something being displayed as a range of different shades of gray. Grayscale gets used everywhere from document editing to video games and complex image processing algorithms, so it’s a pretty useful thing to have under our belt.
The most basic grayscale algorithm
To talk about grayscale, we need to agree on a format to apply grayscale to, I’m going to choose bitmaps because…well what other purpose would you have for knowing this? So as I’ve discussed in previous posts, bitmaps are arrays of data, where each part of the array represents the bit-depth of the pixel, in this case the bit-depth is the color depth. Color depth in most cases contains the red, green, and blue values of the pixel. So we know that every pixel gets it’s color based on a value it’s fed that tells it the RGB component of the pixel. What this means is that we can use some math to turn that RGB value into a corresponding shade of gray. The quick and dirty way of doing it is to divide the sum of the RGB value by 3, so if we had a pixel with RGB of 255, 5, 129, and we wanted the shade of gray that corresponds to this color, we would say:
(255 + 5 + 129) / 3, or 129 and some change.
With the result of 129, we simply plug this into the RGB component of the pixel, leaving us with an RGB of 129, 129, 129, which gives us a gray color. This method has a pretty major flaw; though it is easy, it doesn’t account for how the human eye sees these colors. Let’s talk about that next.
Accounting for the human eye
So as I mentioned just before, the human eye doesn’t perceive RGB color space as being weighted evenly, which is what our color averaging algorithm from before did. To truly account for the human eye, you actually need to weight the RGB values differently. To do this, we use the formula as follows (Let’s assume the same RGB values as before):
(255 * 0.3) + (5 * 0.59) + (129 * 0.11)
If you’re paying attention, you’ve probably noticed that the numbers we multiply each value by add up to a total of 1. This is how we weight the colors, the human eye is sensitive to green light more than any other color, so it accounts for nearly 2/3rd’s of our brightness, red is second strongest, so it accounts for nearly 1/3rd, and blue is very weak, so it accounts for slightly over 1/10th of the brightness, the sum is the gray value we use for our RGB value. Why, you may be asking, do we use 0.3, 0.59 and 0.11 for this formula? Well, to be honest with you I have no idea how they arrived at those exact numbers, other than the fact that the eye is more sensitive to some of those colors, ergo they get greater or lesser values, but I don’t know the actual science behind why blue is 1/10th of the total, etc.
That’s really all that I feel like covering for now. There are other components to grayscaling, but I feel that they deserve their own post (stuff like desaturation, etc.) for now though this covers the absolute basics of grayscale, so I hope it was useful for you. In a future post I will provide an implementation in some language of my choosing on how to grayscale an image. Thanks for reading and be sure to leave any feedback you may have.