v9.1.1
Menu

RGB to Grayscale Conversion

easyFREEColor SpacesComputer Vision 2

Implement a function to convert a list of RGB pixels to their corresponding grayscale values. This process involves transforming color spaces, where each pixel's luminance is calculated based on its red, green, and blue components.

The human visual system is more sensitive to certain colors, which is reflected in the weighted sum used for conversion. The sensitivity to green is highest, followed by red, and then blue, which is represented by the coefficients in the conversion formula.

To perform the conversion, follow these steps:

  1. Extract the R, G, and B values from each pixel.
  2. Apply the weighted sum to calculate the luminance.
  3. Round the result to obtain the grayscale value.
Y=round(0.299⋅R+0.587⋅G+0.114⋅B)Y = \text{round}(0.299 \cdot R + 0.587 \cdot G + 0.114 \cdot B)

This technique is widely used in image processing applications.

Example

Input
pixels = [[255, 0, 0], [0, 255, 0], [0, 0, 255]]
Output
[76, 150, 29]
Reasoning
  • Input: pixels is a list of pixels, each a triple [R,G,B][R, G, B] of channel intensities from 00 (off) to 255255 (full). The sample holds pure red, pure green and pure blue, each at full strength.
  • Math: each pixel collapses to one luminance number Y=0.299R+0.587G+0.114BY = 0.299R + 0.587G + 0.114B, rounded to the nearest integer. With only one channel lit, YY is just that channel's weight times 255255:
    • red: 0.299×255=76.245→760.299 \times 255 = 76.245 \to 76
    • green: 0.587×255=149.685→1500.587 \times 255 = 149.685 \to 150
    • blue: 0.114×255=29.07→290.114 \times 255 = 29.07 \to 29
  • Output: one integer per pixel, in the same order as the input, giving [76, 150, 29].
  • Notice that three equally bright colours get very different grays: green looks brightest to the eye, blue darkest. Because the weights add up to 11, white [255,255,255][255, 255, 255] maps to 255255 and black to 00, so the result stays in [0,255][0, 255].

Constraints

  • Each pixel is [R, G, B] with values in [0, 255]
  • Return list of integers (use round())
  • Pure Python, no libraries needed

Learn this in

More from Computer Vision 2

solution.py

Test results

Sign in to run your code against the test cases.