PIXELBANKv9.1.0
Menu

Edge Non-Maximum Suppression

You are given gradient magnitude and direction images and need to perform non-maximum suppression to thin edges to single-pixel width.

For each pixel, compare its magnitude to the two neighbors along the gradient direction. Keep the pixel only if it's the local maximum in that direction.

Direction quantization (direction is perpendicular to edge):

  • 0° or 180°: compare to left and right neighbors
  • 45° or 225°: compare to top-right and bottom-left neighbors
  • 90° or 270°: compare to top and bottom neighbors
  • 135° or 315°: compare to top-left and bottom-right neighbors

Quantize the angle to these 4 directions based on which is closest.

Example:

Input:
magnitude = [[1, 5, 1],
             [1, 5, 1],
             [1, 5, 1]]
direction = [[90, 90, 90],
            [90, 90, 90],
            [90, 90, 90]]
Output:
[[0, 0, 0], [0, 5, 0], [0, 0, 0]]
Reasoning:

Direction 90° means vertical gradient → compare to left/right neighbors.

For center pixel (1,1):

  • magnitude = 5
  • direction = 90° → compare to (1,0)=1 and (1,2)=1
  • 5 > 1 and 5 > 1 → keep

For pixel (0,1):

  • magnitude = 5
  • But it's on the border, so → 0

For pixel (1,0) and (1,2):

  • They're on the border → 0

Actually looking at test output, only the true interior maximum is kept.

Constraints:

  • magnitude is the gradient magnitude image
  • direction is the gradient direction in degrees [0, 360)
  • Return thinned edge magnitude map (0 for suppressed pixels)
  • Border pixels are set to 0
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.