Skip to content
DeepForgeby svx
All problems
HardComputer Visioncv-036

Sobel Gradient Magnitude

Problem

Compute the Sobel gradient magnitude of a grayscale image.

Use Gx = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]] and Gy = [[1, 2, 1], [0, 0, 0], [-1, -2, -1]] in valid mode, then output sqrt(Gx^2 + Gy^2) rounded to 4 decimal places. The output shape is (H - 2) x (W - 2).

Hint

Compute both directional responses, then take the Euclidean norm.

Test cases

case 1
in: [[[5,5,5],[5,5,5],[5,5,5]]]
expected: [[0]]
case 2
in: [[[0,0,0],[0,0,0],[0,0,10]]]
expected: [[14.1421]]
case 3
in: [[[0,0,0],[0,0,0],[5,5,5]]]
expected: [[20]]
case 4
in: [[[0,0,0,0],[0,10,10,0],[0,10,10,0],[0,0,0,0]]]
expected: [[42.4264,42.4264],[42.4264,42.4264]]
Starter codePython
def sobel_magnitude(image):
    # Your code here
    pass

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