PIXELBANKv8.2.1
Menu

Sobel Edge Detection

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=(101202101),Gy=(121000121)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,255,0],[0,255,0],[0,255,0]]
Reasoning:
  • The input image is a 3×3 matrix with a sharp vertical change from 0 (left column) to 255 (right column), so the Sobel GxG_x kernel, which detects vertical edges, will have large responses in the center column and near-zero on the uniform columns.

  • For the center column, each 3×3 neighborhood straddles 0 on the left and 255 on the right; convolving with GxG_x yields a large non-zero gradient magnitude (after computing G=Gx2+Gy2G = \sqrt{G_x^2 + G_y^2}), which is then clamped/normalized to 255, marking an edge.

  • For the left and right columns, each 3×3 neighborhood is mostly uniform (all 0 or all 255), so the gradients are near 0, and their magnitudes stay 0 after normalization.

  • Thus, the output has strong edges only in the middle column: [[0,255,0],[0,255,0],[0,255,0]][[0,255,0],[0,255,0],[0,255,0]].

Constraints:

  • Return edge magnitude image
  • Round to nearest integer
  • Clamp to [0, 255]
Editor

Test Results

0/0
Run code to see test results.