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.
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.
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.
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.
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.
Sign in to take notes on this problem
Accepts: array
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.
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.
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.
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.
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.
Sign in to take notes on this problem
Accepts: array