Edges, contours and blobs
Sobel gradients, the four stages of Canny, and turning a binary mask into outlines and objects.
- Compute image gradients with Sobel kernels and combine them into magnitude and direction
- Walk through Canny's non-maximum suppression and hysteresis
- Extract contours and read the hierarchy array
An edge is a place where brightness changes quickly — a large gradient. The Sobel kernels estimate the two partial derivatives, each combined with a little smoothing across the other direction:
\[G_x = \begin{pmatrix} -1 & 0 & 1 \\ -2 & 0 & 2 \\ -1 & 0 & 1 \end{pmatrix} * I, \qquad G_y = \begin{pmatrix} -1 & -2 & -1 \\ 0 & 0 & 0 \\ 1 & 2 & 1 \end{pmatrix} * I, \qquad \vert\nabla I\vert = \sqrt{G_x^2 + G_y^2}\]11_edge_detection.py blurs first and then calls cv2.Sobel with ddepth=cv2.CV_64F — signed floats, because a dark-to-bright edge gives a positive derivative and a bright-to-dark one a negative one, and an 8-bit output would clip the negatives to zero.
sobelx = cv2.Sobel(src=img_blur, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=5)
sobely = cv2.Sobel(src=img_blur, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=5)
sobelxy = cv2.Sobel(src=img_blur, ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5)
edges = cv2.Canny(image=img_blur, threshold1=100, threshold2=200)
A caution about the third line: dx=1, dy=1 is the mixed second derivative \(\partial^2 I / \partial x\,\partial y\), not a combined edge map. It responds to corners and diagonal texture and is weak on straight edges — on img.png its largest value is about a quarter of \(G_x\)’s. The edge strength is the magnitude above.
Canny in four stages
The script’s docstring lists the stages; the animation runs them on a small image whose disc fades from high contrast on the left to low on the right:
- Blur to suppress noise.
- Gradient magnitude and direction with Sobel.
- Non-maximum suppression: keep a pixel only if it is larger than both neighbours along the gradient direction, thinning thick ridges to one pixel.
- Hysteresis: pixels above the high threshold are edges; pixels between the two thresholds are edges only if connected to one. This keeps long edges whole where their contrast dips, without admitting isolated noise.
Contours and blobs
13_contour_detection_01.py thresholds a grey image and calls cv2.findContours, which traces the boundary of every white region:
ret, thresh = cv2.threshold(img_gray, 150, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, mode=cv2.RETR_TREE,
method=cv2.CHAIN_APPROX_NONE)
CHAIN_APPROX_NONE stores every boundary pixel; CHAIN_APPROX_SIMPLE keeps only the end points of straight runs, so a rectangle becomes four points. The hierarchy array holds [next, previous, first_child, parent] for each contour; RETR_TREE fills in the full nesting, RETR_EXTERNAL returns outer contours only, and RETR_LIST ignores nesting. From a contour, cv2.contourArea, cv2.boundingRect and cv2.moments give size, box and centroid.
10_blob_detection.py packages a similar idea: SimpleBlobDetector thresholds at many levels, groups connected pixels, merges centres that agree across levels, then filters the blobs by area, circularity, convexity and inertia ratio.
Try it yourself. In the animation, raise the high threshold to 300 and then lower it to 120. Count how the “kept” weak pixels change, and explain why a lower high threshold reduces the number of weak pixels kept. Then run cv2.Canny on a photo of your own with (t, 2t) for t = 25, 50, 100 and pick the value where the object outlines are complete but the texture is not.