Pseudocode
PROCEDURE greyscale(image)
FOR x = 0 .. width DO
FOR y = 0 .. height DO
c <- pixel(x, y)
c' <- avg(c)
pixel(x, y) <- c'
END FOR
END FOR
END PROCEDURE
Description
Loop through all pixels in the image, calculate the average (mean) value for that pixel then set the pixel values for all channels to be the calculated value.
There are many ways to calculate what this new value should be, any value where all the channels are the same value will create a black and white image.
Average Value
One of the simplest to implement is averaging the value of the pixel:
\[v = \frac{\sum_iP_i}{N}\]
Where:
- v
-
final (new) pixel value
- p
-
vector containing N channels
- N
-
number of channels
Related Concepts
-
Calculating Averages
Acknowledgements
Adapted from Pseudocode produced for COMP120 by Dr. Micheal Scott.