AI Hints
Home›Computer Vision Tutorial›Classic Image Processing
Computer Vision · Lesson 5 of 111

Finding Edges

Where things begin and end.

⏱ 2 min read▶ Video at 9:13🎓 Free
Watch this lesson in the Computer Vision courseWatch on YouTube ↗Full Computer Vision course

Draw an apple with one line, and everybody knows what it is. Edges hold most of the information about shape. Your own visual system starts by finding edges, and so do neural networks. So, how do we find them?

Finding Edges
From the video: Finding Edges
  • A line drawing is enough
  • Edges hold shape
  • Vision starts here

Sobel and Canny

An edge is a sudden change in brightness. The Sobel filter measures that change, in both directions. Canny goes further, with several clever steps. It smooths the image, finds the gradients, thins the edges, and then keeps only the strong ones, plus any weak ones that connect to them. Run it. Vertical and horizontal edges appear separately, and Canny gives a clean line drawing. It's from nineteen eighty six, and it's still widely used.

edges.pypython
import cv2
import matplotlib.pyplot as plt
from skimage import data

gray = data.camera()
gx = cv2.Sobel(gray, cv2.CV_64F, 1, 0)
gy = cv2.Sobel(gray, cv2.CV_64F, 0, 1)
canny = cv2.Canny(gray, 80, 180)

fig, ax = plt.subplots(1, 3, figsize=(10, 3.2))
for a, im, n in zip(ax, [abs(gx), abs(gy), canny], ["Vertical edges", "Horizontal edges", "Canny"]):
    a.imshow(im, cmap="gray"); a.set_title(n); a.axis("off")
plt.tight_layout(); plt.savefig("edges.png", dpi=110)
print(f"Edge pixels found by Canny: {(canny > 0).mean():.1%}")
Output
Edge pixels found by Canny: 9.3%

Tuning Canny

Canny has two thresholds. Values above the high one are definite edges. Values below the low one are thrown away. Those in between are kept only if they touch a strong edge. Blur first if the image is noisy. In real work, you'd pick these by trying different values on sample images, or by automatically basing them on the image's median brightness.

High thresholddefinite edges
Low thresholdkept if connected
Blur firstreduces false edges
Auto thresholdsbased on the median

Summary

Edges capture shape, and Canny is the classic way to find them. But for counting apples, we need solid shapes, not outlines. Next, separating objects from their background.

Key takeaways
  • Edges are sudden brightness changes
  • Canny links strong edges
  • Blur before edge detection
Next up: Thresholding — separating objects from background