Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Edge detection and linking

Edge detection finds where image intensity changes sharply; edge linking converts detected edge pixels into continuous contours for later tasks like object recognition and measurement.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Edges show object boundaries, surface changes and important features in images. Engineers use edge detection and linking to extract shapes for measurement, to provide inputs to higher-level tasks like segmentation, object detection and Hough-based line/circle finding, and to reduce data before classification or tracking.

Key ideas

Start simple and build up.

  • Image intensity: the scalar value I(x,y) at pixel coordinates (x,y). We treat intensity as a unitless digital number (DN) unless a radiometric unit is given.

  • Edge: an abrupt change in image intensity. Formally, an edge is a location in the image where the spatial derivative of intensity is large. In practice we look for maxima of derivative magnitude or zero-crossings of second derivative.

  • Gradient: for a continuous image I(x,y), the gradient is the vector of partial derivatives ∇I = (∂I/∂x, ∂I/∂y). The gradient points in the direction of greatest increase in intensity; its magnitude gives the strength of change.

Exact principle used in many edge detectors: detect points where the magnitude of the gradient is locally maximal in the gradient direction.

Derivation of discrete gradient: For a continuous function f(x), central difference approximates derivative: ∂f/∂x ≈ (f(x+Δx) − f(x−Δx)) / (2Δx). For images with pixel spacing Δx = Δy = 1 (one pixel), the discrete central difference simplifies to half the difference of neighbor intensities. Extending to 2D gives simple convolution kernels that approximate ∂I/∂x and ∂I/∂y.

  • Edge detector: an operator that maps an image to a set of edge responses. Popular detectors are Roberts, Prewitt, Sobel, Laplacian, Laplacian of Gaussian (LoG / Marr–Hildreth) and Canny.

  • Sobel operator: a discrete convolution operator approximating image gradients using 3×3 kernels. It combines smoothing (to reduce noise) with differencing.

  • Laplacian: the sum of second partial derivatives, ∇²I = ∂²I/∂x² + ∂²I/∂y². The Laplacian highlights regions of rapid change and its zero-crossings often indicate edges.

  • Laplacian of Gaussian (LoG): smoothing the image with a Gaussian (to reduce noise) and then applying the Laplacian. It finds edges by detecting zero-crossings in the filtered result.

  • Canny detector: a multi-stage detector designed to satisfy three criteria: (1) good detection (low probability of missing real edges), (2) good localization (detected edges are close to true edges), and (3) single response to a single edge. Steps: (a) Gaussian smoothing, (b) gradient computation, (c) non-maximum suppression, (d) double threshold and hysteresis linking.

  • Non-maximum suppression: thin wide gradient responses by keeping a pixel only if its gradient magnitude is a local maximum along the gradient direction. Implementations often quantize θ to 0°, 45°, 90°, 135° or use interpolation to compare along the exact gradient direction.

  • Hysteresis thresholding (edge linking): use two thresholds: high TH and low TL (TH > TL). Pixels with magnitude > TH are strong edges. Pixels with magnitude in [TL, TH] are weak; a weak pixel is accepted if connected (usually 8-connected) to a strong pixel. Others are suppressed. This links edge fragments while suppressing noise. Typical choices: TL ≈ 0.4–0.5 of TH; thresholds are often set relative to image noise statistics.

  • Edge thinning / skeletonization: methods to reduce edge width to one-pixel lines. Non-maximum suppression is the common first thinning step.

  • Edge linking by graph/path methods and Hough transform: after detecting edge pixels, linking them into continuous contours can use local-following (trace along the edge tangent, i.e., perpendicular to the gradient), global methods (connectivity and chain codes), or transform methods (Hough transform for straight lines or known shapes).

Diagrams to draw in answers (describe exactly):

  • Draw a 3×3 pixel grid with coordinates labelled (x−1,y−1) to (x+1,y+1). Show the Sobel Gx kernel overlaid with kernel weights at each grid cell. Draw arrows for gradient direction and label the gradient vector ∇I at the center pixel. Label axes x (right) and y (down or up—state convention).
  • For Canny non-maximum suppression: draw a bright ridge (1D), show gradient direction normal to ridge, mark three sample pixels along that line and indicate which pixel is local maximum.

Assumptions often used in derivations: pixel spacing = 1, intensities are reasonably smooth except at edges, Gaussian smoothing is isotropic.

Formulas

∇I = (∂I/∂x, ∂I/∂y) — ∂I/∂x and ∂I/∂y are partial derivatives of intensity with respect to x and y; I is image intensity (unitless DN); applies to continuous intensity models and underpins gradient-based detectors.

|∇I| = sqrt((∂I/∂x)^2 + (∂I/∂y)^2) — |∇I| is gradient magnitude, DN per pixel; it measures strength of intensity change.

θ = atan2(∂I/∂y, ∂I/∂x) — θ is gradient direction in radians (or degrees); applies when computing orientation for non-maximum suppression.

∂I/∂x ≈ (I(x+1,y) − I(x−1,y)) / 2 — central difference approximation for ∂I/∂x; units: DN per pixel; assumes pixel spacing = 1.

Sobel Gx kernel = [[-1, 0, 1],[-2, 0, 2],[-1, 0, 1]] — convolve this kernel with image to approximate ∂I/∂x; kernel entries are unitless multipliers; result is DN per pixel (up to a scale factor).

Sobel Gy kernel = [[-1,-2,-1],[0,0,0],[1,2,1]] — approximates ∂I/∂y; result is DN per pixel (up to a scale factor).

Gaussian: G(x,y;σ) = (1/(2πσ^2)) · exp(−(x^2 + y^2)/(2σ^2)) — G is Gaussian smoothing kernel; σ is standard deviation in pixels; units: 1/pixel^2 inside normalization factor but kernel values are unitless multipliers.

LoG(x,y;σ) = ∇^2 G(x,y;σ) = [(x^2 + y^2 − 2σ^2) / (2πσ^6)] · exp(−(x^2 + y^2)/(2σ^2)) — Laplacian of Gaussian; σ in pixels; kernel values unitless; used to detect zero-crossings after convolution.

Double threshold condition: strong if M(x,y) ≥ TH, weak if TL ≤ M(x,y) < TH, reject if M(x,y) < TL — M is gradient magnitude; TH and TL are thresholds in same units as M (DN); applies in Canny hysteresis.

Worked examples

Example 1 — Sobel gradient at center pixel (numerical): Given 3×3 image (intensity unit: DN): Row1: [0, 0, 0] Row2: [0, 0, 0] Row3: [255, 255, 255] We compute Sobel response at center pixel (row2,col2). Sobel Gx convolution (element-wise multiply and sum): Top row: (−1)0 + 00 + 10 = 0 Middle row: (−2)0 + 00 + 20 = 0 Bottom row: (−1)255 + 0255 + 1*255 = −255 + 0 + 255 = 0 So Gx = 0 DN/pixel. Sobel Gy convolution: Top row: (−1)0 + (−2)0 + (−1)0 = 0 Middle row: 0 Bottom row: 1255 + 2255 + 1255 = 255 + 510 + 255 = 1020 DN So Gy = 1020 DN/pixel. Gradient magnitude: |∇I| = sqrt(Gx^2 + Gy^2) = sqrt(0 + 1020^2) = 1020 DN/pixel. Direction θ = atan2(1020, 0) = 90° (downwards if y increases downward). This shows a strong vertical intensity change (horizontal edge) at center.

Example 2 — Canny hysteresis linking (small array): Given gradient magnitudes M (units: DN/pixel): [ [10, 40, 10], [50,100, 45], [15, 35, 20] ] Choose thresholds TL = 30 DN/pixel, TH = 90 DN/pixel. Step 1: strong pixels (≥90): only M(2,2)=100 at center → mark (2,2) strong. Step 2: weak pixels (30≤M<90): 40 at (1,2), 50 at (2,1), 45 at (2,3), 35 at (3,2) → mark these weak. Step 3: accept weak pixels that are 8-connected to any strong pixel. Center (2,2) is strong; its 8-neighbors include the four weak pixels listed, so all of them are promoted to edges. Step 4: remaining pixels (10, 15, 20) are below TL and suppressed. Final linked edge pixel coordinates (row,column): (2,2), (1,2), (2,1), (2,3), (3,2).

Example 3 — zero-crossing detection for a 1D LoG response (theory answer style): Given a 1D LoG-filtered signal values at positions x = −2..2: [0.2, -0.5, 1.1, -0.4, 0.1]. Zero crossings occur where sign changes between neighbors. Sign changes are between x=−2 and −1 (0.2 to −0.5) and between x=−1 and 0 (−0.5 to 1.1) and between 0 and 1 (1.1 to −0.4) and between 1 and 2 (−0.4 to 0.1). Each crossing is a candidate edge position; refine by checking magnitude or interpolation. A full exam answer should show interpolation to estimate sub-pixel crossing location.

Common mistakes

  • Using only a single threshold (no hysteresis) — leads to broken edges or many noise responses.
  • Wrong sign conventions or axis direction, leading to incorrect interpretation of gradient direction (remember how your coordinate system defines +y).
  • Using Sobel without smoothing on a noisy image — high false positives.
  • Treating Laplacian zero-crossings without pre-smoothing (LoG) — zero-crossings are sensitive to noise.
  • Wrong connectivity (4 vs 8) choice in hysteresis without justification — Canny normally uses 8-connectivity.

For your exam

Question types: short definitions (define edge, gradient, Laplacian, non-maximum suppression), derivations (derive discrete central difference and Sobel kernels), algorithm description (steps of Canny, explain hysteresis), numerical problems (compute Sobel response at a pixel), diagram questions (draw kernels and gradient vectors), and short programs/flowcharts (algorithmic steps for edge linking).

What full-marks answers include:

  • Precise definitions (bold, brief), assumptions (pixel spacing = 1, smoothing required), and equations used.
  • For derivations: show step-by-step difference approximation from Taylor expansion or limit definition to central difference and then to kernel form.
  • For algorithm descriptions: list steps in order and explain purpose of each (smoothing reduces noise, non-maximum suppression thins edges, hysteresis links edges).
  • For numericals: write formula used, substitute numbers step-by-step, carry units (DN), and show final bold result.
  • For diagrams: label axes, kernel weights, pixel coordinates and gradient direction.

Quick check

  1. Define the gradient of an image in one sentence.
  2. What are the three design criteria of the Canny detector?
  3. In hysteresis thresholding, what happens to a weak pixel that is not connected to any strong pixel?

Answers: 1) The gradient is the vector of partial derivatives (∂I/∂x, ∂I/∂y) showing direction and rate of maximum intensity increase. 2) Good detection, good localization, single response to a single edge. 3) It is suppressed (rejected) and not considered an edge.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?