GrayScale Conversion

samir khanal
2 min readOct 2, 2018

--

An image is a collection of pixels. Pixels are arranged to form an image. Size of an image is represented in matrix form as IxJ, where i is the number of pixels in row and j is the number of pixels in column. Each pixel has its pixel values with three values: Red, Green and Blue.

Grayscale conversion means converting an image into gray representation, omitting other colours. This can be done by applying mathematical formulation on the RGB values and replacing them in the image or creating new image with those new values.

Grayscale conversion has different methods. Some of them are given below:

Simple average: We perform simple average of RGB values of each pixel.It can be calculated as:

Avg =(R+G+B)/3

i.e. Avg =(pixel[i][j][0]+pixel[i][j][1]+pixel[i][j][2])/3

where, i = length of image, j = width of image and [0],[1],[2] are for selecting RGB values.

Simple average grayscale conversion:(left)original image (right)grayscaled image

Weighted Average: We multiply the values of RGB with certain percentage (or weights) determining the strength or intensity of the colour for gray value. It can be calculated as:

Avg = R*0.299 + G*0.587 +B*0.144

which means, Green~=60%, Red~=30%, Blue~=11%

i.e. Avg =(0.299*pixel[i][j][0]+0.587*pixel[i][j][1]+0.114*pixel[i][j][2])/3

Weighted average grayscale conversion:(left)original image (right)grayscaled image

Lightness: We calculate maximum and minimum values between the RGB values and calculate its mean to give gray value. It can be calculated as:

Avg = (max(R,G,B)+min(R,G,B))/2

Lightness grayscale conversion:(left)original image (right)grayscaled image

Luminosity: We multiply the values of RGB with certain percentage (or weights) determining the strength or intensity of the colour for gray value which accounts more to human perception. It can be calculated as:

Avg = 0.21*R + 0.72*G + 0.07*B

i.e. Avg=(0.21*pixel[i][j][0]+0.72*pixel[i][j][1]+0.07*pixel[i][j][2])/3

Luminosity grayscale conversion:(left)original image (right)grayscaled image

--

--