📘
RGB to Grayscale
EasyColor Conversion
Implement a function to convert an RGB pixel to grayscale using the luminosity method, which is a widely used technique in Computer Vision. This method involves calculating a weighted average of the RGB values to produce a grayscale value that approximates human perception.
The human eye perceives colors differently, with varying sensitivities to red, green, and blue light, which is why we use weighted averaging to convert RGB to grayscale. The weights used in this method reflect the sensitivity of the human eye to different colors, with green contributing most to perceived luminance.
To perform this conversion, follow these steps:
- Extract the RGB values from the input pixel
- Apply the weighted sum formula to calculate the grayscale value
- Round the result to obtain the final grayscale value
This technique is widely used in image processing applications.
Example:
Input:
rgb_to_gray([255, 0, 0])
Output:
54
Reasoning:
0.2126×255 + 0.7152×0 + 0.0722×0 ≈ 54
Constraints:
- RGB values are in range [0, 255]
- Return grayscale value rounded to nearest integer
- Output should be in range [0, 255]
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.