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:
- For each interior pixel (skip border pixels, set them to 0):
- Quantize the gradient direction to one of 4 angles: , , , (using modulo 180)
- Check the two neighbors along that direction:
- : left and right neighbors and
- : diagonal neighbors and
- : top and bottom and
- : diagonal and
- If the pixel's magnitude is both neighbors, keep it; otherwise suppress to 0.
Quantization ranges (using angle mod 180):
- or →
- →
- →
- →
Example
magnitude = [[5, 5, 5], [5, 10, 5], [5, 5, 5]] direction = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[0, 0, 0], [0, 10, 0], [0, 0, 0]]
- The gradient direction is 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 () is greater than or equal to both its left and right neighbors (), 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 , which is not greater than its right neighbor, also , but since they are equal and the pixel is not a local maximum along the direction in this specific comparison context, it gets suppressed), so they are suppressed to .
- Border pixels are set to 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