Loading content...
In image processing and computer vision, converting a color image to grayscale is a fundamental operation used in countless applications—from edge detection and feature extraction to reducing computational complexity in neural networks. While there are multiple approaches to grayscale conversion, the luminosity method is widely regarded as the gold standard because it mimics how the human eye perceives brightness.
The human eye is not equally sensitive to all colors. Research in color science has established that we perceive green as the brightest, followed by red, and then blue. The luminosity method leverages these perceptual differences by applying weighted coefficients derived from the Rec. 601 (ITU-R BT.601) standard:
$$\text{Gray} = 0.299 \times R + 0.587 \times G + 0.114 \times B$$
This formula ensures that the resulting grayscale image retains the perceptual brightness relationships present in the original color image. For example, a bright yellow (high red and green) will appear lighter than a deep blue of similar intensity, matching human visual expectations.
Input Representation: The input is a 3D array representing an RGB image with dimensions (H, W, 3):
Your Task: Implement a function that converts an RGB image to grayscale using the luminosity coefficients. The function should:
Error Handling: Return -1 for any of the following validation failures:
image = [[[255, 0, 0], [0, 255, 0]], [[0, 0, 255], [255, 255, 255]]][[76, 150], [29, 255]]This 2×2 image contains four distinct colors—one in each corner:
Top-left — Pure Red (255, 0, 0): Gray = 0.299 × 255 + 0.587 × 0 + 0.114 × 0 = 76.245 → 76
Top-right — Pure Green (0, 255, 0): Gray = 0.299 × 0 + 0.587 × 255 + 0.114 × 0 = 149.685 → 150
Bottom-left — Pure Blue (0, 0, 255): Gray = 0.299 × 0 + 0.587 × 0 + 0.114 × 255 = 29.07 → 29
Bottom-right — Pure White (255, 255, 255): Gray = 0.299 × 255 + 0.587 × 255 + 0.114 × 255 = 255
Notice how green (150) appears much brighter than red (76) or blue (29), reflecting human color perception. White correctly maps to 255.
image = [[[128, 64, 32], [255, 128, 0]], [[0, 128, 255], [100, 100, 100]]][[79, 151], [104, 100]]This example demonstrates mixed colors and neutral gray:
Top-left — Brownish tone (128, 64, 32): Gray = 0.299 × 128 + 0.587 × 64 + 0.114 × 32 = 38.272 + 37.568 + 3.648 = 79.488 → 79
Top-right — Bright Orange (255, 128, 0): Gray = 0.299 × 255 + 0.587 × 128 + 0.114 × 0 = 76.245 + 75.136 + 0 = 151.381 → 151
Bottom-left — Cyan-ish Blue (0, 128, 255): Gray = 0.299 × 0 + 0.587 × 128 + 0.114 × 255 = 0 + 75.136 + 29.07 = 104.206 → 104
Bottom-right — Neutral Gray (100, 100, 100): Gray = 0.299 × 100 + 0.587 × 100 + 0.114 × 100 = 100 → 100
Neutral gray values (where R = G = B) always map to themselves, validating that the coefficients sum to exactly 1.0.
image = [[[256, 0, 0], [0, 255, 0]]]-1The top-left pixel has a red value of 256, which exceeds the valid range [0, 255]. Since pixel values must be 8-bit unsigned integers (0-255), this input is invalid and the function returns -1.
Constraints