Converting Colourful Images Into Grayscale: Two Methods
26 September 2012
About This ArticleTurning 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 ValueWe 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:
Method 2: LuminosityAnother 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:
ComparisonThe 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:
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"