Sobel Edge Detection
Intuition
An edge is where brightness changes quickly. The Sobel operator estimates the horizontal and vertical brightness change at every pixel, smoothing slightly to resist noise.
Precise explanation
Sobel kernels (applied as cross-correlation, CV-10):
responds to vertical edges (left→right change), to horizontal edges (top→bottom).
Magnitude ; direction .
We compute valid outputs only (no padding), so the result is (H−2, W−2) and border artefacts
are avoided.
Worked example
Image with left half 0 and right half 10 (vertical edge): = 40 along the edge, = 0; magnitude 40.
Common mistakes
- Swapping and .
- Using
uint8(negative gradients wrap around). - Expecting thin edges — Sobel gives thick responses; non-maximum suppression (Canny) thins them.
Why this matters
Edge maps were the foundation of classical vision; CNNs' first layers learn Sobel-like filters.
Library implementation
scipy.ndimage.sobel(img, axis=1) and axis=0 compute the horizontal and vertical Sobel
derivatives (with border handling); on interior pixels they equal your valid-region gx and gy.
np.hypot(gx, gy) gives the magnitude in both cases.
References
- OpenCV Tutorials (OpenCV 4.13.0) — OpenCV team (
S023) - Scientific Python Lectures — Scientific Python developers (
S006)