Weighted Grayscale Conversion

Solve It

The color white has red, green, and blue values all set to 255, and the color black has red, green, and blue values all set to 0. In general, when all three color values are equal, the result is some shade of gray. Previously we created a grayscale version of an image by setting all color components of each pixel to the average of the original three color component values. However, research tells us that a "truer" image comes from a weighted average of the components. We will calculate the value of our red, green, and blue components by taking 30% of the red component, 59% of the green component, and 11% of the blue component.

Code It

// weighted grey scale var image = new SimpleImage("duvall.jpg"); for (var p of image.values()) { //var avg = (p.getGreen() + p.getRed() + p.getBlue()) / 3; var avg = 0.3 * p.getRed() + 0.59 * p.getGreen() + 0.11 * p.getBlue(); p.setRed(avg); p.setBlue(avg); p.setGreen(avg); debug(avg); } print(image);

See It

 
 

Available Images


rodger.png
[315x424]

astrachan.jpg
[240x360]

duvall.jpg
[200x300]

hilton.jpg
[140x210]

Drop your images onto the area above to make it available within your code editor on this page. Note: your images will not be uploaded anywhere, they will stay on your computer.