Chromaticity: Colour Without Brightness

~8 mincode completion

Implement rgb_chromaticity(image).

  • image has shape (H, W, 3) and every pixel has R + G + B > 0.
  • Return an array of shape (H, W, 3) where each pixel is divided by its own channel sum.

Examples

[2, 4, 2] sums to 8, so chromaticity is [0.25, 0.5, 0.25]

Input
rgb_chromaticity([[[2, 4, 2]]])
Output
[[[0.25, 0.5, 0.25]]]

Doubling the brightness of a pixel does not change its chromaticity

Input
rgb_chromaticity([[[2, 4, 2], [4, 8, 4]]])
Output
[[[0.25, 0.5, 0.25], [0.25, 0.5, 0.25]]]

Hints

Hint 1

Keep the reduced dimension with keepdims=True so broadcasting lines up.

Hint 2

A common slip here: divided by the max channel instead of the sum.

Requirements

  • image: array of shape (H, W, 3), every pixel sums to a positive number

  • Return array of shape (H, W, 3); each pixel sums to 1

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~8 min

8 employers weight this skill

4 autonomy companies, 1 defense company, 1 health and bio company, 1 enterprise vendor, 1 AI product company. Top match scores 81.

Python
import numpy as np


def rgb_chromaticity(image):
    """
    Normalised rgb: each channel divided by the per-pixel channel sum.

    Args:
        image: array of shape (H, W, 3), every pixel sums to a positive number

    Returns:
        array of shape (H, W, 3); each pixel sums to 1
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.