📘
Sobel Edge Detection
EasyEdge Detection
Apply the Sobel operator in the X direction to a 2D grayscale image (matrix).
The Sobel Gx kernel is: Gx=−1−2−1000121
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) where H and W 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 Gx 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 (3−2)×(3−2)=1×1).
- We calculate the sum of element-wise products between the kernel and the overlapping image region: (−1⋅1)+(0⋅2)+(1⋅3)+(−2⋅4)+(0⋅5)+(2⋅6)+(−1⋅7)+(0⋅8)+(1⋅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]], 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
Python 3.13.1
Test Results
0/0Run code to see test results.