Image Gradient Magnitude

~12 mincode completion

Implement image_gradient_magnitude(image) returning the magnitude array.

Examples

Vertical step of 3: magnitude is 3 on the right column

Input
image_gradient_magnitude([[0, 0, 3], [0, 0, 3], [0, 0, 3]])
Output
[[0, 3], [0, 3]]

Horizontal step of 4: magnitude is 4 on the only output row

Input
image_gradient_magnitude([[0, 0, 0], [4, 4, 4]])
Output
[[4, 4]]

Hints

Hint 1

Take the square root at the end, not inside the sum.

Hint 2

Reach for forward rather than central differences.

Requirements

  • image: 2D array of shape (H, W)

  • Return array of shape (H-1, W-1)

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~12 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 93.

Python
import numpy as np


def image_gradient_magnitude(image):
    """
    Forward-difference gradient magnitude on a 2D image.

    Args:
        image: 2D array of shape (H, W)

    Returns:
        array of shape (H-1, W-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.