Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

147. Color space conversion

easy
GeneralGeneral
senior

Implement an RGB → Grayscale color space conversion, a common preprocessing step in computer vision. Given an image of RGB pixels, convert it to a 2D grayscale image using a weighted luminance formula.

The grayscale intensity is defined as:

Y=0.299R+0.587G+0.114BY = 0.299R + 0.587G + 0.114B

Requirements

Implement the function

python

Rules:

  • Convert each pixel [R, G, B] into a single grayscale value using the formula above.
  • Return a 2D NumPy array.
  • Do not use any prebuilt image/color conversion utilities (e.g., OpenCV, PIL).
  • Keep the output values as floats (no rounding required).

Example

python

Output:

python
Input Signature
ArgumentType
imagenp.ndarray
Output Signature
Return NameType
valuenp.ndarray

Constraints

  • Input array shape must be (H, W, 3).

  • Use NumPy vectorization; no OpenCV/PIL utilities.

  • Return NumPy array.

Hint 1

Ensure input image is a NumPy array of floats with shape (H, W, 3).

Hint 2

Use the luminance weights w = np.array([0.299, 0.587, 0.114]) and combine them with the RGB channels by taking a weighted sum across the last axis (e.g., image @ w or np.tensordot).

Hint 3

The result should be a NumPy array of shape (H, W).

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
senior
entry
Tags
numpy
vectorization
color-conversion
luminance
39 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit