Edge detection finds points in an image where intensity changes sharply, which usually mark object boundaries, texture changes, or depth discontinuities. Almost all classical methods reduce to estimating the image gradient and looking for large magnitudes.
Gradients
Treat the image as a function of position. Its gradient is
The magnitude tells you how strong the edge is; the orientation tells you which way it runs. On a discrete grid the derivatives are approximated by convolving with small kernels.
Sobel and Prewitt
Both are kernels that combine differentiation in one direction with smoothing in the perpendicular direction (to suppress noise). Sobel weights the center row/column more heavily.
is the transpose of in each case.
import torch
import torch.nn.functional as F
def sobel_edges(img): # img: (H, W) float tensor in [0, 1]
kx = torch.tensor([[-1., 0., 1.],
[-2., 0., 2.],
[-1., 0., 1.]])
ky = kx.t()
x = img[None, None] # (1, 1, H, W)
w = torch.stack([kx, ky])[:, None] # (2, 1, 3, 3)
g = F.conv2d(x, w, padding=1) # (1, 2, H, W)
gx, gy = g[0, 0], g[0, 1]
return torch.sqrt(gx**2 + gy**2), torch.atan2(gy, gx)The Canny pipeline
Canny is the classic multi-stage detector that produces thin, well-connected edges:
- Smooth with a Gaussian to reduce noise sensitivity.
- Gradients: compute magnitude and orientation (e.g. via Sobel).
- Non-maximum suppression: thin edges by keeping only pixels that are a local maximum of magnitude along the gradient direction.
- Double thresholding: classify pixels as strong (above high threshold), weak (between), or suppressed (below low).
- Hysteresis: keep weak pixels only if they connect to a strong pixel, linking edges while dropping isolated noise.
Laplacian of Gaussian (LoG)
Instead of first derivatives, use the second derivative: edges sit at zero crossings of the Laplacian . Because second derivatives amplify noise, the image is first blurred with a Gaussian, giving the LoG operator:
This is often approximated cheaply by a Difference of Gaussians (DoG). The scale selects which edge sizes are detected.
Common pitfalls
- Skipping the smoothing step makes every operator hopelessly noise-sensitive; noise looks like edges.
- Thresholds are scene-dependent; a single fixed threshold rarely transfers across images or lighting.
- Gradient methods respond to gradual shading, not just true object boundaries; large or LoG scale choice trades detail for robustness.