v9.1.1
Menu

Sobel Edge Detection

mediumFREELinear FilteringImage ProcessingCV: Image Processing

Implement a Sobel Edge Detection algorithm to identify edges in an image by applying linear filtering techniques. This task involves using Sobel operators to compute the gradient of an image intensity function.

The Sobel operator is a discrete differential operator that computes the gradient of an image intensity function, which is a measure of the rate of change of the intensity in the x and y directions. This is represented by the gradient magnitude, G=Gx2+Gy2G = \sqrt{G_x^2 + G_y^2}, where GxG_x and GyG_y are the x and y components of the gradient.

Here are the steps to detect edges:

  1. Apply the x-direction Sobel kernel, GxG_x, to the image.
  2. Apply the y-direction Sobel kernel, GyG_y, to the image.
  3. Compute the gradient magnitude.
Gx=(−101−202−101),Gy=(−1−2−1000121)G_x = \begin{pmatrix} -1 & 0 & 1 \\ -2 & 0 & 2 \\ -1 & 0 & 1 \end{pmatrix}, \quad G_y = \begin{pmatrix} -1 & -2 & -1 \\ 0 & 0 & 0 \\ 1 & 2 & 1 \end{pmatrix}

This technique is widely used in image processing and computer vision applications.

Example

Input
sobel([[0,0,255],[0,0,255],[0,0,255]])
Output
[[0, 0, 0], [0, 255, 0], [0, 0, 0]]
Reasoning
  • Only the centre pixel (1,1)(1, 1) has a full 3×3 neighbourhood, so the 1-pixel border stays 0.
  • The centre's neighbourhood is the whole image: left and middle columns 0, right column 255.
  • Gx=(−1−2−1)⋅0+(1+2+1)⋅255=1020G_x = (-1 - 2 - 1) \cdot 0 + (1 + 2 + 1) \cdot 255 = 1020.
  • Gy=−(0+0+255)+(0+0+255)=0G_y = -(0 + 0 + 255) + (0 + 0 + 255) = 0, since the top and bottom rows are identical.
  • Magnitude: 10202+02=1020\sqrt{1020^2 + 0^2} = 1020, clamped to 255255.
  • Output: [[0,0,0],[0,255,0],[0,0,0]][[0, 0, 0], [0, 255, 0], [0, 0, 0]], a strong vertical edge at the centre.

Constraints

  • Return edge magnitude image
  • Round to nearest integer
  • Clamp to [0, 255]
  • Border pixels (no full 3×3 neighbourhood) stay 0

More from CV: Image Processing

solution.py

Test results

Sign in to run your code against the test cases.