Histogram Equalisation

~14 mincode completion

Implement equalize_histogram(image, levels).

  • image is a 2D integer array with values in 0 .. levels-1.
  • Return a 2D integer array of the same shape.

Examples

A dark 2x4 image is stretched onto 0 .. 15

Input
equalize_histogram([[0, 0, 0, 1], [1, 1, 2, 2]], 16)
Output
[[0, 0, 0, 9], [9, 9, 15, 15]]

An image that already uses every level evenly is unchanged

Input
equalize_histogram([[0, 1], [2, 3]], 4)
Output
[[0, 1], [2, 3]]

Eight levels, uneven counts: 0,0,1,1,2,2,3,7 become 0,0,2,2,5,5,6,7

Input
equalize_histogram([[0, 0, 1, 1], [2, 2, 3, 7]], 8)
Output
[[0, 0, 2, 2], [5, 5, 6, 7]]

Hints

Hint 1

Index the array with a boolean mask to keep only the elements that match.

Hint 2

Do not forget to subtract cdf min. That step is easy to skip.

Requirements

  • image: 2D integer array, values in 0 .. levels-1

  • levels: number of grey levels L

  • Return 2D integer array, same shape, values in 0 .. levels-1

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~14 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 equalize_histogram(image, levels):
    """
    Global histogram equalisation.

    Args:
        image:  2D integer array, values in 0 .. levels-1
        levels: number of grey levels L

    Returns:
        2D integer array, same shape, values in 0 .. levels-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.