Edge detector

An edge is an image location where intensity changes sharply in one direction. Edge detection is a fundamental image-processing operation, and its core ingredient — the image gradient — underlies corner detection, optical flow, and direct SLAM methods alike.

Grayscale and smoothing first

Edge detection (like most SLAM front-end processing) operates on grayscale images: gradients only need intensity, one channel is 3x faster to process than three, and color is inconsistent across lighting. The standard conversion is the luminance formula I=0.299R+0.587G+0.114BI = 0.299R + 0.587G + 0.114B.

Since differentiation amplifies noise, images are smoothed before gradient computation with a Gaussian blur — convolution with the kernel

G(x,y)=12πσ2exp ⁣(x2+y22σ2)G(x,y) = \frac{1}{2\pi\sigma^2}\exp\!\left(-\frac{x^2+y^2}{2\sigma^2}\right)

The choice of σ\sigma sets the scale of edges you detect: small σ\sigma preserves fine detail (and noise), large σ\sigma keeps only coarse structure.

Sobel operator

The Sobel operator computes image gradients via convolution with two 3×33 \times 3 kernels:

Kx=[101202101],Ky=[121000121]K_x = \begin{bmatrix} -1 & 0 & 1 \\ -2 & 0 & 2 \\ -1 & 0 & 1 \end{bmatrix}, \qquad K_y = \begin{bmatrix} -1 & -2 & -1 \\ 0 & 0 & 0 \\ 1 & 2 & 1 \end{bmatrix}

The gradient magnitude is I=(KxI)2+(KyI)2|\nabla I| = \sqrt{(K_x * I)^2 + (K_y * I)^2}, and the gradient direction is atan2(KyI,KxI)\mathrm{atan2}(K_y * I,\, K_x * I). Sobel combines differentiation (the [1,0,1][-1, 0, 1] pattern) with smoothing perpendicular to it (the [1,2,1][1, 2, 1] weights), making it more noise-tolerant than naive finite differences.

Canny edge detector

The Canny detector is a multi-stage algorithm that turns raw gradients into thin, well-localized edge curves:

  1. Gaussian smoothing to suppress noise.
  2. Sobel gradient computation (magnitude and direction).
  3. Non-maximum suppression along the gradient direction, thinning ridges to single-pixel width: a pixel survives only if its magnitude exceeds both neighbours along the gradient.
  4. Hysteresis thresholding with a high and a low threshold: strong edges (above high) are kept, and weak edges (between low and high) are kept only if connected to strong ones. This preserves continuous contours while rejecting isolated noise responses.
import cv2
edges = cv2.Canny(gray, threshold1=50, threshold2=150)  # low, high

Both detectors are one-liners in OpenCV (cv::Sobel, cv::Canny) and are worth implementing once from scratch to internalize how convolution and gradients work.

Common pitfalls

Why it matters for SLAM

Image gradients computed by Sobel are the input to the Harris structure tensor and to Lucas-Kanade optical flow, so edge detection is a stepping stone to the feature machinery of SLAM front-ends. Edges themselves are also used directly: line/edge features complement points in low-texture man-made environments (e.g., PL-SLAM), and direct methods such as LSD-SLAM implicitly rely on high-gradient (edge-like) pixels for photometric alignment.