Sub topic 7
Finding Intensity Changes with Sobel and Scharr
Prerequisites and the gradient idea
This chapter assumes basic Python, NumPy array shapes and dtypes, OpenCV image loading, and the difference between grayscale and color images.
An image gradient measures how quickly pixel intensity changes. A large change often indicates an edge, such as the boundary between a dark object and a bright background. A small gradient usually occurs inside a region with similar brightness.
The Sobel operator estimates these changes in two directions:
- The x-gradient measures changes from left to right. It responds strongly to vertical edges.
- The y-gradient measures changes from top to bottom. It responds strongly to horizontal edges.
Each gradient is signed. A positive value means intensity increases in the chosen direction. A negative value means it decreases.
Sobel filters
Sobel combines a derivative with a small amount of smoothing. OpenCV applies the filter with cv2.Sobel. Use a signed or floating-point output depth. If you store a negative gradient directly in an unsigned 8-bit image, the value can be clipped or wrapped, which destroys information.
import cv2
import numpy as np
img = cv2.imread("input.jpg")
if img is None:
raise FileNotFoundError("input.jpg could not be read")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
gx = cv2.Sobel(blur, cv2.CV_32F, 1, 0, ksize=3)
gy = cv2.Sobel(blur, cv2.CV_32F, 0, 1, ksize=3)
gx_view = cv2.convertScaleAbs(gx)
gy_view = cv2.convertScaleAbs(gy)
cv2.imwrite("sobel_x.png", gx_view)
cv2.imwrite("sobel_y.png", gy_view)The dx and dy arguments select the derivative direction. With ksize=3, Sobel uses a 3-by-3 kernel. Larger kernels consider a wider neighborhood and reduce sensitivity to tiny changes, but they also blur fine edges.
The displayed images use absolute values because ordinary image files cannot show negative values. Keep gx and gy in floating-point form for later calculations.
Scharr for small kernels
Scharr is a related operator designed to provide a more accurate rotational estimate when using a 3-by-3 kernel. It often gives better results than Sobel for diagonal or fine edges. Use cv2.Scharr when you want a 3-by-3 derivative and do not need to choose another kernel size.
scharr_x = cv2.Scharr(blur, cv2.CV_32F, 1, 0)
scharr_y = cv2.Scharr(blur, cv2.CV_32F, 0, 1)
cv2.imwrite("scharr_x.png", cv2.convertScaleAbs(scharr_x))
cv2.imwrite("scharr_y.png", cv2.convertScaleAbs(scharr_y))| Operator | Typical use | Main choice |
|---|---|---|
| Sobel | General gradient estimation | Choose the kernel size |
| Scharr | More accurate 3-by-3 gradients | Fixed 3-by-3 design |
Common errors
- Applying Sobel to a color image without deciding how to combine channel results.
- Saving signed gradients as
uint8before taking their absolute values. - Treating
gxas a complete edge map. It only measures one direction. - Using a very large kernel and losing narrow features.
- Comparing gradient images without checking their scaling. Display conversion changes how values look but not the underlying gradient data.
The x and y gradients can later be combined into a gradient magnitude and direction. Those values provide a direction-independent description of intensity changes and are useful inputs to edge detectors.
Exercise
Why should a Sobel derivative usually use cv2.CV_32F or another signed output depth?
Show answer
Derivatives can be negative. A floating-point or signed output preserves both increasing and decreasing intensity changes before display conversion or further calculations.