Finding Edges
Where things begin and end.
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?

- 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.
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%}")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.
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