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:
magnitude = [[1, 5, 1],
[1, 5, 1],
[1, 5, 1]]
direction = [[90, 90, 90],
[90, 90, 90],
[90, 90, 90]][[0, 0, 0], [0, 5, 0], [0, 0, 0]]
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
More from CV: Feature Detection and Matching
- Background Knowledge
Edge detection in images is typically based on the image gradient, which measures how quickly intensity changes in different directions. After smoothing (e.g., with a Gaussian) and applying a gradient operator (e.g., Sobel), you get:
- a gradient magnitude image G(x,y), showing how strong the edge is at each pixel
- a gradient direction image θ(x,y), showing the direction of maximum change (perpendicular to the edge).
Raw gradient magnitudes often produce thick edges (several pixels wide), since many neighboring pixels along the gradient direction can have similar large magnitudes. Non-maximum suppression (NMS) is used to thin these responses so that edges become one-pixel wide ridges: it keeps only local maxima of the gradient magnitude along the gradient direction and suppresses others to zero.
To do this efficiently, we quantize the continuous gradient direction θ into a small set of directions (here 4: 0°, 45°, 90°, 135°). For each pixel, we look at the two neighbors in the quantized direction of the gradient and keep the pixel only if its magnitude is greater than or equal to both neighbors; otherwise, we set it to zero. This is the core of NMS in edge detection (as used, for example, in the Canny edge detector).
- Algorithm / Approach
General pattern for edge non-maximum suppression:
- For each pixel (i,j):
- Read its gradient magnitude M(i,j) and direction θ(i,j).
- Quantize θ(i,j) to one of a small set of canonical directions (0°, 45°, 90°, 135°).
- Based on that quantized direction, choose the two neighboring pixels that lie along the gradient direction (i.e., on both sides of (i,j) along that line).
- Compare M(i,j) with the magnitudes at those two neighbors.
- If M(i,j) is a local maximum (≥ both neighbors), keep it; otherwise, set its output to 0.
This is a single pass over the image, using local comparisons (3 pixels per direction), so it is inherently O(H×W) where H,W are the image dimensions.
- Step-by-Step Strategy
Here’s a clear implementation plan:
- Inputs and outputs
- Input:
- mag[h][w]: gradient magnitudes (floats or doubles).
- dir[h][w]: gradient directions in radians or degrees (often from atan2).
- Output:
- nms[h][w]: result after non-maximum suppression (same shape as mag).
- Normalize / interpret angles
- Make sure directions are in a consistent range, e.g.:
- Degrees: −180∘ to 180∘ or 0∘ to 360∘.
- You may convert radians to degrees if easier:
angle_deg = dir[i][j] * 180.0 / math.pi
- Bring angles into [0°, 180°) or [0°, 360°)
- Often we only need 0–180°, because gradient direction is symmetric:
if angle_deg < 0:
angle_deg += 180.0
- Quantize direction into 4 bins Common mapping (angles in degrees, example for [0°,180°)):
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.