Blog


Converting Colourful Images Into Grayscale: Two Methods

26 September 2012
Lime cat from colour to greyscale

About This Article

Turning a colourful image into a grayscale one can give the image/photo a more vintage and abstract feel, because of the colours being limited to shades of grey (255 not 50). There are several ways to do this programmatically, but in this article we're going to focus on two.

Method 1: Average Colour Value

We can achieve a shade of grey when all primary colour values (red, green and blue) are equal. For example, black is RGB 0,0,0 and white is 255,255,255. We can make a light grey colour by setting the RGB values to (say) 200,200,200.Say we have a pixel on our image, where the RGB values are 20,70 and 150 respectively. We need one number to represent all three colour values. A way to achieve this is to take an average of the three values. In this case:
the result of which is 80. So we can convert the RGB values of the pixel to 80,80,80.Repeat the same process for all pixels in the image until you end up with the grayscale image.Here is a code example of method 1 in Java

Method 2: Luminosity

Another method for converting an image into grayscale is Luminosity. Luminosity is a bit like the first method, but more sophisticated to take the human perception of colour into account. The human eye is more sensitive to green and least sensitive to blue. Below is the equation for getting the colour of a pixel:
Here is a code example of method 2 in Java

Comparison

The end results of the two algorithms are pretty much the same, but here's the two end images side by side so you can compare:
Lime cat greyscaled with average colour and luminosity techniques side by side for comparison
Which image do you think is the best result of grayscaling?

You might also like:

10 April 2013 - Version 2.0 of LSX is here. What's new?
25 March 2013 - Dear Larry... We need to talk...
02 March 2013 - Who needs it anyway?
19 January 2013 - Java has a vulnerability. What should we do?
11 January 2013 - A common question is to explain object oriented programming in layman's terms. Here I attempt to explain "Like I'm Five"

Comments

About Me

Savvas Stephanides
Hey there. I'm Savvas, but you can call me Sav. I am a Computer Science graduate. I like programming, designing clean and useful user interfaces and learning about Internet memes. I mostly code in Java, for desktop and Android applications, and PHP for web applications. Want to talk to me about anything? Drop me a message through the Contact form. Here's where you can find me:

2012 Savvas Stephanides. Contact me. Like me on Facebook, follow me on Twitter or see my professional profile over at LinkedIn.