Back to Practice
#0021
Convert RGB Image To Grayscale
EasyML12 min10 XP
Problem
Given an RGB image array of shape (height, width, 3), return a grayscale image using 0.299*R + 0.587*G + 0.114*B.
Why This Matters
Many vision pipelines start by understanding image arrays. This problem makes channels, height, width, and pixel values concrete.
Function Signature
def rgb_to_grayscale(image):
Examples
Example 1
Inputone red pixel [255, 0, 0]
Outputabout 76.245
Red contributes 0.299 of its intensity to luminance.
Constraints
- Input shape is (height, width, 3).
- Return shape is (height, width).
- Do not average channels equally.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 4 hidden categories
Known primary colors
Inputred, green, blue pixels
Expectedweighted grayscale intensities
Green contributes most to perceived brightness in this formula.
Hidden Test Categories
Two-row imageBlack imageWhite imageFloating point pixel values