Skip to content
DeepForgeby svx
All problems
HardComputer Visioncv-074

Canny Non-Max Suppression

Problem

Run the non-maximum suppression step of Canny edge detection.

For every interior pixel, quantize the gradient angle (degrees mod 180) and keep the magnitude only when it is >= both neighbors along that direction, otherwise output 0. Angles below 22.5 or at least 157.5 use left/right neighbors, below 67.5 the anti-diagonal, below 112.5 up/down, and the rest the main diagonal. Border pixels are always 0.

Hint

The quantized angle picks which pair of opposite neighbors to compare against.

Test cases

case 1
in: [[[0,0,0],[0,5,0],[0,0,0]],[[0,0,0],[0,0,0],[0,0,0]]]
expected: [[0,0,0],[0,5,0],[0,0,0]]
case 2
in: [[[0,0,0],[0,5,4],[0,0,0]],[[0,0,0],[0,0,0],[0,0,0]]]
expected: [[0,0,0],[0,5,0],[0,0,0]]
case 3
in: [[[0,0,0],[0,4,5],[0,0,0]],[[0,0,0],[0,0,0],[0,0,0]]]
expected: [[0,0,0],[0,0,0],[0,0,0]]
case 4
in: [[[0,5,0],[0,4,0],[0,3,0]],[[0,0,0],[0,90,0],[0,0,0]]]
expected: [[0,0,0],[0,0,0],[0,0,0]]
Starter codePython
def canny_nms(magnitude, angle):
    # Your code here
    pass

4 test cases · run your code against them in the editor