MCS4304 · Topic 5Edge detection
5
Edge detectiongradient · Prewitt / Sobel · Laplacian · LoG · Canny Practise · 15 Qs MCQ · 14

Exam card

  • Edge = a significant local intensity change, usually on a region boundary. Edge pixels change abruptly; an edge = connected edge pixels. Causes: object/surface boundaries (geometric), and specularity, shadows, inter-reflections (non-geometric).
  • Gradient ∇I = [Ix, Iy] · G = √(Ix²+Iy²) · θ = tan⁻¹(Iy/Ix). The edge runs ⟂ to the gradient. Ix = Iy = 50 gives G ≈ 70.7, θ = 45°, edge at 135°.
  • Differences: forward · backward · central. Derivative in one direction + smoothing in the other: Prewitt [−1 0 1]×[1 1 1]ᵀ · Sobel ×[1 2 1]ᵀ. Threshold T → binary edge map (manual, histogram, percentile, Otsu, adaptive).
  • Laplacian ∇²I = Ixx+Iyy, 4-nbr [0 1 0; 1 −4 1; 0 1 0]. Edge at a zero crossing: L₁L₂ < 0 and |L₁−L₂| ≥ T. Noise-sensitive, no orientation.
  • LoG (Marr–Hildreth) = ∇²(Gσ ∗ I): smooth → Laplacian → zero crossings → threshold.
  • Canny: Gaussian → Sobel gradient (M, θ) → non-maximum suppression → double threshold (TL, TH) → hysteresis (keep weak only if connected to strong).

Key diagram

object boundarysurface orientationshadowspecularity

Key terms

edgeedge pixeledge detectorobject / surface boundaryspecularity · shadow · inter-reflectionpartial derivatives Ix, Iygradient ∇Imagnitude Gdirection θforward / backward / central differencePrewittSobelthreshold TOtsu's methodLaplacian ∇²zero crossingLoG / Marr–HildrethCannynon-maximum suppressiondouble thresholdinghysteresis

What this topic covers

  1. What is an edge, and what causes one?
  2. The gradient
  3. From derivatives to filters: Prewitt and Sobel
  4. Second derivative: the Laplacian
  5. Laplacian of Gaussian (LoG, the Marr–Hildreth operator)
  6. Canny edge detector

Deep summary

Everything here follows the Topic 5 slides; each part shows its slide numbers. Parts marked added are study aids we wrote, not lecture content.

1 · An edge is a jump in a row of pixels Topic 5 · slides 3–9

one image row (like slides 4–5)20180intensity along the rowjump of 160 = edge1st difference: flat = 0, peak at the edgewhat causes edges (slide 9)geometric• object boundary: depth jump,surface colour / texture• surface boundary: orientation turnsnon-geometric• specularity (highlights)• shadows · inter-reflections
swipe to see the whole diagram

Slide 3: edges are significant local changes of intensity, usually on the boundary between two regions. Slides 4–5 draw one image row as a graph: flat stretches are regions, sudden jumps are edges.

Slide 8 separates three words: an edge pixel is where the intensity changes abruptly; an edge is a set of connected edge pixels; an edge detector finds edge pixels. The goal (slide 6) is a line drawing of the scene, from which corners, lines and curves are taken for recognition and segmentation.

In the toy row, the step from 20 to 180 gives a difference of 160; the small wobble 20 → 22 gives only 2. Slide 7 shows the same idea on a photo: several detectors, several line drawings.

2 · Derivatives of the three slide-12 pictures Topic 5 · slides 10–12, 15

slide 12: “what would be the derivatives?” (one row of each, forward differences)uniform blackI00000Ix0000Ix = 0, Iy = 0 everywhereleft-to-right rampI050100150200Ix50505050Ix = 50 everywhere, Iy = 0white box on blackI002552550Ix02550−255non-zero only at the box border
swipe to see the whole diagram

Slides 10–11: treat the image as a function I(x, y): ℝ² → ℝ. Its rate of change is the first derivative, taken in both directions: Ix = ∂I/∂x and Iy = ∂I/∂y.

Slide 12 asks for the derivatives of three pictures. Uniform black: nothing changes, so both are 0. Ramp: each step right adds 50, so Ix = 50 everywhere (constant, no single edge) and Iy = 0. White box: zero inside and outside, +255 entering the box and −255 leaving it; along the top and bottom sides Iy does the same.

added The rows of numbers and the forward difference I(x+1) − I(x) (slide 15) are ours; the slide shows only the three pictures.

3 · Gradient magnitude and direction: slide 14 worked Topic 5 · slides 13–14

Ix = 50Iy = 50G = 70.7θ = 45°edge at 135°(⟂ to gradient)

Slide 13 stacks the two derivatives into the gradient ∇I = [Ix, Iy], with magnitude G = √(Ix² + Iy²) and direction θ = tan⁻¹(Iy/Ix).

Slide 14 worked: Ix = Iy = 50. ① G = √(2500 + 2500) = √5000 ≈ 70.7. ② The direction of maximum increase is θ = tan⁻¹(50/50) = tan⁻¹(1) = 45°. ③ The edge runs perpendicular to that: 45° + 90° = 135° (the same line as −45°).

The gradient points from dark to bright, across the edge; the edge line runs along the boundary.

4 · From differences to Prewitt and Sobel, then a threshold Topic 5 · slides 15–22

row: three differences at x10204070x−1xx+1forward 70 − 40 = 30backward 40 − 20 = 20central (70 − 20)/2 = 25patch101050101050101090Prewitt Sx−101−101−101Sobel Sx−101−202−101Gx = 40 + 40 + 80 = 160Gx = 40 + 2·40 + 80 = 200Sobel Gy (transposed mask) = (10+20+90) − (10+20+50) = 40G = √(200² + 40²) ≈ 204 ≥ T = 150 → E = 1 (edge pixel)
swipe to see the whole diagram

Slide 15: an image has only whole-pixel samples, so the derivative is approximated by a forward, backward or central difference. On the row 10 20 40 70 at x (value 40) they give 30, 20 and 25.

Slide 16: differences amplify noise, so take the derivative in one direction and smooth in the perpendicular direction. Slide 17: Prewitt = [−1 0 1] across × [1 1 1]ᵀ down; Sobel = the same × weighted [1 2 1]ᵀ. On the patch, Prewitt sums the three row differences 40 + 40 + 80 = 160; Sobel doubles the middle one: 200.

Slide 18: threshold the magnitude into a binary map, E[i, j] = 1 if G[i, j] ≥ T, else 0. Slide 22 lists ways to choose T: manual, histogram analysis, percentile, Otsu, adaptive. added T = 150 and the patch are our example; slides 19–21 show the two operators on a photo.

5 · Laplacian: zero crossings, the threshold, and LoG Topic 5 · slides 23–29

clean step: Ixx = I(x+1) − 2I(x) + I(x−1)I101010909090Ixx080−80080 · (−80) < 0 and |80 − (−80)| = 160 ≥ T = 100 → edgenoise: Laplacian alone vs LoGI1010141010Ixx4−84crossings, but |4 − (−8)| = 12 < 100 → rejectedGσ∗I1011121110∇²0−20after smoothing ([1 2 1]/4): no sign change at all4-neighbour mask[0 1 0; 1 −4 1; 0 1 0]centre 10 with right column 90: 10+10+90+10−40 = 80
swipe to see the whole diagram

Slide 23: the second derivative measures how the rate of change changes, ∇²I = ∂²I/∂x² + ∂²I/∂y². Slide 24’s approximation Ixx = I[i, j+1] − 2I[i, j] + I[i, j−1] (and Iyy the same way down) gives the 4-neighbour mask.

On a clean step the response goes 0, 80, −80, 0: the edge sits at the zero crossing between 80 and −80. Slide 25 marks it only if L₁L₂ < 0 and |L₁ − L₂| ≥ T: here −6400 < 0 and 160 ≥ 100. A noise bump of 4 also flips sign (4, −8, 4) but its jump of 12 fails the threshold.

Slide 28: the Laplacian is noise-sensitive and a scalar, so it gives no edge orientation. Slide 29’s LoG = ∇²(Gσ ∗ I) (Marr–Hildreth) smooths first, then: Laplacian → significant zero crossings → threshold. added Smoothing with [1 2 1]/4 (the 1-D row of Topic 4’s Gaussian) turns the bump into 10 11 12 11 10, and the crossings vanish.

6 · Canny steps 1–2 on a small image: smooth, then gradient Topic 5 · slides 30–32 · Topic 4 · slide 29

I (5 × 7)1010101050909010101010509090101010104580801070101040707010101010356060noise pixel 70① Is = Gσ ∗ I1010102050809010101019.748.877.887.513.817.513.818.84571.28017.52517.517.54062.57013.817.513.816.636.255.962.570 → 25 after smoothing② M = √(Gx² + Gy²)004015923815941192334149230156544045281242111507623015100185134742723199016411140thick ridge: 124 · 211 · 150pixel (row 2, col 4): Is = (10+2·50+90 + 2·10+4·45+2·80 + 10+2·40+70)/16 = 720/16 = 45Gx = (77.8−19.7) + 2(71.25−18.75) + (62.5−17.5) = 208.1 · Gy = 160 − 195 = −35M = √(208.1² + 35²) ≈ 211 · θ = tan⁻¹(−35/208.1) ≈ −9.5° (gradient almost horizontal)
swipe to see the whole diagram

Slide 30: Sobel gives thick, noise-prone edges and the Laplacian spurious zero crossings; Canny aims for clear, thin, connected edges. Slide 31 lists five steps; here are steps 1–2 on a small image: a vertical edge that fades from 10|90 at the top to 10|60 at the bottom, plus one noise pixel (70).

Step 1 (slide 32): Is = Gσ ∗ I with the 3 × 3 Gaussian from Topic 4 slide 29, H = 1/16 [1 2 1; 2 4 2; 1 2 1]. The noise pixel drops from 70 to 25.

Step 2: Gx = Sx ∗ Is, Gy = Sy ∗ Is with the Sobel masks, then M and θ. The box shows every number for the highlighted pixel. The edge is now a 3-pixel-wide ridge in M (124, 211, 150 in row 2).

added Border pixels copy their nearest neighbour, and masks are laid on the image without flipping; the slides don’t specify either. Values are rounded for display.

7 · Canny steps 3–5: thin, classify, connect Topic 5 · slides 33–36

③ after non-max suppression0000238000000230004045002110023000185002723190164040124 < 211 > 150 → keep 211④ TL = 30, TH = 1800000238000000230004045002110023000185002723190164040navy strong · amber weak⑤ hysteresis → final11111one thin, connected linekept: 238 230 211 185 (strong) + 164 (weak, touches 185)rejected: 40, 45 and 40 are weak but touch no strong pixel (red ✕); all values below 30 are dropped at step 4
swipe to see the whole diagram

Step 3 · non-maximum suppression (slide 33): for each pixel, find the gradient direction and compare M with the two neighbours approximately along it; keep it only if it is the local maximum. Row 2 has θ ≈ −9.5°, so compare left and right: 124 < 211 > 150. Only 211 survives; the ridge becomes one pixel wide.

Step 4 · double threshold (slide 34): M ≥ TH = 180 is strong (238, 230, 211, 185); TL = 30 ≤ M < TH is weak (164, and the noise leftovers 40, 45, 40); M < TL is rejected (23, 27, 19 …).

Step 5 · hysteresis (slide 35): strong pixels are accepted; a weak pixel is accepted only if connected to a strong one. 164 touches 185, so the faint bottom of the edge survives; the noise leftovers touch nothing strong and go. Slide 36 shows the result on a photo. added TL, TH and “connected” = the 8 surrounding pixels are our choices.

8 · Exam traps Topic 5 · slides 13–14, 17, 25, 28, 31–35 added

  • Edge orientation is not θ: θ is the gradient (maximum increase); the edge is perpendicular, θ + 90° (slides 13–14).
  • Prewitt vs Sobel differ only in the smoothing vector, [1 1 1]ᵀ vs [1 2 1]ᵀ; the derivative part [−1 0 1] is the same (slide 17).
  • A sign change alone is not an edge: a zero crossing needs L₁L₂ < 0 and |L₁ − L₂| ≥ T (slide 25).
  • The Laplacian gives no orientation: it is a scalar (slide 28). Gradient methods give θ.
  • Canny order: smooth → gradient → non-max suppression → double threshold → hysteresis (slide 31). Thinning comes before thresholding.
  • Weak ≠ rejected: a weak pixel survives if it is connected to a strong one (slide 35).

9 · Quick check Topic 5 · slides 13, 25, 34–35 added

Q1. A pixel has Ix = 0 and Iy = 80. Give G, θ and the edge orientation.
G = √(0 + 6400) = 80; θ = 90° (the gradient points straight along y); the edge is perpendicular to it, a horizontal edge at 0° (slide 13).
Q2. Neighbouring Laplacian responses are 30 and −10, with T = 50. Is there an edge?
30 · (−10) = −300 < 0, so the sign changes, but |30 − (−10)| = 40 < 50. No edge: both conditions must hold (slide 25).
Q3. Canny with TL = 50, TH = 150: a pixel with M = 100 sits next to a pixel with M = 200. What happens to it?
100 is a weak candidate (slide 34); its neighbour is strong, so hysteresis keeps it (slide 35).

Exam questions · 15 Practise these 15

Q1What is an edge? List the causes of edges in an image.
Edges are significant local changes of intensity, typically on the boundary between two regions. Edge pixels are where intensity changes abruptly, and an edge is a set of connected edge pixels. Edge detection aims to produce a line drawing of the scene from which features such as corners, lines and curves are taken for recognition and segmentation. Geometric causes: object boundaries (a break in depth and/or surface colour or texture) and surface boundaries (a break in surface orientation and/or colour or texture). Non-geometric causes: specularity, shadows and inter-reflections.
Q2An edge pixel has Ix = 50 and Iy = 50. Find G, the direction of maximum increase, and the local edge orientation.
G = √(50² + 50²) = √5000 ≈ 70.7. Direction of maximum increase θ = tan⁻¹(50/50) = 45°, which is where the gradient points. The edge runs perpendicular to the gradient, so its orientation is 45° + 90° = 135° (equivalently −45°).
Q3Explain the Prewitt and Sobel operators and how a binary edge map is produced.
Both approximate the first derivative in one direction with [−1 0 1] and smooth in the perpendicular direction to reduce noise. Prewitt smooths with [1 1 1]ᵀ, giving Gx = [−1 0 1; −1 0 1; −1 0 1]. Sobel uses a weighted smoothing [1 2 1]ᵀ, giving Gx = [−1 0 1; −2 0 2; −1 0 1], which is a little more robust to noise. Transpose each for Gy. Compute G = √(Gx² + Gy²), then threshold: E[i,j] = 1 if G ≥ T, else 0. T can be chosen manually, from the histogram, as a percentile, by Otsu’s method, or adaptively.
Q4How does the Laplacian detect edges? What are its drawbacks, and how does LoG address them?
The Laplacian ∇²I = ∂²I/∂x² + ∂²I/∂y² measures how the rate of change changes (4-neighbour mask [0 1 0; 1 −4 1; 0 1 0]). An edge lies at a zero crossing: neighbouring responses L₁ and L₂ must have L₁·L₂ < 0 and |L₁ − L₂| ≥ T. Drawbacks: very noise-sensitive (spurious zero crossings) and a scalar response, so no edge orientation. LoG (Marr–Hildreth) = ∇²(Gσ ∗ I): smooth with a Gaussian first, then take the Laplacian, find significant zero crossings, and reject weak ones with a threshold.
Q5Describe the five steps of the Canny edge detector and why it is needed.
Why: Sobel-type operators give thick, noise-sensitive edges, and Laplacian methods give spurious zero crossings. Canny produces thin, connected, low-noise edges. (1) Gaussian smoothing: Is = Gσ ∗ I. (2) Gradient: Sobel Gx, Gy, then magnitude M and angle θ. (3) Non-maximum suppression: keep a pixel only if its M is larger than its two neighbours along the gradient direction, which thins edges to one pixel. (4) Double thresholding: M ≥ TH strong, TL ≤ M < TH weak, M < TL rejected. (5) Hysteresis tracking: accept strong pixels, accept weak pixels connected to strong ones, and reject isolated weak pixels.
Q6Calculate · An edge pixel has Ix = 30 and Iy = 40. Find the gradient magnitude G, the direction of maximum increase θ, and the local edge orientation.
Ix = 30G = 50Iy = 4053.1°edge ≈ 143.1°(θ + 90°)
Topic 5 slides 13–14 G = √(30² + 40²) = √(900 + 1600) = √2500 = 50. θ = tan⁻¹(40/30) = tan⁻¹(1.333) ≈ 53.1°: the gradient points that way, from dark to bright. The edge runs perpendicular: 53.1° + 90° ≈ 143.1° (the same line as −36.9°). Same method as the slide 14 example, with different numbers.
Q7Classify · Classify the cause of each edge as geometric (object or surface boundary) or non-geometric (specularity, shadow, inter-reflection): (a) a mug against the wall behind it (b) where a table top meets its side (c) a bright shine on a car bonnet (d) a tree’s shadow on a road (e) a red wall casting reddish light onto a white floor.
mug in front of a wallobject boundarytable top meets its sidesurface boundaryshine on a car bonnetspecularitytree shadow on a roadshadowred wall lights a white floorinter-reflectionblue = geometric · amber = non-geometric
Topic 5 slide 9 (a) geometric, object boundary: a jump in depth between the mug and the wall. (b) geometric, surface boundary: the surface orientation changes at the corner. (c) non-geometric, specularity: a highlight, not a real boundary. (d) non-geometric, shadow: the road is the same, only the lighting changes. (e) non-geometric, inter-reflection: light bounced from one surface onto another. Non-geometric edges are real intensity changes, but they are not object outlines.
Q8Calculate · Apply the Sobel masks to the 3 × 3 patch [20 20 20; 20 20 20; 80 80 80]. Find Gx, Gy, G and say which way the edge runs.
patch202020202020808080Sobel Sy−1−2−1000121Gx = 0Gy = 240G = 240gradient points down (θ = 90°) → horizontal edge
Topic 5 slides 13, 17 Sx = [−1 0 1; −2 0 2; −1 0 1]: each row is flat, so Gx = 0. Sy (the transpose) = bottom row weighted [1 2 1] minus top row weighted [1 2 1]: (80 + 160 + 80) − (20 + 40 + 20) = 320 − 80 = 240. G = √(0² + 240²) = 240. θ = tan⁻¹(240/0) = 90°: the gradient points along y (down the rows), so the edge is horizontal, between the 20-rows and the 80-row.
Q9Compare · Compute Gx for the patch [10 10 70; 10 10 70; 10 10 10] with Prewitt and with Sobel, and explain where the difference comes from.
patch101070101070101010row differences60600weights111211PrewittSobelGx: Prewitt 60+60+0 = 120 · Sobel 60+120+0 = 180
Topic 5 slides 17, 19–21 Both take the difference right − left in each row: 60, 60, 0. Prewitt adds them equally ([1 1 1]ᵀ): 60 + 60 + 0 = 120. Sobel weights the centre row double ([1 2 1]ᵀ): 60 + 2·60 + 0 = 180. The derivative part is identical; only the smoothing vector differs. Sobel trusts the pixel’s own row more than its neighbours. Slides 19–21 compare the two on the cameraman photo.
Q10Apply · Gradient magnitudes along a row are 12, 45, 160, 210, 90, 20. Write the binary edge map for T = 100 and for T = 50, and say what lowering T does.
G along a row12451602109020T=100001100T=50001110lower T → more edge pixels (and more noise)
Topic 5 slides 18, 22 E = 1 when G ≥ T (slide 18). T = 100: 0 0 1 1 0 0. T = 50: 0 0 1 1 1 0, since 90 now passes. A lower T keeps weaker edges, but also more noise responses; a higher T gives cleaner maps with possible gaps. That is why slide 22 lists ways to pick T: manual, histogram, percentile, Otsu, adaptive. Neither map is thin here (two or three 1s side by side), which is the thick-edge problem Canny fixes.
Q11Calculate · Apply the 4-neighbour Laplacian to the centre of [10 20 10; 30 50 30; 10 20 10]. Its neighbour’s response is L₂ = 60. With T = 100, is there an edge between them?
patch1020103050301020104-nbr mask0101−41010L₁ = −100L₂ = 60T = 100(−100)(60) < 0 ✓ · |−100 − 60| = 160 ≥ 100 ✓ → edge
Topic 5 slides 24–25 Mask [0 1 0; 1 −4 1; 0 1 0]: L₁ = 20 + 30 + 30 + 20 − 4·50 = 100 − 200 = −100. Zero-crossing test (slide 25): L₁L₂ = (−100)(60) = −6000 < 0 ✓, and |L₁ − L₂| = |−160| = 160 ≥ 100 ✓. Both hold, so an edge is marked between the two pixels. A negative Laplacian at a bright centre is expected: it is a local peak.
Q12Explain · Explain why Prewitt and Sobel smooth in the direction perpendicular to the derivative. Use three rows whose right − left differences are 40, 70 (noise +30) and 40.
right − left difference in three rowsrow above: 40centre row: 70 (noise +30)row below: 40no smoothing: 70[1 1 1]/3: 150/3 = 50[1 2 1]/4: 220/4 = 55error 30 → 10 → 15; the true difference is 40
Topic 5 slides 15–17 Differences amplify noise (slide 15 approximations work on single samples). Slide 16’s two-step procedure takes the derivative in one direction and averages the neighbouring responses in the perpendicular direction. Using the centre row alone gives 70, off by 30. added Averaging with [1 1 1] (Prewitt) gives (40 + 70 + 40)/3 = 50; with [1 2 1] (Sobel) (40 + 140 + 40)/4 = 55. The noise is spread over three rows, so a real edge (all rows ≈ 40) stays strong while a one-row blip is weakened. The normalising /3 and /4 are ours; the slide masks leave them out.
Q13Match · Match each situation to the threshold-selection method from the slides: (a) the lighting and noise vary across the image (b) the magnitude histogram has two clear groups (c) you want to keep the strongest 10 % of responses (d) a quick one-off image you can inspect (e) you study the magnitude distribution to find a value separating weak from strong.
lighting varies across imageadaptivehistogram has two clear humpsOtsu’s methodkeep the strongest 10 %percentile-basedtry values and lookmanualpick a value between weak and stronghistogram analysis
Topic 5 slide 22 (a) adaptive thresholding: T varies across the image. (b) Otsu’s method: automatically splits the histogram into two groups, when that split is appropriate. (c) percentile-based: T = the 90th percentile of the magnitudes. (d) manual: experiment and inspect. (e) histogram analysis: examine the distribution and pick a separating value.
Q14Apply · After Canny step 2, five pixels across an edge (along the gradient direction) have M = 60, 150, 220, 180, 70. Apply non-maximum suppression. What is left, and why is this step needed?
M along the gradient direction (θ ≈ 0°)60015002202201800700only 220 beats both neighbours (150 and 180)
Topic 5 slides 30, 33 Compare each pixel with its two neighbours along the gradient (slide 33). 150 < 220 and 180 < 220 are not maxima; 60 and 70 aren’t either. Only 220 is a local maximum, so the row becomes 0 0 220 0 0. Gradient responses spread over several pixels (here five), so plain Sobel gives thick edges (slide 30). NMS thins each edge to one pixel, before any threshold is applied.
Q15Outline · Outline Canny steps 4 and 5 on this chain of neighbouring pixels after NMS: M = 170, 120, 90, 0, 80, 60, with TL = 50 and TH = 150.
a chain of pixels after NMS1701120190100800600TL = 50 · TH = 150 · the 0 breaks the chain
Topic 5 slides 34–35 Step 4, double threshold (slide 34): 170 ≥ 150 is strong; 120, 90, 80, 60 lie between 50 and 150, so they are weak; 0 < 50 is rejected. Step 5, hysteresis (slide 35): accept the strong 170; accept 120 (touches 170) and 90 (touches 120, connected through the chain). 80 and 60 are weak but cut off by the 0, so no strong pixel reaches them: rejected. Result: 1 1 1 0 0 0. added “Connected” through a chain of weak pixels is the usual reading of slide 35.

Quick MCQs · 14 MCQs in Practise

Not the exam format: quick checks that you understood the topic. Choose 1 or all that apply, then Check.