Beginner level · Module 4: Image Filtering and Edge Detection · Lesson 2 of 3
Gradients and the Sobel Operator
Learners compute horizontal and vertical gradients with Sobel kernels on a small array and on photographs, and combine them into an edge-strength map.
In this lesson you will
- Compute image gradients with Sobel kernels
- Combine gradients into magnitude and direction
Key concepts
gradientSobel x and y kernelsgradient magnitudecv2.Sobel with CV_64Fedge direction
The lesson
An edge is a place where brightness changes quickly. The Sobel x kernel responds to changes from left to right (vertical edges); the Sobel y kernel responds to changes from top to bottom (horizontal edges).
The gradient magnitude, sqrt(gx² + gy²), measures edge strength whatever its direction. Compute gradients in a signed, larger type (cv2.Sobel(gray, cv2.CV_64F, 1, 0)), because gradients can be negative or larger than 255.
Gradient maps are useful in their own right, for example to highlight cracks or the outlines of objects in inspection photographs, and they are the basis of the Canny detector and of HOG features.
Step by step
- Apply Sobel x and y kernels to the 6×6 example array by hand for one position.
- Run the code example and check the outputs.
- Compute gx, gy and magnitude for a photo of a wall or road.
- Display the three maps side by side.
Finding a vertical edge with Sobel kernels
Sliding two 3×3 Sobel kernels over a small dark-to-bright image shows exactly how gradients reveal edges.
import numpy as npfrom numpy.lib.stride_tricks import sliding_window_view # 6 x 6 grayscale image: dark on the left (20), bright on the right (200)img = np.array([[20, 20, 20, 200, 200, 200]] * 6, dtype=np.int32) sobel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])sobel_y = sobel_x.T # responds to top-to-bottom changes windows = sliding_window_view(img, (3, 3)) # every 3 x 3 patch: shape (4, 4, 3, 3)gx = (windows * sobel_x).sum(axis=(2, 3))gy = (windows * sobel_y).sum(axis=(2, 3))magnitude = np.hypot(gx, gy) # sqrt(gx**2 + gy**2)edges = magnitude > 100 print(gx)print(gy.max(), gy.min())print(edges.astype(int))
Output
[[ 0 720 720 0]
[ 0 720 720 0]
[ 0 720 720 0]
[ 0 720 720 0]]
0 0
[[0 1 1 0]
[0 1 1 0]
[0 1 1 0]
[0 1 1 0]]How it works
Each output value is the sum of a 3×3 patch multiplied by the kernel. Patches that lie entirely in the dark or bright half give 0, because the left and right columns cancel out. Patches that straddle the boundary give (200 − 20) × (1 + 2 + 1) = 720, so the vertical edge appears as a strong response in the two middle columns. The Sobel y kernel measures top-to-bottom change, and there is none, so gy is 0 everywhere. The magnitude combines both directions, and thresholding it marks the edge pixels. The output is 4×4 because only complete patches are used. cv2.Sobel pads the borders and returns a full-size result with the same values inside. Note the int32 type: with uint8, negative gradients and values above 255 would be lost. The Canny detector goes further and thins this two-pixel-wide response to a single line.
Key concepts
3×3 kernelsliding windowshorizontal and vertical gradientsgradient magnitudewhy int32, not uint8
Practice activity
Produce and interpret gradient maps for three infrastructure photographs (walls, roads, bridges).
Expected result: Correct gradient maps and a note on which structures produce strong edges.
Check your understanding
-
Which Sobel kernel detects vertical edges?
Show answer
The x kernel, which measures left-to-right changes.
-
Why use CV_64F for Sobel outputs?
Show answer
Gradients can be negative or exceed 255, which uint8 cannot store.
Further learning
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.