v9.1.1
Menu

Sobel Edge Detection

easyFREEEdge DetectionComputer Vision 2

Apply the Sobel operator in the X direction to a 2D grayscale image (matrix).

The Sobel GxG_x kernel is: Gx=[−101−202−101]G_x = \begin{bmatrix} -1 & 0 & 1 \\ -2 & 0 & 2 \\ -1 & 0 & 1 \end{bmatrix}

Perform valid convolution (no padding) by sliding the kernel over the image. For each position, compute the sum of element-wise products between the kernel and the overlapping image region. Return the absolute value of each result, rounded to 4 decimal places.

The output matrix will have dimensions (H−2)×(W−2)(H-2) \times (W-2) where HH and WW are the input dimensions.

Example

Input
image = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output
[[8]]
Reasoning
  • image is a 3×33 \times 3 grid of grayscale intensities: rows [1, 2, 3], [4, 5, 6], [7, 8, 9]. Brightness rises by 1 per step to the right and by 3 per step down.
  • With no padding, the 3×33 \times 3 kernel fits in exactly one position, so the output is (3−2)×(3−2)=1×1(3-2) \times (3-2) = 1 \times 1.
  • Multiply each pixel by the kernel weight in the same spot and add: (−1)(1)+0(2)+1(3)+(−2)(4)+0(5)+2(6)+(−1)(7)+0(8)+1(9)=8(-1)(1) + 0(2) + 1(3) + (-2)(4) + 0(5) + 2(6) + (-1)(7) + 0(8) + 1(9) = 8.
  • Read it column-wise: the middle column has weight 0, so the kernel measures right column minus left column, with the centre row counted twice: 1(3−1)+2(6−4)+1(9−7)=2+4+2=81(3-1) + 2(6-4) + 1(9-7) = 2 + 4 + 2 = 8.
  • The absolute value of 88 is 88, giving [[8]]. Integer pixels give an integer sum, so no decimals appear.
  • Notice the vertical change of 3 per row contributes nothing: GxG_x only responds to horizontal change. A left-right mirror would give −8-8, which the absolute value turns back into 88.

Constraints

  • Input image is a 2D list of numbers (at least 3x3)
  • Return a 2D list of absolute gradient values
  • Round each value to 4 decimal places

More from Computer Vision 2

solution.py

Test results

Sign in to run your code against the test cases.