PIXELBANKv9.1.0
Menu

Non-Maximum Suppression for Edge Thinning

Given gradient magnitude and direction matrices (same size), thin edges by keeping only local maxima along the gradient direction.

Algorithm:

  1. For each interior pixel (skip border pixels, set them to 0):
  2. Quantize the gradient direction to one of 4 angles: 0°0°, 45°45°, 90°90°, 135°135° (using modulo 180)
  3. Check the two neighbors along that direction:
    • 0°0°: left and right neighbors (i,j−1)(i, j-1) and (i,j+1)(i, j+1)
    • 45°45°: diagonal neighbors (i−1,j+1)(i-1, j+1) and (i+1,j−1)(i+1, j-1)
    • 90°90°: top and bottom (i−1,j)(i-1, j) and (i+1,j)(i+1, j)
    • 135°135°: diagonal (i−1,j−1)(i-1, j-1) and (i+1,j+1)(i+1, j+1)
  4. If the pixel's magnitude is ≥\geq both neighbors, keep it; otherwise suppress to 0.

Quantization ranges (using angle mod 180):

  • [0,22.5)[0, 22.5) or [157.5,180)[157.5, 180) → 0°0°
  • [22.5,67.5)[22.5, 67.5) → 45°45°
  • [67.5,112.5)[67.5, 112.5) → 90°90°
  • [112.5,157.5)[112.5, 157.5) → 135°135°

Example:

Input:
magnitude = [[5, 5, 5], [5, 10, 5], [5, 5, 5]]
direction = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
Output:
[[0, 0, 0], [0, 10, 0], [0, 0, 0]]
Reasoning:
  • The gradient direction is 0°0° for all pixels, so we compare each pixel's magnitude with its left and right neighbors.
  • For the middle pixel in the second row, its magnitude (1010) is greater than or equal to both its left and right neighbors (55), so it is kept as is.
  • For all other interior pixels, their magnitudes are not greater than or equal to both their neighbors (e.g., the top-middle pixel has a magnitude of 55, which is not greater than its right neighbor, also 55, but since they are equal and the pixel is not a local maximum along the 0°0° direction in this specific comparison context, it gets suppressed), so they are suppressed to 00.
  • Border pixels are set to 00 as per the algorithm, resulting in the final output: [[0, 0, 0], [0, 10, 0], [0, 0, 0]]

Constraints:

  • magnitude and direction are 2D lists of the same size (at least 3x3)
  • direction values are in degrees
  • Border pixels are always set to 0
  • Return the thinned magnitude matrix
🔒

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.
Non-Maximum Suppression for Edge Thinning - Medium | PixelBank