Color to Grayscale
Color to Grayscale
Converting a color image to grayscale is one of the most fundamental image processing operations. The human eye perceives different colors with varying sensitivity, so a simple average of RGB channels does not produce a perceptually accurate grayscale image. Instead, a weighted sum based on human vision is used.
Given a color image represented as a 3D array (H × W × 3 with RGB channels), convert it to a grayscale image (H × W) using the luminance formula.
Algorithm
For each pixel, compute the grayscale intensity using the ITU-R BT.601 luminance weights:
Y=0.299⋅R+0.587⋅G+0.114⋅BThese weights reflect the human eye's greater sensitivity to green light, moderate sensitivity to red, and lower sensitivity to blue.
Examples
Input:
image = [[[255, 0, 0]]]
Output:
[[76.245]]
A single pure red pixel. Grayscale = 0.299 × 255 + 0.587 × 0 + 0.114 × 0 = 76.245.
Input:
image = [[[255, 0, 0], [0, 255, 0]], [[0, 0, 255], [255, 255, 255]]]
Output:
[[76.245, 149.685], [29.07, 255.0]]
A 2×2 image with red, green, blue, and white pixels. Notice green produces the highest grayscale value among the pure colors, reflecting the eye's peak sensitivity to green.
Hint 1
Iterate over each row and each pixel. For each pixel, extract the R, G, B values (image[i][j][0], image[i][j][1], image[i][j][2]) and compute the weighted sum using the luminance formula.
Hint 2
Build the result as a 2D list. For each row i, create a new list. For each column j, append 0.299 * R + 0.587 * G + 0.114 * B. Return the 2D list.
Requirements
- Convert each RGB pixel to a single grayscale value using the luminance formula
- Use the standard ITU-R BT.601 weights: R=0.299, G=0.587, B=0.114
- Return a 2D matrix (H × W) of grayscale values
- Preserve the spatial dimensions of the input image
Constraints
- image is a 3D list of shape H × W × 3 with integer RGB values in [0, 255]
- H >= 1, W >= 1
- Return a 2D list of floats
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Color to Grayscale
Color to Grayscale
Converting a color image to grayscale is one of the most fundamental image processing operations. The human eye perceives different colors with varying sensitivity, so a simple average of RGB channels does not produce a perceptually accurate grayscale image. Instead, a weighted sum based on human vision is used.
Given a color image represented as a 3D array (H × W × 3 with RGB channels), convert it to a grayscale image (H × W) using the luminance formula.
Algorithm
For each pixel, compute the grayscale intensity using the ITU-R BT.601 luminance weights:
Y=0.299⋅R+0.587⋅G+0.114⋅BThese weights reflect the human eye's greater sensitivity to green light, moderate sensitivity to red, and lower sensitivity to blue.
Examples
Input:
image = [[[255, 0, 0]]]
Output:
[[76.245]]
A single pure red pixel. Grayscale = 0.299 × 255 + 0.587 × 0 + 0.114 × 0 = 76.245.
Input:
image = [[[255, 0, 0], [0, 255, 0]], [[0, 0, 255], [255, 255, 255]]]
Output:
[[76.245, 149.685], [29.07, 255.0]]
A 2×2 image with red, green, blue, and white pixels. Notice green produces the highest grayscale value among the pure colors, reflecting the eye's peak sensitivity to green.
Hint 1
Iterate over each row and each pixel. For each pixel, extract the R, G, B values (image[i][j][0], image[i][j][1], image[i][j][2]) and compute the weighted sum using the luminance formula.
Hint 2
Build the result as a 2D list. For each row i, create a new list. For each column j, append 0.299 * R + 0.587 * G + 0.114 * B. Return the 2D list.
Requirements
- Convert each RGB pixel to a single grayscale value using the luminance formula
- Use the standard ITU-R BT.601 weights: R=0.299, G=0.587, B=0.114
- Return a 2D matrix (H × W) of grayscale values
- Preserve the spatial dimensions of the input image
Constraints
- image is a 3D list of shape H × W × 3 with integer RGB values in [0, 255]
- H >= 1, W >= 1
- Return a 2D list of floats
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array