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+Gy2, where Gx and Gy are the x and y components of the gradient.
Here are the steps to detect edges:
Apply the x-direction Sobel kernel, Gx, to the image.
Apply the y-direction Sobel kernel, Gy, to the image.
Compute the gradient magnitude.
Gx=−1−2−1000121,Gy=−101−202−101
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) 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=1020.
Gy=−(0+0+255)+(0+0+255)=0, since the top and bottom rows are identical.
Magnitude: 10202+02=1020, clamped to 255.
Output: [[0,0,0],[0,255,0],[0,0,0]], a strong vertical edge at the centre.
Sobel edge detection is a classic gradient-based method to find edges in images. An edge is a location where intensity changes sharply; mathematically, this corresponds to a large gradient of the image function. If we treat a grayscale image as a 2D function I(x,y), then edges are where the derivatives ∂I/\partialx and ∂I/\partialy are large.
Direct derivatives are noisy, so Sobel uses 3×3 convolution kernels that both approximate the derivative and apply slight smoothing (the central row/column is weighted more). Convolving the image with Gx approximates the horizontal derivative (change along x, detecting vertical edges), and convolving with Gy approximates the vertical derivative (change along y, detecting horizontal edges). From these two components, you can compute an edge magnitudeG=Gx2+Gy2 and optionally a direction θ=arctan2(Gy,Gx).
Algorithm / General Approach
At a high level, the pattern is:
Interpret Sobel operators as convolution filters.
For each pixel, apply the 3×3 Sobel kernels to a 3×3 neighborhood to get:
gx(x,y): horizontal gradient component.
gy(x,y): vertical gradient component.
Combine gx and gy to get:
Magnitude: G(x,y)=gx2+gy2.
Optionally apply a threshold to get a binary edge map.
So the problem is essentially: implement 2D convolution with given kernels, then compute per-pixel gradient magnitude.
Step-by-Step Strategy
Input preparation
Ensure you have a grayscale image (2D array).
If you start with RGB, convert to grayscale first (e.g., weighted sum of channels).
Pad the input (e.g., with zeros or edge replication) and produce same-sized output.
Compute Gx and Gy via convolution
For each pixel (i,j) (excluding borders if not padded):
Take its 3×3 neighborhood.
Multiply element-wise with Gx and sum → gx(i,j).
Multiply element-wise with Gy and sum → gy(i,j).
Example (pseudo-code):
for i in range(1, H-1):
for j in range(1, W-1):
gx = 0
gy = 0
for u in range(-1, 2):
for v in range(-1, 2):
val = img[i+u][j+v]
gx += val * Gx[u+1][v+1]
gy += val * Gy[u+1][v+1]
Gx_img[i][j] = gx
Gy_img[i][j] = gy