PIXELBANKv8.2.1
Menu

Sobel Edge Detection

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

The Sobel GxG_x kernel is: Gx=[101202101]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 (H2)×(W2)(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:
  • The input image is a 3x3 matrix: [[1, 2, 3], [4, 5, 6], [7, 8, 9]].
  • We apply the Sobel GxG_x kernel to the image using valid convolution. Since the kernel is 3x3, the output will be a 1x1 matrix (because the input dimensions are 3x3, so (32)×(32)=1×1(3-2) \times (3-2) = 1 \times 1).
  • We calculate the sum of element-wise products between the kernel and the overlapping image region: (11)+(02)+(13)+(24)+(05)+(26)+(17)+(08)+(19)=1+0+38+0+127+0+9=8(-1 \cdot 1) + (0 \cdot 2) + (1 \cdot 3) + (-2 \cdot 4) + (0 \cdot 5) + (2 \cdot 6) + (-1 \cdot 7) + (0 \cdot 8) + (1 \cdot 9) = -1 + 0 + 3 -8 + 0 + 12 - 7 + 0 + 9 = 8.
  • The final output is the absolute value of the result, which is already positive, rounded to 4 decimal places: [[8.0000]][[8.0000]], but since the problem doesn't specify to keep trailing zeros, it is [[8]].

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
Editor

Test Results

0/0
Run code to see test results.