CSE 4227 · Prof. Kalpoma · AUST · CLO1 + CLO2 + CLO3

DIP, every dot connected

Every past question of hers (Spring 2022 to Spring 2025, plus the Fall 2025 quizzes) as the real paper image, next to the worked answer and simulator from your lesson pages. Grouped by chapter, each with the pattern she asks in and the formula wall. Fall 2024 and the other examiners' sections are left out.

Night-before revision plan

  1. 15 min: the dot map and the cue words. Say out loud which method each story word points to.
  2. 2 hours: chapter by chapter. For each one, read the pattern box, then the formula wall. Look at each question image, say the first two lines of the answer, then open Worked answer to check. Don't redo the sums you already know. Run one simulator only if a step feels foggy.
  3. 30 min: Q1 theory points and Q3 case bank. Keywords matter more than wording.
  4. 10 min: traps and the final checklist. Then sleep.
Click any question image to zoom.

The paper

In plain wordsThree questions. Q1 asks you to explain, Q2 asks you to calculate, Q3 gives a story (a hospital, a satellite, a scanner) and asks which method fits. The same 12 methods answer all three; only the way you write changes.
questionwhat it wantschoicehow you write it
Q1 theory · 25define, compare, list effectsany 5 of 6 (one per chapter)definition → formula → 2–3 points → small sketch
Q2 math · 24numbers: histogram, filter, compression, chain code / morphologyabout 3 of 4formula → table of every step → final grid / code → one-line comment
Q3 case · 21a real problem; pick and justify methodsany 3 of 4problem → method → why → steps (with equation) → result

Time: about 2.5 minutes per mark. A 7-mark case = 17 minutes. Never spend more than 20 minutes on one part.

Where marks hide: the formula line, the working table and the final labelled answer each carry marks on their own. A right answer with no table loses about half.

Dot map: every past question by chapter and CLO

Each row is a chapter. Each column is a section of the paper: Q1 is CLO1 theory, Q2 is CLO2 math, Q3 is CLO3 case. Click a question to jump to its image and answer. Fall 2024 and the other examiners' sections are left out.

In plain wordsEvery topic is one stop on the same road: take a picture, make it look better, find the object, describe its shape, store it small. Once you see the road, you know which chapter a question comes from.
1 · Capturedecks 01–03

sampling, quantization, bits, pixels, neighbours, distance

2 · Enhancedecks 04–07

point ops (negative, log, gamma, stretch, slice, bit-plane) → histogram → smooth → sharpen

3 · Find objectsdecks 08–09

edges (gradient, Laplacian, Canny) or regions (threshold, region growing)

4 · Clean shapedecks 11–12

erosion, dilation, opening, closing, boundary, hit-or-miss

5 · Describe shapedeck 10

chain code → CFD → shape number

6 · Store / colourdecks 13–14

Huffman, Golomb, LZW, RLE, RMSE · RGB, CMY, HSI

The same idea shows up twice (connect these dots)

first meetingsecond meetingthe link
thresholding s = 1 if r > k (deck 04)global / iterative threshold (deck 09), binary input for morphology (deck 11)one cut turns gray into object vs background
contrast stretching (deck 04)histogram equalization (deck 05)both spread values over 0…L−1; HE does it automatically from the CDF
mean / Gaussian smoothing (deck 06)Canny step 1, LoG (deck 08)smooth first so noise doesn't make fake edges
Laplacian ∇²f (deck 07)LoG / zero crossings (deck 08)2nd derivative: thin double edges, sensitive to noise
Sobel / Prewitt gradient (deck 07)Canny step 2 (deck 08)1st derivative: edge strength and direction
median filter (deck 06)salt-and-pepper case questions (Q3)median ignores extreme 0/255 values and keeps edges
boundary β(A) = A − (A ⊖ B) (deck 11)chain code walks that boundary (deck 10)first find the outline, then code it
erosion (deck 11)hit-or-miss = two erosions (deck 12)HMT erodes the object with B1 and the background with B2
entropy H (deck 13)Huffman Lavg (deck 13)H is the floor: Lavg ≥ H, efficiency = H / Lavg
histogram p(rk) = nk/N (deck 05)probabilities for Huffman and entropy (deck 13)same counting table, different use
intensity I in HSI (deck 14)gray-level methods (decks 04–06)enhance I only, keep H and S, colours stay true

Cue words: story word, then method

In plain wordsCase questions hide the method inside a story. Spot the problem word, map it to the method, then justify with one line of how the method works. Learn this table and Q3 becomes matching.

Enhancement stories

you readit meansyou write / use
too dark, underexposed X-ray / MRI, details hidden in shadowsdark values squashed togetherlog or gamma γ < 1; or histogram equalization
washed out, too bright, aerial photo, fadedbright values squashed togethergamma γ > 1 (3.0–4.0 on the slides)
low contrast, dull, narrow histogramvalues use only part of 0…L−1contrast stretching or histogram equalization
highlight a range: tumor, bone [120, 200], water in satelliteone band of gray levels mattersgray-level slicing: give both approaches (background 0, or keep r)
huge dynamic range, Fourier spectrumvalues 0 to 10⁶ won't fit 0–255log transform
negative film, white lesions in dark mammogramdark ↔ light swap helps viewingnegative s = L−1−r
hidden watermark, which bits matter, compress by bitsimage = stack of bit planesbit-plane slicing: top planes carry the shape
want the look of another imagetarget histogram givenhistogram matching (specification)

Noise and sharpness stories

you readit meansyou write / use
salt-and-pepper, impulse noise, black and white dotsisolated extreme 0 / 255 pixelsmedian filter 3×3 (keeps edges)
Gaussian noise, grainy, random noiseevery pixel slightly offmean / weighted average / Gaussian filter (blurs a bit)
only pepper (black dots) / only salt (white dots)one-sided impulsesmax filter removes pepper, min filter removes salt
blurred, fine detail, sharpen, faded ink edgesedges too softLaplacian g = f − ∇²f, or unsharp masking / high-boost
find edges / outline of an organ, crack, road edgebig change in intensitySobel / Prewitt gradient, best: Canny
noisy image but need thin, clean, connected edgesedge detector that resists noiseCanny (smooth, gradient, NMS, double threshold, hysteresis)
straight lines: roads, lanes, fracture linespixels lying on a lineline masks (−1 −1 −1 / 2 2 2) or Hough transform (one line: votes in (ρ, θ))

Segmentation stories

you readit meansyou write / use
separate object from background, text from papertwo intensity groupsthresholding; iterative T = ½(μ₁ + μ₂)
uneven illumination, shadow on the documentone T can't fit everywherelocal / adaptive threshold (T from each neighbourhood)
group similar pixels from a seed point, tumor regiongrow from a known pixelregion growing: |neighbour − pixel it grows from| ≤ T
"partition pixels depending on intensity"the words for thresholdingthresholding (SP23 Q1(b)ii)
"groups similar pixels"the words for region-basedregion growing or split & merge (SP23 Q1(b)iv)

Shape stories (binary images)

you readit meansyou write / use
broken characters, gaps in roads, cracks to joinsmall breaks in the objectdilation (or closing to keep thickness)
small bright spots, specks, small particles to removeobjects smaller than the SEopening (erode, then dilate)
small holes inside objects, reflections to fillbackground specks inside the objectclosing (dilate, then erode)
touching coins, count objectsthin necks join the objectserosion to split, then count connected components
outline / boundary of roads, organ edge (binary)the object's border pixelsboundary β(A) = A − (A ⊖ B)
find corners, end points, a fixed patternmatch object AND backgroundhit-or-miss A ⊛ B = (A ⊖ B₁) ∩ (Aᶜ ⊖ B₂)
recognise a shape whatever its rotation / startdescribe the boundary by numberschain code → CFD → NCFD (shape number)

Storage and colour stories

you readit meansyou write / use
medical, legal, must not lose any detaillossless onlyHuffman, LZW, RLE, Golomb, arithmetic
many repeating patterns / colours, bitmap, fixed-length codesthe same sequences come backLZW (dictionary, 9-bit codes)
long runs of the same value, fax, binary documentsconsecutive equal pixelsRLE (value, run length)
some gray levels far more common than othersunequal probabilitiesHuffman (short codes for frequent values)
small integers / prediction errors, geometric spreadsmall values commonGolomb Gm(n)
whole message as one numbernon-block codearithmetic coding
how good is the decompressed image?fidelity criterionRMSE (objective) or rating scale (subjective)
printing, ink, printersubtractive colourCMY / CMYK
monitor, camera, displayadditive colourRGB
segment by colour, lighting changes, describe colour like humansseparate colour from brightnessHSI (use Hue)
TrapWhen a case lists four methods (SP25 Q3(d): chain code, histogram, Hough, power-law), give each one line of what it does, one line of how it helps here, and one limitation. That is the 3-part pattern she marks.

1. Pixels, resolution, neighbours and distance

decks 01–03 · 6 past questions
The pattern: how she asks it
  • Q1 (4 marks): 4- vs 8- vs m-connectivity, then count the connected components of a small 0/1 grid (SP25 1b). Sampling vs quantization and image types also come here.
  • Q2 small part: D4 / D8 / De between two pixels, or the shortest 4-, 8- or m-path through a set V (SP23 2e, SP22 3b, SP24 1a).
  • Arithmetic: bits = M × N × k, levels L = 2k; zoom by pixel replication / nearest neighbour.

Connects to: m-adjacency comes back in chain code (the boundary is a path) and in morphology (connected components, coin counting).

typeCLOtimeswhere
Distances D4/D8/De, paths, adjacency, componentsCLO25SP25 1b, SP24 1a, SP23 2e, SP22 3b, F21 B4b
Neighbours, adjacency, m-connectivity, distance propertiesCLO12SP25 1b, SP24 1a
Fundamentals: sampling, quantization, intensity levels, image typesCLO11SP22 3a
Formula wall: every formula with a worked line
whatformulaone worked line
gray levelsL = 2k, range 0 … L−1k = 12 → L = 4096, range 0–4095
image sizeb = M × N × k bits (÷ 8 = bytes)1024 × 768 × 12 = 9,437,184 bits = 1,179,648 bytes
nearest-neighbour zoomposition = ⌈i × old / new⌉, i = 1…new3×3 → 5×5: ⌈0.6 i⌉ = 1 2 2 3 3 (copy those rows / columns)
city-block D4|x − s| + |y − t|p(1,2), q(4,6): 3 + 4 = 7
chessboard D8max(|x − s|, |y − t|)max(3, 4) = 4
Euclidean De√[(x − s)² + (y − t)²]√(9 + 16) = 5
  • Sampling = digitise coordinates (spatial resolution, blocky if too few). Quantization = digitise amplitude (gray-level resolution, false contouring if too few).
  • 4-adjacent: q in N4(p). 8-adjacent: q in N8(p). m-adjacent: q in N4(p), or q in ND(p) and N4(p) ∩ N4(q) has no pixel in V. m removes the double paths of 8-adjacency.
  • Distance properties: D ≥ 0 (= 0 only if p = q), D(p,q) = D(q,p), D(p,z) ≤ D(p,q) + D(q,z). Always D4 ≥ De ≥ D8.

Past questions: look, recall, then open the answer

SP25 1bCLO1 · Neighbours, adjacency, m-connectivity, distance properties · 2CLO2 · Distances D4/D8/De, paths, adjacency, components · 2
State the difference between 4-neighbor, 8-neighbor and m-connectivity. Given a 4x4 binary image segment [(1010),(0101),(1010),(0101)], determine the number of connected components under each connectivity type and discuss the effect of diagonal adjacency on these components.
Worked answer (deck 03)
SP25 Final Q1(b)4 marks

State the difference between 4-neighbour, 8-neighbour and m-connectivity. For the 4×4 binary image [1010; 0101; 1010; 0101], find the number of connected components under each connectivity and discuss the effect of diagonal adjacency.

4-connectivity: only the up/down/left/right neighbours link pixels. 8-connectivity: the four diagonal neighbours link too. m-connectivity: 4-neighbours always link; a diagonal neighbour links only if the two pixels have no common 4-neighbour with a value in V (removes the double paths of 8-connectivity).

1010
0101
1010
0101
V = {1}

Every 1 has only 0s as 4-neighbours, so with 4-connectivity each 1 is alone: 8 components. Every 1 touches other 1s diagonally, so 8-connectivity joins all of them: 1 component. For each diagonal pair the two shared 4-neighbours are 0 (not in V), so every diagonal link is m-adjacent as well: 1 component.

Effect of diagonal adjacency: it merges pixels that touch only at corners, dropping the count from 8 to 1. m-connectivity keeps those joins here and only removes redundant (ambiguous) diagonal links where a 4-connection already exists.

SP24 1aCLO1 · Neighbours, adjacency, m-connectivity, distance properties · 2CLO2 · Distances D4/D8/De, paths, adjacency, components · 3
What are the properties of the Distance Function for Distance Metrics if there are 3 pixels a, b and c? How to calculate the distances City Block, Chess Board, Euclidean between two pixels a and b? [2+3]
Worked answer (deck 03)
SP24 Final Q1(a)2 + 3 marks

What are the properties of the distance function for 3 pixels a, b and c? How are the city-block, chessboard and Euclidean distances between a and b calculated?

Properties: D(a,b) ≥ 0 with D(a,b) = 0 iff a = b; D(a,b) = D(b,a); D(a,c) ≤ D(a,b) + D(b,c).

For a = (x, y), b = (s, t): city block D4 = |x − s| + |y − t|; chessboard D8 = max(|x − s|, |y − t|); Euclidean De = √[(x − s)² + (y − t)²]. Example a = (0,0), b = (3,4):

D4(a,b) = |0 − 3| + |0 − 4| = 7 (city block)
D8(a,b) = max(|0 − 3|, |0 − 4|) = max(3, 4) = 4 (chessboard)
De(a,b) = √[(0 − 3)² + (0 − 4)²] = √25 = 5.0000 (Euclidean)
SP23 2eCLO2 · Distances D4/D8/De, paths, adjacency, components · 5
5x4 image [9,34,20,0,16,21,50,5,5,6,11,15,7,8,10,2,8,7,9,0], V={10,20,9,7,0,5,1,3}. i) Does any 8-path exist between 9th and 19th pixels? Explain. ii) Find their City Block, Chess Board, Euclidean distances. [2+3]
Worked answer (deck 03)
SP23 Final Q2(e)2 + 3 marks

5×4 image [9, 34, 20, 0, 16, 21, 50, 5, 5, 6, 11, 15, 7, 8, 10, 2, 8, 7, 9, 0], V = {10, 20, 9, 7, 0, 5, 1, 3}. (i) Does an 8-path exist between the 9th and the 19th pixels? (ii) Their city-block, chessboard and Euclidean distances.

5×4 = 5 rows × 4 columns. Counting row by row from 1: the 9th pixel is (2,0) = 5 and the 19th is (4,2) = 9.

934200
1621505
561115
78102
8790
grey = not in V

(i) Yes: (2,0)=5 → (3,0)=7 → (4,1)=7 → (4,2)=9. All values are in V and each step goes to an 8-neighbour (the step (3,0) → (4,1) is diagonal).

(ii)

D4(p,q) = |2 − 4| + |0 − 2| = 4 (city block)
D8(p,q) = max(|2 − 4|, |0 − 2|) = max(2, 2) = 2 (chessboard)
De(p,q) = √[(2 − 4)² + (0 − 2)²] = √8 = 2.8284 (Euclidean)

Try it, step by step: SP23 Q2(e): the 8-path from the 9th to the 19th pixel.

SP22 3aCLO1 · Fundamentals: sampling, quantization, intensity levels, image types · 5
i) Mention two techniques that made the digital representation of analog world possible [3]. ii) 'The more intensity levels used, the finer level of detail discernible in an image' - explain why [2].
Worked answer (deck 01–02)
SP22 Final Q3(a)3 + 2 marks

(i) Mention two techniques that made the digital representation of the analog world possible. (ii) “The more intensity levels used, the finer the level of detail discernible in an image.” Explain why.

(i) Sampling: measuring the continuous scene only at a grid of points, which digitizes the coordinates and fixes the spatial resolution M × N. Quantization: rounding each sampled brightness to one of L discrete levels (L = 2k), which digitizes the amplitude. Together they turn the analog picture into a matrix of integers.

(ii) With more levels the step between neighbouring gray values is smaller, so small brightness differences (soft shading, fine texture) get different numbers and stay visible. With few levels, many different intensities are rounded to the same value; smooth areas turn into flat bands with visible jumps between them (false contouring, slides 30–34), and fine detail is lost.

SP22 3bCLO2 · Distances D4/D8/De, paths, adjacency, components · 8
4x4 image [[1,4,3,4],[2,3,4,7],[0,5,1,6],[3,4,7,1]], V={0,1,2,4}; p=(0,0) top-left, r=(0,3) top-right, q=(3,3) bottom-right. i) Properties of distance function for p,q,r [2] ii) D4(p,q), D4(p,r), D8(p,q), De(p,q) [2] iii) N4(p), ND(p), N8(p) [1.5] iv) 4-path between p and q? [1] v) m-path between p and q? [1.5]
Worked answer (deck 03)
SP22 Final Q3(b)2 + 2 + 1.5 + 1 + 1.5 marks

4×4 image, V = {0, 1, 2, 4}; p = (0,0) top-left, r = (0,3) top-right, q = (3,3) bottom-right. (i) Properties of the distance function for p, q, r. (ii) D4(p,q), D4(p,r), D8(p,q), De(p,q). (iii) N4(p), ND(p), N8(p). (iv) Is there a 4-path between p and q? (v) An m-path between p and q?

1434
2347
0516
3471
image; grey cells are not in V

(i) D(p,q) ≥ 0 and = 0 only if p = q; D(p,q) = D(q,p); D(p,r) ≤ D(p,q) + D(q,r).

(ii) D4(p,q) = |0 − 3| + |0 − 3| = 6; D4(p,r) = |0 − 0| + |0 − 3| = 3; D8(p,q) = max(3, 3) = 3; De(p,q) = √(9 + 9) = 4.2426.

(iii) p is a corner, so the other neighbours fall outside: N4(p) = {(1,0) = 2, (0,1) = 4}; ND(p) = {(1,1) = 3}; N8(p) = {(1,0) = 2, (0,1) = 4, (1,1) = 3}.

(iv) 4-path: none. From p, the 4-neighbours in V are (0,1) = 4 and (1,0) = 2; from them every 4-step leads to 3, 5 or 7 (not in V), so q cannot be reached.

(v) m-path: (0,0)=1 → (0,1)=4 → (1,2)=4 → (2,2)=1 → (3,3)=1. Check the diagonal (0,1) → (1,2): shared 4-neighbours (0,2) = 3 and (1,1) = 3 are not in V, so the step is m-adjacent; likewise (2,2) → (3,3) with (2,3) = 6, (3,2) = 7.

Try it, step by step: SP22 Q3(b): the 4-path fails; the m-path goes round through the diagonals.

F21 B4bCLO2 · Distances D4/D8/De, paths, adjacency, components · 6
Subsets S1 [[0,0,0,3,2],[0,1,0,2,1],[0,0,0,1,1],[0,0,1(x),0,0],[0,0,1,1,1]] and S2 [[2,1(y),2,1,2],[1,1,2,2,0],[1,0,2,3,2],[2,2,3,2,2],[0,0,1,1,1]] side by side. i) Draw a digital path, explain [2] ii) 4-path between x and y for V={1,2}? [1] iii) 4-path and m-path for V={1,0}? [2] iv) Are S1 and S2 4-adjacent for V={1,2}? [1]
Worked answer (deck 03)
F21 Final B4(b)2 + 1 + 2 + 1 marks

S1 (columns 0–4) and S2 (columns 5–9) side by side; x = (3,2) in S1, y = (0,6) in S2. (i) Define a digital path. (ii) Is there a 4-path between x and y for V = {1, 2}? (iii) 4-path and m-path for V = {0, 1}. (iv) Are S1 and S2 4-adjacent for V = {1, 2}?

0003221212
0102111220
0001110232
0010022322
0011100111
x = (3,2), y = (0,6)

(i) A sequence of distinct pixels from x to y, each adjacent (4, 8 or m) to the next, all with values in V; the number of steps is its length.

(ii) V = {1, 2}: no 4-path. x's 4-neighbours are 0, 0, 1 and 0; the 1 below leads only to more 1s at the bottom that never reach the top-right through 1s and 2s.

(iii) V = {0, 1}: 4-path (3,2)=1 → (2,2)=0 → (2,3)=1 → (2,4)=1 → (1,4)=1 → (1,5)=1 → (1,6)=1 → (0,6)=1; m-path (3,2)=1 → (2,2)=0 → (2,3)=1 → (2,4)=1 → (1,4)=1 → (1,5)=1 → (1,6)=1 → (0,6)=1 (the same here: a 4-path is also an m-path).

(iv) Yes: column 4 of S1 and column 5 of S2 touch with values in {1, 2}: row 0: 2 | 2, row 1: 1 | 1, row 2: 1 | 1.

Try it, step by step: F21 B4(b): change nothing for V = {0, 1}: watch the 4-path being found.

More worked examples: slides and other examiners (10)
Slide 25 · answer the questions (deck 01–02)
Slide 25 · answer the questionsworked

Q1 1 bit → 21 = 2 levels (0, 1). Q2 2 bits → 4 levels. Q3 p bits → 2p levels. Q4 256 intensities → log2 256 = 8 bits.

Q5 3-bit 5×5 image: gray-level resolution L = 2³ = 8 levels (range 0–7); spatial resolution 5 × 5 = 25 pixels; size b = 5 × 5 × 3 = 75 bits; bit depth 3.

F22 Final B4(b) · F21 Final A3(b) (other examiners' papers) (deck 01–02)
F22 Final B4(b) · F21 Final A3(b) (other examiners' papers)5 · 6 marks

F22: define sampling and quantization; bits for 512 intensities; spatial resolution and bit size of a 7×5 image. F21: a 256×256 image with 64 gray levels: bit depth and size in bytes; Mach band effect; index image; why more levels show finer detail.

F22: sampling digitizes coordinates, quantization digitizes amplitude. 512 intensities → k = log2 512 = 9 bits. A 7×5 image has spatial resolution 7 × 5 = 35 pixels; with 9 bits per pixel, b = 7 × 5 × 9 = 315 bits.

F21: 64 levels → k = log2 64 = 6 bits; b = 256 × 256 × 6 = 393,216 bits = 393,216 / 8 = 49,152 bytes (48 KB). Mach band effect: at the boundary between two flat bands of different brightness the eye sees a darker strip on the dark side and a brighter strip on the light side (overshoot/undershoot) that is not in the image. Index image: each pixel stores an index into a colour table (palette) holding the actual RGB values. The last part is the same as SP22 (ii) above.

Slides 49–54 · 3×3 → 6×6 (deck 01–02)
Slides 49–54 · 3×3 → 6×6worked

Step 1 · ratios: Ratiorow = 3/6, Ratiocol = 3/6.

Step 2 · positions = ratio × [1 … 6], rounded up (ceil):

rows: 3/6 × [1 2 3 4 5 6] = [0.5 1 1.5 2 2.5 3] → ceil → [1 1 2 2 3 3]
cols: 3/6 × [1 2 3 4 5 6] = [0.5 1 1.5 2 2.5 3] → ceil → [1 1 2 2 3 3]

Step 3 · copy: output pixel (i, j) = old pixel (row position i, column position j).

10422
2187
91425
original 3×3
1010442222
1010442222
22181877
22181877
9914142525
9914142525
output 6×6

Try it, step by step: the slide example. Change the new size (e.g. 5×5, 7×7, 9×9) to see how the positions change; the class-work matrices are solved just below.

Slide 55 · class work (deck 01–02)
Slide 55 · class worksolved

2×2 → 5×5

Step 1 · ratios: Ratiorow = 2/5, Ratiocol = 2/5.

Step 2 · positions = ratio × [1 … 5], rounded up (ceil):

rows: 2/5 × [1 2 3 4 5] = [0.4 0.8 1.2 1.6 2] → ceil → [1 1 2 2 2]
cols: 2/5 × [1 2 3 4 5] = [0.4 0.8 1.2 1.6 2] → ceil → [1 1 2 2 2]

Step 3 · copy: output pixel (i, j) = old pixel (row position i, column position j).

14
37
original 2×2
11444
11444
33777
33777
33777
output 5×5

3×3 → 9×9

Step 1 · ratios: Ratiorow = 3/9, Ratiocol = 3/9.

Step 2 · positions = ratio × [1 … 9], rounded up (ceil):

rows: 3/9 × [1 2 3 4 5 6 7 8 9] = [0.333 0.667 1 1.33 1.67 2 2.33 2.67 3] → ceil → [1 1 1 2 2 2 3 3 3]
cols: 3/9 × [1 2 3 4 5 6 7 8 9] = [0.333 0.667 1 1.33 1.67 2 2.33 2.67 3] → ceil → [1 1 1 2 2 2 3 3 3]

Step 3 · copy: output pixel (i, j) = old pixel (row position i, column position j).

143
723
560
original 3×3
111444333
111444333
111444333
777222333
777222333
777222333
555666000
555666000
555666000
output 9×9

4×4 → 7×7

Step 1 · ratios: Ratiorow = 4/7, Ratiocol = 4/7.

Step 2 · positions = ratio × [1 … 7], rounded up (ceil):

rows: 4/7 × [1 2 3 4 5 6 7] = [0.571 1.14 1.71 2.29 2.86 3.43 4] → ceil → [1 2 2 3 3 4 4]
cols: 4/7 × [1 2 3 4 5 6 7] = [0.571 1.14 1.71 2.29 2.86 3.43 4] → ceil → [1 2 2 3 3 4 4]

Step 3 · copy: output pixel (i, j) = old pixel (row position i, column position j).

1437
2345
2643
4710
original 4×4
1443377
2334455
2334455
2664433
2664433
4771100
4771100
output 7×7
Slide 11 · practice (deck 03)
Slide 11 · practiceworked

For the 7×7 image (rows and columns numbered from 1, as on the slide) find N4(3,4), ND(3,4), N8(3,4), N4(1,1), N4(4,1), N8(7,7).

49474845464342
47414344454948
48474945504349
49494949495050
48484848484950
48484950505049
49505051515050
rows/columns 1–7; (3,4) = 45 outlined
  • N4(3,4) = {(2,4) = 44, (4,4) = 49, (3,3) = 49, (3,5) = 50}
  • ND(3,4) = {(2,3) = 43, (2,5) = 45, (4,3) = 49, (4,5) = 49}
  • N8(3,4) = {(2,4) = 44, (4,4) = 49, (3,3) = 49, (3,5) = 50, (2,3) = 43, (2,5) = 45, (4,3) = 49, (4,5) = 49}
  • N4(1,1) = {(2,1) = 47, (1,2) = 47}: a corner, only 2 neighbours inside
  • N4(4,1) = {(3,1) = 48, (5,1) = 48, (4,2) = 49}: on the left border, 3 inside
  • N8(7,7) = {(6,7) = 49, (7,6) = 50, (6,6) = 50}: a corner, 3 inside
Slide 21 · 4- and 8-adjacency (deck 03)
Slide 21 · 4- and 8-adjacencyworked

V = {0, 1, 2, 3, 5}. A = (0,0), B = (0,1), E = (1,1), C = (2,2), D = (3,3). Which pairs are 4-adjacent / 8-adjacent?

4232
3313
2352
2125
A=4 at (0,0), B=2, E=3, C=5, D=5
pairneighbours?values in V?4-adjacent8-adjacent
A, Byes (4-neighbours)A = 4 ∉ Vnono
A, CnoA = 4 ∉ Vnono
B, Cno: (0,1) and (2,2) are 2 rows apartyesnono
C, Ddiagonalyesnoyes
C, Ediagonalyesnoyes
Slide 22 · m-adjacency (deck 03)
Slide 22 · m-adjacencyworked

V = {0, 1, 2, 3, 5}. A = (0,0) = 4, B = (0,1) = 2, C = (2,2) = 5, D = (3,3) = 0. Which pairs are m-adjacent?

4232
3313
2354
2160
C = 5 at (2,2), D = 0 at (3,3)

A, B / A, C: no, A = 4 ∉ V. B, C: not neighbours. C, D: D ∈ ND(C); N4(C) ∩ N4(D) = {(2,3) = 4, (3,2) = 6}, and neither 4 nor 6 is in V, so C and D are m-adjacent.

Slide 20 · 4-, 8- and m-paths (deck 03)
Slide 20 · 4-, 8- and m-pathsworked

V = {1, 2}; p = (1,3), q = (3,3) in the slide's numbering (top-right and bottom-right).

011
020
001
rows/cols 1–3 on the slide; here (0,2) = p and (2,2) = q

8-path: two exist: (1,3) → (1,2) → (2,2) → (3,3) and (1,3) → (2,2) → (3,3). That ambiguity is what m-adjacency removes.

m-path: only (1,3) → (1,2) → (2,2) → (3,3). The diagonal step (1,3) → (2,2) is not allowed because they share the 4-neighbour (1,2) = 1 ∈ V. The step (2,2) → (3,3) is allowed: their shared 4-neighbours (2,3) and (3,2) are both 0.

4-path: absent: from (2,2) the only way down is through 0s.

Try it, step by step: slide 20: switch between 4-, 8- and m-paths and watch which steps are allowed.

Slide 23 · m-connected path (deck 03)
Slide 23 · m-connected pathworked

V = {1, 2}. Find the m-connected path between p (bottom-left) and q (top-right). Is a 4-connected path possible?

4232
3313
2322
2123
p = (3,0), q = (0,3)

4-path: none. m-path: (3,0)=2 → (3,1)=1 → (3,2)=2 → (2,2)=2 → (1,2)=1 → (0,3)=2, i.e. 2(p) → 1 → 2 → 2 → 1 → 2(q) as on the slide.

Try it, step by step: slide 23: the m-path, step by step. Try 4-path too: it fails.

Slide 32 · practice (deck 03)
Slide 32 · practiceworked

p = (3,4), q = (6,7). Find D4, D8, De.

D4(p,q) = |3 − 6| + |4 − 7| = 6 (city block)
D8(p,q) = max(|3 − 6|, |4 − 7|) = max(3, 3) = 3 (chessboard)
De(p,q) = √[(3 − 6)² + (4 − 7)²] = √18 = 4.2426 (Euclidean)

2. Point operations: negative, log, gamma, stretching, slicing, bit planes

deck 04 · 9 past questions
The pattern: how she asks it
  • Q1: short notes on negative / log / power-law; the piecewise (r1,s1), (r2,s2) cases: identity, threshold, stretch (SP23 2d, SP22 2a, F21 B3b).
  • Q2: bit-plane slicing of a small grid (SP25 2b i, SP24 2b i); gray-level slicing map (F25 quiz).
  • Q3 case: dark or washed-out X-ray, so power-law with γ < 1 or > 1, plus contrast stretching numbers (F23 3d, SP24 3e).

Connects to: Stretching and HE do the same job (spread the gray levels). Gamma/log are the first step in almost every Q3 enhancement pipeline.

typeCLOtimeswhere
Piecewise linear:CLO13SP23 2d, SP22 2a, F21 B3b
Contrast stretching numericCLO22SP24 3e, F23 3d
Intensity transforms notesCLO12F23 1b, F25 quiz Q1C 1
Negative / log / gamma numericCLO21SP24 3e
Gray-levelCLO21F25 quiz Q1C 2a
Resolution / bits / levels arithmeticCLO21F25 quiz Q1C 2b
Formula wall: every formula with a worked line
methodformulaworked lineuse
negatives = (L−1) − r8-bit, r = 100 → 155white detail in dark areas
logs = c · log(1 + r), c = (L−1)/log(L)c = 255/log 256 = 105.89; r = 50 → 105.89 × log 51 = 181expand dark, compress bright
gammas = (L−1) · (r / (L−1))γ3-bit, r = 3: γ = 0.5 → 7 × 0.6547 = 5; γ = 2 → 7 × 0.1837 = 1γ < 1 brighter, γ > 1 darker
min–max stretchs = (r − rmin)(L−1) / (rmax − rmin)[50, 150] → [0, 255]: r = 100 → 50 × 255/100 = 127.5 → 128fill the whole range
thresholds = L−1 if r > k, else 0k = 127: 90 → 0, 200 → 255binary object vs background
bit plane nbitn = ⌊r / 2n−1⌋ mod 2 (weight 2n−1)13 = 1101: planes 4,3,2,1 = 1,1,0,1top planes = shape, bottom = noise

Piecewise contrast stretching with (r₁, s₁), (r₂, s₂)

s =
(s₁ / r₁) · r
0 ≤ r < r₁
s₁ + (s₂ − s₁)(r − r₁) / (r₂ − r₁)
r₁ ≤ r ≤ r₂
s₂ + (L−1 − s₂)(r − r₂) / (L−1 − r₂)
r > r₂

Three special settings: r₁ = s₁ and r₂ = s₂ → identity (no change). r₁ = r₂, s₁ = 0, s₂ = L−1 → thresholding. r₁ ≤ r₂ and s₁ < s₂ in between → contrast stretching. Draw all three graphs.

Gray-level slicing, range [A, B] (F25 quiz: bone [120, 200])

approach 1 =
L−1
A ≤ r ≤ B
0
otherwise
approach 2 =
L−1
A ≤ r ≤ B
r
otherwise

r = 100 → 0 / 100 · r = 150 → 255 / 255 · r = 220 → 0 / 220. Approach 1 = binary, background gone; approach 2 = background kept.

TrapFind L first. A 3-bit image means L − 1 = 7, so gamma and log use 7, not 255. Round .5 up.

Past questions: look, recall, then open the answer

SP24 3eCLO2 · Contrast stretching numeric · 6CLO2 · Negative / log / gamma numeric · 2
4x4 8-bit image [100 105 107 100; 120 125 120 130; 115 117 112 118; 130 140 145 151]. Illustrate the output after contrast stretching to [0-255] using s=T(r); express s=T(r). Then apply thresholding to the stretched image: r>180 -> 255, r<90 -> 0, 90<=r<=180 -> negative transformation. Show final output image. [4+4]
Worked answer (deck 04)

Same answer as F23 3d (F23 Q3(d) part 1 = SP24 Q3(e) part 1).

SP24 Q3(e) part 24 marks

Apply to the stretched image: r > 180 → 255; r < 90 → 0; 90 ≤ r ≤ 180 → negative transformation. Show the final image.

s =
255
if r > 180
255 − r
if 90 ≤ r ≤ 180
0
if r < 90
090180255075165255r (input)s (output)255 − r(90,165)(180,75)
SP24 rule: three pieces. 0 below 90, negative between 90 and 180, 255 above 180.
stretched rrules
0< 90 → 00
25< 90 → 00
35< 90 → 00
60< 90 → 00
75< 90 → 00
85< 90 → 00
90255 − 90165
100255 − 100155
125255 − 125130
150255 − 150105
200> 180 → 255255
225> 180 → 255255
255> 180 → 255255
025350
100125100150
75856090
150200225255
stretched r
0000
155130155105
000165
105255255255
final output
F23 1bCLO1 · Intensity transforms notes · 4
Write short notes on Negative transformation and Intensity level slicing. [2+2]
Worked answer (deck 04)
F23 Q1(b), first half2 marks

Write a short note on the Negative transformation.

The negative transformation is a basic linear intensity transformation that reverses the intensity values of an image (light ↔ dark). For an image with L gray levels in the range [0, L−1]:

s = (L − 1) − r

where r is the input and s the output gray level. Its graph is a straight line from (0, L−1) down to (L−1, 0) [draw it].

Use: enhancing white or gray detail embedded in dark regions, especially when black areas dominate, e.g. medical X-ray and mammogram images.

Example (8-bit): r = 100 gives s = 255 − 100 = 155.

F23 Q1(b), second half2 marks

Write a short note on intensity-level slicing.

Intensity-level (gray-level) slicing highlights a specific range [A, B] of gray levels in an image. It is similar to thresholding, and it is useful when different features of an image lie in different gray levels (e.g. blood vessels in an angiogram).

Two basic approaches: (1) without background: highlight [A, B] and reduce all other levels to a low constant level; (2) with background: highlight [A, B] and preserve all other levels. [Draw both small graphs.]

F23 3dCLO2 · Contrast stretching numeric · 10
4x4 segment {100,105,107,100; 120,125,120,130; 115,117,112,118; 130,140,145,151}, 8-bit, range [100-151]. Output after contrast stretching to [0-255] using s=T(r). Then apply contrast stretching graph (Fig.3) a=90, b=180, m=100: r>180 -> 255, r<90 -> 0, between: r<100 s near 0, r>100 s near 255. Show final output image. [5+5]
Worked answer (deck 04)
F23 Q3(d) part 1 = SP24 Q3(e) part 15 marks

A 4×4 segment of an 8-bit image has range [100–151]. Find the output after contrast stretching to [0–255] using s = T(r), and express s = T(r).

100105107100
120125120130
115117112118
130140145151
input image

Given: Min = 100, Max = 151, NewMin = 0, NewMax = 255.

Formula: s = (r − Min) × (NewMax − NewMin)/(Max − Min) + NewMin

(NewMax − NewMin)/(Max − Min) = (255 − 0)/(151 − 100) = 255/51 = 5

s = T(r) = 5 (r − 100)

Apply to every gray level:

rworkings
100(255 − 0)/(151 − 100) × (100 − 100) + 0 = 5 × 00
105(255 − 0)/(151 − 100) × (105 − 100) + 0 = 5 × 525
107(255 − 0)/(151 − 100) × (107 − 100) + 0 = 5 × 735
112(255 − 0)/(151 − 100) × (112 − 100) + 0 = 5 × 1260
115(255 − 0)/(151 − 100) × (115 − 100) + 0 = 5 × 1575
117(255 − 0)/(151 − 100) × (117 − 100) + 0 = 5 × 1785
118(255 − 0)/(151 − 100) × (118 − 100) + 0 = 5 × 1890
120(255 − 0)/(151 − 100) × (120 − 100) + 0 = 5 × 20100
125(255 − 0)/(151 − 100) × (125 − 100) + 0 = 5 × 25125
130(255 − 0)/(151 − 100) × (130 − 100) + 0 = 5 × 30150
140(255 − 0)/(151 − 100) × (140 − 100) + 0 = 5 × 40200
145(255 − 0)/(151 − 100) × (145 − 100) + 0 = 5 × 45225
151(255 − 0)/(151 − 100) × (151 − 100) + 0 = 5 × 51255

Output image:

100105107100
120125120130
115117112118
130140145151
input r
025350
100125100150
75856090
150200225255
stretched s
01001512550125255r (input)s (output)s = 5(r − 100)(100, 0)(151, 255)125 → 125
Min-max stretch: the band 100–151 is spread across the whole 0–255.

Check: the old contrast was 151 − 100 = 51; the new contrast is 255 − 0 = 255. Midpoint m = 125 (slide 40).

F23 Q3(d) part 25 marks

Apply the contrast-stretching graph with a = 90, b = 180, m = 100 to the stretched image: r > 180 → 255; r < 90 → 0; between them, r < 100 moves close to 0 and r > 100 moves close to 255. Show the final image.

090m1802550255r (input)s (output)a = 90b = 180steep at m = 100r > 180 → 255
Slide 42 S-curve: below 90 → 0, above 180 → 255, between them values move away from m.
s =
255
if r > 180
close to 0 (darker)
if 90 ≤ r < 100
close to 255 (brighter)
if 100 < r ≤ 180
0
if r < 90

Apply to each stretched value (taking the curve at its limit: the dark side goes to 0 and the bright side goes to 255):

stretched rrules
0< 900
25< 900
35< 900
60< 900
75< 900
85< 900
9090–100, toward 00
100≥ 100, toward 255255
125≥ 100, toward 255255
150≥ 100, toward 255255
200> 180255
225> 180255
255> 180255

r = 100 sits exactly at m. I place it on the bright side; write your assumption in the script.

025350
100125100150
75856090
150200225255
stretched r
0000
255255255255
0000
255255255255
final output

Bright pixels became brighter and dark pixels darker, so the result has higher contrast (here, the limit is a binary image).

SP23 2dCLO1 · Piecewise linear: · 5
Points A(r1,s1), B(r2,s2) control the piecewise linear transformation. Illustrate the relation between (r1,s1) and (r2,s2) for Linear Identity, Thresholding and Contrast Stretching. [1+2+2]
Worked answer (deck 04)
SP23 2(d) · F22 A1(b)ii · SP22 2(a) · F21 B3(b)5–6 marks, asked 5 times

Points A(r1, s1) and B(r2, s2) control the piecewise linear transformation. Illustrate the relation between (r1, s1) and (r2, s2) for (i) linear identity (ii) thresholding (iii) contrast stretching.

The shape of the piecewise linear function s = T(r) is decided by the two points (r1, s1) and (r2, s2), with r1 ≤ r2 and s1 ≤ s2 so the function stays increasing.

(i) Linear identity: r1 = s1 and r2 = s2. Both points lie on the diagonal, so the line is s = r and the gray levels do not change.

(ii) Thresholding: r1 = r2, s1 = 0, s2 = L−1. The line jumps vertically at r1, so every pixel below r1 becomes 0 and every pixel above becomes L−1: a binary image.

(iii) Contrast stretching: r1 < r2 and s1 < s2. The middle piece between r1 and r2 is steeper than 45°, so those gray levels are spread over a wider output range and contrast increases. Special case, min-max stretching: (r1, s1) = (rmin, 0), (r2, s2) = (rmax, L−1).

0r1r2L−10s1s2L−1r (input)s (output)(r1,s1)(r2,s2)
(i) Identity: r1 = s1, r2 = s2
0r1 = r2L−10L−1r (input)s (output)(r1, 0)(r2, L−1)
(ii) Thresholding: r1 = r2, s1 = 0, s2 = L−1
0r1r2L−10s1s2L−1r (input)s (output)(r1,s1)(r2,s2)
(iii) Contrast stretching: r1 < r2, s1 < s2
SP22 2aCLO1 · Piecewise linear: · 5
Points (r1,s1),(r2,s2) control the piecewise linear transformation. Illustrate the relation for i) Linear Identity [1] ii) Thresholding [2] iii) Contrast Stretching [2].
Worked answer (deck 04)

Same answer as SP23 2d (SP23 2(d) · F22 A1(b)ii · SP22 2(a) · F21 B3(b)).

F21 B3bCLO1 · Piecewise linear: · 6
Contrast stretching is a piecewise linear transformation; points (r1,s1),(r2,s2). Illustrate the relation for i) Linear Identity [1] ii) Thresholding [2] iii) Contrast Stretching [3].
Worked answer (deck 04)

Same answer as SP23 2d (SP23 2(d) · F22 A1(b)ii · SP22 2(a) · F21 B3(b)).

F25 quiz Q1C 1CLO1 · Intensity transforms notes · 6
Discuss logarithmic transformations for image enhancement. [6]
Worked answer (F25 quiz page)
Fall 25 Quiz 1 (set C) Q16 marks

Discuss logarithmic transformations for image enhancement.

Definition. The log transformation is a basic intensity transformation:

s = c · log(1 + r)

r = input gray level, s = output gray level, c = a constant, r ≥ 0. The 1 is added so that log 0 never occurs. To map 0…255 onto 0…255, c = 255 / log(256) = 105.89.

Shape. Draw this graph in the answer script:

0501001502002550128255r (input)s (output)loginverse log10→11050→181100→212
s = c·log(1 + r), c = 105.89 (8-bit). Dashed: identity s = r.

The curve lies above the diagonal. It is steep for small r and flat for large r.

Properties.

  • For low input values the range is expanded: dark regions are spread out and their details become visible.
  • For high input values the range is compressed: bright regions are squeezed together.

Worked values (c = 105.89):

rc·log(1 + r)s
10105.89 × log(11) = 110.3110
50105.89 × log(51) = 180.8181
100105.89 × log(101) = 212.2212
255105.89 × log(256) = 255.0255

The input band 0–50 (20% of the range) becomes 0–181 (71%). The band 100–255 shrinks to 212–255.

Application. Use it when the input has a very large dynamic range that the display cannot show. The standard example is the Fourier spectrum, whose values run from 0 to about 1.5×106. On a display only the bright centre shows; after the log transform the whole spectrum is visible. That is why it is called dynamic-range compression.

Inverse log, s = 10r/c − 1, does the opposite: it expands the bright values and compresses the dark ones.

Try it: move r and watch where it lands on the curve. Notice how a small step in the dark end moves s a lot, and the same step in the bright end hardly moves it.

F25 quiz Q1C 2aCLO2 · Gray-level · 11
Medical X-ray image (8-bit) needs enhancement of bone structures in range [120, 200]. Apply gray level slicing for both approaches (with/without background suppression). i) Write the transformation functions [4] ii) Show the mapping for pixel values 100, 150 and 220 [4] iii) Sketch the transformation curves [3].
Worked answer (F25 quiz page)
Fall 25 Quiz 2(a)11 marks

A medical X-ray image (8-bit) needs its bone structures in the range [120, 200] enhanced. Apply gray-level slicing with both approaches. (i) Write the transformation functions [4] (ii) Show the mapping for pixel values 100, 150 and 220 [4] (iii) Sketch the transformation curves [3].

Given: 8-bit, so L − 1 = 255. Band of interest A = 120, B = 200. Highlight level = 255.

(i) Transformation functions

Approach 1, without background (other levels suppressed):

s =
255
if 120 ≤ r ≤ 200
0
otherwise

Approach 2, with background (other levels preserved):

s =
255
if 120 ≤ r ≤ 200
r
otherwise

(ii) Mapping

rinside [120, 200]?Approach 1 sApproach 2 s
100no0100
150yes255255
220no0220

r = 100 is below the band: approach 1 gives 0, approach 2 keeps 100. r = 150 is inside: both give 255. r = 220 is above: approach 1 gives 0, approach 2 keeps 220.

(iii) Curves

01202002550255r (input)s (output)
Approach 1: 0, jump to 255 on [120, 200], back to 0.
01202002550255r (input)s (output)
Approach 2: s = r, lifted to 255 on [120, 200].

Result: approach 1 shows only the bones, white on black. Approach 2 shows the bones in white with the rest of the X-ray still visible.

Try it: type any pixel value, or change the band, and read off both approaches. The dot moves on both curves.

F25 quiz Q1C 2bCLO2 · Resolution / bits / levels arithmetic · 3
How many different shades of grey are possible in a 10-bit grayscale image? Show their numeric range. Illustrate the expression of intensity resolution of any gray image. [3]
Worked answer (F25 quiz page)
Fall 25 Quiz 2(b)3 marks

How many shades of gray are possible in a 10-bit grayscale image? Show the numeric range. Write the expression for intensity resolution.

Intensity resolution = number of gray levels a pixel can take. With k bits per pixel:

L = 2k

Here k = 10, so L = 210 = 1024 shades.

Range: 0 to L − 1 = 0 to 1023 (0 = black, 1023 = white).

Answer1024 gray levels, from 0 to 1023. General expression: L = 2k, range [0, 2k − 1].
In plain wordsEvery extra bit doubles the number of grays. 8 bits give 256, 10 bits give 1024, so the steps between neighbouring grays are 4 times finer. Fewer bits give visible bands (false contouring).

Try it: slide k and read off L = 2k and the range. The strip shows the gray steps, which become visible bands at small k.

More worked examples: slides and other examiners (10)
Slide 12 · Example 1 (deck 04)
Slide 12 · Example 1worked

The matrix gives the pixel values of an 8-bit image r. Apply the negative transform and find the resulting pixel values.

Given: 8-bit image, so L = 28 = 256.

Formula: s = (L − 1) − r = 255 − r. Apply it to every pixel.

r255 − rs
85255 − 85170
88255 − 88167
89255 − 89166
90255 − 90165
95255 − 95160
98255 − 98157
99255 − 99156
100255 − 100155
102255 − 102153
105255 − 105150
110255 − 110145
115255 − 115140
135255 − 135120
140255 − 140115
145255 − 145110
1001109095
98140145135
89908885
10210599115
input image r
155145165160
157115110120
166165167170
153150156140
negative image s
Slide 13 · Exercise (deck 04)
Slide 13 · Exercisesolved

The matrix gives the pixel values of a 5-bit image r. Apply the negative transform.

Given: 5-bit image, so L = 25 = 32 and L − 1 = 31.

Formula: s = 31 − r.

r31 − rs
1631 − 1615
1831 − 1813
1931 − 1912
2031 − 2011
2131 − 2110
2331 − 238
2631 − 265
2731 − 274
2931 − 292
3031 − 301
3131 − 310
21262930
19212030
16162631
19182723
input r (5-bit)
10521
1210111
151550
121348
negative s
Mini example (slide 75 image, T = 3) (deck 04)
Mini example (slide 75 image, T = 3)worked

Given: 3-bit image, threshold T = 3. Rule: s = 1 if r > 3, otherwise s = 0.

Values 0, 1, 2, 3 become 0. Values 4, 5, 6, 7 become 1.

21210
71432
24137
13463
14134
input r
00000
10100
01001
00110
01001
thresholded s
Numeric log example (8-bit) (deck 04)
Numeric log example (8-bit)worked

Choose c so that the top input stays at the top: c = (L−1) / log(1 + (L−1)) = 255 / log10(256) = 255 / 2.4082 = 105.89.

Formula: s = 105.89 × log10(1 + r), then round.

r1 + rlog10(1+r)× cs
010.00000.00
120.301031.932
560.778282.482
10111.0414110.3110
50511.7076180.8181
1001012.0043212.2212
2002012.3032243.9244
2552562.4082255.0255

Inputs 0–50 went to 0–181 (spread out). Inputs 100–255 went to 212–255 (squeezed). Any log base gives the same s, as long as c uses the same base.

Practice · gamma on a 3-bit image (deck 04)
Practice · gamma on a 3-bit imageworked

The 3×3 image below is transformed using the gamma transformation with γ = 0.5 and γ = 2.0. Find the outputs and discuss the effect of changing γ.

146
027
352
input image

Step 1, find L. The largest value is 7, so the image is 3-bit: L = 8, L − 1 = 7. Take c = 1 on the normalized values.

Step 2, formula. s = 7 × (r/7)γ, rounded to the nearest integer.

Step 3, one value in full (r = 3, γ = 0.5): 3/7 = 0.4286; 0.42860.5 = 0.6547; 7 × 0.6547 = 4.58 → 5.

Step 4, table for every gray level, γ = 0.5:

rr/7(r/7)0.5×7s
00.00000.00000.000
10.14290.37802.653
20.28570.53453.744
30.42860.65474.585
40.57140.75595.295
50.71430.84525.926
60.85710.92586.486
71.00001.00007.007

γ = 2.0:

rr/7(r/7)2.0×7s
00.00000.00000.000
10.14290.02040.140
20.28570.08160.571
30.42860.18371.291
40.57140.32652.292
50.71430.51023.574
60.85710.73475.145
71.00001.00007.007

Step 5, replace each pixel.

146
027
352
input
356
047
564
γ = 0.5
025
017
141
γ = 2.0
0123456701234567r (input)s (output)γ = 0.5γ = 2.0
Practice mapping drawn: γ = 0.5 points sit above the diagonal, γ = 2.0 below.

Step 6, effect of γ.

  • γ = 0.5 (< 1) pushes values up: the image becomes brighter. Dark levels 1–3 are spread to 3–5 (more detail in dark areas); bright levels 4–7 bunch at 5–7.
  • γ = 2.0 (> 1) pushes values down: the image becomes darker. Levels 0–1 collapse to 0 and 2–3 to 1; bright levels 5–7 are spread to 4–7 (more detail in bright areas).
  • So a smaller γ brightens and gives contrast to dark regions; a larger γ darkens and gives contrast to bright regions. γ = 1 leaves the image unchanged.
Short note (deck 04)
Short notetheory
  • Many devices (image capture, printing, display) respond to intensity according to a power law, so they need to be corrected. A CRT monitor has an intensity-to-voltage response with exponent about 1.8 to 2.5, so images look darker than they should.
  • Gamma correction is the process used to correct this power-law response: pre-process the image with s = c·r1/2.5 so the monitor's darkening cancels out.
  • Background: human vision is nonlinear (more sensitive to small changes in dark tones than bright ones), while digital sensors are linear.
  • Different display devices have different gammas, so each needs its own correction.
  • Why needed: to display an image accurately on screen (without correction it looks bleached out or, more likely, too dark); to reproduce colours accurately; and for general-purpose contrast manipulation.
Slides 60–61 · 3-bit example (deck 04)
Slides 60–61 · 3-bit exampleworked

Step 1. Write each pixel of the 3-bit image in binary.

67667
00012
11123
45542
66677
3-bit image
110111110110111
000000000001010
001001001010011
100101101100010
110110110111111
binary

Step 2. Collect the 1st bit of every pixel (MSB plane), the 2nd bit (centre plane), and the 3rd bit (LSB plane).

11111
00000
00000
11110
11111
MSB plane (×4)
11111
00001
00011
00001
11111
centre plane (×2)
01001
00010
11101
01100
00011
LSB plane (×1)

Check one pixel: 6 = 110 gives MSB 1, centre 1, LSB 0.

Slides 63–67 (deck 04)
Slides 63–67worked

Given: A = [167 133 111; 144 140 135; 159 154 148] and its bit format:

101001111000010101101111
100100001000110010000111
100111111001101010010100
A in binary

8th plane only. For 10100111, multiply only the 8th bit by 27 = 128: (1×128) + (0×0) + (1×0) + (0×0) + (0×0) + (1×0) + (1×0) + (1×0) = 128. 111 = 01101111 has 8th bit 0, so it gives 0.

Bitwise AND view (slides 64–65): pixel AND 10000000 (128) keeps the 8th bit only; pixel AND 01000000 (64) keeps the 7th bit only.

1281280
128128128
128128128
8th plane image
0064
000
000
7th plane image
12812864
128128128
128128128
8th + 7th planes

8th + 7th combined (slide 66): for 10100111, (1×128) + (0×64) = 128. For 01101111, (0×128) + (1×64) = 64.

Plane 1 (LSB) and plane 8 (MSB) as 0/1 images (slides 71–72):

111
001
100
plane 1 (LSB)
110
111
111
plane 8 (MSB)
Slide 74 · Exercise (deck 04)
Slide 74 · Exercisesolved

Using the same A, reconstruct the image using only (a) the 5th and 4th bit planes (b) the 7th and 6th bit planes.

(a) weights 24 = 16 and 23 = 8. Example 01101111 (111): 5th bit 0, 4th bit 1, so 0 + 8 = 8. Example 10011111 (159): 16 + 8 = 24.

(b) weights 26 = 64 and 25 = 32. Example 01101111: 64 + 32 = 96. Example 10100111 (167): 0 + 32 = 32.

008
1680
242416
(a) 5th + 4th
32096
000
000
(b) 7th + 6th

(a) is dark and noisy-looking because the low planes hold little of the picture; (b) keeps more structure.

Slide 75 · Class work (deck 04)
Slide 75 · Class workall parts

Apply these enhancement techniques to the 3-bit image I: digital negative; bit planes; thresholding with T = 3; intensity slicing with both approaches, a = 3, b = 5, raising the level to 5. Show the enhanced images.

21210
71432
24137
13463
14134
I (3-bit)

Given: 3 bits per pixel, so L = 8 and L − 1 = 7.

(1) Digital negative. s = (L − 1) − r = 7 − r. 0→7, 1→6, 2→5, 3→4, 4→3, 6→1, 7→0.

56567
06345
53640
64314
63643
negative

(2) Bit planes. Binary: 0 = 000, 1 = 001, 2 = 010, 3 = 011, 4 = 100, 6 = 110, 7 = 111.

00000
10100
01001
00110
01001
plane 2 (MSB, ×4)
10100
10011
10011
01011
00010
plane 1 (×2)
01010
11010
00111
11001
10110
plane 0 (LSB, ×1)

Check: 4·b2 + 2·b1 + b0 rebuilds I (e.g. 6 → 1, 1, 0 → 4 + 2 = 6).

(3) Thresholding, T = 3.

s =
1
if r > 3
0
if r ≤ 3
00000
10100
01001
00110
01001
thresholded

Note: this equals the MSB plane, because r > 3 exactly when the first bit is 1.

(4) Intensity slicing, [a, b] = [3, 5], highlight level 5.

Approach 1: s =
5
if 3 ≤ r ≤ 5
0
otherwise
Approach 2: s =
5
if 3 ≤ r ≤ 5
r
otherwise
00000
00550
05050
05505
05055
without background
21210
71552
25157
15565
15155
with background
01234567057r (input)s (output)
Approach 1
01234567057r (input)s (output)
Approach 2

3. Histogram: types, equalization, matching

deck 05 · 15 past questions
The pattern: how she asks it
  • Q2(a), 8 marks, nearly every paper: a 3-bit 5×5 grid or a table of counts. Write PDF, CDF, s = round(7·CDF), then the new histogram, and draw both.
  • Q1: the 4 histogram types (dark, bright, low, high contrast), whether two images can have the same histogram, the definition and steps of HE.
  • Rare: histogram matching (F22 A3b uses the slide 49 counts).

Connects to: HE is the automatic version of contrast stretching (ch 3). Its output flattens the histogram, which helps the thresholding and segmentation steps later.

typeCLOtimeswhere
Histogram equalizationCLO29SP25 2a, SP24 2a, F23 2a, SP23 3b, F22 A3b, SP22 2b, F21 B3a, F25 quiz Q1D 2b, F25 quiz Q2C 2
Histogram conceptsCLO16F23 1a, SP23 2b, F22 A2b, F25 quiz Q1D 1, F25 quiz Q1D 2a, F25 quiz Q2C 1
Histogram equalization definition / steps / effectCLO11F22 A2b
Formula wall: every formula with a worked line
p(rk) = nk / N  →  CDF = Σ p  →  sk = round( (L−1) × CDFk )

Worked (3-bit, N = 64, nk = [8, 10, 10, 2, 12, 16, 4, 2]). Do every row like the first two:

r = 0: p = 8/64 = 0.125, CDF = 0.125, 7 × 0.125 = 0.875 → 1.   r = 1: p = 10/64 = 0.15625, CDF = 0.28125, 7 × 0.28125 = 1.969 → 2.

rnkpCDF7 × CDFs
080.12500.12500.8751
1100.15620.28121.9692
2100.15620.43753.0623
320.03120.46883.2813
4120.18750.65624.5945
5160.25000.90626.3446
640.06250.96886.7817
720.03121.00007.0007

New histogram: add the counts that land on the same s: s = 3 gets 10 + 2 = 12; s = 7 gets 4 + 2 = 6. Total stays 64.

816081102123412516667gray levelnk
Equalized histogram: spread out, not perfectly flat, because levels are discrete. Write that sentence.

Matching (if asked): equalize the input → H, equalize the target → S, map each r to the z whose S is closest to its H (ties → higher S).

TrapDraw three things: the original histogram, the equalized histogram and the transformation (staircase). Each is a separate mark.

Past questions: look, recall, then open the answer

SP25 2aCLO2 · Histogram equalization · 8
For the 5x5 input image [(0 0 1 1 2),(0 1 1 2 4),(1 1 2 4 5),(1 3 4 5 6),(3 3 5 6 7)] of 3 bits, find a transformation function that will approximately equalize its histogram. i) Illustrate it in detail [4] ii) Provide the transformed equalized output image [1] iii) Sketch the transformation curve [1] iv) Draw the input and output histograms [2].
Worked answer (deck 05)
SP25 Q2(a) · SP24 Q2(a)8–9 marks

For the 5×5, 3-bit input image, find a transformation function that will approximately equalize its histogram. (i) Illustrate in detail (ii) give the equalized output image (iii) sketch the transformation curve (iv) draw the input and output histograms.

Given: 3-bit image, so L = 23 = 8 and L − 1 = 7. Total pixels N = 25.

00112
01124
11245
13456
33567
input image

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk01234567
nk37333321

Check: Σ nk = 3 + 7 + 3 + 3 + 3 + 3 + 2 + 1 = 25.

3.573071323334352617rknk
Original histogram (nk against rk)
0.140.280.1200.2810.1220.1230.1240.1250.0860.047rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 7; round off (.5 goes up).

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
030.12000.12007 × 0.1200 = 0.84001
170.28000.40007 × 0.4000 = 2.80003
230.12000.52007 × 0.5200 = 3.64004
330.12000.64007 × 0.6400 = 4.48004
430.12000.76007 × 0.7600 = 5.32005
530.12000.88007 × 0.8800 = 6.16006
620.08000.96007 × 0.9600 = 6.72007
710.04001.00007 × 1.0000 = 7.00007

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
013
137
243
343
453
563
672
771

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
133
377
43 + 3 = 66
533
633
72 + 1 = 33
3.5703127364353637sknk
Equalized histogram (n against new level sk)
0123456701234567input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Equalized output image: replace every pixel r by its s.

00112
01124
11245
13456
33567
input
11334
13345
33456
34567
44677
equalized output

Try it, step by step: SP25 / SP24, every step.

SP24 2aCLO2 · Histogram equalization · 9
For the 5x5 image [(0 0 1 1 2),(0 1 1 2 4),(1 1 2 4 5),(1 3 4 5 6),(3 3 5 6 7)] find a transformation function that will approximately equalize its histogram: i) illustrate in detail ii) transformed equalized image iii) sketch transformation curve iv) draw equalized histogram [4+1+2+2]
Worked answer (deck 05)

Same answer as SP25 2a (SP25 Q2(a) · SP24 Q2(a)).

F23 1aCLO1 · Histogram concepts · 4
What is the histogram of an image? Can you reconstruct the image from the histogram? How does the histogram differ for dark, bright, low contrast or high contrast image? [1+1+2]
Worked answer (deck 05)
F23 Q1(a)4 marks

What is the histogram of an image? Can you reconstruct the image from the histogram? How does the histogram differ for a dark, bright, low-contrast or high-contrast image? [1+1+2]

Histogram: a graphical representation of the distribution of pixel intensity values in an image. For gray levels in [0, L−1] it is the discrete function h(rk) = nk, where nk is the number of pixels with gray level rk.

Reconstruction: No. A histogram holds only statistical information (how many pixels have each value), not the location of the pixels. Different images can have the same histogram, e.g. a half-black/half-white image and a checkerboard with the same numbers of black and white pixels.

Four types [draw the four small histograms]:

dark → brightn
dark
dark → brightn
bright
dark → brightn
low contrast
dark → brightn
high contrast
  • Dark: components concentrated on the low (left) side of the gray scale.
  • Bright: concentrated on the high (right) side.
  • Low contrast: a narrow band, usually in the middle of the scale.
  • High contrast: spread over a broad range, fairly uniformly.
F23 2aCLO2 · Histogram equalization · 6
Qubits.png 4x4, 5-bit: [[16,30,10,18],[25,1,0,12],[16,13,9,5],[19,14,28,0]]. i) histogram ii) compute and sketch normalized histogram iii) compute and sketch equalized histogram iv) sketch equalization transformation curve [1+1+3+1]
Worked answer (deck 05)
F23 Q2(a) · Qubits.png6 marks

4×4, 5-bit image. (i) histogram (ii) compute and sketch the normalized histogram (iii) compute and sketch the equalized histogram (iv) sketch the transformation curve.

Given: 5-bit image, so L = 25 = 32 and L − 1 = 31. Total pixels N = 16.

16301018
251012
161395
1914280
input image

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk012345678910111213141516171819202122232425262728293031
nk21000100011011102011000001001010

Check: Σ nk = 2 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 2 + 1 + 1 + 1 + 1 + 1 = 16.

122011151911011211311421611811912512813031rknk
Original histogram (nk against rk)
0.060.120.1200.0610.0650.0690.06100.06120.06130.06140.12160.06180.06190.06250.06280.063031rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 31; round off (.5 goes up). Only the intensities with non-zero occurrence are listed, as the slide says.

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
020.12500.125031 × 0.1250 = 3.87504
110.06250.187531 × 0.1875 = 5.81256
510.06250.250031 × 0.2500 = 7.75008
910.06250.312531 × 0.3125 = 9.687510
1010.06250.375031 × 0.3750 = 11.625012
1210.06250.437531 × 0.4375 = 13.562514
1310.06250.500031 × 0.5000 = 15.500016
1410.06250.562531 × 0.5625 = 17.437517
1620.12500.687531 × 0.6875 = 21.312521
1810.06250.750031 × 0.7500 = 23.250023
1910.06250.812531 × 0.8125 = 25.187525
2510.06250.875031 × 0.8750 = 27.125027
2810.06250.937531 × 0.9375 = 29.062529
3010.06251.000031 × 1.0000 = 31.000031

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
042
161
581
9101
10121
12141
13161
14171
16212
18231
19251
25271
28291
30311

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
422
611
811
1011
1211
1411
1611
1711
2122
2311
2511
2711
2911
3111
120241618110112114116117221123125127129131sknk
Equalized histogram (n against new level sk)
048121620242831048121620242831input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Equalized output image: replace every pixel r by its s.

16301018
251012
161395
1914280
input
21311223
276414
2116108
2517294
equalized output

Try it, step by step: F23 (5-bit): only the levels that occur get a table row.

SP23 2bCLO1 · Histogram concepts · 5
What is histogram of an image? Write the four basic intensity characteristics of image and their corresponding histograms. [1+2+2]
Worked answer (deck 05)
SP23 Q2(b) · Fall 25 Quiz (D) Q2(a)4–5 marks

What is the histogram of an image? Explain the four basic intensity characteristics of an image and their corresponding histograms.

Write the definition and h(rk) = nk as above, then the four types with their four sketches. Add the meaning of each:

  • Dark image: most pixels have low values, so the histogram sits on the left; the image looks dark and details in shadows are hidden.
  • Bright image: most pixels have high values, so the histogram sits on the right; the image looks washed out.
  • Low-contrast image: pixels occupy a narrow range of gray levels, so the histogram is narrow; the image looks dull and gray.
  • High-contrast image: pixels cover almost the whole range and the histogram is spread fairly evenly; the image shows a lot of gray-level detail.
SP23 3bCLO2 · Histogram equalization · 8
4x4 image, 4-bit gray values [1,2,4,7,2,1,3,1,5,2,1,4] (only 12 printed; student wrote 7,6,3,1). i) Compute and sketch histogram and normalized histogram ii) equalized histogram iii) equalization transformation function. [3+3+2]
Worked answer (F25 quiz page)
SP23 Q3(b)8 marks

4-bit image, so L = 16 and the levels are 0 to 15. MN = 16. The last row (7 6 3 1) is how the student copy reads.

i) Histogram and normalized histogram: count each level, p(rk) = nk/16.

2.550513223241516278gray levelnk
histogram h(rk) = nk (levels 9–15 are 0)

iii) Transformation sk = round(15 · CDF), .5 rounds up:

rknkp(rk)CDF15 · CDFsk
155/16 = 0.31255/16 = 0.312515 × 0.3125 = 4.68755
233/16 = 0.18758/16 = 0.500015 × 0.5000 = 7.50008
322/16 = 0.125010/16 = 0.625015 × 0.6250 = 9.37509
422/16 = 0.125012/16 = 0.750015 × 0.7500 = 11.250011
511/16 = 0.062513/16 = 0.812515 × 0.8125 = 12.187512
611/16 = 0.062514/16 = 0.875015 × 0.8750 = 13.125013
722/16 = 0.125016/16 = 1.000015 × 1.0000 = 15.000015

ii) Equalized histogram: move each count to its new level sk.

2.550553829211112113215gray levelnk
equalized histogram: the counts now spread from 5 to 15

Mapping: 1→5, 2→8, 3→9, 4→11, 5→12, 6→13, 7→15. The levels spread over the whole 0–15 range, so contrast goes up.

Try it, step by step: the same 4-bit image, step by step.

F22 A2bCLO1 · Histogram concepts · 3CLO1 · Histogram equalization definition / steps / effect · 2
What is histogram of an image? How does the histogram differ for dark, bright, low contrast, high contrast image of the same scene? Write the steps of histogram equalization algorithm. [1+2+2]
Worked answer (deck 05)
F22 A2(b)5 marks

What is the histogram of an image? How does the histogram differ for dark, bright, low-contrast and high-contrast images of the same scene? Write the steps of the histogram equalization algorithm. [1+2+2]

Definition and four types as above. Algorithm (input: image f with L levels):

  1. Compute the histogram h(rk) = nk.
  2. Compute the PDF p(rk) = nk/N.
  3. Compute the CDF, the cumulative sum of the PDF.
  4. Compute sk = (L − 1) × CDF(rk).
  5. Round sk to get the new intensity values, and replace every rk by sk.
F22 A3bCLO2 · Histogram equalization · 8
Histogram table r=0..7, n=[790,1023,850,656,329,245,122,81]. i) compute/sketch normalized histogram ii) compute/sketch equalized histogram iii) mapping of new gray levels into number of pixels iv) plot transformation curve. [2+4+1+1]
Worked answer (deck 05)
F22 A3(b) · same counts as slide 498 marks

Histogram r = 0..7, n = [790, 1023, 850, 656, 329, 245, 122, 81]. (i) normalized histogram (ii) equalized histogram (iii) mapping of new gray levels into number of pixels (iv) transformation curve.

Here, as on slide 49, the PDF is rounded to 2 decimal places before the CDF.

Given: 3-bit image, so L = 23 = 8 and L − 1 = 7. Total pixels N = 4096.

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk01234567
nk790102385065632924512281

Check: Σ nk = 790 + 1023 + 850 + 656 + 329 + 245 + 122 + 81 = 4096.

511.5102379001023185026563329424551226817rknk
Original histogram (nk against rk)
0.120.250.1900.2510.2120.1630.0840.0650.0360.027rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 7; round off (.5 goes up).

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
07900.190.197 × 0.19 = 1.331
110230.250.447 × 0.44 = 3.083
28500.210.657 × 0.65 = 4.555
36560.160.817 × 0.81 = 5.676
43290.080.897 × 0.89 = 6.236
52450.060.957 × 0.95 = 6.657
61220.030.987 × 0.98 = 6.867
7810.021.007 × 1.00 = 7.007

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
01790
131023
25850
36656
46329
57245
67122
7781

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
1790790
310231023
5850850
6656 + 329 = 985985
7245 + 122 + 81 = 448448
511.51023079012102334850598564487sknk
Equalized histogram (n against new level sk)
0123456701234567input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Try it, step by step: F22 with the PDF rounded to 2 places, as on slide 49.

SP22 2bCLO2 · Histogram equalization · 8
4x4 image, 5-bit [(11,10,12,7),(6,8,7,7),(5,6,11,11),(9,9,7,7)]. i) histogram [1] ii) normalized histogram [2] iii) equalized histogram [4] iv) transformation curve [1].
Worked answer (deck 05)
SP22 Q2(b)8 marks

4×4, 5-bit image. (i) histogram (ii) normalized histogram (iii) equalized histogram (iv) transformation curve.

Given: 5-bit image, so L = 25 = 32 and L − 1 = 31. Total pixels N = 16.

1110127
6877
561111
9977
input image

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk012345678910111213141516171819202122232425262728293031
nk00000125121310000000000000000000

Check: Σ nk = 1 + 2 + 5 + 1 + 2 + 1 + 3 + 1 = 16.

2.550152657182911031111231rknk
Original histogram (nk against rk)
0.160.3100.0650.1260.3170.0680.1290.06100.19110.061231rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 31; round off (.5 goes up). Only the intensities with non-zero occurrence are listed, as the slide says.

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
510.06250.062531 × 0.0625 = 1.93752
620.12500.187531 × 0.1875 = 5.81256
750.31250.500031 × 0.5000 = 15.500016
810.06250.562531 × 0.5625 = 17.437517
920.12500.687531 × 0.6875 = 21.312521
1010.06250.750031 × 0.7500 = 23.250023
1130.18750.937531 × 0.9375 = 29.062529
1210.06251.000031 × 1.0000 = 31.000031

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
521
662
7165
8171
9212
10231
11293
12311

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
211
622
1655
1711
2122
2311
2933
3111
2.5501226516117221123329131sknk
Equalized histogram (n against new level sk)
048121620242831048121620242831input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Equalized output image: replace every pixel r by its s.

1110127
6877
561111
9977
input
29233116
6171616
262929
21211616
equalized output

Try it, step by step: SP22 (5-bit), every step.

F21 B3aCLO2 · Histogram equalization · 8
Enigma.png 4x4 5-bit [[16,30,8,10],[25,1,12,0],[6,13,9,5],[19,14,29,4]]. i) What is histogram? Calculate histogram [2] ii) normalized histogram [2] iii) equalized histogram [3] iv) transformation curve [1].
Worked answer (deck 05)
F21 B3(a) · Enigma.png8 marks

(i) What is a histogram? Calculate it. (ii) normalized histogram (iii) equalized histogram (iv) transformation curve. 4×4, 5-bit image.

Histogram: a graphical representation of the distribution of pixel intensity values, h(rk) = nk.

Given: 5-bit image, so L = 25 = 32 and L − 1 = 31. Total pixels N = 16.

1630810
251120
61395
1914294
input image

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk012345678910111213141516171819202122232425262728293031
nk11001110111011101001000001000110

Check: Σ nk = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 16.

0.511011141516181911011211311411611912512913031rknk
Original histogram (nk against rk)
0.030.060.0600.0610.0640.0650.0660.0680.0690.06100.06120.06130.06140.06160.06190.06250.06290.063031rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 31; round off (.5 goes up). Only the intensities with non-zero occurrence are listed, as the slide says.

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
010.06250.062531 × 0.0625 = 1.93752
110.06250.125031 × 0.1250 = 3.87504
410.06250.187531 × 0.1875 = 5.81256
510.06250.250031 × 0.2500 = 7.75008
610.06250.312531 × 0.3125 = 9.687510
810.06250.375031 × 0.3750 = 11.625012
910.06250.437531 × 0.4375 = 13.562514
1010.06250.500031 × 0.5000 = 15.500016
1210.06250.562531 × 0.5625 = 17.437517
1310.06250.625031 × 0.6250 = 19.375019
1410.06250.687531 × 0.6875 = 21.312521
1610.06250.750031 × 0.7500 = 23.250023
1910.06250.812531 × 0.8125 = 25.187525
2510.06250.875031 × 0.8750 = 27.125027
2910.06250.937531 × 0.9375 = 29.062529
3010.06251.000031 × 1.0000 = 31.000031

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
021
141
461
581
6101
8121
9141
10161
12171
13191
14211
16231
19251
25271
29291
30311

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
211
411
611
811
1011
1211
1411
1611
1711
1911
2111
2311
2511
2711
2911
3111
0.51012141618110112114116117119121123125127129131sknk
Equalized histogram (n against new level sk)
048121620242831048121620242831input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Equalized output image: replace every pixel r by its s.

1630810
251120
61395
1914294
input
23311216
274172
1019148
2521296
equalized output

Try it, step by step: F21 (5-bit), every step.

F25 quiz Q1D 1CLO1 · Histogram concepts · 6
How to generate an image Histogram? What does an image Histogram say? [3+3=6]
Worked answer (F25 quiz page)
Fall 25 Quiz (set D) Q16 marks

How do you generate an image histogram? What does an image histogram say? [3+3]

Generate: (1) find the number of gray levels L = 2k and make one bin per level 0 … L−1; (2) scan the image and, for each pixel with value rk, add 1 to bin h(rk); (3) plot nk against rk as bars. Σ h = total number of pixels.

Example (slide 7, 3-bit 4×4): h = [1, 1, 3, 5, 1, 2, 1, 2].

2.551011325314251627rknk
h(rk) for the slide 7 image

What it says: how many pixels have each intensity, i.e. the tonal distribution. From its shape we can tell whether the image is dark, bright, low- or high-contrast; judge the overall quality and the possibility of enhancement; check whether the image was scanned properly; and choose a threshold for object detection and segmentation. It gives only statistical information and no indication of pixel locations.

Try it: pick an image type. The page makes a small image of that type and counts its histogram from scratch, the same way as in steps 1–3.

F25 quiz Q1D 2aCLO1 · Histogram concepts · 4
Explain the four basic types of intensity characteristics of an image and their corresponding histograms. [4]
Worked answer (F25 quiz page)
Fall 25 Quiz 1 (set D) Q2(a) · also SP23 Q2(b)4 marks

Explain the four basic types of intensity characteristics of an image and their corresponding histograms.

The histogram of a digital image with gray levels 0 … L−1 is h(rk) = nk, the number of pixels with gray level rk. Its shape shows the four basic types:

7149014111263341567gray levelnk
(1) Dark image
7140112336411514697gray levelnk
(2) Bright image
102001421632044567gray levelnk
(3) Low-contrast image
365061625364655657gray levelnk
(4) High-contrast image
  1. Dark image: most pixels have low values, so the histogram is crowded on the left (dark) side. The image looks dark and details in the shadows are hidden.
  2. Bright image: most pixels have high values, so the histogram is crowded on the right side. The image looks washed out.
  3. Low-contrast image: the pixels occupy a narrow range of gray levels in the middle, so the histogram is narrow. The image looks dull and grayish.
  4. High-contrast image: the pixels cover almost the whole range, and the histogram is spread fairly evenly. The image shows a lot of gray-level detail and looks sharp.

Histogram equalization tries to turn types 1–3 into type 4.

Try it: click each type: a fresh random image of that kind and its counted histogram. Compare with the four sketches.

F25 quiz Q1D 2bCLO2 · Histogram equalization · 10
3-bit 8x8 image, histogram counts {0:2, 1:3, 2:5, 3:6, 4:9, 5:12, 6:14, 7:13}. i) Sketch normalized histogram [2] ii) Compute and sketch the equalized histogram [2+4=6] iii) Sketch the transformation function curve [2].
Worked answer (F25 quiz page)
Fall 25 Quiz (set D) Q2(b)10 marks

A 3-bit 8×8 image has histogram counts {{0:2, 1:3, 2:5, 3:6, 4:9, 5:12, 6:14, 7:13}}. (i) Sketch the normalized histogram (ii) compute and sketch the equalized histogram (iii) sketch the transformation function curve.

Given: 3-bit image, so L = 23 = 8 and L − 1 = 7. Total pixels N = 64.

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk01234567
nk23569121413

Check: Σ nk = 2 + 3 + 5 + 6 + 9 + 12 + 14 + 13 = 64.

7142031526394125146137rknk
Original histogram (nk against rk)
0.110.220.0300.0510.0820.0930.1440.1950.2260.207rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 7; round off (.5 goes up).

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
020.03120.03127 × 0.0312 = 0.21880
130.04690.07817 × 0.0781 = 0.54691
250.07810.15627 × 0.1562 = 1.09381
360.09380.25007 × 0.2500 = 1.75002
490.14060.39067 × 0.3906 = 2.73443
5120.18750.57817 × 0.5781 = 4.04694
6140.21880.79697 × 0.7969 = 5.57816
7130.20311.00007 × 1.0000 = 7.00007

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
002
113
215
326
439
5412
6614
7713

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
022
13 + 5 = 88
266
399
41212
61414
71313
714208162931245146137sknk
Equalized histogram (n against new level sk)
0123456701234567input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Try it, step by step: Fall 25 quiz set D: counts are given, so it starts at the table.

F25 quiz Q2C 1CLO1 · Histogram concepts · 6
What is the histogram of an image? Two images of size 20x20 with equal numbers of black (0) and white (1) pixels: are their histograms identical? Explain. [2+4=6]
Worked answer (F25 quiz page)
Fall 25 Quiz 2 (set C) Q12 + 4 marks

What is the histogram of an image? Two 20×20 images have equal numbers of black (0) and white (1) pixels. Are their histograms identical? Explain.

Histogram. For a digital image with gray levels in [0, L−1], the histogram is the discrete function

h(rk) = nk, k = 0, 1, …, L−1

where nk is the number of pixels with gray level rk. It is drawn as bars of nk against rk. The normalized histogram is p(rk) = nk / MN.

The two images. Each has 20 × 20 = 400 pixels, and equal numbers of black and white, so 200 black and 200 white. For both images:

h(0) = 200, h(1) = 200
1002002000 (black)2001 (white)gray levelnk
Histogram of image 1 = histogram of image 2

Yes, the histograms are identical, even if one image is half black and half white and the other is a checkerboard. A histogram records only how many pixels have each value, not where they are. It has no spatial information.

So different images can have the same histogram, and an image cannot be rebuilt from its histogram.

Try it: rearrange the same 200 black and 200 white pixels. The picture changes; the counted histogram never does.

F25 quiz Q2C 2CLO2 · Histogram equalization · 14
4x5, 3-bit image [1,2,4,7,0,2,1,3,1,5,5,2,1,4,1,7,6,3,1,4]. i) Draw histogram [2] ii) Sketch normalized histogram [2] iii) Compute and sketch equalized histogram [2+4=6] iv) Sketch transformation function curve [2] v) Show the equalized output image [2].
Worked answer (F25 quiz page)
Fall 25 Quiz (set C) Q214 marks

A 4×5, 3-bit image [1,2,4,7,0; 2,1,3,1,5; 5,2,1,4,1; 7,6,3,1,4]. (i) Draw the histogram (ii) sketch the normalized histogram (iii) compute and sketch the equalized histogram (iv) sketch the transformation function curve (v) show the equalized output image.

Given: 3-bit image, so L = 23 = 8 and L − 1 = 7. Total pixels N = 20.

12470
21315
52141
76314
input image

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk01234567
nk16323212

Check: Σ nk = 1 + 6 + 3 + 2 + 3 + 2 + 1 + 2 = 20.

361061322334251627rknk
Original histogram (nk against rk)
0.150.300.0500.3010.1520.1030.1540.1050.0560.107rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 7; round off (.5 goes up).

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
010.05000.05007 × 0.0500 = 0.35000
160.30000.35007 × 0.3500 = 2.45002
230.15000.50007 × 0.5000 = 3.50004
320.10000.60007 × 0.6000 = 4.20004
430.15000.75007 × 0.7500 = 5.25005
520.10000.85007 × 0.8500 = 5.95006
610.05000.90007 × 0.9000 = 6.30006
720.10001.00007 × 1.0000 = 7.00007

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
001
126
243
342
453
562
661
772

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
011
266
43 + 2 = 55
533
62 + 1 = 33
722
3610162354353627sknk
Equalized histogram (n against new level sk)
0123456701234567input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Equalized output image: replace every pixel r by its s.

12470
21315
52141
76314
input
24570
42426
64252
76425
equalized output

Try it, step by step: Fall 25 quiz set C, every step.

More worked examples: slides and other examiners (11)
Slides 6–7 · 3-bit 4×4 example (deck 05)
Slides 6–7 · 3-bit 4×4 exampleworked

Given: 3-bit image, so L = 23 = 8 levels (0–7). One bin per level: h(0) … h(7).

5533
4333
0672
1722
image f

Count each value: 0 appears once, 1 once, 2 three times, 3 five times, 4 once, 5 twice, 6 once, 7 twice.

rk01234567
h(rk)11351212
2.551011325314251627intensity levelnk
Histogram: one bar per gray level

Sum of all bars = 16 = number of pixels. The slide writes this as Σ h(x) = area of the image.

Try it, step by step: count each gray level of the slide 7 image and watch its bar grow.

Slide 8 · Class practice (deck 05)
Slide 8 · Class practicesolved

sample.bmp is a 4×4 image with 6-bit gray values. Calculate its histogram and sketch it.

20211560
60156015
15602115
15201520
sample.bmp (6-bit, levels 0–63)

n = 16 pixels. Only four values occur:

rk15202160all other levels
h(rk)73240
3.57715320221460input intensitynk
Sketch: bars at 15, 20, 21, 60; every other level 0 to 63 has height 0.

Try it, step by step: the 6-bit sample.bmp: only 4 levels occur, all other bins stay 0.

Slide 19 · worked (deck 05)
Slide 19 · workedworked
9898
2747
6461
4074
image (cropped), n = 16
bin0123456789
h(rk)1110402322
p(rk)1/161/161/1604/1602/163/162/162/16
0.1250.250.062500.062510.0625230.25450.12560.18870.12580.1259gray levelp(rk)
Normalized histogram; bars add to 1

Try it, step by step: count, then divide each count by N = 16.

Slide 20 · Class work (deck 05)
Slide 20 · Class worksolved

sample.bmp is a 4×4 image with 8-bit gray values. Calculate its normalized histogram (PDF).

2021015060
6015060150
15060210150
1502015020
sample.bmp (8-bit)

n = 16. Counts: 20 → 3, 60 → 4, 150 → 7, 210 → 2; all other levels 0.

rknkp(rk) = nk/16
2033/16 = 0.1875
6044/16 = 0.2500
15077/16 = 0.4375
21022/16 = 0.1250
sum161.0000
0.21880.43750.1875200.25600.43751500.125210gray levelp(rk)
PDF of sample.bmp
Mini illustration (3-bit, so 7 replaces 255) (deck 05)
Mini illustration (3-bit, so 7 replaces 255)worked

Levels used: 2 to 4. g = 7(f − 2)/(4 − 2): 2 → 0, 3 → 3.5 → 4, 4 → 7.

4.5901529364567gray levelnk
Before: narrow, low contrast
4.5950123945667gray levelnk
After stretching: spread over 0–7, but the same 3 bars with gaps between
Slides 36–40 · 3-bit 4×5 example (deck 05)
Slides 36–40 · 3-bit 4×5 exampleworked

Given: 3-bit image, so L = 23 = 8 and L − 1 = 7. Total pixels N = 20.

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk01234567
nk16323212

Check: Σ nk = 1 + 6 + 3 + 2 + 3 + 2 + 1 + 2 = 20.

361061322334251627rknk
Original histogram (nk against rk)
0.150.300.0500.3010.1520.1030.1540.1050.0560.107rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 7; round off (.5 goes up).

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
010.05000.05007 × 0.0500 = 0.35000
160.30000.35007 × 0.3500 = 2.45002
230.15000.50007 × 0.5000 = 3.50004
320.10000.60007 × 0.6000 = 4.20004
430.15000.75007 × 0.7500 = 5.25005
520.10000.85007 × 0.8500 = 5.95006
610.05000.90007 × 0.9000 = 6.30006
720.10001.00007 × 1.0000 = 7.00007

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
001
126
243
342
453
562
661
772

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
011
266
43 + 2 = 55
533
62 + 1 = 33
722
3610162354353627sknk
Equalized histogram (n against new level sk)
0123456701234567input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Try it, step by step: the whole slide 36–40 example: count, the 5 table steps per level, the equalized histogram, then the output image pixel by pixel.

Slide 59 · Home work (deck 05)
Slide 59 · Home workproof

An image is histogram-equalized. Show that a second pass of histogram equalization produces exactly the same result as the first pass.

After the first pass, every input level rk has been moved to sk = round((L−1)·CDF(rk)). The order of levels is kept, since the CDF never decreases.

In the equalized image, the number of pixels at or below level sk is exactly the number of original pixels at or below rk (for the last rk that maps to sk). So the CDF of the new image at sk equals the old CDF at rk.

The second pass therefore computes round((L−1)·CDF) from the same CDF values and gets the same sk again. Each level maps to itself: the second mapping is the identity, and the image does not change.

Numeric check with the slide 36 result: equalized counts at s = 0, 2, 4, 5, 6, 7 are 1, 6, 5, 3, 3, 2. A second equalization gives new levels 0→0, 2→2, 4→4, 5→5, 6→6, 7→7, every level unchanged.

Slides 60–61 · Class work (deck 05)
Slides 60–61 · Class worksolved

Equalize the given histogram: r = 0..7, n = [100, 90, 50, 20, 0, 0, 0, 0].

Given: 3-bit image, so L = 23 = 8 and L − 1 = 7. Total pixels N = 260.

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk01234567
nk1009050200000

Check: Σ nk = 100 + 90 + 50 + 20 = 260.

5010010009015022034567rknk
Original histogram (nk against rk)
0.190.380.3800.3510.1920.0834567rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 7; round off (.5 goes up). Only the intensities with non-zero occurrence are listed, as the slide says.

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
01000.38460.38467 × 0.3846 = 2.69233
1900.34620.73087 × 0.7308 = 5.11545
2500.19230.92317 × 0.9231 = 6.46156
3200.07691.00007 × 1.0000 = 7.00007

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
03100
1590
2650
3720

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
3100100
59090
65050
72020
5010001210034905506207sknk
Equalized histogram (n against new level sk)
0123456701234567input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Try it, step by step: the slide 60 class work.

Slides 49–53 · 3-bit, 64×64 (MN = 4096) (deck 05)
Slides 49–53 · 3-bit, 64×64 (MN = 4096)worked

Get the histogram transformation function and make the output image follow the specified histogram pz = [0, 0, 0, 0.15, 0.20, 0.30, 0.20, 0.15].

Step 1, equalize the original histogram to get Hk.

rknkPDFCDF(L−1)·CDFHk
07900.190.191.331
110230.250.443.083
28500.210.654.555
36560.160.815.676
43290.080.896.236
52450.060.956.657
61220.030.986.867
7810.021.007.007

Step 2, equalize the specified histogram to get Sk.

zqpz(zq)CDF(L−1)·CDFSk
00.000.000.000
10.000.000.000
20.000.000.000
30.150.151.051
40.200.352.452
50.300.654.555
60.200.855.956
70.151.007.007

Step 3, relate the two. For each rk, take its Hk, find the S value closest to it, and map rk to that z. If two S values are equally close, take the higher one (slide 55). If several z share that S value, take the first such z.

rkHkclosest Smap to z
0113
1324
2555
3666
4666
5777
6777
7777

Output histogram: add the pixels that land on each z.

zpixels addedn(z)
0–0
1–0
2–0
3790790
410231023
5850850
6656 + 329 = 985985
7245 + 122 + 81 = 448448
511.5102379001023185026563329424551226817rknk
Original histogram
511.51023012790310234850598564487zqnk
Output (specified) histogram
0123456701234567input rkoutput zq
Transformation function rk → zq

Compare with the slide: the actual output histogram has p(z) = 0.00, 0.00, 0.00, 0.19, 0.25, 0.21, 0.24, 0.11, close to the specified one, though not exact, because levels are discrete.

Try it, step by step: all three matching steps for slide 49: equalize the input, equalize the target, then relate them level by level.

Slides 55–58 · second example (N = 64) (deck 05)
Slides 55–58 · second example (N = 64)worked

Original histogram n = [8, 10, 10, 2, 12, 16, 4, 2]. Desired histogram n = [0, 0, 0, 0, 20, 20, 16, 8]. Find the mapping and the output histogram.

Step 1, equalize the original histogram to get Hk.

rknkPDFCDF(L−1)·CDFHk
080.120.120.841
1100.160.281.962
2100.160.443.083
320.030.473.293
4120.190.664.625
5160.250.916.376
640.060.976.797
720.031.007.007

Step 2, equalize the specified histogram to get Sk.

zqspecified npz(zq)CDF(L−1)·CDFSk
000.000.000.000
100.000.000.000
200.000.000.000
300.000.000.000
4200.310.312.172
5200.310.624.344
6160.250.876.096
780.121.007.007

Step 3, relate the two. For each rk, take its Hk, find the S value closest to it, and map rk to that z. If two S values are equally close, take the higher one (slide 55). If several z share that S value, take the first such z.

rkHkclosest Smap to z
0124
1224
2345
3345
4566
5666
6777
7777

Output histogram: add the pixels that land on each z.

zpixels addedn(z)
0–0
1–0
2–0
3–0
48 + 10 = 1818
510 + 2 = 1212
612 + 16 = 2828
74 + 2 = 66
81680101102231241654627rknk
Original histogram
1428012318412528667zqnk
Output (specified) histogram
0123456701234567input rkoutput zq
Transformation function rk → zq

Try it, step by step: slide 55, including the ties that go to the higher S.

Short note · continuous CDF (slide 62) (deck 05)
Short note · continuous CDF (slide 62)theory

For continuous intensities the equalizing transformation is s = T(r) = (L − 1) ∫0r pr(w) dw. In the discrete case it becomes s = T(r) = (L − 1) Σ p(rj) for j = 0 … k, i.e. (L − 1) times the CDF. This is why the algorithm multiplies the CDF by L − 1.

4. Spatial filters: smoothing and sharpening

decks 06–07 · 10 past questions
The pattern: how she asks it
  • Q2(b): one image, several tasks at a point: mean, weighted, median, Laplacian, then sharpen (SP25 2b, SP24 2b, F23 2b).
  • Q1: objectives of sharpening, derive the Laplacian from 2nd derivatives, draw 4 masks (SP25 1a, SP24 1b).
  • Q3 / short: salt-and-pepper means median; Gaussian noise means mean or weighted; median vs mean (F22 A1a, SP22 1a).

Connects to: Smoothing = average = blur, and it removes noise. Sharpening = derivative, which picks up edges. The same derivative masks become Sobel and Prewitt in segmentation.

typeCLOtimeswhere
Spatial filter at a pixelCLO27SP25 2b, SP24 2b, F23 2b, SP23 2f, SP23 3f, SP22 1a, F21 B4a
Sharpening: objectives, derivatives, Laplacian derivation & masksCLO14SP25 1a, SP24 1b, SP23 2f, SP23 3f
Smoothing, noise, median vs meanCLO14F23 2b, F22 A1a, SP22 1a, F21 B4a
Bit-plane slicing / rebuild image from planesCLO22SP25 2b, SP24 2b
Case: enhancement / filtering / segmentation pipeline choiceCLO31F22 A1a
Formula wall: every formula with a worked line

One window, every filter. Centre pixel = 200 (a noise spike):

102030
4020060
708090
window f
121
242
121
weights / 16
filterruleresult at the centre
mean (box)Σ / 9600 / 9 = 66.67 → 67
weighted averageΣ w·f / 161400 / 16 = 87.5
mediansort 9, take the 5th10 20 30 40 60 70 80 90 200 → 60 (spike gone)
min / maxsmallest / largest10 / 200

Laplacian sharpening (2nd derivative)

∇²f = f(x+1,y) + f(x−1,y) + f(x,y+1) + f(x,y−1) − 4f(x,y)
101010
105010
101010
f
010
1-41
010
mask (centre −4)

∇²f = 10 + 10 + 10 + 10 − 4×50 = -160. Centre negative, so g = f − ∇²f = 50 + 160 = 210: the bright dot gets brighter, detail stands out. One-pass mask: [0 −1 0; −1 5 −1; 0 −1 0].

Sobel gradient (1st derivative)

101010
101010
909090
f
-101
-202
-101
Gx
-1-2-1
000
121
Gy

Gx = (10 + 20 + 90) − (10 + 20 + 90) = 0. Gy = (90 + 180 + 90) − (10 + 20 + 10) = 320. |∇f| ≈ |Gx| + |Gy| = 320: a strong horizontal edge. Direction α = tan⁻¹(Gy / Gx).

Unsharp masking: mask = f − blurred f; g = f + k · mask. k = 1 unsharp, k > 1 high-boost.

1st derivative (gradient)2nd derivative (Laplacian)
thick edges, non-zero along rampsthin double edges, zero on ramps, zero crossing at the edge
less noise-sensitivevery noise-sensitive, strong on isolated points
gives directionno direction, one mask

Past questions: look, recall, then open the answer

SP25 1aCLO1 · Sharpening: objectives, derivatives, Laplacian derivation & masks · 4
What are the objectives of sharpening spatial filters? Derive the Laplacian operator using image derivatives and draw four Laplacian masks.
Worked answer (deck 07)

Same answer as SP23 2f (SP23 Q2(f)i · SP25 Q1(a) first part).

SP25 Q1(a)4 marks

What are the objectives of sharpening spatial filters? Derive the Laplacian operator using image derivatives and draw four Laplacian masks.

Objectives: highlight fine detail, remove blurring, highlight edges and emphasize intensity transitions (see above).

Derivation. The Laplacian of f(x,y) is the sum of the second derivatives in x and y:

∇²f = ∂²f/∂x² + ∂²f/∂y²

Using the forward 2nd differences:

∂²f/∂x² = f(x+1,y) + f(x−1,y) − 2f(x,y)

∂²f/∂y² = f(x,y+1) + f(x,y−1) − 2f(x,y)

Adding:

∇²f = f(x+1,y) + f(x−1,y) + f(x,y+1) + f(x,y−1) − 4f(x,y)

As masks: the horizontal [1 −2 1] plus the vertical [1; −2; 1] give

000
1-21
000
x part
010
0-20
010
y part
010
1-41
010
= positive Laplacian

This is the positive Laplacian, from forward derivatives. With the backward form 2f(x) − f(x−1) − f(x+1) we get ∇²f = 4f(x,y) − f(x−1,y) − f(x+1,y) − f(x,y−1) − f(x,y+1), the negative Laplacian:

0-10
-14-1
0-10
negative Laplacian

Four Laplacian masks (Figure 3.39): two 4-neighbour and two that include the diagonal neighbours:

010
1-41
010
(a) 4-nbr, centre −4
111
1-81
111
(b) 8-nbr, centre −8
0-10
-14-1
0-10
(c) 4-nbr, centre 4
-1-1-1
-18-1
-1-1-1
(d) 8-nbr, centre 8

In all cases the sum of the values in the filter is zero, so a flat area gives 0 and only changes produce a response.

Try it, step by step: the four Laplacian masks on the slide 26 image. Notice the positive and negative masks give the same number with opposite sign.

SP25 2bCLO2 · Bit-plane slicing / rebuild image from planes · 2CLO2 · Spatial filter at a pixel · 6
7x7 image segment, 3 bits/pixel; binary images of bit plane 2, 1, 0 given. i) Construct the original image from the bit planes [2]. ii) At the (3,2) point of the original image: Task 1 perform a 3x3 Max filter [2]; Task 2 perform a 3x3 Negative Laplacian filter [2]; Task 3 design a 3x3 spatial mask that averages the 4 neighbors of a point (x,y) but excludes the point itself [2].
Worked answer (deck 04 · deck 06 · deck 07)
SP25 Q2(b)i · SP24 Q2(b)i2–3 marks

A 7×7 image segment has 3 bits per pixel. The binary images of bit planes 2, 1 and 0 are given. Construct the original image.

1111111
1110111
1100011
1000001
1100011
0000000
1111111
bit plane 2 (MSB)
1111111
1111111
1000001
0000000
0000000
1111111
1111111
bit plane 1
1111111
1110111
1100011
1110111
1100011
1110111
1111111
bit plane 0 (LSB)

Rule: in a 3-bit image the planes are 2, 1, 0 with weights 22 = 4, 21 = 2, 20 = 1. So

f(x,y) = 4·b2(x,y) + 2·b1(x,y) + 1·b0(x,y)

Every bit pattern that occurs:

b2 b1 b0workingvalue
1 1 14·1 + 2·1 + 1·17
1 0 14·1 + 2·0 + 1·15
0 1 14·0 + 2·1 + 1·13
0 1 04·0 + 2·1 + 1·02
0 0 14·0 + 2·0 + 1·11
0 0 04·0 + 2·0 + 1·00

Example: row 2, column 1 has bits 1, 0, 1, so 4 + 0 + 1 = 5.

7777777
7772777
7500057
5110115
5500055
3332333
7777777
reconstructed original image
SP25 Q2(b)ii · Tasks 1 and 32 + 2 marks

At point (3,2) of the reconstructed 7×7 image: Task 1, perform a 3×3 Max filter. Task 3, design a 3×3 spatial mask that averages the 4 neighbours of a point (x,y) but excludes the point itself.

Original image (rebuilt from bit planes; see the deck 04 page):

Task 1

000000000
077777770
077727770
075000570
051101150
055000550
033323330
077777770
000000000
window at (3,2); zero padding outside

3×3 max: sorted 0, 0, 0, 0, 0, 1, 1, 5, 5 → largest = 5

Task 2 (negative Laplacian, deck 07):

000000000
077777770
077727770
075000570
051101150
055000550
033323330
077777770
000000000
window at (3,2); zero padding outside
500
110
500
window
0-10
-14-1
0-10
negative Laplacian

Laplacian response: (-1)(0) + (-1)(1) + (4)(1) + (-1)(0) + (-1)(0) = 3

Task 3. The 4 neighbours are up, down, left and right. Give each weight 1, the centre 0, the corners 0, and divide by 4 so the weights add to 1:

010
101
010
1/4 ×
g(x,y) = ¼ [ f(x−1,y) + f(x+1,y) + f(x,y−1) + f(x,y+1) ]

Try it, step by step: SP25 at (3,2): max, negative Laplacian, and the 4-neighbour mask.

SP25 Q2(b)ii Task 2 · negative Laplacian at (3,2)2 marks
000000000
077777770
077727770
075000570
051101150
055000550
033323330
077777770
000000000
window at (3,2)
500
110
500
window
0-10
-14-1
0-10
negative Laplacian

Σ w·z = (-1)(0) + (-1)(1) + (4)(1) + (-1)(0) + (-1)(0) = 3

Negative Laplacian response at (3,2) = 3.

Try it, step by step: SP25 Task 2 at (3,2).

SP24 1bCLO1 · Sharpening: objectives, derivatives, Laplacian derivation & masks · 5
Define the first-order and second-order discrete derivatives of a 1-D digital image function f(x). What kind of relation can be obtained between first- and second-order derivatives based on edge productions that show a transition like a ramp of constant slope? [3+2]
Worked answer (deck 07 · deck 08)
SP24 Q1(b)3 + 2 marks

Define the first-order and second-order discrete derivatives of a 1-D digital function f(x). What relation can be obtained between first- and second-order derivatives for an edge that is a ramp of constant slope?

First-order derivative (difference of consecutive pixels): ∂f/∂x = f(x+1) − f(x).

Second-order derivative (difference of differences): ∂²f/∂x² = [f(x+1) − f(x)] − [f(x) − f(x−1)] = f(x+1) + f(x−1) − 2f(x).

Relation along a ramp of constant slope (strip values 5 5 4 3 2 1 0 0):

  • The 1st derivative is nonzero along the entire ramp (−1 at every step of the ramp) and zero in flat areas, so it produces thick edges.
  • The 2nd derivative is zero along the ramp and nonzero only at its onset and end, with opposite signs (−1 at the start, +1 at the end). So it produces a double edge one pixel thick, separated by zeros, and the sign change (zero crossing) marks the edge location.
  • The 2nd derivative also responds much more strongly to fine detail such as an isolated point (6, −12, 6 against −6 for the 1st), which is why it's preferred for sharpening.

[Draw the strip and the two derivative graphs above.]

SP24 Final Q1(b)3 + 2 marks

Define the first-order and second-order discrete derivatives of a 1-D digital image function f(x). What relation can be obtained between first- and second-order derivatives for an edge that is a ramp of constant slope?

∂f/∂x = f(x + 1) − f(x)      ∂²f/∂x² = f(x + 1) + f(x − 1) − 2f(x)

Example ramp 5 5 4 3 2 1 0 0: 1st derivative 0 −1 −1 −1 −1 −1 0; 2nd derivative −1 0 0 0 0 1.

Relation on a ramp: (1) the 1st derivative is nonzero (constant) along the entire ramp, so it gives a thick edge; (2) the 2nd derivative is nonzero only at the onset and end of the ramp, with opposite signs (a double edge), and zero along the ramp; (3) the line joining those two values crosses zero near the middle of the ramp: this zero crossing marks the edge centre, where the 1st derivative is largest. The sign of the 2nd derivative also tells whether the edge goes dark→light or light→dark.

SP24 2bCLO2 · Bit-plane slicing / rebuild image from planes · 3CLO2 · Spatial filter at a pixel · 6
7x7 image, 3 bits/pixel, bit planes 2,1,0 given (clean). i) Construct the original image from the bit planes. ii) At (3,3) of the original: Task 1 3x3 Max filter; Task 2 3x3 Positive Laplacian filter; Task 3 design a 3x3 mask averaging the 4 neighbors excluding the point itself. [3+6]
Worked answer (deck 04 · deck 06 · deck 07)

Same answer as SP25 2b (SP25 Q2(b)i · SP24 Q2(b)i).

SP24 Q2(b)ii · same image, point (3,3)6 marks

At (3,3): Task 1, 3×3 Max filter; Task 2, 3×3 positive Laplacian; Task 3, the 4-neighbour averaging mask.

Task 1

000000000
077777770
077727770
075000570
051101150
055000550
033323330
077777770
000000000
window at (3,3); zero padding outside

3×3 max: sorted 0, 0, 0, 0, 0, 0, 0, 1, 1 → largest = 1

Task 2

000000000
077777770
077727770
075000570
051101150
055000550
033323330
077777770
000000000
window at (3,3); zero padding outside
000
101
000
window
010
1-41
010
positive Laplacian

Laplacian response: (1)(0) + (1)(1) + (-4)(0) + (1)(1) + (1)(0) = 2

Task 3: the same mask 1/4 [0 1 0; 1 0 1; 0 1 0] as above.

Try it, step by step: SP24 at (3,3).

SP24 Q2(b)ii Task 2 · positive Laplacian at (3,3)2 marks
000000000
077777770
077727770
075000570
051101150
055000550
033323330
077777770
000000000
window at (3,3)
000
101
000
window
010
1-41
010
positive Laplacian

Σ w·z = (1)(0) + (1)(1) + (-4)(0) + (1)(1) + (1)(0) = 2

Positive Laplacian response at (3,3) = 2.

Try it, step by step: SP24 Task 2 at (3,3).

F23 2bCLO1 · Smoothing, noise, median vs mean · 2CLO2 · Spatial filter at a pixel · 4
aust.png 5x5 with random 0 and 255 pixels: [[0,12,0,0,23],[255,145,200,110,17],[255,160,90,250,15],[0,255,130,185,15],[15,20,20,21,21]]; zero padding. i) Which noise affects aust.png? ii) Apply 3x3 Mean and 3x3 Median filter at point (1,1). iii) Is it possible to remove the noise by applying only the Max or Min filter? Justify. [1+3+2]
Worked answer (deck 06)
F23 Q2(b) · aust.png6 marks

aust.png (5×5) has random 0 and 255 pixels; zero padding. (i) Which noise affects aust.png? (ii) Apply a 3×3 mean and a 3×3 median filter at point (1,1). (iii) Is it possible to remove the noise using only a Max or a Min filter? Justify. [1+3+2]

(i) The image has random black (0) and white (255) pixels scattered among normal values: salt-and-pepper (impulse) noise.

(ii)

0000000
001200230
0255145200110170
025516090250150
00255130185150
015202021210
0000000
window at (1,1); zero padding outside

3×3 mean: (0 + 12 + 0 + 255 + 145 + 200 + 255 + 160 + 90) / 9 = 1117/9 = 124.11 ≈ 124

3×3 median: sorted: 0, 0, 12, 90, 145, 160, 200, 255, 255 → 5th value = 145

The mean (124) is dragged by the two 255s and the 0s; the median (145) is a real neighbouring value, and the extremes end up at the two ends of the sorted list.

(iii) No. A max filter picks the largest value, so it removes pepper (0) but spreads salt (255) to every neighbour of a white dot. A min filter removes salt but spreads pepper. Salt-and-pepper noise contains both at once, so either filter alone makes half of the noise worse. The median filter removes both, because both extremes are pushed to the ends of the sorted list.

Try it, step by step: F23 aust.png. Opens at (1,1); try all four filters.

SP23 2fCLO1 · Sharpening: objectives, derivatives, Laplacian derivation & masks · 2CLO2 · Spatial filter at a pixel · 3
i) What is the objective of sharpening spatial filters? ii) Convolving an image with 3x3 [0,0,0,0,1,0,0,0,0]: relation between original and modified image? [2+3]
Worked answer (deck 06 · deck 07)
SP23 Q2(f)ii · slides 48–493 marks

An image is convolved with the 3×3 mask [0 0 0; 0 1 0; 0 0 0]. What is the relation between the original and the modified image?

At every pixel, g(x,y) = 0·z1 + … + 1·z5 + … + 0·z9 = z5 = f(x,y). Only the centre coefficient is 1, and it multiplies the pixel itself.

So g(x,y) = f(x,y): the output is identical to the original (no change). This mask is the identity, also called the unit impulse. Its coefficients add to 1, so brightness is kept, and no neighbour is mixed in, so nothing is blurred or shifted.

1020304050
1121314151
1222324252
1323334353
1424344454
test image
000
010
000
mask
213141
223242
233343
output (inner 3×3) = same values
SP23 Q2(f)i · SP25 Q1(a) first part2 marks

What are the objectives of sharpening spatial filters?

Sharpening spatial filters highlight fine detail in an image. Their objectives are: (1) to remove blurring from images; (2) to highlight edges; (3) to emphasize transitions (sudden changes) in image intensity. They work by spatial differentiation (first- or second-order derivatives defined by differences), which responds strongly where intensity changes and gives zero in flat areas. Uses: photo enhancement, medical visualization, industrial defect detection, electronic printing, military guidance.

SP23 3fCLO2 · Spatial filter at a pixel · 6CLO1 · Sharpening: objectives, derivatives, Laplacian derivation & masks · 2
i) 3x3 image [2,5,8,5,1,3,4,7,2]: smooth with 3x3 average filter, zero padding; filtered output with calculations [3]. ii) 3x3 median filter on 4x4 [0,2,1,0,5,2,3,8,0,1,3,4,3,0,7,2]; ignore border, zero in output border [3]. iii) Derive a positive Laplacian filter mask to enhance an image in a single filtering operation [2].
Worked answer (deck 06 · deck 07)
SP23 Q3(f) i–ii · SP22 Q1(a)ii3 + 3 marks

(i) Smooth the 3×3 image [2 5 8; 5 1 3; 4 7 2] with a 3×3 average filter, zero padding; show the calculations. (ii) Apply a 3×3 median filter to the 4×4 image; ignore the border and put zero in the output border.

(i) Pad with a ring of zeros, then average every 3×3 window:

00000
02580
05130
04720
00000
zero-padded input
pixelsum of the 9 values in its windowaveragerounded
(0,0)0 + 0 + 0 + 0 + 2 + 5 + 0 + 5 + 113/9 = 1.441
(0,1)0 + 0 + 0 + 2 + 5 + 8 + 5 + 1 + 324/9 = 2.673
(0,2)0 + 0 + 0 + 5 + 8 + 0 + 1 + 3 + 017/9 = 1.892
(1,0)0 + 2 + 5 + 0 + 5 + 1 + 0 + 4 + 724/9 = 2.673
(1,1)2 + 5 + 8 + 5 + 1 + 3 + 4 + 7 + 237/9 = 4.114
(1,2)5 + 8 + 0 + 1 + 3 + 0 + 7 + 2 + 026/9 = 2.893
(2,0)0 + 5 + 1 + 0 + 4 + 7 + 0 + 0 + 017/9 = 1.892
(2,1)5 + 1 + 3 + 4 + 7 + 2 + 0 + 0 + 022/9 = 2.442
(2,2)1 + 3 + 0 + 7 + 2 + 0 + 0 + 0 + 013/9 = 1.441
258
513
472
input
132
343
221
smoothed output (rounded)

(ii) Only the 4 inner pixels have a full window:

0210
5238
0134
3072
input
pixelsorted windowmedian
(1,1)0, 0, 1, 1, 2, 2, 3, 3, 52
(1,2)0, 1, 1, 2, 2, 3, 3, 4, 82
(2,1)0, 0, 1, 2, 3, 3, 3, 5, 73
(2,2)0, 1, 2, 2, 3, 3, 4, 7, 83
0000
0220
0330
0000
median output (border = 0)

(Part iii, the single-pass Laplacian, is on the deck 07 page.)

Try it, step by step: SP23 (i): all 9 pixels with zero padding.

Try it, step by step: SP23 (ii): the 4 inner pixels, border ignored.

SP23 Q3(f)iii2 marks

Derive a positive Laplacian filter mask that enhances an image in a single filtering operation.

Positive Laplacian: ∇²f = f(x+1,y) + f(x−1,y) + f(x,y+1) + f(x,y−1) − 4f(x,y), mask [0 1 0; 1 −4 1; 0 1 0]. Its centre is negative (w5 = −4), so the enhanced image is g = f − ∇²f:

g(x,y) = f(x,y) − [f(x+1,y) + f(x−1,y) + f(x,y+1) + f(x,y−1) − 4f(x,y)]

= 5f(x,y) − f(x+1,y) − f(x−1,y) − f(x,y+1) − f(x,y−1)

0-10
-15-1
0-10
single-pass enhancement mask

Convolving the image with this one mask gives the sharpened image directly. Its coefficients add to 1, so flat areas keep their brightness.

F22 A1aCLO1 · Smoothing, noise, median vs mean · 3CLO3 · Case: enhancement / filtering / segmentation pipeline choice · 6
i) Explain how you can smooth an image. If you subtract the smoothed image from the original, what happens if you add the subtracted result back to the original? [3] ii) Group selfies of batch 'Integer 43' suffer salt-and-pepper noise. Apply the de-noising process: which filtering is most effective? How does it enhance aesthetics? [6]
Worked answer (deck 06)
F22 A1(a)3 + 6 marks

(i) Explain how you can smooth an image. If you subtract the smoothed image from the original, what happens if you add the result back to the original? (ii) Group selfies of batch "Integer 43" suffer from salt-and-pepper noise. Apply a de-noising process: which filtering is most effective, and how does it improve the photos?

(i) Smooth by convolving the image with a low-pass mask whose coefficients add to 1, e.g. the 3×3 mean 1/9 [all 1] or the weighted 1/16 [1 2 1; 2 4 2; 1 2 1]. Each pixel becomes the (weighted) average of its neighbourhood, which blurs and reduces noise.

Original − smoothed = the detail (mask) image: it holds only edges and fine detail, since the smooth parts cancel out. Adding this detail back to the original, g = f + (f − f̄), sharpens the image: edges and fine detail are emphasized. This is unsharp masking (with a weight k > 1 on the detail, high-boost filtering; see deck 07). It is the same as the slide 52–54 mask 2δ − box.

(ii) Use a 3×3 median filter.

  • Salt-and-pepper noise is random black (0) and white (255) dots. In each 3×3 window these extremes sort to the two ends of the list, so the median, a real neighbouring value, replaces them.
  • Unlike the mean filter, the median does not smear the dots into gray blotches, and it keeps the edges of faces, eyes and hair sharp, because it never creates new in-between values.
  • It introduces no new values and is very effective for impulse noise (slides 43–44). If the noise is dense, a second pass or a 5×5 median removes what's left, at the cost of a little extra blur.
  • Result: clean skin and background, sharp facial features, and natural-looking group photos.
SP22 1aCLO1 · Smoothing, noise, median vs mean · 5CLO2 · Spatial filter at a pixel · 4
i) Explain with an example how a spatial filter affects an image [2]. ii) 3x3 image [(2,5,8),(5,1,3),(4,7,2)]: apply 3x3 average filter with zero padding; filtered output? [4] iii) Why does median filter preserve more edge sharpness than average filter? [3]
Worked answer (deck 06)

Same answer as SP23 3f (SP23 Q3(f) i–ii · SP22 Q1(a)ii).

SP22 Q1(a) i, iii2 + 3 marks

(i) Explain with an example how a spatial filter affects an image. (iii) Why does the median filter preserve more edge sharpness than the average filter?

(i) A spatial filter consists of a neighbourhood and a predefined operation. The mask is moved from pixel to pixel; at each position the response, the sum of products of mask coefficients and the pixels under them, becomes the new value of the centre pixel. Example: with the 1/9 mean mask, the pixel 94 in the slide-19 image becomes (110+120+90+91+94+98+90+91+99)/9 = 98.1. Applied everywhere, sharp features and noise are averaged out and the image looks smoother (blurred).

(iii) At an edge the 3×3 window holds pixels from both sides. The average mixes them, so it outputs an in-between value that did not exist in the image: the step becomes a ramp and the edge blurs. The median outputs one of the actual pixel values: whichever side has the majority (5 or more of the 9 pixels) wins, so pixels stay on their own side and the step stays a step. It also ignores a single extreme value instead of averaging it in. [Draw the 1-D step figure above.]

F21 B4aCLO2 · Spatial filter at a pixel · 6CLO1 · Smoothing, noise, median vs mean · 2
3-bit 4x4 [[7,1,0,5],[6,3,6,2],[6,4,4,1],[1,5,7,0]], Laplacian [[0,1,0],[1,-4,1],[0,1,0]], zero padding. i) 3x3 mean filter at (2,2) [2] ii) 3x3 median at (0,0) [2] iii) Laplacian at (1,1) [2] iv) Why median preserves more edge sharpness than mean [2].
Worked answer (deck 06 · deck 07)
F21 B4(a)8 marks

3-bit 4×4 image, Laplacian [0 1 0; 1 −4 1; 0 1 0], zero padding. (i) 3×3 mean filter at (2,2) (ii) 3×3 median at (0,0) (iii) Laplacian at (1,1) (iv) Why does the median preserve more edge sharpness than the mean?

(i)

000000
071050
063620
064410
015700
000000
window at (2,2); zero padding outside

3×3 mean: (3 + 6 + 2 + 4 + 4 + 1 + 5 + 7 + 0) / 9 = 32/9 = 3.56 ≈ 4

(ii)

000000
071050
063620
064410
015700
000000
window at (0,0); zero padding outside

3×3 median: sorted: 0, 0, 0, 0, 0, 1, 3, 6, 7 → 5th value = 0

Five of the nine values are padding zeros, so the median is 0 (this is why zero padding darkens corners).

(iii)

000000
071050
063620
064410
015700
000000
window at (1,1); zero padding outside
710
636
644
window
010
1-41
010
positive Laplacian

Laplacian response: (1)(1) + (1)(6) + (-4)(3) + (1)(6) + (1)(4) = 5

(iv) Same answer as SP22 (iii): the median picks a real value from the majority side of the edge, while the mean blends both sides into a new in-between value.

Try it, step by step: F21: opens at (2,2) for the mean; click (0,0) for the median, (1,1) for the Laplacian.

F21 B4(a)iii2 marks
000000
071050
063620
064410
015700
000000
window at (1,1)
710
636
644
window
010
1-41
010
positive Laplacian

Σ w·z = (1)(1) + (1)(6) + (-4)(3) + (1)(6) + (1)(4) = 5

Laplacian response at (1,1) = 5.

Try it, step by step: F21 (iii) at (1,1).

More worked examples: slides and other examiners (14)
Slide 8 · correlation example (deck 06)
Slide 8 · correlation exampleworked

Kernel [−1 0 1; 2 1 2; 1 −2 0] placed on the top-left 3×3 block of the input:

526
434
392
pixels under the kernel
-101
212
1-20
kernel

(−1×5) + (0×2) + (1×6) + (2×4) + (1×3) + (2×4) + (1×3) + (−2×9) + (0×2) = −5 + 0 + 6 + 8 + 3 + 8 + 3 − 18 + 0 = 5.

5 is written in the output at the position of the centre pixel (3). Then the kernel moves one step right.

Try it, step by step: the slide 8 kernel visiting every position of the 8×8 source.

Slides 14, 57–63 · filtering an impulse (deck 06)
Slides 14, 57–63 · filtering an impulseworked

Take an image that is all 0 except one pixel = 1 (an impulse) and a kernel with entries a…i.

00000
00000
00100
00000
00000
impulse F
abc
def
ghi
kernel H
00000
0ihg0
0fed0
0cba0
00000
correlation output: kernel flipped
00000
0abc0
0def0
0ghi0
00000
convolution output: kernel itself

Correlation with an impulse gives the kernel flipped horizontally and vertically. Convolution flips the kernel first, so the output equals the filter kernel. Slide 14 (Figure 3.30) shows the same thing with kernel 1…9: correlation gives 9 8 7 / 6 5 4 / 3 2 1, convolution gives 1 2 3 / 4 5 6 / 7 8 9.

Slide 19 · standard averaging at the pixel 94 (deck 06)
Slide 19 · standard averaging at the pixel 94worked
000000
0110120901300
09194982000
09091991000
0829685900
000000
image; shaded = 3×3 window, outlined = centre

g = (110 + 120 + 90 + 91 + 94 + 98 + 90 + 91 + 99) / 9 = 883/9 = 98.11 ≈ 98.1

Try it, step by step: slide 19 (mean) and slide 35 (weighted) on every pixel. It opens on the 94.

Slide 35 · weighted average at the same pixel (deck 06)
Slide 35 · weighted average at the same pixelworked

Mask 1/16 [1 2 1; 2 4 2; 1 2 1]. Multiply each pixel by its weight:

(1×110 + 2×120 + 1×90 + 2×91 + 4×94 + 2×98 + 1×90 + 2×91 + 1×99)/16

= (110 + 240 + 90 + 182 + 376 + 196 + 90 + 182 + 99)/16 = 1565/16 = 97.81 ≈ 98

TrapThe slide prints 97.25 ≈ 97 here, but the nine terms add to 1565, not 1556, so the correct value is 97.8125 ≈ 98. Show your addition in the exam so the method marks are safe either way.
Slides 20–27 · mean filter over a whole 10×10 image (deck 06)
Slides 20–27 · mean filter over a whole 10×10 imageworked

The mask moves from point to point; at each point (x,y) the response is calculated. Border pixels are skipped (discard method), so the output is 8×8.

0000000000
0000000000
000909090909000
000909090909000
000909090909000
00090090909000
000909090909000
0000000000
00900000000
0000000000
input f (a 90-valued box with one dark hole and one stray 90)
010203030302010
020406060604020
030609090906030
030508080906030
030508080906030
020305050604020
1020303030302010
10101000000
output g (8×8)

First few positions, as on the slides: g = 1/9 × (0+0+0+0+0+0+0+0+90) = 10, then 20, 30, 30. At the hole: 1/9 × (0+0+0+0+90+90+90+90+90) = 50. Fully inside the box: 1/9 × (9×90) = 90.

Notice: the hard edge of the box became a ramp (0, 10, 20, 30 …), the dark hole was filled in (50 instead of 0), and the stray 90 spread into a faint 10-valued patch. That's blurring and noise reduction at the same time.

Try it, step by step: the whole slides 20–27 walk-through, all 64 positions.

Slide 31 · "Think for mask size 5×5 or 7×7?" (deck 06)
Slide 31 · "Think for mask size 5×5 or 7×7?"solved

An m×m mask has (m − 1)/2 rows hanging over each side. Discarding removes that many rows and columns on every side, i.e. m − 1 in total.

  • 3×3: 1 per side → 512×512 becomes 510×510.
  • 5×5: 2 per side → 508×508.
  • 7×7: 3 per side → 506×506.
Slide 42 · median at the pixel 94 (deck 06)
Slide 42 · median at the pixel 94worked
000000
0110120901300
09194982000
09095991000
0829685900
000000
image; shaded = window

Step 1, sort ascending: 90, 90, 91, 94, 95, 98, 99, 110, 120. Step 2, replace the original value by the median: 95.

Try it, step by step: sort and pick on every pixel. Switch to min or max to compare.

Slide 43 · median of a spike (deck 06)
Slide 43 · median of a spikeworked
101520
239027
333130
the 90 is a spike

sorted: 10, 15, 20, 23, 27, 30, 31, 33, 90 → 5th value = 27. The 90 is replaced by 27.

Slide 45 data · median vs mean on salt-and-pepper (deck 06)
Slide 45 data · median vs mean on salt-and-pepperworked

The noisy image has random 0 (pepper) and 255 (salt) pixels. Below, both 3×3 filters are applied to the inner 5×5 pixels:

494925548484949
470484949490
474849492555049
49255494905050
484848484849255
4804950505049
255505051515050
noisy image
4748494949
4849495050
4949494950
4848484849
4849505050
original (inner 5×5)
6666947266
6666666761
7171666690
6666434467
6644495072
3×3 mean
4849494949
4849494949
4849494950
4849494950
4849505050
3×3 median

The median output is within 1 of the original everywhere. The mean output carries the noise along: a 255 raises its whole neighbourhood to about 70, and a 0 pulls it down.

Try it, step by step: median vs mean on the salt-and-pepper image, pixel by pixel.

Slides 50–51 · mask [0 0 0; 0 0 1; 0 0 0] (deck 06)
Slides 50–51 · mask [0 0 0; 0 0 1; 0 0 0]worked

Now the 1 sits to the right of centre, so g(x,y) = f(x, y+1): each output pixel takes the value of its right-hand neighbour. The picture shifts left by 1 pixel.

1020304050
1121314151
1222324252
1323334353
1424344454
test image
000
001
000
mask
314151
324252
334353
output (inner 3×3)

The output at the position of 21 is 31 (the value to its right): every value moved one column to the left.

Try it, step by step: identity, shift and the 2δ − box sharpener on the test image.

Slides 52–54 · [0 0 0; 0 2 0; 0 0 0] − 1/9 [all 1] (deck 06)
Slides 52–54 · [0 0 0; 0 2 0; 0 0 0] − 1/9 [all 1]worked

2 × (identity) − (mean) gives g = 2f − f̄ = f + (f − f̄), where f̄ is the local average.

f − f̄ is the detail (how much a pixel differs from its neighbourhood). Adding it back makes differences bigger: a sharpening filter that "emphasizes differences with the local average". Its coefficients still add to 2 − 1 = 1, so flat areas keep their brightness.

404070
404070
404070
dark-side pixel 40
407070
407070
407070
bright-side pixel 70

Dark side: mean = (6×40 + 3×70)/9 = 450/9 = 50, so g = 2×40 − 50 = 30. Bright side: mean = (3×40 + 6×70)/9 = 540/9 = 60, so g = 2×70 − 60 = 80.

The edge 40 | 70 became 30 | 80: the jump is bigger, so the edge looks sharper. This is the idea behind unsharp masking in deck 07.

Slides 8–9, 14 · the image strip (deck 07)
Slides 8–9, 14 · the image stripworked

Gray levels along the line A–B of the slide image, with both derivatives worked out (forward 1st difference, central 2nd difference):

x0123456789101112131415161718192021222324
f(x)5543210006000013100007777
1st: f(x+1) − f(x)-1-1-1-1-1006-600012-2-10007000
2nd: f(x+1) + f(x−1) − 2f(x)-10000106-1260011-411007-700
048121620240367xvalue
Image strip A–B: ramp (x 0–6), isolated point (x 9), thin line (x 14–16), flat segment, step (x 21).
04812162024-606xvalue
1st derivative: nonzero along the entire ramp, zero in flat areas, a single response at the step.
04812162024-12-606xvalue
2nd derivative: nonzero only at the onset and end of the ramp, a double response (+, −) at the point and the step.

Example cells: at x = 9 (the isolated point 6), the 1st derivative is f(10) − f(9) = 0 − 6 = −6 and the 2nd derivative is f(10) + f(8) − 2f(9) = 0 + 0 − 12 = −12.

Try it, step by step: both derivatives along the whole strip, one position at a time. Switch the Operation menu for the 2nd derivative.

Slide 26 · negative Laplacian on the pixel 154 (deck 07)
Slide 26 · negative Laplacian on the pixel 154worked
0000000
01531571561531550
01591561581561590
01551581541561600
01541571581601600
01571571571561550
0000000
image; shaded = window, outlined = pixel 154

Step 1, filter: negative Laplacian [0 −1 0; −1 4 −1; 0 −1 0] at 154. Up = 158, left = 158, right = 156, down = 158.

∇²f = 4×154 − 158 − 158 − 156 − 158 = 616 − 630 = -14. This is the filtered value.

Step 2, sharpen: the mask centre is positive, so g = f + ∇²f = 154 + (-14) = 140.

The pixel was darker than its neighbours (154 against about 157) and became even darker (140), so the local contrast went up.

Try it, step by step: slide 26 sharpening: the two-step way (Laplacian, then add or subtract) and the one-pass masks. All give 140 at the 154.

Slide 53 · Class work (deck 07)
Slide 53 · Class workall parts

3-bit 4×4 image. Find the filtered output using the negative Laplacian, a 3×3 mean, a 3×3 median, a 3×3 Sobel and a 3×3 Prewitt operator. Ignore the border pixels and put zero in the output border.

0267
1164
4527
1260
input image

Only the four inner pixels (shaded) have a full 3×3 window.

(1) Negative Laplacian [0 −1 0; −1 4 −1; 0 −1 0]: 4×centre − up − down − left − right.

pixelworkingvalue
(1,1)4×1 − 2 − 5 − 1 − 6-10
(1,2)4×6 − 6 − 2 − 1 − 411
(2,1)4×5 − 1 − 2 − 4 − 211
(2,2)4×2 − 6 − 6 − 5 − 7-16

(2) 3×3 mean

pixelsum of window÷ 9rounded
(1,1)273.003
(1,2)404.444
(2,1)283.113
(2,2)333.674

(3) 3×3 median

pixelsorted windowmedian
(1,1)0, 1, 1, 2, 2, 4, 5, 6, 62
(1,2)1, 2, 2, 4, 5, 6, 6, 7, 75
(2,1)1, 1, 1, 2, 2, 4, 5, 6, 62
(2,2)0, 1, 2, 2, 4, 5, 6, 6, 74

(4) Sobel: Gx = (z3 + 2z6 + z9) − (z1 + 2z4 + z7), Gy = (z7 + 2z8 + z9) − (z1 + 2z2 + z3), |G| = |Gx| + |Gy|.

pixelGxGy|G|
(1,1)14620
(1,2)13-518
(2,1)628
(2,2)5-38

(5) Prewitt: the same with weights 1.

pixelGxGy|G|
(1,1)9312
(1,2)10-111
(2,1)819
(2,2)3-36

Output images (border = 0):

0000
0-10110
011-160
0000
negative Laplacian
0000
0340
0340
0000
mean
0000
0250
0240
0000
median
0000
020180
0880
0000
Sobel |G|
0000
012110
0960
0000
Prewitt |G|

Worked Sobel at (1,1): window [0 2 6; 1 1 6; 4 5 2]. Gx = (6 + 2×6 + 2) − (0 + 2×1 + 4) = 20 − 6 = 14. Gy = (4 + 2×5 + 2) − (0 + 2×2 + 6) = 16 − 10 = 6. |G| = 20.

Try it, step by step: the slide 53 class work: switch filters, the border is ignored as the question says.

5. Segmentation: edges, Canny, threshold, region growing

decks 08–09 · 13 past questions
The pattern: how she asks it
  • Q1: goal of edge detection, 2 gradient operators, the role of NMS in Canny, what region growing is (SP25 1e, SP24 1e, F23 1e).
  • Q2: Sobel Gx, Gy, |G|, α at a pixel (SP25 2d, SP22 6b); full Canny numeric with NMS and hysteresis (F23 3a, 10 marks).
  • Q3 case: pipeline for a medical image: enhance, smooth, edges or threshold, region growing, morphology (SP25 3a, SP23 1b).

Connects to: Edges use the ch 7 derivatives. Thresholding needs a two-peak histogram (ch 5). The masks it produces are then cleaned with morphology (ch 9).

typeCLOtimeswhere
Segmentation: 2 approaches, edges, gradient ops, LoG, Canny steps/criteria/NMS/hysteresis, FP/FN, region growingCLO16SP25 1e, SP24 1e, F23 1e, SP23 3e, SP22 6a, F25 quiz Q3D 1
Case: enhancement / filtering / segmentation pipeline choiceCLO34SP25 3a, SP24 3a, SP23 1b, F22 A1b
Spatial filter at a pixelCLO22SP25 2d, SP22 6b
Canny numericCLO22SP25 2d, F23 3a
Hough transformCLO21SP25 2d
Case: which morphological op and whyCLO31SP24 3a
Piecewise linear:CLO11F22 A1b
Chain code + CFD/NCFDCLO21SP22 6b
Formula wall: every formula with a worked line

Canny: 3 criteria, 5 steps

Criteria: low error rate (find real edges only), good localisation (on the true edge), single response (one pixel per edge).

  1. Smooth with a Gaussian.
  2. Gradient magnitude and angle (Sobel).
  3. Non-maximum suppression: keep a pixel only if it beats its two neighbours along the gradient direction (thin edges).
  4. Double threshold TH and TL (ratio 2:1 or 3:1): strong ≥ TH, weak between.
  5. Hysteresis: keep a weak pixel only if it is 8-adjacent to a strong one.

Thresholding

g(x,y) = 1 if f(x,y) > T, else 0    Tnew = ½(μ₁ + μ₂)

Iterative T, values 10 20 30 200 210 220: start T₀ = mean = 115. Group above: mean 210; group at or below: mean 20. T₁ = ½(210 + 20) = 115 = T₀, so stop.

Region growing: start at a seed, add a 4- or 8-neighbour when |neighbour − the pixel it grows from| ≤ T, repeat until nothing joins. Split & merge: split a block into 4 while (max − min) > T, merge neighbours that are similar.

Line masks (sum 0, the 2s lie along the line): horizontal [−1 −1 −1; 2 2 2; −1 −1 −1], vertical [−1 2 −1; −1 2 −1; −1 2 −1], ±45° with the 2s on a diagonal.

Past questions: look, recall, then open the answer

SP25 1eCLO1 · Segmentation: 2 approaches, edges, gradient ops, LoG, Canny steps/criteria/NMS/hysteresis, FP/FN, region growing · 4
What is the primary goal of edge detection in image processing? Name two gradient-based operators used for edge detection. What is the role of non-maximum suppression in Canny edge detection? What is Region Growing in image segmentation?
Worked answer (deck 08)
SP25 Final Q1(e)4 marks

What is the primary goal of edge detection? Name two gradient-based operators. What is the role of non-maximum suppression in Canny? What is region growing?

Goal: to find the pixels where intensity changes sharply, i.e. object boundaries, so the image can be segmented and its structure kept with much less data.

Two gradient operators: Sobel and Prewitt (also Roberts).

NMS role: thins the thick gradient ridges to one-pixel-wide edges by keeping only the local maximum along the gradient direction; this improves localisation and removes false responses.

Region growing: start from seed pixel(s) and repeatedly add neighbouring pixels that are similar (e.g. |difference| ≤ T) until no more can be added (details on the deck 09 page).

SP25 2dCLO2 · Spatial filter at a pixel · 2CLO2 · Canny numeric · 2CLO2 · Hough transform · 4
i) 3x3 patch [10,10,10,10,50,10,10,10,10]. Apply the Sobel operator in the horizontal direction; gradient value at the center pixel? [2] ii) In Canny, a pixel has gradient magnitude 80 after non-maxima suppression; double thresholds low=50, high=100. How is the pixel classified? [2] iii) Find the line passing through points (2,1),(3,2),(4,3). Are they collinear? Derive the polar representation of a line used in the Hough Transform. Why is it preferred in the Hough Transform? [4]
Worked answer (deck 07 · deck 08)
SP25 Q2(d)i2 marks

3×3 patch [10 10 10; 10 50 10; 10 10 10]. Apply the Sobel operator in the horizontal direction. What is the gradient value at the centre pixel?

101010
105010
101010
patch
-101
-202
-101
Sobel Gx

Gx = (z3 + 2z6 + z9) − (z1 + 2z4 + z7) = (10 + 20 + 10) − (10 + 20 + 10) = 40 − 40 = 0.

(With the other mask, Gy = (10 + 20 + 10) − (10 + 20 + 10) = 0 too.) The gradient is 0: the left and right columns are equal, so there is no horizontal change across the centre. The Sobel masks have a 0 centre weight, so the bright point 50 itself is not seen. A Laplacian would respond strongly: 4×50 − 4×10 = 160.

Try it, step by step: SP25 (d)(i): Sobel gives 0 at the centre; the Laplacian does not.

SP25 Final Q2(d)(i)2 marks

3×3 patch [10 10 10; 10 50 10; 10 10 10]. Apply the Sobel operator in the horizontal direction; gradient value at the centre pixel?

Gx = (10 + 2×10 + 10) − (10 + 2×10 + 10) = 0. The left and right columns are equal, and Sobel's centre weight is 0, so the bright point is not seen by the gradient (Gy = 0 too). The full working is also on the deck 07 page.

SP25 Final Q2(d)(ii)2 marks

A pixel has gradient magnitude 80 after non-maxima suppression; low = 50, high = 100. How is it classified?

50 ≤ 80 < 100, so it is a weak edge pixel. In hysteresis it is kept (becomes an edge) only if it is 8-connected to a strong pixel (≥ 100); otherwise it is discarded as noise.

SP25 Final Q2(d)(iii)4 marks

Find the line through (2,1), (3,2), (4,3). Are they collinear? Derive the polar representation of a line used in the Hough transform. Why is it preferred?

Slope (2,1)→(3,2) = (2 − 1)/(3 − 2) = 1; slope (3,2)→(4,3) = (3 − 2)/(4 − 3) = 1. Equal slopes through a common point, so the points are collinear: y = x − 1.

Polar form: describe a line by the perpendicular from the origin: its length ρ and its angle θ. Every point (x, y) on the line satisfies ρ = x cos θ + y sin θ. For y = x − 1 (x − y − 1 = 0) the unit normal is (1, −1)/√2, so θ = −45° and ρ = 1/√2 ≈ 0.707. Check (2,1): 2 cos(−45°) + 1 sin(−45°) = 0.707.

Why preferred: in y = ax + b the slope a goes to infinity for vertical lines, so the parameter space is unbounded. In (ρ, θ) both parameters are bounded: θ ∈ [−90°, 90°], ρ ∈ [−D, D] with D the image diagonal, so a finite accumulator array works for every line.

SP25 3aCLO3 · Case: enhancement / filtering / segmentation pipeline choice · 10
Scanned document image suffers from uneven illumination, faded ink and background noise. Task: enhance for readability, then segment text line and text for OCR. i) Describe two enhancement techniques suitable for this case [4]. ii) Which sharpening method (Laplacian or Sobel) would be most effective for this enhancement, and why? [3] iii) After sharpening, which segmentation technique would you use to separate text and text line? [3]
Worked answer (deck 04 · deck 09)
SP25 Q3(a)i4 marks

A scanned document suffers from uneven illumination, faded ink and background noise. Describe two enhancement techniques suitable for this case.

1. Contrast stretching. Faded ink and paper occupy a narrow band of gray levels, so the text has low contrast. Stretching that band to the full [0, L−1] with s = (r − rmin)(L−1)/(rmax − rmin) makes the ink darker and the paper brighter, which improves readability.

2. Power-law (gamma) transformation with γ > 1. Faded strokes are light gray. A γ greater than 1 pushes light values down, so faint strokes become darker and clearer against the background.

(Thresholding then turns the enhanced page into black text on white, ready for OCR. For the background noise, a median filter from the smoothing chapter is the usual partner.)

SP25 Final Q3(a) · case study4 + 3 + 3 marks

A scanned document suffers from uneven illumination, faded ink and background noise. Enhance it for readability, then segment text lines and text for OCR. (i) Two enhancement techniques. (ii) Laplacian or Sobel for sharpening, and why? (iii) Which segmentation technique to separate text and text lines?

(i) Enhancement. 1. Noise removal with a median filter (3×3): removes the background specks (impulse noise) while keeping the edges of the strokes. 2. Contrast enhancement for the faded ink: contrast stretching or histogram equalization spreads the crowded gray levels, so faint strokes become darker against the paper (a power-law with γ > 1 also darkens light ink). Because the illumination is uneven, apply it locally (per region) or first remove the slowly varying background.

(ii) Laplacian. Sharpening needs to boost fine detail (thin strokes, character edges) in every direction: the Laplacian is an isotropic 2nd derivative and g = f − ∇²f (or f + ∇²f for the negative mask) sharpens all orientations. Sobel is a 1st-derivative edge detector that gives thick edges and responds per direction; it is used to find edges, not to sharpen. The Laplacian amplifies noise, so denoise first (step i).

(iii) Local (adaptive) thresholding. A single global T fails because one part of the page is darker (slide 49). Use T(x,y) from each pixel's neighbourhood, e.g. the local mean, (max + min)/2, or Niblack T = m + k·s, to turn text into 1 and paper into 0. Then group the text pixels: connected components (or region growing) give characters and words; rows of components with empty gaps between them give the text lines.

SP24 1eCLO1 · Segmentation: 2 approaches, edges, gradient ops, LoG, Canny steps/criteria/NMS/hysteresis, FP/FN, region growing · 5
What is the Hysteresis thresholding of Canny Edge Detection? What are false positive and false negative edge pixels? How is it overcome in the Canny edge detection algorithm? [1+2+2]
Worked answer (deck 08)
SP24 Final Q1(e)1 + 2 + 2 marks

What is hysteresis thresholding in Canny? What are false positive and false negative edge pixels? How are they overcome?

Hysteresis thresholding: uses two thresholds. Pixels ≥ TH are strong (sure) edges; pixels < TL are discarded; pixels in between are weak and kept only if 8-connected to a strong edge. It links edge pieces and fills gaps.

False positive: non-edge (noise) marked as edge (threshold too low). False negative: real edge missed (threshold too high).

Overcome: NMS keeps only local maxima (fewer false positives); the low threshold plus the connectivity rule recovers faint parts of real edges (fewer false negatives) while isolated weak responses are removed.

SP24 3aCLO3 · Case: enhancement / filtering / segmentation pipeline choice · 4CLO3 · Case: which morphological op and why · 4
Image corrupted by salt and pepper noise in the object part (not background). i) Provide the best way to denoise this image; explain. ii) Which morphological operation would you prefer to eliminate? Explain your process. [4+4]
Worked answer (deck 06 · deck 11)
SP24 Q3(a)i4 marks

An image is corrupted by salt-and-pepper noise in the object part only (not the background). Give the best way to denoise it and explain.

Use a median filter (3×3), applied to the object region. Salt-and-pepper noise consists of isolated extreme values (0 and 255). The median of each window ignores them, since they sort to the ends, and replaces each noisy pixel with a real value from its neighbourhood. It keeps the object's edges sharp, unlike the mean, which would blur the object boundary and smear the dots.

Because the background is clean, the filter can be restricted to the object (e.g. a mask from thresholding), so the background stays untouched and no extra blur is added there. Part (ii), morphological removal, is on the morphology page.

SP24 Q3(a)ii4 marks

An image is corrupted by salt-and-pepper noise in the object part only. Which morphological operation would you use to remove it? Explain the process.

Inside a white object, the visible noise is the pepper: small black dots, i.e. tiny holes in the foreground. Use closing A • B = (A ⊕ B) ⊖ B with a small (3×3) SE: the dilation fills each black dot because the SE touches white on all sides; the erosion then restores the object's outline. Any white specks left outside the object could then be removed with an opening (open–close filtering). The background is untouched because it has no noise.

F23 1eCLO1 · Segmentation: 2 approaches, edges, gradient ops, LoG, Canny steps/criteria/NMS/hysteresis, FP/FN, region growing · 4
Write down the types of image segmentation based on two basic properties of intensity values. How many steps are in Canny Edge detection algorithm and what are the steps? [1+1+2]
Worked answer (deck 08)
F23 Final Q1(e)(i) · F25 Quiz Q3D Q1 (part)1 + … marks

Write down the types of image segmentation based on the two basic properties of intensity values. (F25 quiz: what are the two principal approaches to image segmentation?)

Segmentation algorithms use one of two properties of intensity:

1. Discontinuity: partition the image where intensity changes sharply, e.g. at edges. Methods: point, line and edge detection (gradient, LoG, Canny); watershed.

2. Similarity: partition the image into regions that are alike by a predefined rule. Methods: thresholding, region growing, region splitting and merging, template matching.

F23 Final Q1(e)(ii) · F22 B4(a) (another examiner)1 + 2 · 2 + 3 marks

How many steps are in Canny and what are they? (F22: explain the three main criteria of Canny.)

5 steps: smoothing, finding gradients, non-maximum suppression, double thresholding, edge tracking by hysteresis.

Three criteria: good detection (mark all real edges), good localisation (detected edge as close as possible to the true edge), single (clear) response (one response per edge).

F23 3aCLO2 · Canny numeric · 10
Canny has 5 steps. Given 3x3 result of first two steps (magnitude, direction): [[200 down, 40 down, 57 down-left],[10 right, 20 up, 101 left],[50 up, 41 up, 74 left]]. i) Complete remaining 3 steps with high threshold 100, low 45. ii) Identify false positive / false negative edges. iii) What happens with high 245, low 10? [5+2+3]
Worked answer (deck 08)
F23 Final Q3(a)5 + 2 + 3 marks

Canny has 5 steps. The 3×3 result of the first two steps (magnitude, direction) is given. (i) Complete the remaining 3 steps with high threshold 100 and low threshold 45. (ii) Identify false positive / false negative edges. (iii) What happens with high = 245 and low = 10?

200 ↓40 ↓57 ↙
10 →20 ↑101 ←
50 ↑41 ↑74 ←
given: M and gradient direction

Step 3 · non-maximum suppression: compare each pixel with its two neighbours along its arrow; keep it only if it is not smaller than them.

pixelMarrowneighbours along the arrowresult
(0,0)200↓(1,0) = 10, (-1,0) outsidekept
(0,1)40↓(1,1) = 20, (-1,1) outsidekept
(0,2)57↙(1,1) = 20, (-1,3) outsidekept
(1,0)10→(1,1) = 20, (1,-1) outsidesuppressed → 0
(1,1)20↑(0,1) = 40, (2,1) = 41suppressed → 0
(1,2)101←(1,1) = 20, (1,3) outsidekept
(2,0)50↑(1,0) = 10, (3,0) outsidekept
(2,1)41↑(1,1) = 20, (3,1) outsidekept
(2,2)74←(2,1) = 41, (2,3) outsidekept
2004057
00101
504174
gN

Step 4 · double threshold (TH = 100, TL = 45):

20000
00101
000
gNH strong
0057
000
50074
gNL weak

Step 5 · hysteresis:

weak pixelvaluestrong 8-neighboursdecision
(0,2)57101 at (1,2)kept
(2,0)50nonediscarded
(2,2)74101 at (1,2)kept
200057
00101
0074
final edge map g

(ii) 74 at (2,2) is weak but survives because it touches the strong 101: if it was noise it is a false positive. 50 at (2,0) is weak with no strong neighbour and is dropped: if it was a real edge it is a false negative (40 and 41 are below TL and dropped too).

(iii) With TH = 245 nothing is strong, so hysteresis keeps no weak pixel: the output is empty (every real edge becomes a false negative). TL = 10 alone would let almost everything in as weak (false positives); 245:10 breaks the 2:1–3:1 rule.

Try it, step by step: F23 Q3(a): all three remaining steps, pixel by pixel. Set TH = 245, TL = 10 for part (iii).

SP23 1bCLO3 · Case: enhancement / filtering / segmentation pipeline choice · 9
Brain tumor detection flow (acquisition, preprocessing, segmentation ROI, feature extraction, ...). i) Method to remove noise from scan images in preprocessing? [2] ii) Method to partition pixels depending on intensity values? [2] iii) Which transformation function highlights specific ranges of pixel values? [3] iv) What segmentation method groups similar pixels? [2]
Worked answer (deck 04 · deck 09)
SP23 1(b)iii · F22 A1(b)i, tumor case3 marks

For tumor detection from medical scans, which transformation function highlights specific ranges of pixel values?

Gray-level (intensity-level) slicing. The tumor tissue occupies a particular band of gray values [A, B]. Slicing maps that band to a high value (L−1), so the tumor stands out.

It can be applied in two ways: without background (all other levels → 0, only the tumor region is visible) or with background (other levels unchanged, so the tumor is seen in its anatomical context).

s =
L − 1
if A ≤ r ≤ B
0 (or r)
otherwise
SP23 Final Q1(b) · brain tumour pipeline2 + 2 + 3 + 2 marks

Brain tumour detection: acquisition → preprocessing → segmentation (ROI) → feature extraction… (i) Method to remove noise from the scans in preprocessing? (ii) Method to partition pixels depending on intensity values? (iii) Which transformation function highlights specific ranges of pixel values? (iv) Which segmentation method groups similar pixels?

(i) Spatial smoothing: a median filter for salt-and-pepper (impulse) noise, keeps edges sharp; a Gaussian / averaging filter for random noise (deck 06).

(ii) Thresholding: g(x,y) = 1 if f(x,y) > T else 0. T can be found automatically with the iterative global method, or locally (adaptive) if the scan's contrast is uneven.

(iii) Intensity-level (gray-level) slicing: make the range [A, B] bright and either darken or keep the other levels, so the tumour's intensity range stands out (deck 04).

(iv) Region growing (from a seed inside the tumour, add neighbours with |difference| ≤ T); split-and-merge also groups similar pixels.

SP23 3eCLO1 · Segmentation: 2 approaches, edges, gradient ops, LoG, Canny steps/criteria/NMS/hysteresis, FP/FN, region growing · 8
i) Apply Canny to extract edges from f(x,y) of m x n; draw schematic of whole process to g(x,y). ii) Explain non-maxima suppression gN(x,y) OR hysteresis thresholding gNH, gNL. iii) Draw four line detection masks: horizontal, +45, vertical, -45. [3+3+2]
Worked answer (deck 08)
SP23 Final Q3(e)(iii)2 marks

Draw the four line detection masks: horizontal, +45°, vertical, −45°.

-1-1-1
222
-1-1-1
R1 horizontal
-1-12
-12-1
2-1-1
R2 +45°
-12-1
-12-1
-12-1
R3 vertical
2-1-1
-12-1
-1-12
R4 −45°

The 2s mark the direction each mask looks for; every mask sums to 0.

SP23 Final Q3(e)(i)(ii)3 + 3 marks

(i) Apply Canny to extract edges from f(x,y) of size m × n; draw a schematic of the whole process to g(x,y). (ii) Explain non-maxima suppression gN(x,y) OR hysteresis thresholding gNH, gNL.

f(x,y) Gaussian G(x,y): fs = G ★ f Sobel/Prewitt: gx, gy M = √(gx² + gy²), α = tan⁻¹(gy/gx) NMS → gN TH, TL → gNH, gNL connectivity → g(x,y)

(ii) Hysteresis: gNH = gN ≥ TH, gNL = gN ≥ TL, then gNL = gNL − gNH. (a) locate the next unvisited edge pixel p in gNH; (b) mark as valid all weak pixels of gNL connected to p (8-connectivity); (c) repeat until all of gNH is visited; (d) set to 0 the pixels of gNL not marked; the output is gNH plus the marked gNL pixels. (Or explain NMS as in SP22 above.)

F22 A1bCLO3 · Case: enhancement / filtering / segmentation pipeline choice · 3CLO1 · Piecewise linear: · 6
i) Tumor detection from medical scans: which transformation function highlights specific ranges of pixel values? [3] ii) Points (r1,s1),(r2,s2) control the piecewise linear transformation; illustrate relation for Linear Identity, Thresholding, Contrast Stretching. [6]
Worked answer (deck 04)

Same answer as SP23 2d (SP23 2(d) · F22 A1(b)ii · SP22 2(a) · F21 B3(b)).

Same answer as SP23 1b (SP23 1(b)iii · F22 A1(b)i, tumor case).

SP22 6aCLO1 · Segmentation: 2 approaches, edges, gradient ops, LoG, Canny steps/criteria/NMS/hysteresis, FP/FN, region growing · 5
How many steps in Canny edge detection, what are they? Explain any one step. What are false positive and false negative edge pixels? How does Canny reduce these errors? [5]
Worked answer (deck 08)
SP22 Final Q6(a) · F21 A2(a) (another examiner)5 · 8 marks

How many steps are in Canny edge detection and what are they? Explain any one step (F21: explain non-maximum suppression). What are false positive and false negative edge pixels? How does Canny reduce these errors? (F21 also: what is image segmentation?)

Segmentation (F21): dividing an image into meaningful regions, e.g. objects and background, as the first step of image analysis.

Five steps: (1) Gaussian smoothing; (2) gradient magnitude and direction; (3) non-maximum suppression; (4) double thresholding; (5) edge tracking by hysteresis.

Non-maximum suppression: the gradient magnitude forms wide ridges around edges. For each pixel, round its gradient angle to 0°, 45°, 90° or 135° and compare its magnitude with the two neighbours along that direction; keep it only if it is the largest, otherwise set it to 0. This thins edges to one pixel.

False positive: a pixel marked as edge that is not a real edge (noise), caused by a threshold that is too low. False negative: a real edge pixel that is missed, caused by a threshold that is too high.

How Canny reduces them: NMS removes non-peak pixels (fewer false positives); double thresholding with hysteresis keeps weak pixels only when they connect to strong edges, so real but faint edge parts are kept (fewer false negatives) while isolated weak noise is dropped.

SP22 6bCLO2 · Spatial filter at a pixel · 4CLO2 · Chain code + CFD/NCFD · 4
i) 3x3 [(3,7,6),(2,5,8),(3,4,7)]: Sobel gradient magnitude image; edge direction of central pixel [4]. ii) Grid image of black points (coordinates 0..11) with arrow at reference pixel: chain code invariant to starting point and rotation, 8-neighbor [4].
Worked answer (deck 07 · deck 08 · deck 10)
SP22 Q6(b)i4 marks

3×3 image [3 7 6; 2 5 8; 3 4 7]. Find the Sobel gradient-magnitude image and the edge direction of the central pixel.

Centre pixel: z1…z9 = 3, 7, 6, 2, 5, 8, 3, 4, 7.

Gx = (z3 + 2z6 + z9) − (z1 + 2z4 + z7) = (6 + 16 + 7) − (3 + 4 + 3) = 29 − 10 = 19

Gy = (z7 + 2z8 + z9) − (z1 + 2z2 + z3) = (3 + 8 + 7) − (3 + 14 + 6) = 18 − 23 = -5

|∇f| ≈ |Gx| + |Gy| = 19 + 5 = 24

Direction (deck 08 slide 23): gradient angle α = tan⁻¹(Gy/Gx) = tan⁻¹(-5/19) = -14.74°. The edge runs perpendicular to the gradient: edge direction = α − 90° = -104.74°.

Whole magnitude image with zero padding (same formulas at every pixel):

376
258
347
input
1912-19
2119-21
1314-13
Gx
92021
-3-5-1
-9-20-21
Gy
283240
242422
223434
|Gx| + |Gy|

Try it, step by step: SP22: the full Sobel magnitude image with zero padding.

SP22 Final Q6(b)(i)4 marks

3×3 image [3 7 6; 2 5 8; 3 4 7]. Find the Sobel gradient-magnitude image and the edge direction of the central pixel.

Gx = (z3 + 2z6 + z9) − (z1 + 2z4 + z7) = (6 + 16 + 7) − (3 + 4 + 3) = 19; Gy = (z7 + 2z8 + z9) − (z1 + 2z2 + z3) = (3 + 8 + 7) − (3 + 14 + 6) = -5.

|∇f| ≈ |19| + |-5| = 24 (exact √(19² + -5²) = 19.65).

Gradient angle α = tan⁻¹(-5/19) = -14.74°; edge direction = α − 90° = -104.74°.

Whole magnitude image with zero padding:

376
258
347
input
1912-19
2119-21
1314-13
Gx
92021
-3-5-1
-9-20-21
Gy
283240
242422
223434
|Gx| + |Gy|

Try it, step by step: Sobel and Prewitt step by step on SP22 and on the SP25 bright point.

SP22 Final Q6(b)(ii)4 marks

Grid image of black points (axes x to the right, y upward) with an arrow at the reference pixel. Find the chain code that is invariant to the starting point and rotation, 8-neighbour.

Reading the figure. Points are written (x, y). (1,4) is filled but lies inside the outer boundary, so the outer chain passes through (0,4). In (x, y) form "up" means y + 1.

Step 1 · chain code. Start at the arrow pixel (3,5) and walk clockwise (the arrow enters (3,5) from (2,6)) round the boundary. Each move to the next boundary pixel gets the number of its direction (8-direction compass: 0 = right, then counterclockwise).

01234566543210177555333117
boundary with each move's code · ringed = start · axes x →, y ↑
01234567
8-direction code: 0 = right, counting counterclockwise, 45° apart
movefrom → todirectioncode
1(3,5) → (4,6)up-right ↗1
2(4,6) → (5,5)down-right ↘7
3(5,5) → (6,4)down-right ↘7
4(6,4) → (5,3)down-left ↙5
5(5,3) → (4,2)down-left ↙5
6(4,2) → (3,1)down-left ↙5
7(3,1) → (2,2)up-left ↖3
8(2,2) → (1,3)up-left ↖3
9(1,3) → (0,4)up-left ↖3
10(0,4) → (1,5)up-right ↗1
11(1,5) → (2,6)up-right ↗1
12(2,6) → (3,5)down-right ↘7
CC = 177555333117  (12 moves)

Step 2 · start-point invariance (shape number of slide 18). The boundary is a loop, so any digit can be the first. Write the code from every start (circular shifts) and pick the one that reads as the smallest number.

from 1177555333117
from 2775553331171
from 3755533311717
from 4555333117177
from 5553331171775
from 6533311717755
from 7333117177555
from 8331171775553
from 9311717755533
from 10117177555333
from 11171775553331
from 12717755533311
all circular shifts of the CC · highlighted = smallest
AnswerStart-point invariant chain code = 117177555333 (smallest shift, starting at digit 10).

Step 3 · circular first difference (CFD). Slide 22 rule: CFDi = (ci+1 − ci) mod 8; the last digit wraps round to the first. This removes rotation.

i123456789101112
this ci177555333117
next ci+1775553331171
next − this60-200-200-206-6
mod 8 = CFD606006006062
first difference, last column wraps round (next of the last digit = the first digit)
CFD = 606006006062

Step 4 · normalise (NCFD). Smallest circular shift of the CFD. This removes the start point too.

from 1606006006062
from 2060060060626
from 3600600606260
from 4006006062606
from 5060060626060
from 6600606260600
from 7006062606006
from 8060626060060
from 9606260600600
from 10062606006006
from 11626060060060
from 12260600600606
all circular shifts of the CFD · highlighted = smallest
AnswerChain code invariant to start point and rotation (NCFD, shape number of slides 21–22) = 006006062606.

Try it, step by step: SP22 points, labelled in (x, y) like the paper.

F25 quiz Q3D 1CLO1 · Segmentation: 2 approaches, edges, gradient ops, LoG, Canny steps/criteria/NMS/hysteresis, FP/FN, region growing · 6
What are the two principal approaches to image segmentation? Why is a Laplacian filter typically combined with a Gaussian smoothing filter (LOG operator) before performing edge detection in noisy environments? [4+2=6]
Worked answer (F25 quiz page)
Fall 25 Quiz 3 (set D) Q14 + 2 marks

What are the two principal approaches to image segmentation? Why is a Laplacian filter typically combined with a Gaussian smoothing filter (LoG operator) before performing edge detection in noisy environments?

Two approaches (based on two basic properties of intensity values):

  1. Discontinuity: partition the image where the intensity changes abruptly. Methods: point, line and edge detection (gradient/Sobel, LoG, Canny).
  2. Similarity: partition the image into regions that are alike by a predefined rule. Methods: thresholding, region growing, region splitting and merging.

Why Gaussian + Laplacian (LoG).

  • The Laplacian is a 2nd derivative, so it is very sensitive to noise: every noisy pixel gives a strong response and false zero crossings.
  • Gaussian smoothing first removes the noise, so the zero crossings of the Laplacian then mark real edges only.
  • Both are linear, so they combine into one LoG mask ∇²G (cheaper), and its zero crossings give thin, well-localised edges.

Small proof (a noisy step, 2nd derivative f(x+1) + f(x−1) − 2f(x)):

signal2nd derivativezero crossings
noisy10 13 9 12 10 11 40 38 41 39 42 40-7 7 -5 3 28 -31 5 -5 5 -58
smoothed [1 2 1]/410 11 11 11 11 18 32 39 40 40 41 40-1 0 0 7 7 -7 -6 -1 1 -23

Noisy: 8 sign changes, almost all false. Smoothed: the big one (7 → −7) sits at the real step, and the leftover small ones are weak, so a threshold on the jump drops them.

Try it, step by step: 2nd derivative of the NOISY step: the signs flip again and again in the flat part.

Try it, step by step: the same step after Gaussian-like smoothing: one strong +/− pair at the real edge.

More worked examples: slides and other examiners (4)
Slide 59 · example (deck 08)
Slide 59 · exampleworked

gN is given (after step 3); TH = 100, TL = 45.

200057
045101
5000
gN
20000
00101
000
gNH (≥ 100)
0057
0450
5000
gNL (45–99)
200057
045101
000
final g

57 touches 101 and 45 touches 200 and 101: kept. 50's neighbours are 0, 45 (weak) and 0: no strong neighbour, so it is dropped.

Slides 14–17 · worked example (deck 09)
Slides 14–17 · worked exampleworked

The seed is the centre of the image. (a) Grow the region in horizontal and vertical directions (4-connectivity) when the difference between two pixel values is ≤ 5. (b) What if the region is grown in horizontal, vertical and diagonal directions (8-connectivity)?

10101010101010
10101069701010
59106064595660
10591060701062
10605965671065
10101010101010
10101010101010
input; seed (3,3) = 60 outlined

(a) 4-connectivity. Pixels that join, in the order they are added (each compared with the pixel it grows from):

grows fromneighbourtest
(3,3) = 60(2,3) = 64|64 − 60| = 4 ≤ 5
(3,3) = 60(4,3) = 65|65 − 60| = 5 ≤ 5
(2,3) = 64(1,3) = 69|69 − 64| = 5 ≤ 5
(2,3) = 64(2,2) = 60|60 − 64| = 4 ≤ 5
(2,3) = 64(2,4) = 59|59 − 64| = 5 ≤ 5
(4,3) = 65(4,4) = 67|67 − 65| = 2 ≤ 5
(1,3) = 69(1,4) = 70|70 − 69| = 1 ≤ 5
(2,4) = 59(2,5) = 56|56 − 59| = 3 ≤ 5
(4,4) = 67(3,4) = 70|70 − 67| = 3 ≤ 5
(2,5) = 56(2,6) = 60|60 − 56| = 4 ≤ 5
(2,6) = 60(3,6) = 62|62 − 60| = 2 ≤ 5
(3,6) = 62(4,6) = 65|65 − 62| = 3 ≤ 5
10101010101010
10101069701010
59106064595660
10591060701062
10605965671065
10101010101010
10101010101010
(a) region: 13 pixels

(b) 8-connectivity also reaches across corners, so the 59 at (3,1), the 60 and 59 at (4,1), (4,2) and the 59 at (2,0) join too:

10101010101010
10101069701010
59106064595660
10591060701062
10605965671065
10101010101010
10101010101010
(b) region: 17 pixels
Answer4-connectivity: 13 pixels; 8-connectivity: 17 pixels. The 10s never join (|10 − 60| = 50 > 5).

Try it, step by step: slides 14–17: watch every neighbour check. Switch “Grow in” to 8-connectivity for part (b).

F22 Final B5(a) (another examiner's paper) (deck 09)
F22 Final B5(a) (another examiner's paper)5 marks part

7×7 image; region growing with the seed at the centre, 8-direction growth when |difference| ≤ 4.

15151515151515
15151568691515
64646367706815
15157065617166
15156973581515
15151515151515
15151515151515
input; seed (3,3) = 65
grows fromneighbourtest
(3,3) = 65(2,2) = 63|63 − 65| = 2 ≤ 4
(3,3) = 65(2,3) = 67|67 − 65| = 2 ≤ 4
(3,3) = 65(3,4) = 61|61 − 65| = 4 ≤ 4
(3,3) = 65(4,2) = 69|69 − 65| = 4 ≤ 4
(2,2) = 63(2,1) = 64|64 − 63| = 1 ≤ 4
(2,3) = 67(1,3) = 68|68 − 67| = 1 ≤ 4
(2,3) = 67(1,4) = 69|69 − 67| = 2 ≤ 4
(2,3) = 67(2,4) = 70|70 − 67| = 3 ≤ 4
(2,3) = 67(3,2) = 70|70 − 67| = 3 ≤ 4
(3,4) = 61(4,4) = 58|58 − 61| = 3 ≤ 4
(4,2) = 69(4,3) = 73|73 − 69| = 4 ≤ 4
(2,1) = 64(2,0) = 64|64 − 64| = 0 ≤ 4
(1,4) = 69(2,5) = 68|68 − 69| = 1 ≤ 4
(2,4) = 70(3,5) = 71|71 − 70| = 1 ≤ 4
(2,5) = 68(3,6) = 66|66 − 68| = 2 ≤ 4
15151515151515
15151568691515
64646367706815
15157065617166
15156973581515
15151515151515
15151515151515
region: 16 pixels

Values 64, 64, 63 on row 2 and 66 on row 3 never join: no chain of steps ≤ 4 reaches them. (The question also asks for split and merge with T ≤ 5; a 7×7 image cannot be cut into equal quadrants, so state that you pad it to 8×8 or split unevenly, then follow the method below.)

Try it, step by step: F22 B5(a): 8-direction growth with T = 4.

Practice · iterative threshold (deck 09)
Practice · iterative thresholdworked

Pixel values: 10, 12, 15, 11, 200, 190, 205, 14, 198, 13, 201, 16. Find the threshold (ΔT = 0.5).

TG1 (> T)μ1G2 (≤ T)μ2new TΔT
90.417{200, 190, 205, 198, 201}198.800{10, 12, 15, 11, 14, 13, 16}13.000105.90015.483
105.900{200, 190, 205, 198, 201}198.800{10, 12, 15, 11, 14, 13, 16}13.000105.9000.000
AnswerT ≈ 105.90: the 5 bright values are object (1), the 7 dark values are background (0).

Try it, step by step: the iterative threshold, one round at a time. Try another start T: it lands on the same answer.

6. Chain code (boundary representation)

deck 10 · 5 past questions
The pattern: how she asks it
  • Q2: walk the boundary and write the 4- or 8-direction chain code, then the first difference (CFD) and its smallest rotation, the shape number (SP24 3d, SP23 2c, F25 quiz 3D).
  • Q1: definition, and the two problems: depends on the start point (normalise by smallest rotation) and on rotation (use the difference).
  • Q3 mix: fracture X-ray with chain code + histogram + gamma (SP25 3d).

Connects to: The boundary it walks is the one morphology extracts (A − (A ⊖ B)), and it follows 8- or 4-adjacency from ch 2.

typeCLOtimeswhere
Chain code + CFD/NCFDCLO24SP24 3d, SP23 2c, F21 B1b, F25 quiz Q3D 2
Chain code definition / problemsCLO12SP23 2c, F21 B1b
Case: mixed techniques - applications, advantages, limitationsCLO31SP25 3d
Hough transformCLO21SP25 3d
Formula wall: every formula with a worked line

Compass: 0 = right, counting anticlockwise: 1 ↗, 2 ↑, 3 ↖, 4 ←, 5 ↙, 6 ↓, 7 ↘ (4-direction: 0 →, 1 ↑, 2 ←, 3 ↓).

  1. CC: walk the boundary in the direction asked; write the arrow number of each move until you are back at the start.
  2. CFD trick: from each digit to the next, count anticlockwise steps on the compass. Same arrow = 0. The last digit goes to the first.
  3. NCFD trick: underline the 0s. Start at the longest run of 0s (the end joins the start), read to the end, wrap round. Two runs tie? Compare the digits right after them. No 0? Use the smallest digit.

Worked (slide 12 / SP24): CC = 0 0 7 7 5 6 5 5 4 3 2 2 2 1 2

CFD: 0→0 = 0 steps, 0→7 = 7 steps, 7→7 = 0, 7→5 = 6, … last 2→0 = 6. CFD = 070617077700716

0s at places 1, 3, 7, 11, 12. Longest run = places 11–12 (two 0s). Start there: 0 0 7 1 6 | 0 7 0 6 1 7 0 7 7 7 → NCFD = 007160706170777

whatinvariant to
CCtranslation
smallest shift of CC+ start point
CFD+ rotation (45° steps)
NCFD = shape numberstart point + rotation

Past questions: look, recall, then open the answer

SP25 3dCLO3 · Case: mixed techniques - applications, advantages, limitations · 7.5CLO2 · Hough transform · 2.5
A medical imaging team analyzes X-ray images of bones to detect fractures. They apply chain code representation, histogram analysis, Hough transform and power-law transformation. Discuss how each technique contributes to detection and enhancement of fractures, highlighting applications, advantages or limitations.
Worked answer (deck 10)
SP25 Final (CLO3 case study)case study

A medical imaging team analyses X-ray images of bones to detect fractures. They apply chain code representation, histogram analysis, the Hough transform and power-law transformation. Discuss how each technique contributes to detecting fractures and enhancing the image, with applications, advantages or limitations.

Power-law (gamma) transformation s = c·rγ first adjusts contrast: γ < 1 brightens dark regions so thin, faint fracture lines in dense bone become visible; γ > 1 darkens washed-out regions. Cheap, pointwise; limitation: one γ for the whole image, needs tuning.

Histogram analysis (and equalisation) shows how gray levels are spread; it helps choose a threshold to separate bone from soft tissue and spreads crowded levels to raise contrast. Limitation: global, can amplify noise.

Chain code representation: after segmenting the bone, its outer boundary is written as a chain code. A healthy bone outline is smooth (CFD mostly 0, small turns); a fracture shows as a sudden break or sharp turns (large CFD digits such as 2–6) or a gap in the boundary. The normalised CFD (shape number) lets the team compare the bone's shape with a template or an earlier scan regardless of start point or rotation (patient position). Advantages: compact, translation and (after normalisation) rotation invariant. Limitations: sensitive to noise and scale; resample the boundary first.

Hough transform finds straight lines (and other parametric shapes) even when broken: fracture lines are often line-like, so peaks in the (ρ, θ) accumulator locate them and their angle, and bone shafts can be fitted as lines to spot misalignment. Robust to gaps and noise; limitation: heavier computation, needs good edge maps.

Together: power-law + histogram → clearer image; segmentation and chain code → bone outline and shape change; Hough → fracture line position and orientation.

SP24 3dCLO2 · Chain code + CFD/NCFD · 8
Chain code for boundary of an object (grid figure, clockwise). Compute the chain code which is invariant to both the starting point and rotation. Show final chain code and illustrate the proof. [4+4]
Worked answer (deck 10)
SP24 Final Q3(d)4 + 4 marks

Write the chain code for the boundary of the object (grid figure, clockwise, start at the arrow). Compute the chain code which is invariant to both the starting point and rotation. Show the final chain code and illustrate the proof.

This is the slide 12 digit-0 boundary.

Step 1 · chain code. Start at the arrowed pixel (0,2) and walk clockwise round the boundary. Each move to the next boundary pixel gets the number of its direction (8-direction compass: 0 = right, then counterclockwise).

01234560123456007756554322212
boundary with each move's code · ringed = start
01234567
8-direction code: 0 = right, counting counterclockwise, 45° apart
movefrom → todirectioncode
1(0,2) → (0,3)right →0
2(0,3) → (0,4)right →0
3(0,4) → (1,5)down-right ↘7
4(1,5) → (2,6)down-right ↘7
5(2,6) → (3,5)down-left ↙5
6(3,5) → (4,5)down ↓6
7(4,5) → (5,4)down-left ↙5
8(5,4) → (6,3)down-left ↙5
9(6,3) → (6,2)left ←4
10(6,2) → (5,1)up-left ↖3
11(5,1) → (4,1)up ↑2
12(4,1) → (3,1)up ↑2
13(3,1) → (2,1)up ↑2
14(2,1) → (1,2)up-right ↗1
15(1,2) → (0,2)up ↑2
CC = 007756554322212  (15 moves)

Step 2 · circular first difference (CFD). Slide 22 rule: CFDi = (ci+1 − ci) mod 8; the last digit wraps round to the first. This removes rotation.

i123456789101112131415
this ci007756554322212
next ci+1077565543222120
next − this070-21-10-1-1-100-11-2
mod 8 = CFD070617077700716
first difference, last column wraps round (next of the last digit = the first digit)
CFD = 070617077700716

Step 3 · normalise (NCFD). Smallest circular shift of the CFD. This removes the start point too.

from 1070617077700716
from 2706170777007160
from 3061707770071607
from 4617077700716070
from 5170777007160706
from 6707770071607061
from 7077700716070617
from 8777007160706170
from 9770071607061707
from 10700716070617077
from 11007160706170777
from 12071607061707770
from 13716070617077700
from 14160706170777007
from 15607061707770071
all circular shifts of the CFD · highlighted = smallest
AnswerChain code invariant to start point and rotation (NCFD, shape number of slides 21–22) = 007160706170777.

Proof (illustration). (a) Start point. Start instead at pixel (3,5) (move 6): CC′ = 655432221200775. Its CFD is 707770071607061, a circular shift of the CFD above, and its smallest shift is again 007160706170777.

(b) Rotation. Rotate the object 90° counterclockwise: every move turns by 90°, so every code digit increases by 2 (mod 8): CC″ = 221170776544434. CFD″ = 070617077700716, identical to the CFD above, because (ci+1 + 2) − (ci + 2) = ci+1 − ci. So NCFD″ = 007160706170777.

Same final code in both cases, so the NCFD is invariant to the start point and to rotation.

Try it, step by step: SP24 boundary (same as slide 12). After the run, change the difference rule to see why you must name it.

SP23 2cCLO1 · Chain code definition / problems · 1CLO2 · Chain code + CFD/NCFD · 4
What is Chain Code? For the boundary figure (counterclockwise), compute the chain code that is invariant to starting point (8-direction). [1+4]
Worked answer (deck 10)
SP23 Final Q2(c)1 + 4 marks

What is chain code? For the boundary figure (counterclockwise), compute the chain code that is invariant to the starting point (8-direction).

Chain code is a boundary representation: starting from a boundary pixel, the contour is written as a sequence of direction numbers (0–7 for 8-connectivity, 0 = right, counterclockwise), one per step to the next boundary pixel. It is compact and translation invariant.

Reading the figure. Polygon edges counterclockwise from the bottom vertex: up-right ×2, right ×2, up ×2, up-left ×2, left ×2, down-left ×2, down-right ×2, down ×2.

Step 1 · chain code. Start at the bottom vertex and walk counterclockwise round the boundary. Each move to the next boundary pixel gets the number of its direction (8-direction compass: 0 = right, then counterclockwise).

012345601234561100223344557766
boundary with each move's code · ringed = start
01234567
8-direction code: 0 = right, counting counterclockwise, 45° apart
CC = 1100223344557766  (16 moves)

Step 2 · start-point invariance (shape number of slide 18). The boundary is a loop, so any digit can be the first. Write the code from every start (circular shifts) and pick the one that reads as the smallest number.

from 11100223344557766
from 21002233445577661
from 30022334455776611
from 40223344557766110
from 52233445577661100
from 62334455776611002
from 73344557766110022
from 83445577661100223
from 94455776611002233
from 104557766110022334
from 115577661100223344
from 125776611002233445
from 137766110022334455
from 147661100223344557
from 156611002233445577
from 166110022334455776
all circular shifts of the CC · highlighted = smallest
AnswerStart-point invariant chain code = 0022334455776611 (smallest shift, starting at digit 3).

(Not asked, but if they add rotation: CFD = 0702010101020703, NCFD = 0101010207030702.)

Try it, step by step: SP23 polygon (code 1100223344557766). Normalise is set to CC because the question asks only for start-point invariance.

F21 B1bCLO1 · Chain code definition / problems · 3CLO2 · Chain code + CFD/NCFD · 3
Boundary grid figure (anticlockwise). i) What is Chain Code? Problems in chain code representation [3]. ii) Chain code invariant to both starting point and rotation, 8-neighbor [3].
Worked answer (deck 10)
F21 Final B1(b)3 + 3 marks

(i) What is chain code? Problems in chain code representation. (ii) For the boundary (grid figure, anticlockwise), find the chain code invariant to both starting point and rotation, 8-neighbour.

(i) A chain code represents a boundary by a start pixel and a sequence of direction numbers (8-dir: 0 = right, 1 = up-right, … 7 = down-right), one per step along the contour. Problems: it depends on the starting point; it changes with rotation; its length changes with scale; it is sensitive to noise (small bumps change many digits); codes get long for big boundaries; different traversal/connectivity choices give different codes (ambiguity). Fixes: shape number / NCFD and resampling.

(ii)

Step 1 · chain code. Start at the arrowed bottom pixel (8,3) and walk anticlockwise round the boundary. Each move to the next boundary pixel gets the number of its direction (8-direction compass: 0 = right, then counterclockwise).

01234567891001234567811002100233344456556677
boundary with each move's code · ringed = start
01234567
8-direction code: 0 = right, counting counterclockwise, 45° apart
movefrom → todirectioncode
1(8,3) → (7,4)up-right ↗1
2(7,4) → (6,5)up-right ↗1
3(6,5) → (6,6)right →0
4(6,6) → (6,7)right →0
5(6,7) → (5,7)up ↑2
6(5,7) → (4,8)up-right ↗1
7(4,8) → (4,9)right →0
8(4,9) → (4,10)right →0
9(4,10) → (3,10)up ↑2
10(3,10) → (2,9)up-left ↖3
11(2,9) → (1,8)up-left ↖3
12(1,8) → (0,7)up-left ↖3
13(0,7) → (0,6)left ←4
14(0,6) → (0,5)left ←4
15(0,5) → (0,4)left ←4
16(0,4) → (1,3)down-left ↙5
17(1,3) → (2,3)down ↓6
18(2,3) → (3,2)down-left ↙5
19(3,2) → (4,1)down-left ↙5
20(4,1) → (5,1)down ↓6
21(5,1) → (6,1)down ↓6
22(6,1) → (7,2)down-right ↘7
23(7,2) → (8,3)down-right ↘7
CC = 11002100233344456556677  (23 moves)

Step 2 · circular first difference (CFD). Slide 22 rule: CFDi = (ci+1 − ci) mod 8; the last digit wraps round to the first. This removes rotation.

i1234567891011121314151617181920212223
this ci11002100233344456556677
next ci+110021002333444565566771
next − this0-102-1-10210010011-101010-6
mod 8 = CFD07027702100100117010102
first difference, last column wraps round (next of the last digit = the first digit)
CFD = 07027702100100117010102

Step 3 · normalise (NCFD). Smallest circular shift of the CFD. This removes the start point too.

from 107027702100100117010102
from 270277021001001170101020
from 302770210010011701010207
from 427702100100117010102070
from 577021001001170101020702
from 670210010011701010207027
from 702100100117010102070277
from 821001001170101020702770
from 910010011701010207027702
from 1000100117010102070277021
from 1101001170101020702770210
from 1210011701010207027702100
from 1300117010102070277021001
from 1401170101020702770210010
from 1511701010207027702100100
from 1617010102070277021001001
from 1770101020702770210010011
from 1801010207027702100100117
from 1910102070277021001001170
from 2001020702770210010011701
from 2110207027702100100117010
from 2202070277021001001170101
from 2320702770210010011701010
all circular shifts of the CFD · highlighted = smallest
AnswerChain code invariant to start point and rotation (NCFD, shape number of slides 21–22) = 00100117010102070277021.

Try it, step by step: F21 boundary, anticlockwise from the arrow at the bottom (23 moves).

F25 quiz Q3D 2CLO2 · Chain code + CFD/NCFD · 14
Boundary grid figure (anticlockwise). Compute the 8-directional chain code that is: i) invariant to the starting point [7] ii) invariant to both the starting point and rotation [7].
Worked answer (F25 quiz page)
F25 Quiz (Q3D) Q27 + 7 marks

For the boundary in the grid figure (anticlockwise), compute the 8-directional chain code that is (i) invariant to the starting point; (ii) invariant to both the starting point and rotation.

Step 1 · chain code. Start at the top pixel (1,2) and walk anticlockwise round the boundary. Each move to the next boundary pixel gets the number of its direction (8-direction compass: 0 = right, then counterclockwise).

0123401234565576711233
boundary with each move's code · ringed = start
01234567
8-direction code: 0 = right, counting counterclockwise, 45° apart
movefrom → todirectioncode
1(1,2) → (2,1)down-left ↙5
2(2,1) → (3,0)down-left ↙5
3(3,0) → (4,1)down-right ↘7
4(4,1) → (5,1)down ↓6
5(5,1) → (6,2)down-right ↘7
6(6,2) → (5,3)up-right ↗1
7(5,3) → (4,4)up-right ↗1
8(4,4) → (3,4)up ↑2
9(3,4) → (2,3)up-left ↖3
10(2,3) → (1,2)up-left ↖3
CC = 5576711233  (10 moves)

Step 2 · start-point invariance (shape number of slide 18). The boundary is a loop, so any digit can be the first. Write the code from every start (circular shifts) and pick the one that reads as the smallest number.

from 15576711233
from 25767112335
from 37671123355
from 46711233557
from 57112335576
from 61123355767
from 71233557671
from 82335576711
from 93355767112
from 103557671123
all circular shifts of the CC · highlighted = smallest
AnswerStart-point invariant chain code = 1123355767 (smallest shift, starting at digit 6).

Step 3 · circular first difference (CFD). Slide 22 rule: CFDi = (ci+1 − ci) mod 8; the last digit wraps round to the first. This removes rotation.

i12345678910
this ci5576711233
next ci+15767112335
next − this02-11-601102
mod 8 = CFD0271201102
first difference, last column wraps round (next of the last digit = the first digit)
CFD = 0271201102

Step 4 · normalise (NCFD). Smallest circular shift of the CFD. This removes the start point too.

from 10271201102
from 22712011020
from 37120110202
from 41201102027
from 52011020271
from 60110202712
from 71102027120
from 81020271201
from 90202712011
from 102027120110
all circular shifts of the CFD · highlighted = smallest
AnswerChain code invariant to start point and rotation (NCFD, shape number of slides 21–22) = 0110202712.

Try it, step by step: F25 quiz boundary. Set “Normalise” to CC for part (i), CFD for part (ii).

TrapPart (i) = shape number of the chain code itself (start at the smallest rotation). Part (ii) = first difference (CFD) first, then its shape number (NCFD). Use the CFD/NCFD trick from the chain-code trainer.
More worked examples: slides and other examiners (2)
Slide 22 · worked in full (4-direction) (deck 10)
Slide 22 · worked in full (4-direction)worked

CC = 0 3 0 3 2 2 1 1 (start at the circled corner).

i12345678
this ci03032211
next ci+130322110
next − this3-33-10-10-1
mod 4 = CFD31330303
first difference, last column wraps round (next of the last digit = the first digit)

CFD = 31330303. Circular shifts: 31330303, 13303033, 33030331, 30303313, 03033133, 30331330, 03313303, 33133030. Smallest = 03033133.

Slide 29 · Class work (deck 10)
Slide 29 · Class workworked

An image is given (dots on a grid). Write down the 8-chain code and find the shape number of it. Starting point: the top dot marked by the arrow.

Step 1 · chain code. Start at the top dot (0,3), the arrow and walk clockwise round the boundary. Each move to the next boundary pixel gets the number of its direction (8-direction compass: 0 = right, then counterclockwise).

012345601234567076666553321212
boundary with each move's code · ringed = start
01234567
8-direction code: 0 = right, counting counterclockwise, 45° apart
movefrom → todirectioncode
1(0,3) → (0,4)right →0
2(0,4) → (1,5)down-right ↘7
3(1,5) → (2,5)down ↓6
4(2,5) → (3,5)down ↓6
5(3,5) → (4,5)down ↓6
6(4,5) → (5,5)down ↓6
7(5,5) → (6,4)down-left ↙5
8(6,4) → (7,3)down-left ↙5
9(7,3) → (6,2)up-left ↖3
10(6,2) → (5,1)up-left ↖3
11(5,1) → (4,1)up ↑2
12(4,1) → (3,2)up-right ↗1
13(3,2) → (2,2)up ↑2
14(2,2) → (1,3)up-right ↗1
15(1,3) → (0,3)up ↑2
CC = 076666553321212  (15 moves)

Step 2 · start-point invariance (shape number of slide 18). The boundary is a loop, so any digit can be the first. Write the code from every start (circular shifts) and pick the one that reads as the smallest number.

from 1076666553321212
from 2766665533212120
from 3666655332121207
from 4666553321212076
from 5665533212120766
from 6655332121207666
from 7553321212076666
from 8533212120766665
from 9332121207666655
from 10321212076666553
from 11212120766665533
from 12121207666655332
from 13212076666553321
from 14120766665533212
from 15207666655332121
all circular shifts of the CC · highlighted = smallest
AnswerStart-point invariant chain code = 076666553321212 (smallest shift, starting at digit 1).

Step 3 · circular first difference (CFD). Slide 22 rule: CFDi = (ci+1 − ci) mod 8; the last digit wraps round to the first. This removes rotation.

i123456789101112131415
this ci076666553321212
next ci+1766665533212120
next − this7-1000-10-20-1-11-11-2
mod 8 = CFD770007060771716
first difference, last column wraps round (next of the last digit = the first digit)
CFD = 770007060771716

Step 4 · normalise (NCFD). Smallest circular shift of the CFD. This removes the start point too.

from 1770007060771716
from 2700070607717167
from 3000706077171677
from 4007060771716770
from 5070607717167700
from 6706077171677000
from 7060771716770007
from 8607717167700070
from 9077171677000706
from 10771716770007060
from 11717167700070607
from 12171677000706077
from 13716770007060771
from 14167700070607717
from 15677000706077171
all circular shifts of the CFD · highlighted = smallest
AnswerChain code invariant to start point and rotation (NCFD, shape number of slides 21–22) = 000706077171677.

Try it, step by step: slide 29 class work, from the arrowed top dot.

7. Morphology: erosion, dilation, opening, closing, hit-or-miss

decks 11–12 · 16 past questions
The pattern: how she asks it
  • Q2 / Q3: erode or dilate a grid with a given SE, then open or close it (SP24 3c, SP22 4b, F23 2d, SP23 1a).
  • Q1: define dilation as a set, explain opening and closing, and what closing does to small holes (SP25 1f, SP24 1c).
  • Q3 case: which operation and why: noise specks mean opening, gaps or holes mean closing, touching coins mean erosion then count, boundary = A − erosion (SP25 3c, F23 3b). Hit-or-miss finds corners and end points (F23 2e, SP23 3c).

Connects to: It works on binary masks, the output of thresholding (ch 8). Boundary extraction feeds the chain code.

typeCLOtimeswhere
Morphology: SE, erosion/dilation eq & effects, opening/closingCLO16SP25 1f, SP24 1c, F22 A2c, SP22 4a, F21 B1a, F25 quiz Q4C 1
Dilation / erosion / opening / closing on a gridCLO25SP24 3c, F23 2d, SP23 1a, SP22 4b, F25 quiz Q4C 2
Case: which morphological op and whyCLO34SP25 3c, F23 3b, F22 A3c, F21 B1a
Hit-or-miss: corners / end pointsCLO24F23 2e, SP23 3c, F22 A3c, F21 B1a
Formula wall: every formula with a worked line
operationequationplain ruleeffect
erosionA ⊖ B = {z | (B)z ⊆ A}SE must fit (all 1s on object)shrink, delete specks, split joins
dilationA ⊕ B = {z | (B̂)z ∩ A ≠ ∅}SE must hit (any 1 on object)grow, fill holes, join gaps
openingA ∘ B = (A ⊖ B) ⊕ Berode, then dilateremove small objects and thin bridges, size kept
closingA • B = (A ⊕ B) ⊖ Bdilate, then erodefill small holes and gaps, size kept
boundaryβ(A) = A − (A ⊖ B)object minus its eroded self1-pixel inner outline
hit-or-missA ⊛ B = (A ⊖ B₁) ∩ (Aᶜ ⊖ B₂)1s match object AND 0s match background; × ignoredcorners, end points, patterns
thinning / thickeningA − (A ⊛ B) / A ∪ (A ⊛ B)subtract / add the matchesskeleton / grow outline

1-D worked, row 1 0 0 0 1 1 1 0 1 1, SE [1 1 1] (ends ignored):

Erosion takes the minimum under the SE: windows 100, 000, 001, 011, 111, 110, 101, 011 → 0 0 0 0 1 0 0 0.

Dilation takes the maximum: → 1 0 1 1 1 1 1 1 (only the 000 window stays 0).

Memory hook: Erosion Eats, Dilation Deposits. Opening = E then D (opens gaps between things, removes specks). Closing = D then E (closes holes).

TrapShow the middle image (A ⊖ B or A ⊕ B) in every opening / closing answer. For hit-or-miss draw each SE with 1 / 0 / ×, one output grid per SE, then the OR.

Past questions: look, recall, then open the answer

SP25 1fCLO1 · Morphology: SE, erosion/dilation eq & effects, opening/closing · 4
Define dilation mathematically for a binary image. Explain the opening and closing morphological operations. What happens when you apply closing to an image with small holes?
Worked answer (deck 11)
SP25 Q1(f)4 marks

Define dilation mathematically for a binary image. Explain the opening and closing operations. What happens when you apply closing to an image with small holes?

Dilation: A ⊕ B = {z | (B̂)z ∩ A ≠ ∅}: the set of all displacements z such that the reflected SE B̂, translated to z, overlaps A in at least one pixel. It grows objects, fills small holes and bridges gaps.

Opening A ∘ B = (A ⊖ B) ⊕ B, an erosion followed by a dilation: removes objects and protrusions smaller than B, breaks thin bridges and smooths the outline, without changing the size of the remaining objects.

Closing A • B = (A ⊕ B) ⊖ B, a dilation followed by an erosion: fills small holes and gaps, joins nearby objects, and smooths indentations, again without changing the core size.

Closing on small holes: the dilation fills every hole smaller than B (the SE reaches the object on all sides of the hole); the following erosion shrinks the object back to its original outline, but the filled holes cannot reopen because the SE now fits there. So the small holes disappear and the object's size and boundary stay the same (e.g. the pizza image closed with a 22-pixel disc).

SP25 3cCLO3 · Case: which morphological op and why · 10
A satellite image shows the road networks of the AUST campus, but has noise (small bright spots), gaps in roads and overlapping structures. i) Which morphological operation would you apply first to remove the small bright spots noise, and why? [3] ii) After noise removal roads appear fragmented with small gaps; which operation connects them and how? [3] iii) To highlight the boundaries of roads, develop the most suitable morphological operation [3]. iv) Can highlighting boundaries help distinguish roads from background terrain? [1]
Worked answer (deck 11)
SP25 Q3(c)3 + 3 + 3 + 1 marks

A satellite image of the AUST campus road network has noise (small bright spots), gaps in roads and overlapping structures. (i) Which morphological operation first removes the small bright spots, and why? (ii) After noise removal the roads are fragmented with small gaps: which operation connects them, and how? (iii) Develop the most suitable operation to highlight road boundaries. (iv) Can highlighting boundaries help distinguish roads from the background terrain?

(i) Opening, A ∘ B = (A ⊖ B) ⊕ B, with a small disk/square SE larger than the spots. The erosion deletes every bright spot that cannot contain B; the dilation then grows the roads back to their original width, so only the noise is removed. (Dilation first would enlarge the spots instead.)

(ii) Closing, A • B = (A ⊕ B) ⊖ B, with an SE a bit longer than the gaps (a line SE along the road direction works best). The dilation joins the road fragments across each gap; the erosion shrinks the roads back to their width, but the filled gaps stay filled, so the network becomes connected without fattening.

(iii) Boundary extraction β(A) = A − (A ⊖ B) with a 3×3 SE: erode the cleaned road image and subtract it from itself, leaving a one-pixel outline of every road. (Or (A ⊕ B) − A for the outer edge.)

(iv) Yes. The boundaries outline the road shapes sharply, separating them from the surrounding terrain; they can be used to segment, measure and map the road network.

SP24 1cCLO1 · Morphology: SE, erosion/dilation eq & effects, opening/closing · 5
Show the mathematical equation of Erosion and Dilation. Describe the Opening and Closing morphological operations. [1+2+2]
Worked answer (deck 11)
SP24 Q1(c) · SP22 Q4(a) · Fall 25 quiz (set C) Q13–5 marks

Give the equations of erosion and dilation; describe opening and closing. / Describe two basic morphological operations with equations, effects and applications. / Define dilation and erosion in binary images; how do they affect object size and shape?

Erosion A ⊖ B = {z | (B)z ⊆ A}: a pixel stays 1 only where the SE fits inside the object. Effects: shrinks foreground objects (removes the outer layer), removes objects smaller than B, smooths the boundary by cutting spikes, can split touching objects, enlarges holes. Uses: removing isolated noise pixels, separating touching coins before counting, boundary extraction β(A) = A − (A ⊖ B).

Dilation A ⊕ B = {z | (B̂)z ∩ A ≠ ∅}: a pixel becomes 1 where the SE hits the object. Effects: expands foreground objects (adds an outer layer), fills small holes, bridges gaps, joins nearby objects, smooths intrusions. Uses: repairing broken characters in low-resolution text, filling holes, edge detection (A ⊕ B) − A.

Size and shape: erosion makes objects smaller and thinner (size shrinks by the SE radius); dilation makes them larger and thicker. The SE's shape decides the direction: a vertical SE acts only vertically, a disk rounds corners.

Opening (A ⊖ B) ⊕ B removes small objects and thin bridges, keeps size. Closing (A ⊕ B) ⊖ B fills small holes and gaps, keeps size. Both are idempotent.

SP24 3cCLO2 · Dilation / erosion / opening / closing on a grid · 8
Binary image of Greek letter theta (Fig 3.1). What morphological operation(s) would you use to obtain the number zero (0) from theta? Four SEs given (horizontal 1x2, cross, vertical 1x2 variants), use any as many times. Explain choice and show outputs in the grids. [2+2+4]
Worked answer (deck 11)
SP24 Q3(c)2 + 2 + 4 marks

Binary image of the Greek letter θ. Which morphological operation(s) turn θ into the digit 0? Four SEs are given: SE1 horizontal 1×2, SE2 cross, SE3 vertical 2×1 (origin + below), SE4 vertical 3×1. Use any as many times as needed; explain the choice and show the outputs.

0001111000
0011111100
0110000110
0110000110
0110000110
1100000011
1111111111
1100000011
0110000110
0110000110
0110000110
0011111100
0001111000
θ (13×10)

What must go: the middle bar (row 6) is only 1 pixel tall. Every part of the ring is at least 2 pixels thick in the vertical direction. So an opening with SE3 (vertical, 2 pixels) removes the bar and keeps the ring: θ ∘ SE3 = (θ ⊖ SE3) ⊕ SE3.

Why not the others: SE1 is horizontal and the bar is long horizontally, so the bar survives. The cross (SE2) needs 3 pixels in both directions, which also deletes the thin ring corners. SE4 needs 3 pixels vertically, which deletes the 2-row top and bottom of the ring.

Step 1, erosion θ ⊖ SE3 (keep a pixel only if it and the pixel below are both 1). Step 2, dilation by SE3 (put back the pixel below every survivor).

0001111000
0010000100
0110000110
0110000110
0100000010
1100000011
1100000011
0100000010
0110000110
0110000110
0010000100
0001111000
0000000000
θ ⊖ SE3
0001111000
0011111100
0110000110
0110000110
0110000110
1100000011
1100000011
1100000011
0110000110
0110000110
0110000110
0011111100
0001111000
θ ∘ SE3 = the digit 0

Try it, step by step: SP24: the opening with SE3, both passes.

F23 2dCLO2 · Dilation / erosion / opening / closing on a grid · 6
Image A (5x6 binary grid) and structuring element B (3x1 vertical, all 1). Compute i) Reflection of B ii) A dilated by B iii) A^c eroded by B iv) Opening of A by B (write process with mathematical equation). [1+1+2+2]
Worked answer (deck 11)

Same answer as SP22 4b (SP22 Q4(b) · F23 Q2(d) (same image)).

F23 2eCLO2 · Hit-or-miss: corners / end points · 6
Apply morphological Hit-and-Miss transformation to detect any corner from an image ('CS' binary image A). i) Design four structuring elements for detecting four different corners and explain. ii) Applying your SEs extract all corners of image A; show output for each SE and combined final output. [3+3]
Worked answer (deck 12)
F23 Q2(e)3 + 3 marks

Apply the hit-and-miss transform to detect the corners of the binary image A ("CS"). (i) Design four SEs for detecting the four different corners and explain them. (ii) Using your SEs, extract all corners of A; show the output for each SE and the combined final output.

(i) Each SE is 3×3 with the origin at the centre: 1 = must be object, 0 = must be background, × = don't care. The centre and the two arm pixels are 1; the two opposite neighbours and the cell between them are 0; the rest are ×. B1 finds bottom-left corners (arms going up and right), B2 bottom-right, B3 top-right, B4 top-left. Outside the image counts as background.

(ii)

×1×
011
00×
B1 (up + right: bottom-left corner)
0000000000000
0000000000000
0000000000000
0000000000000
0000000100000
0000000000000
0000000000000
0100000100000
A ⊛ B1: 3 found (4, 7), (7, 1), (7, 7)
×1×
110
×00
B2 (up + left: bottom-right corner)
0000000000000
0000000000000
0000000000000
0000000000000
0000000000000
0000000000000
0000000000000
0000010000010
A ⊛ B2: 2 found (7, 5), (7, 11)
×00
110
×1×
B3 (down + left: top-right corner)
0000000000000
0000010000010
0000000000000
0000000000000
0000000000010
0000000000000
0000000000000
0000000000000
A ⊛ B3: 3 found (1, 5), (1, 11), (4, 11)
00×
011
×1×
B4 (down + right: top-left corner)
0000000000000
0100000100000
0000000000000
0000000000000
0000000000000
0000000000000
0000000000000
0000000000000
A ⊛ B4: 2 found (1, 1), (1, 7)

Combine with OR: a pixel is a corner if any SE marked it.

0000000000000
0111110111110
0100010100010
0100000100000
0100000111110
0100000000010
0100010100010
0111110111110
input A
0000000000000
0100010100010
0000000000000
0000000000000
0000000100010
0000000000000
0000000000000
0100010100010
final: 10 corners

Try it, step by step: F23 CS image: each corner SE in turn, then the OR. Use “Next phase” to move to the next SE.

F23 3bCLO3 · Case: which morphological op and why · 10
i) Automated coin counting: coins are circular blobs but some touch. Which morphological operations separate touching coins for accurate counting? Why? [5] ii) Microscopic particle image (Fig.2): remove small insignificant particles keeping larger ones. Which morphological operations? Explain mathematically with applications. [5]
Worked answer (deck 11)
F23 Q3(b)5 + 5 marks

(i) Automated coin counting: coins are circular blobs, but some touch. Which morphological operations separate touching coins for accurate counting, and why? (ii) A microscope image has small insignificant particles and larger ones: which operations remove the small ones but keep the large ones? Explain mathematically, with applications.

(i) Binarize the image, then apply erosion A ⊖ B = {z | (B)z ⊆ A} with a disk SE (repeated if needed). Touching coins are joined only by narrow necks; the disk cannot fit in a neck, so the necks disappear first and each coin becomes a separate, smaller blob. Then count the blobs with connected-component labeling (slides 43–44). Counting is not affected by the coins shrinking. (A dilation afterwards, i.e. an opening, would restore size, but could rejoin coins, so count after the erosion.)

(ii) Opening with an SE (disk) a little larger than the insignificant particles: A ∘ B = (A ⊖ B) ⊕ B. Erosion removes every particle that cannot contain B, i.e. all the small ones; the larger particles survive as shrunken cores, and the dilation grows them back to almost their original shape and size. Applications: cleaning cell or particle images before counting, removing dust in industrial inspection, removing noise in scanned documents.

SP23 1aCLO2 · Dilation / erosion / opening / closing on a grid · 9
Images X and Y (binary grids). i) Design a structuring element so that Y = X dilated; mark origin [3]. ii) Mathematical expression of the dilation [2]. iii) Erode Y with the same SE; show eroded image [2]. iv) Explain erosion mathematically [2].
Worked answer (deck 11)
SP23 Q1(a)3 + 2 + 2 + 2 marks

Images X and Y are given. (i) Design a structuring element B so that Y = X ⊕ B; mark the origin. (ii) Write the mathematical expression of the dilation. (iii) Erode Y with the same SE and show the eroded image. (iv) Explain erosion mathematically.

000000
000110
000000
000000
001000
000100
000010
X
011110
011100
000000
101000
110100
011010
001100
Y

(i) Take an isolated pixel of X, e.g. (6,4). In Y it has become (5,4), (6,3), (6,2), (5,2), and (6,4) itself is gone. So B holds the offsets (−1,0), (0,−1), (0,−2), (−1,−2) relative to its origin, and the origin itself is not part of B (value 0). Every other pixel of X gives the same pattern, which confirms it.

101
110
B (2×3), origin = bottom-right cell, value 0

Check: X ⊕ B reproduces Y exactly.

(ii) X ⊕ B = {z | (B̂)z ∩ X ≠ ∅} = {x + b | x ∈ X, b ∈ B}.

(iii) Y ⊖ B = {z | (B)z ⊆ Y}: z survives only if all four offsets from z land on 1s in Y.

000000
000110
000000
000000
001000
000100
000010
Y ⊖ B

Here Y ⊖ B gives back X exactly (this image is lucky; in general (X ⊕ B) ⊖ B is the closing, which can also fill gaps).

(iv) A ⊖ B = {z | (B)z ⊆ A}: the set of all origin positions z where the SE translated by z lies completely inside A. Equivalently, g(x,y) = 1 if B fits A at (x,y), else 0.

Try it, step by step: SP23 (i): X ⊕ B gives Y. The SE is not symmetric, so watch the reflected cells.

Try it, step by step: SP23 (iii): Y ⊖ B.

SP23 3cCLO2 · Hit-or-miss: corners / end points · 8
Binary image 'CSE-44'. i) Which morphological technique locates all end points? ii) Design four SEs for detecting four different end points. iii) Extract all end points using your SEs, explain. iv) How many end points detected in final output? [1+4+2+1]
Worked answer (deck 12)
SP23 Q3(c)1 + 4 + 2 + 1 marks

Binary image "CSE-44". (i) Which morphological technique locates all end points? (ii) Design four SEs for four different end points. (iii) Extract all end points with your SEs and explain. (iv) How many end points are in the final output?

(i) The hit-or-miss transform A ⊛ B, with one SE per end direction, combined by OR.

(ii) The four endpoint SEs above (centre 1, three 4-neighbours 0, the arm ×).

(iii)

×××
010
×0×
E1 (open above)
000000000000000000000000
000000000000000000000000
000100000000000000000000
000000000000000100000000
000000000000000000000000
000000000000000000010001
000000000000000000000000
A ⊛ E1: 4 found (2, 3), (3, 15), (5, 19), (5, 23)
×0×
010
×××
E2 (open below)
000000000000000000000000
000000000000000001010101
000000000000000000000000
000000000000000100000000
000100000000000000000000
000000000000000000000000
000000000000000000000000
A ⊛ E2: 6 found (1, 17), (1, 19), (1, 21), (1, 23), (3, 15), (4, 3)
×0×
×10
×0×
E3 (open left)
000000000000000000000000
000000001000010000000000
000000000000000000000000
000000000000100100000000
000000000000000000000000
000000000000010000000000
000000000000000000000000
A ⊛ E3: 5 found (1, 8), (1, 13), (3, 12), (3, 15), (5, 13)
×0×
01×
×0×
E4 (open right)
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000100000000
000000000000000000000000
000001000000000000000000
000000000000000000000000
A ⊛ E4: 2 found (3, 15), (5, 5)

Combine with OR: a pixel is a end point if any SE marked it.

000000000000000000000000
111101111011110001010101
100101000010000001010101
100001111011100101110111
100100001010000000010001
111101111011110000010001
000000000000000000000000
input A
000000000000000000000000
000000001000010001010101
000100000000000000000000
000000000000100100000000
000100000000000000000000
000001000000010000010001
000000000000000000000000
final: 14 end points

(iv) 14 end points in the final output.

Try it, step by step: SP23 CSE-44, each SE then the OR.

F22 A2cCLO1 · Morphology: SE, erosion/dilation eq & effects, opening/closing · 5
What is Structuring Element? What are the effects of Erosion and Dilation operation? Explain briefly with applications. [1+2+2]
Worked answer (deck 11)
F22 A2(c)5 marks

What is a structuring element? What are the effects of erosion and dilation? Explain briefly with applications.

A structuring element is a small binary image (template) of any shape and size (square, cross, disk, line) with a defined origin. It is moved over the image like a filter mask, and at each position the fit or hit test between the SE and the image decides the output pixel.

Effects and applications of erosion and dilation: as in the answer above (erosion: shrink, remove noise, separate objects; dilation: grow, fill holes, bridge gaps, repair broken text).

F22 A3cCLO3 · Case: which morphological op and why · 4CLO2 · Hit-or-miss: corners / end points · 4
Coins image. i) Which morphological operation can be used to count the number of coins? Explain process [4]. ii) Design Hit-or-Miss SEs for locating 4-connected endpoints of an image [2]. iii) Locate all 4-connected endpoints of the given binary image using SEs from ii; draw each result and combined one [2].
Worked answer (deck 11 · deck 12)
F22 A3(c)i · slide 434 marks

Coins image: which morphological operation can be used to count the coins? Explain the process.

Erosion followed by connected-component labeling: binarize; erode with a disk to separate the touching coins (and remove small noise); label the connected components; the number of labels = number of coins.

Slides 30–32 · F22 A3(c) ii–iii2 + 2 marks

(ii) Design hit-or-miss SEs for locating the 4-connected endpoints of an image. (iii) Locate all 4-connected endpoints of the given binary image with them; draw each result and the combined one.

(ii) The four SEs above: centre 1, three 4-neighbours 0, one 4-neighbour × (the arm), diagonals ×.

(iii)

×××
010
×0×
E1 (open above)
0000001000000
0000000000000
0000000000000
0100000000000
0000000000000
0000000000000
0000000000000
A ⊛ E1: 2 found (0, 6), (3, 1)
×0×
010
×××
E2 (open below)
0001001001000
0000000000000
0000010000000
0100000000000
0000000000000
0000000000000
0000000000000
A ⊛ E2: 5 found (0, 3), (0, 6), (0, 9), (2, 5), (3, 1)
×0×
×10
×0×
E3 (open left)
0000001000000
0000000000010
0000000000000
0100000000000
0000000000010
0000000100000
0000000000000
A ⊛ E3: 5 found (0, 6), (1, 11), (3, 1), (4, 11), (5, 7)
×0×
01×
×0×
E4 (open right)
0000001000000
0000000100000
0000000000000
0100000000000
0000000000000
0100000000000
0000000000000
A ⊛ E4: 4 found (0, 6), (1, 7), (3, 1), (5, 1)

Combine with OR: a pixel is a 4-connected endpoint if any SE marked it.

0001001001000
0001000111110
0001010001000
0101110001000
0000000001110
0111111100000
0000000000000
input A
0001001001000
0000000100010
0000010000000
0100000000000
0000000000010
0100000100000
0000000000000
final: 10 4-connected endpoints

Try it, step by step: slide 30–32 / F22 image, each endpoint SE then the OR.

SP22 4aCLO1 · Morphology: SE, erosion/dilation eq & effects, opening/closing · 5
Describe two different morphological basic operations with mathematical equations. Explain their effects and give examples of applications. [5]
Worked answer (deck 11)

Same answer as SP24 1c (SP24 Q1(c) · SP22 Q4(a) · Fall 25 quiz (set C) Q1).

SP22 4bCLO2 · Dilation / erosion / opening / closing on a grid · 8
F 5x6 [[0,0,0,0,0,1],[0,0,1,1,1,0],[0,1,1,1,1,0],[0,1,1,1,0,0],[1,0,0,0,0,0]], B = 3x1 vertical [1,1,1]. Compute i) reflection of B ii) F dilated by B iii) F^c eroded by B iv) (F dilated by B) - F v) Opening of F by B vi) Closing of F by B (with equations). [8]
Worked answer (deck 11)
SP22 Q4(b) · F23 Q2(d) (same image)8 / 6 marks

F (5×6) and B = 3×1 vertical [1; 1; 1] with the origin at the centre. Compute (i) the reflection of B (ii) F ⊕ B (iii) Fc ⊖ B (iv) (F ⊕ B) − F (v) the opening of F by B (vi) the closing of F by B, with equations.

000001
001110
011110
011100
100000
F
1
1
1
B (origin centre)

(i) B̂ = {w | w = −b, b ∈ B}. B = {(−1,0), (0,0), (1,0)} → B̂ = {(1,0), (0,0), (−1,0)} = B. B is symmetric about its origin, so B̂ = B.

(ii) F ⊕ B = {z | (B̂)z ∩ F ≠ ∅}: a pixel becomes 1 if it, the pixel above or the pixel below is 1.

001111
011111
011110
111110
111100
F ⊕ B

(iii) Fc ⊖ B = {z | (B)z ⊆ Fc}: complement F, then keep a pixel only if it and both vertical neighbours are 1 in Fc.

111110
110001
100001
100011
011111
Fc
000000
100000
100001
000001
000000
Fc ⊖ B

(iv) (F ⊕ B) − F = the pixels added by the dilation (an outer edge above and below the object):

001110
010001
000000
100010
011100
(F ⊕ B) − F

(v) F ∘ B = (F ⊖ B) ⊕ B:

000000
000000
001100
000000
000000
F ⊖ B
000000
001100
001100
001100
000000
F ∘ B

(vi) F • B = (F ⊕ B) ⊖ B:

001111
011111
011110
111110
111100
F ⊕ B
000000
001110
011110
011100
000000
F • B

Notice: the opening kept only the columns where the object is at least 3 pixels tall (columns 2–3), so everything thinner than B vertically is gone. The closing kept the whole object; the two isolated pixels (0,5) and (4,0) disappeared because they sit on the image border, where the erosion needs the pixel outside the image, which counts as 0.

Try it, step by step: SP22 / F23: every part, switch the Operation menu.

F21 B1aCLO3 · Case: which morphological op and why · 3CLO1 · Morphology: SE, erosion/dilation eq & effects, opening/closing · 3CLO2 · Hit-or-miss: corners / end points · 2
Text image with broken characters. i) Which morphological operation joins broken characters? [3] ii) Define Opening and Closing mathematically with applications [3] iii) Design four Hit-or-Miss SEs for locating four corners [2].
Worked answer (deck 11 · deck 12)
F21 B1(a) i–ii3 + 3 marks

(i) A text image has broken characters: which morphological operation joins them? (ii) Define opening and closing mathematically, with applications.

(i) Dilation with a small SE such as the 3×3 cross [0 1 0; 1 1 1; 0 1 0] (slide 59): any background pixel whose SE touches a character pixel becomes foreground, so the 1–2 pixel breaks are bridged and the characters become whole. A closing (dilation then erosion) joins the breaks the same way but returns the strokes to their original thickness.

(ii) Opening A ∘ B = (A ⊖ B) ⊕ B: removes small noise and thin bridges (cleaning scanned documents, separating objects). Closing A • B = (A ⊕ B) ⊖ B: fills small holes and gaps (repairing broken text, filling reflections in a thresholded object).

F21 B1(a)iii2 marks

Design four hit-or-miss SEs for locating the four corners of an object.

×1×
011
00×
B1 (up + right: bottom-left corner)
×1×
110
×00
B2 (up + left: bottom-right corner)
×00
110
×1×
B3 (down + left: top-right corner)
00×
011
×1×
B4 (down + right: top-left corner)

1 = object, 0 = background, × = don't care, origin at the centre. Apply each one with A ⊛ Bi and OR the four results.

F25 quiz Q4C 1CLO1 · Morphology: SE, erosion/dilation eq & effects, opening/closing · 3
Define dilation and erosion in binary images. How do they affect object size and shape? [3]
Worked answer (F25 quiz page)
SP24 Q1(c) · SP22 Q4(a) · Fall 25 quiz (set C) Q13–5 marks

Give the equations of erosion and dilation; describe opening and closing. / Describe two basic morphological operations with equations, effects and applications. / Define dilation and erosion in binary images; how do they affect object size and shape?

Erosion A ⊖ B = {z | (B)z ⊆ A}: a pixel stays 1 only where the SE fits inside the object. Effects: shrinks foreground objects (removes the outer layer), removes objects smaller than B, smooths the boundary by cutting spikes, can split touching objects, enlarges holes. Uses: removing isolated noise pixels, separating touching coins before counting, boundary extraction β(A) = A − (A ⊖ B).

Dilation A ⊕ B = {z | (B̂)z ∩ A ≠ ∅}: a pixel becomes 1 where the SE hits the object. Effects: expands foreground objects (adds an outer layer), fills small holes, bridges gaps, joins nearby objects, smooths intrusions. Uses: repairing broken characters in low-resolution text, filling holes, edge detection (A ⊕ B) − A.

Size and shape: erosion makes objects smaller and thinner (size shrinks by the SE radius); dilation makes them larger and thicker. The SE's shape decides the direction: a vertical SE acts only vertically, a disk rounds corners.

Opening (A ⊖ B) ⊕ B removes small objects and thin bridges, keeps size. Closing (A ⊕ B) ⊖ B fills small holes and gaps, keeps size. Both are idempotent.

Try it, step by step: a 4×4 square with a spike and a lone noise pixel. Erosion: the square shrinks to 2×2, the spike and noise vanish. Switch to dilation: everything grows by one pixel all round.

F25 quiz Q4C 2CLO2 · Dilation / erosion / opening / closing on a grid · 7
Construct a 6x6 binary image A and a 3x3 structuring element B. Using fundamental morphological operations, formulate an edge detection algorithm for A with B, and demonstrate the procedure systematically with mathematical expressions. [7]
Worked answer (F25 quiz page)
Fall 25 quiz (set C) Q27 marks

Construct a 6×6 binary image A and a 3×3 structuring element B. Using fundamental morphological operations, formulate an edge-detection algorithm for A with B, and demonstrate the procedure systematically with mathematical expressions.

Choose A = a 4×4 square in a 6×6 image; B = 3×3 square, origin at the centre.

000000
011110
011110
011110
011110
000000
A
111
111
111
B

Algorithm (inner boundary): 1. erode: A ⊖ B = {z | (B)z ⊆ A}; 2. subtract: β(A) = A − (A ⊖ B) = A ∩ (A ⊖ B)c.

Step 1: B fits only where the pixel and all 8 neighbours are 1, i.e. the inner 2×2. Step 2: removing those from A leaves the one-pixel ring.

000000
000000
001100
001100
000000
000000
A ⊖ B
000000
011110
010010
010010
011110
000000
β(A) = A − (A ⊖ B)

Alternative (outer edge): (A ⊕ B) − A, dilation then subtraction, gives the ring just outside the square:

111111
111111
111111
111111
111111
111111
A ⊕ B
111111
100001
100001
100001
100001
111111
(A ⊕ B) − A

Try it, step by step: both edge algorithms: erode then subtract, or dilate then subtract.

More worked examples: slides and other examiners (10)
Slide 13 · fitting and hitting with s1 and s2 (deck 11)
Slide 13 · fitting and hitting with s1 and s2solved

Fill in the fit/hit table for the three positions A, B, C of the binary image, with s1 = 3×3 square and s2 = 3×3 cross (origin at the centre).

000000000000
000110000000
001111100000
011111110000
011111110000
001111110000
001111111000
001111111110
000001111110
000000000000
the image; A, B, C are the 3×3 windows centred at (6,3), (2,3), (2,8)
111
111
111
window A
011
111
111
window B
000
000
100
window C
111
111
111
s1
010
111
010
s2
positionfit s1fit s2hit s1hit s2
Ayesyesyesyes
Bnoyesyesyes
Cnonoyesno

A is all 1s, so both fit and both hit. B has a 0 in one corner: the square does not fit (that corner), but the cross does (it ignores corners). C has a single 1 in a corner: the square hits it, the cross misses it completely.

Slides 19–26 · 1-D erosion (deck 11)
Slides 19–26 · 1-D erosionworked

Input 1 0 0 0 1 1 1 0 1 1, SE [1 1 1] with the origin at the centre. At each position take the minimum of the three values under the SE (superimpose and multiply: all three must be 1). The two end pixels are ignored.

position12345678
window1 0 00 0 00 0 10 1 11 1 11 1 01 0 10 1 1
minimum00001000

Output: 0 0 0 0 1 0 0 0. Only the middle of the 1 1 1 run survives.

Try it, step by step: the 1-D example, position by position (switch the menu to dilation for slides 50–57).

Slides 27–38 · Erosion example 1 (8×8, cross SE) (deck 11)
Slides 27–38 · Erosion example 1 (8×8, cross SE)worked
00000000
00011100
00111100
01111000
01110000
01110000
00000000
00000000
original
010
111
010
SE (origin centre)
00000000
00000000
00011000
00110000
00100000
00000000
00000000
00000000
A ⊖ B

Only 5 pixels keep the whole cross inside the object. Every boundary pixel had at least one cross arm on background.

Try it, step by step: erosion example 1, every one of the 64 positions (slides 27–38).

Slides 43–44 · Exercise: count the coins (deck 11)
Slides 43–44 · Exercise: count the coinssolved

Count the number of coins in the given image by performing an erosion operation.

  1. Binarize the image (threshold): coins become white blobs on a black background. Many coins touch each other, so they form joined blobs.
  2. Erode with a disk SE large enough to break the narrow necks where coins touch. Each coin shrinks to a smaller, separate blob.
  3. Use connected-component labeling to count the separate blobs: the count equals the number of coins.
Slides 50–57 · 1-D dilation (deck 11)
Slides 50–57 · 1-D dilationworked

Same input 1 0 0 0 1 1 1 0 1 1, SE [1 1 1]. Now take the maximum of the three values under the SE (border pixels ignored).

position12345678
window1 0 00 0 00 0 10 1 11 1 11 1 01 0 10 1 1
maximum10111111

Output: 1 0 1 1 1 1 1 1. Only position 2, whose window 0 0 0 has no 1 at all, stays 0.

Slide 58 · Dilation example 1 (same image, cross SE) (deck 11)
Slide 58 · Dilation example 1 (same image, cross SE)worked
00000000
00011100
00111100
01111000
01110000
01110000
00000000
00000000
original
010
111
010
SE
00011100
00111110
01111110
11111100
11111000
11111000
01110000
00000000
A ⊕ B

The dilated pixels (the slide's green cells) are A ⊕ B − A: 16 new pixels, one layer all round.

Try it, step by step: dilation example 1, every position.

Slides 74–87 · Opening example (cross SE) (deck 11)
Slides 74–87 · Opening example (cross SE)worked
00001110
00001110
00001110
00011000
01110000
01110000
01110000
00000000
original A
010
111
010
SE
00000000
00000100
00000000
00000000
00000000
00100000
00000000
00000000
step 1: A ⊖ B
00000100
00001110
00000100
00000000
00100000
01110000
00100000
00000000
step 2: (A ⊖ B) ⊕ B = A ∘ B

Erosion leaves only the two centres where the whole cross fits, (1,5) and (5,2). Dilation puts a cross back on each. The narrow link between the two blobs (row 3) and the blob corners are gone: they were thinner than the SE.

Try it, step by step: the slide 74–87 opening: erosion pass, then dilation pass. Use “Next phase” to jump between them.

Slides 95–108 · Closing example (cross SE) (deck 11)
Slides 95–108 · Closing example (cross SE)worked
00000000
00001110
00001110
00000000
01110000
01110000
01110000
00000000
original A
010
111
010
SE
00001110
00011111
00011111
01111110
11111000
11111000
11111000
01110000
step 1: A ⊕ B
00000000
00001110
00001110
00011000
01110000
01110000
01110000
00000000
step 2: (A ⊕ B) ⊖ B = A • B

Dilation joins the two blobs across the gap. Erosion shrinks them back to their original outlines, but the new pixels (3,3) and (3,4) in the gap survive: closing bridged the gap without growing the objects.

Try it, step by step: the slide 95–108 closing: dilation pass, then erosion pass.

Slide 114 · Exercise: which one should we apply here? (deck 11)
Slide 114 · Exercise: which one should we apply here?solved

The input map of Africa (white land) has black text "Northern Africa", "South Africa" written on it; the output is the solid white shape. Which operation was applied?

Closing. The black letters are small dark holes and gaps inside the white foreground. Dilation fills them (the SE touches the white on both sides of each stroke), and the following erosion restores the coastline, so the outline and the size of the continent are unchanged. Opening would not remove holes.

Slides 11–14 · procedure with an extended SE (deck 12)
Slides 11–14 · procedure with an extended SEworked

Slide the SE [1 1 1; × 0 ×; × 0 ×] over the image like an erosion and look for exact fits: three 1s in the row above, a 0 at the centre, a 0 below.

000000000
001110000
000011110
010011111
011111101
001111101
input image
111
×0×
×0×
SE (origin = centre)
000000000
000000000
000100000
000000000
000000010
000000000
output: 2 fits at (2, 3), (4, 7)

At (2,3): the row above is 1 1 1, the centre is 0 and the pixel below is 0, so it is a fit. Same at (4,7). Every other position fails somewhere.

Slide 14. What we actually did: we searched for the pattern, i.e. we actually performed an erosion, but with both the foreground and the background part of the SE: A ⊛ B = (A ⊖ B1) ∩ (Ac ⊖ B2), where B1 is the set of 1s and B2 the set of 0s.

Try it, step by step: the slide 11–14 search, every position: each 1 and 0 of the SE is compared with the image.

8. Compression: redundancy, Huffman, RLE, LZW, Golomb, arithmetic, RMSE

deck 13 · 17 past questions
The pattern: how she asks it
  • Q2(c) / Q3(b), every paper: Huffman + Lavg + entropy + CR (SP25 2c, SP24 2c, SP23 3a, SP22 5b); LZW table (SP25 3b, F23 3c, SP22 1b); RLE; Golomb Gm(n); RMSE.
  • Q1: data redundancy, the 3 types (coding, spatial/inter-pixel, psychovisual), C = n1/n2, R = 1 − 1/C (SP25 1d, F23 1d).

Connects to: Coding redundancy is what Huffman and Golomb remove, spatial redundancy is what RLE and LZW remove, and RMSE measures what lossy coding loses.

typeCLOtimeswhere
Huffman code + L_avg + entropy + CR + redundancyCLO26SP25 2c, SP24 2c, SP23 3a, F22 A3a, SP22 5b, F21 B2a
Compression: why, 3 redundancies, CR & R_D formulaCLO15SP25 1d, F23 1d, SP23 2a, F22 A2a, SP22 5a
Compression ratio / relative redundancy / entropy numbersCLO24SP25 2c, SP23 3a, F22 A3a, SP22 5a
RMSE fidelity criterionCLO24SP25 3b, SP24 3b, F23 2c, F21 B2b
LZW encoding tableCLO23SP25 3b, F23 3c, SP22 1b
Golomb code G_mCLO23SP24 3b, F23 2c, F21 B2b
Run-length encodingCLO22SP25 2c, F22 A3a
Formula wall: every formula with a worked line
whatformulaworked line
compression ratioCR = n₁ / n₂ (original / compressed)512×512×8 = 2,097,152 bits; 64 KB = 524,288 bits → CR = 4
relative redundancyRD = 1 − 1/CR1 − 1/4 = 0.75 → 75% redundant
average lengthLavg = Σ l(rk) p(rk)Huffman below: 1.9 bits/symbol
entropyH = −Σ p log₂ p0.4·1.322 + 0.3·1.737 + 0.2·2.322 + 0.1·3.322 = 1.8464
efficiencyη = H / Lavg1.8464 / 1.9 = 97.2%
RMSEerms = √[ (1/MN) Σ (f̂ − f)² ]every pixel +2: √(36/9) = 2
SNR (ms)Σ f̂² / Σ (f̂ − f)²248 / 36 = 6.889

Huffman (SP25: A 0.4, B 0.3, C 0.2, D 0.1)

Reduce: sort, merge the two smallest: C + D = 0.3 → list 0.4, 0.3, 0.3 (the sum goes below the equal value) → merge 0.3 + 0.3 = 0.6 → list 0.6, 0.4.

Code back: upper branch 0, lower 1: 0.6 = 0, 0.4 = 1 → A = 1; B = 00, (CD) = 01 → C = 010, D = 011.

Lavg = 0.4·1 + 0.3·2 + 0.2·3 + 0.1·3 = 1.9 ≥ H = 1.8464. Fixed 2-bit code → CR = 2 / 1.9 = 1.053.

Golomb Gm(n)

q = ⌊n/m⌋ → q ones + 0  |  r = n mod m, k = ⌈log₂m⌉, c = 2k − m: r < c → r in k−1 bits, else r + c in k bits

G₄(9): q = 2 → 110; r = 1, k = 2, c = 0 → r ≥ c → 1 + 0 = 1 in 2 bits = 01 → 11001.

G₅(12): q = 2 → 110; r = 2, k = 3, c = 3 → r < c → 2 in 2 bits = 10 → 11010.

Arithmetic (slide 53: a1 0.2, a2 0.2, a3 0.4, a4 0.2; message a1 a2 a3 a3 a4)

new low = low + range × (slice start)    new high = low + range × (slice end)
symbolrange beforelowhigh
a1100.2
a20.20.040.08
a30.040.0560.072
a30.0160.06240.0688
a40.00640.067520.0688

Any number in [0.06752, 0.0688) codes the message: the slide picks 0.068.

LZW (slide 77: 4×4 image, every row 39 39 126 126)

Dictionary starts with 0–255. Read a pixel: if (current + pixel) is in the dictionary, keep extending; if not, output the code of current, add (current + pixel) at the next location (256, 257, …), restart from the pixel.

recognizedpixeloutputlocationentry
39
39393925639-39
391263925739-126
126126126258126-126
12639126259126-39
3939
39-3912625626039-39-126
126126

Full output: 39 39 126 126 256 258 260 259 257 126 → 10 codes × 9 bits = 90 bits vs 128 → CR = 1.42.

RLE: write (value, run length). Row 10 10 10 10 12 12 → (10, 4)(12, 2). Short runs make the file bigger (CR < 1): say so.

Which redundancy each method removes

redundancywhat it isremoved by
codingcodes longer than neededHuffman, Golomb, arithmetic
spatial / inter-pixelneighbours repeat or predict each otherLZW, RLE, predictive coding
irrelevant (psychovisual)detail the eye ignoresquantization (lossy)
temporalframes in a video repeatframe differencing

Past questions: look, recall, then open the answer

SP25 1dCLO1 · Compression: why, 3 redundancies, CR & R_D formula · 4
What is data redundancy in images? Name three main types of it. Discuss one redundancy type that can be overcome using variable-length code words. How to calculate relative data redundancy?
Worked answer (deck 13)
SP25 Q1(d)4 marks

What is data redundancy in images? Name its three main types. Discuss the redundancy type that can be overcome using variable-length code words. How do you calculate relative data redundancy?

Data redundancy is unnecessary or repeated data in an image file which, when removed, reduces the file size without losing essential information. It is a mathematically quantifiable entity.

Three types: (1) coding redundancy, (2) spatial / inter-pixel redundancy, (3) psychovisual redundancy (irrelevant information).

Coding redundancy is overcome with variable-length codes. A fixed-length code gives every gray level the same number of bits (e.g. 8), although some levels occur much more often. Variable-length coding (e.g. Huffman) gives shorter code words to more probable gray levels and longer ones to rare levels, so the average length Lavg = Σ l(rk) p(rk) drops below 8. Example (slide 15): Lavg = 1.81 bits instead of 8. This is reversible, so lossless.

Relative data redundancy: with n1 and n2 the bits in the original and compressed data, CR = n1/n2 and RD = 1 − 1/CR. Example: CR = 8/1.81 = 4.42 gives RD = 0.774, so 77.4% of the data was redundant.

SP25 2cCLO2 · Compression ratio / relative redundancy / entropy numbers · 1CLO2 · Run-length encoding · 3CLO2 · Huffman code + L_avg + entropy + CR + redundancy · 4
i) Uncompressed grayscale image 512x512, 8 bits/pixel; after compression file is 64 KB. Compute compression ratio [1]. ii) Encode the 1-D pixel run AAAAABBBCCDAA using RLE (symbol, count) [1]. iii) 1-D row of length 100 alternating ABAB... (50 A, 50 B). Explain RLE performance and compute encoded length, count stored as one byte [2]. iv) Symbols {A:0.4, B:0.3, C:0.2, D:0.1}: build a Huffman code, compute average code length and compare to entropy [4].
Worked answer (deck 13)
SP25 Q2(c)i1 mark

An uncompressed grayscale image is 512×512 with 8 bits/pixel. After compression the file is 64 KB. Compute the compression ratio.

n1 = 512 × 512 × 8 = 2,097,152 bits.

n2 = 64 KB = 64 × 1024 bytes × 8 = 524,288 bits.

CR = n1/n2 = 2,097,152 / 524,288 = 4, i.e. 4:1. (RD = 1 − 1/4 = 0.75: 75% of the original data was redundant.)

SP25 Q2(c) ii–iv1 + 2 + 4 marks

(ii) Encode the 1-D run AAAAABBBCCDAA with RLE (symbol, count). (iii) A row of 100 pixels alternates ABAB… (50 A, 50 B). Explain how RLE performs and compute the encoded length, the count stored as one byte. (iv) Symbols A: 0.4, B: 0.3, C: 0.2, D: 0.1. Build a Huffman code, compute the average code length and compare it with the entropy.

(ii) Group equal neighbours: AAAAA | BBB | CC | D | AA → (A,5) (B,3) (C,2) (D,1) (A,2). 13 symbols become 5 pairs.

(iii) Every run has length 1, so the row becomes 100 pairs (A,1)(B,1)(A,1)… With 1 byte for the symbol and 1 byte for the count, encoded length = 100 × 2 = 200 bytes against 100 bytes originally (one byte per pixel). CR = 100/200 = 0.5: the data doubles. RLE performs badly here: it only helps when there are long runs; small runs result in expansion instead of compression (slide 109).

(iv)

Step 1, sort the probabilities largest first. Steps 2–4, add the two smallest and re-sort, until two values are left (a sum that ties with an existing value goes below it). Step 5, walk back from the last column: upper branch 0, lower branch 1.

SymbolProbabilityCodeReduction 1Reduction 2
A0.410.4  10.6  0
B0.3000.3  000.4  1
C0.20100.3  01
D0.1011
SymbolProbability pCodeLength lp × l
A0.4110.4
B0.30020.6
C0.201030.6
D0.101130.3

Average length Lavg = Σ l·p = (0.4)(1) + (0.3)(2) + (0.2)(3) + (0.1)(3) = 1.9 bits/symbol.

Entropy H = −Σ p log2 p = −(0.4)log2(0.4) + −(0.3)log2(0.3) + −(0.2)log2(0.2) + −(0.1)log2(0.1) = 1.8464 bits/symbol.

Coding efficiency η = H / Lavg = 1.8464 / 1.9 = 97.18%.

Lavg = 1.9 is just above H = 1.8464: Huffman is within 0.0536 bits of the theoretical minimum (efficiency 97.18%). A fixed-length code would need 2 bits.

Try it, step by step: SP25 (iv).

Try it, step by step: SP25 (ii): the runs found one by one.

SP25 3bCLO2 · LZW encoding table · 6CLO2 · RMSE fidelity criterion · 4
i) Compress a collection of bitmap images with many repeating colors and patterns: choose a fixed-length coding algorithm which is lossless (LZW). Explain step-by-step encoding of the 4x4 8-bit image [7 8 40 100, 7 8 40 100, 7 8 40 100, 7 8 40 100] [6]. ii) 3x3 image of 3 bits f(x,y)=[1,4,3,2,3,4,2,4,5] compressed lossy; after decompression all pixel values increased by 2. Provide decompressed image f_d(x,y). Use appropriate fidelity criteria to evaluate the compressed image and measure the error [4].
Worked answer (deck 13)
SP25 Q3(b)i6 marks

A collection of bitmap images with many repeating colours and patterns must be compressed with a lossless fixed-length coding algorithm. Name it and explain step-by-step encoding of the 4×4 8-bit image with every row [7 8 40 100].

Use LZW coding: lossless, fixed-length (9-bit) code words, and it removes the repeated patterns (spatial redundancy) without needing probabilities.

Dictionary locations 0–255 already hold the 256 single gray levels; new sequences go from location 256. Read the pixels left to right, top to bottom. For each pixel: if currently recognized sequence + pixel is in the dictionary, keep growing it; otherwise output the code of the recognized sequence, add the longer sequence to the dictionary, and restart from the pixel.

Currently recognized sequencePixel being processedEncoded outputDictionary location (code word)Dictionary entry
7
7872567-8
84082578-40
401004025840-100
1007100259100-7
78
7-8402562607-8-40
40100
40-100725826140-100-7
78
7-840
7-8-401002602627-8-40-100
1007
100-78259263100-7-8
840
8-401002572648-40-100
100100

Encoded output: 7 8 40 100 256 258 260 259 257 100 (10 code words).

Original = 16 pixels × 8 bits = 128 bits. Compressed = 10 codes × 9 bits = 90 bits. CR = 128/90 = 1.4222 (compression achieved).

Try it, step by step: SP25.

Same answer as SP24 3b (SP24 Q3(b)i · F23 Q2(c)i · F21 B2(b)i · SP25 Q3(b)ii).

SP24 2cCLO2 · Huffman code + L_avg + entropy + CR + redundancy · 9
5x5 image [180 160 160 140 120; 110 110 120 140 120; 110 140 120 120 140; 120 160 160 170 170; 170 120 110 140 110]. i) Table of frequencies per symbol + histogram ii) minimum number of bits per symbol iii) Huffman code for each symbol iv) average number of bits per symbol [1+3+2+3]
Worked answer (deck 13)
SP24 Q2(c)1 + 3 + 2 + 3 marks

5×5, 8-bit image. (i) Table of frequencies and histogram (ii) minimum number of bits per symbol (iii) Huffman code for each symbol (iv) average number of bits per symbol.

180160160140120
110110120140120
110140120120140
120160160170170
170120110140110
image

(i) N = 25.

Symbol (gray level)CountProbability = count/N
12077/25 = 0.28
11055/25 = 0.2
14055/25 = 0.2
16044/25 = 0.16
17033/25 = 0.12
18011/25 = 0.04
3.57511071205140416031701180gray levelnk
Histogram (count per gray level)

(ii) 6 different gray levels appear, so a fixed-length code needs ⌈log2 6⌉ = 3 bits per symbol. The theoretical minimum average (entropy) is H = −Σ p log2 p = 2.4188 bits/symbol; no lossless code can do better.

(iii)–(iv)

Step 1, sort the probabilities largest first. Steps 2–4, add the two smallest and re-sort, until two values are left (a sum that ties with an existing value goes below it). Step 5, walk back from the last column: upper branch 0, lower branch 1.

SymbolProbabilityCodeReduction 1Reduction 2Reduction 3Reduction 4
1200.28010.28  010.32  000.4  10.6  0
1100.2100.2  100.28  010.32  000.4  1
1400.2110.2  110.2  100.28  01
1600.160000.16  0000.2  11
1700.1200100.16  001
1800.040011
SymbolProbability pCodeLength lp × l
1200.280120.56
1100.21020.4
1400.21120.4
1600.1600030.48
1700.12001040.48
1800.04001140.16

Average length Lavg = Σ l·p = (0.28)(2) + (0.2)(2) + (0.2)(2) + (0.16)(3) + (0.12)(4) + (0.04)(4) = 2.48 bits/symbol.

Entropy H = −Σ p log2 p = −(0.28)log2(0.28) + −(0.2)log2(0.2) + −(0.2)log2(0.2) + −(0.16)log2(0.16) + −(0.12)log2(0.12) + −(0.04)log2(0.04) = 2.4188 bits/symbol.

Coding efficiency η = H / Lavg = 2.4188 / 2.48 = 97.53%.

Compression ratio CR = n1/n2 = 8 / 2.48 = 3.2258. Relative redundancy RD = 1 − 1/CR = 1 − 1/3.2258 = 0.6900 → 69.00% of the data was redundant.

Try it, step by step: SP24, built from the counts (probabilities shown as count/25).

SP24 3bCLO2 · RMSE fidelity criterion · 4CLO2 · Golomb code G_m · 4
i) f(x,y)=[1,4,3,2,3,4,2,4,5], 3x3, 3 bits, lossy; after decompression all pixels +2. Provide decompressed image f_d(x,y); use fidelity criteria to evaluate and measure the error. ii) Compute the Golomb code for G4(9). [4+4]
Worked answer (deck 13)
SP24 Q3(b)ii · F23 Q2(c)ii · F21 B2(b)ii · slide 493–4 marks

Compute the Golomb code G4(9).

Step 1: q = ⌊9/4⌋ = 2 → unary code 110.

Step 2: k = ⌈log2 4⌉ = 2; c = 22 − 4 = 0; r = 9 mod 4 = 1. r = 1 ≥ c = 0 → r + c = 1 in k = 2 bits = 01.

Step 3: concatenate: 110 + 01 = G4(9) = 11001.

Try it, step by step: any n and m. Pick n and m from the menus; it opens on G4(9).

SP24 Q3(b)i · F23 Q2(c)i · F21 B2(b)i · SP25 Q3(b)ii2–5 marks

A 3×3, 3-bit image f(x,y) = [1 4 3; 2 3 4; 2 4 5] is compressed with a lossy technique; after decompression every pixel value increased by 2. Give the decompressed image f̂ and evaluate it with a suitable fidelity criterion (RMSE).

143
234
245
original f
365
456
467
decompressed f̂ = f + 2
222
222
222
error e = f̂ − f
444
444
444
e²

erms = [ (1/9) × (9 × 2²) ]1/2 = [ 36/9 ]1/2 = √4 = 2.

SNRms = Σ f̂² / Σ e² = 248 / 36 = 6.8889.

Every pixel is off by exactly 2 gray levels (out of 0–7), so the decompressed image is a brighter copy; the RMSE of 2 is large for a 3-bit image.

Try it, step by step: the RMSE, pixel by pixel.

F23 1dCLO1 · Compression: why, 3 redundancies, CR & R_D formula · 4
Why do we need image compression? How many types of data redundancies are there in an image? Give a brief description of each. [1+1+2]
Worked answer (deck 13)
F23 Q1(d) first part · F22 A2(a) · SP22 Q5(a)i1–2 marks

What is image compression? Why do we need it?

Image compression is the process of reducing the amount of data required to represent a digital image, by removing redundant (repeated or irrelevant) data while keeping the information.

We need it because raw images and videos are huge: for example a 2-hour SD video needs about 224 GB uncompressed, which is too big to store or send. Compression saves storage space and transmission time/bandwidth (internet, multimedia, satellite and medical imaging).

F23 Q1(d) · SP23 Q2(a) · SP22 Q5(a)ii · F22 A2(a)3–5 marks

How many types of data redundancy are there in an image? Describe each briefly. How can compression be implemented? How do you calculate the percentage of redundant data after compression?

There are three types of data redundancy:

  1. Coding redundancy: the code used for the gray levels has more bits than needed, e.g. every pixel stored in 8 bits although a few values dominate. Removed by variable-length coding (Huffman, Golomb, arithmetic): short codes for frequent values.
  2. Spatial (inter-pixel) redundancy: neighbouring pixels are correlated, so a pixel's value can be predicted from its neighbours and the same information is replicated. Removed by run-length coding or LZW. Example: an image of constant horizontal lines compresses 128:1 with run-length pairs.
  3. Psychovisual (irrelevant) redundancy: information the human visual system ignores. Removed by quantization (lossy). Example: a near-flat gray image stored as one average gray level, 65536:1.

Implementation: compression is achieved when one or more of these redundancies are reduced or eliminated, by encoding the data with a suitable scheme (compress → store/transmit → decompress).

Percentage redundant: CR = n1/n2, RD = 1 − 1/CR, and the percentage of redundant data = RD × 100%. Example: CR = 10 → RD = 0.9 → 90% redundant.

F23 2cCLO2 · RMSE fidelity criterion · 2CLO2 · Golomb code G_m · 4
i) Image [1,4,3,2,3,4,2,4,5], 3x3, 3 bits, lossy, all pixels +2 after decompression. Calculate the Root Mean Square Error as fidelity criteria [2]. ii) Compute the Golomb code for G4(9) [4].
Worked answer (deck 13)

Same answer as SP24 3b (SP24 Q3(b)ii · F23 Q2(c)ii · F21 B2(b)ii · slide 49).

Same answer as SP24 3b (SP24 Q3(b)i · F23 Q2(c)i · F21 B2(b)i · SP25 Q3(b)ii).

F23 3cCLO2 · LZW encoding table · 10
Compress bitmap images with repeating colors/patterns using LZW fixed-length coding. Encode the 4x4 8-bit image [5 8 40 120] x 4 rows step by step; codebook uses 9 bits. [10]
Worked answer (deck 13)
F23 Q3(c)10 marks

Encode the 4×4 8-bit image with every row [5 8 40 120] using LZW, step by step; the codebook uses 9 bits.

Dictionary locations 0–255 already hold the 256 single gray levels; new sequences go from location 256. Read the pixels left to right, top to bottom. For each pixel: if currently recognized sequence + pixel is in the dictionary, keep growing it; otherwise output the code of the recognized sequence, add the longer sequence to the dictionary, and restart from the pixel.

Currently recognized sequencePixel being processedEncoded outputDictionary location (code word)Dictionary entry
5
5852565-8
84082578-40
401204025840-120
1205120259120-5
58
5-8402562605-8-40
40120
40-120525826140-120-5
58
5-840
5-8-401202602625-8-40-120
1205
120-58259263120-5-8
840
8-401202572648-40-120
120120

Encoded output: 5 8 40 120 256 258 260 259 257 120 (10 code words).

Original = 16 pixels × 8 bits = 128 bits. Compressed = 10 codes × 9 bits = 90 bits. CR = 128/90 = 1.4222 (compression achieved).

Try it, step by step: F23.

SP23 2aCLO1 · Compression: why, 3 redundancies, CR & R_D formula · 5
How many types of data redundancies are there in an image? Brief description of each. How to calculate the percentage of redundant data after compression? [1+1+3]
Worked answer (deck 13)

Same answer as F23 1d (F23 Q1(d) · SP23 Q2(a) · SP22 Q5(a)ii · F22 A2(a)).

SP23 3aCLO2 · Huffman code + L_avg + entropy + CR + redundancy · 6CLO2 · Compression ratio / relative redundancy / entropy numbers · 2
4x5, 6-bit image [3,10,3,21,34] x 4 rows. i) Compress using Huffman coding; calculate compression ratio. ii) How much data was redundant? Any data lost? iii) 512x512 8-bit image with 5.3 bits/pixel entropy is Huffman coded: maximum compression expected? [3+3+2]
Worked answer (deck 13)

Same answer as SP22 5a (SP22 Q5(a)iii · SP23 Q3(a)iii).

SP23 Q3(a)3 + 3 + 2 marks

4×5, 6-bit image, every row [3, 10, 3, 21, 34]. (i) Compress with Huffman coding and calculate the compression ratio. (ii) How much data was redundant? Was any data lost? (iii) A 512×512 8-bit image with entropy 5.3 bits/pixel is Huffman coded: maximum compression expected?

(i) N = 20.

Symbol (gray level)CountProbability = count/N
388/20 = 0.4
1044/20 = 0.2
2144/20 = 0.2
3444/20 = 0.2

Step 1, sort the probabilities largest first. Steps 2–4, add the two smallest and re-sort, until two values are left (a sum that ties with an existing value goes below it). Step 5, walk back from the last column: upper branch 0, lower branch 1.

SymbolProbabilityCodeReduction 1Reduction 2
30.410.4  10.6  0
100.2010.4  000.4  1
210.20000.2  01
340.2001
SymbolProbability pCodeLength lp × l
30.4110.4
100.20120.4
210.200030.6
340.200130.6

Average length Lavg = Σ l·p = (0.4)(1) + (0.2)(2) + (0.2)(3) + (0.2)(3) = 2 bits/symbol.

Entropy H = −Σ p log2 p = −(0.4)log2(0.4) + −(0.2)log2(0.2) + −(0.2)log2(0.2) + −(0.2)log2(0.2) = 1.9219 bits/symbol.

Coding efficiency η = H / Lavg = 1.9219 / 2 = 96.10%.

Compression ratio CR = n1/n2 = 6 / 2 = 3.0000. Relative redundancy RD = 1 − 1/CR = 1 − 1/3.0000 = 0.6667 → 66.67% of the data was redundant.

Original = 20 × 6 = 120 bits; Huffman = 20 × 2 = 40 bits; CR = 3.0000.

(ii) RD = 1 − 1/3 = 0.6667: 66.7% of the data was redundant. No data was lost: Huffman coding is lossless (a prefix code, decoded exactly).

(iii) Cmax = 8/5.3 = 1.509 (see above).

Try it, step by step: SP23 (i).

F22 A2aCLO1 · Compression: why, 3 redundancies, CR & R_D formula · 5
What do you understand by image compression? Why do we need image compression? How can image compression be implemented? Explain briefly. [1+1+3]
Worked answer (deck 13)

Same answer as F23 1d (F23 Q1(d) first part · F22 A2(a) · SP22 Q5(a)i).

Same answer as F23 1d (F23 Q1(d) · SP23 Q2(a) · SP22 Q5(a)ii · F22 A2(a)).

F22 A3aCLO2 · Huffman code + L_avg + entropy + CR + redundancy · 3CLO2 · Run-length encoding · 3CLO2 · Compression ratio / relative redundancy / entropy numbers · 2
4x5, 6-bit image [[3,10,3,24,35],[3,10,10,24,3],[3,10,10,24,35],[3,10,10,24,35]]. i) Compress using Huffman Coding ii) Compress using Run Length Coding iii) Calculate Compression Ratio for (i) and (ii). [3+3+2]
Worked answer (deck 13)
F22 A3(a)3 + 3 + 2 marks

4×5, 6-bit image. (i) Compress using Huffman coding (ii) compress using run-length coding (iii) compression ratio of (i) and (ii).

31032435
31010243
310102435
310102435
image

(i)

Symbol (gray level)CountProbability = count/N
1077/20 = 0.35
366/20 = 0.3
2444/20 = 0.2
3533/20 = 0.15

Step 1, sort the probabilities largest first. Steps 2–4, add the two smallest and re-sort, until two values are left (a sum that ties with an existing value goes below it). Step 5, walk back from the last column: upper branch 0, lower branch 1.

SymbolProbabilityCodeReduction 1Reduction 2
100.3510.35  10.65  0
30.3010.35  000.35  1
240.20000.3  01
350.15001
SymbolProbability pCodeLength lp × l
100.35110.35
30.30120.6
240.200030.6
350.1500130.45

Average length Lavg = Σ l·p = (0.35)(1) + (0.3)(2) + (0.2)(3) + (0.15)(3) = 2 bits/symbol.

Entropy H = −Σ p log2 p = −(0.35)log2(0.35) + −(0.3)log2(0.3) + −(0.2)log2(0.2) + −(0.15)log2(0.15) = 1.9261 bits/symbol.

Coding efficiency η = H / Lavg = 1.9261 / 2 = 96.31%.

Compression ratio CR = n1/n2 = 6 / 2 = 3.0000. Relative redundancy RD = 1 − 1/CR = 1 − 1/3.0000 = 0.6667 → 66.67% of the data was redundant.

(ii) Row by row (gray level, run length), each number stored in 6 bits:

Row(gray level, run length) pairs
Row 1(3,1) (10,1) (3,1) (24,1) (35,1)
Row 2(3,1) (10,2) (24,1) (3,1)
Row 3(3,1) (10,2) (24,1) (35,1)
Row 4(3,1) (10,2) (24,1) (35,1)

17 pairs = 34 numbers, each stored in 6 bits. Original = 4×5×6 = 120 bits. RLE = 34×6 = 204 bits. CR = 120/204 = 0.5882.

(iii) Huffman: CR = 6/2 = 3.0000. RLE: CR = 0.5882, less than 1, so RLE expands this image (the runs are too short). Huffman is far better here.

Try it, step by step: F22 (i).

Try it, step by step: F22 (ii): every run, row by row.

SP22 1bCLO2 · LZW encoding table · 9
3x3 8-bit image [(3,4,7),(3,4,7),(3,4,7)], lossless LZW fixed length. i) Generate codebook using 9 bits, step-by-step LZW encoding [5]. ii) What kind of redundancy does it reduce? [2] iii) Any compression achieved? Prove it [2].
Worked answer (deck 13)
SP22 Q1(b)5 + 2 + 2 marks

3×3 8-bit image with every row [3 4 7]. (i) Generate a 9-bit codebook and LZW-encode it step by step. (ii) Which redundancy does it reduce? (iii) Is any compression achieved? Prove it.

(i)

Dictionary locations 0–255 already hold the 256 single gray levels; new sequences go from location 256. Read the pixels left to right, top to bottom. For each pixel: if currently recognized sequence + pixel is in the dictionary, keep growing it; otherwise output the code of the recognized sequence, add the longer sequence to the dictionary, and restart from the pixel.

Currently recognized sequencePixel being processedEncoded outputDictionary location (code word)Dictionary entry
3
3432563-4
4742574-7
7372587-3
34
3-472562593-4-7
73
7-342582607-3-4
47
4-7257

Encoded output: 3 4 7 256 258 257 (6 code words).

Original = 9 pixels × 8 bits = 72 bits. Compressed = 6 codes × 9 bits = 54 bits. CR = 72/54 = 1.3333 (compression achieved).

(ii) LZW reduces spatial / inter-pixel redundancy: repeated pixel patterns (3-4, 4-7, 7-3 …) are replaced by single dictionary codes.

(iii) Yes: 72 bits are coded with 6 × 9 = 54 bits, CR = 72/54 = 1.33 > 1.

Try it, step by step: SP22.

SP22 5aCLO1 · Compression: why, 3 redundancies, CR & R_D formula · 3CLO2 · Compression ratio / relative redundancy / entropy numbers · 2
i) Why is image compression needed? [1] ii) How many and what types of data redundancies? [2] iii) 512x512 8-bit image with 5.3 bits/pixel entropy Huffman coded: maximum compression expected? [2]
Worked answer (deck 13)

Same answer as F23 1d (F23 Q1(d) first part · F22 A2(a) · SP22 Q5(a)i).

SP22 Q5(a)iii · SP23 Q3(a)iii2 marks

A 512×512, 8-bit image with entropy 5.3 bits/pixel is Huffman coded. What is the maximum compression that can be expected?

Entropy is the lower limit of the average code length: no lossless code can use fewer than H = 5.3 bits/pixel on average (slide 40). So the best possible Lavg is 5.3 bits/pixel.

Cmax = 8 / 5.3 = 1.509 (about 1.51:1). In bits: 512 × 512 × 8 = 2,097,152 bits against at least 512 × 512 × 5.3 ≈ 1,389,363 bits.

Same answer as F23 1d (F23 Q1(d) · SP23 Q2(a) · SP22 Q5(a)ii · F22 A2(a)).

SP22 5bCLO2 · Huffman code + L_avg + entropy + CR + redundancy · 8
3x3 8-bit [(18,16,16),(11,11,14),(11,14,16)] Huffman. i) Illustrate Huffman encoding [3] ii) entropy [2] iii) redundancy reduced; lossless? [1] iv) Compression achieved? Prove it [2].
Worked answer (deck 13)
SP22 Q5(b)3 + 2 + 1 + 2 marks

3×3, 8-bit image [18 16 16; 11 11 14; 11 14 16]. (i) Illustrate Huffman encoding (ii) entropy (iii) which redundancy is reduced; is it lossless? (iv) Is compression achieved? Prove it.

(i)–(ii) N = 9.

Symbol (gray level)CountProbability = count/N
1133/9 = 0.3333
1633/9 = 0.3333
1422/9 = 0.2222
1811/9 = 0.1111

Step 1, sort the probabilities largest first. Steps 2–4, add the two smallest and re-sort, until two values are left (a sum that ties with an existing value goes below it). Step 5, walk back from the last column: upper branch 0, lower branch 1.

SymbolProbabilityCodeReduction 1Reduction 2
110.333310.3333  10.6667  0
160.3333000.3333  000.3333  1
140.22220100.3333  01
180.1111011
SymbolProbability pCodeLength lp × l
110.3333110.3333
160.33330020.6667
140.222201030.6667
180.111101130.3333

Average length Lavg = Σ l·p = (0.3333)(1) + (0.3333)(2) + (0.2222)(3) + (0.1111)(3) = 2 bits/symbol.

Entropy H = −Σ p log2 p = −(0.3333)log2(0.3333) + −(0.3333)log2(0.3333) + −(0.2222)log2(0.2222) + −(0.1111)log2(0.1111) = 1.8911 bits/symbol.

Coding efficiency η = H / Lavg = 1.8911 / 2 = 94.55%.

Compression ratio CR = n1/n2 = 8 / 2 = 4.0000. Relative redundancy RD = 1 − 1/CR = 1 − 1/4.0000 = 0.7500 → 75.00% of the data was redundant.

(iii) Huffman reduces coding redundancy. It is lossless: each code word is unique and prefix-free, so the image is recovered exactly.

(iv) Original = 9 × 8 = 72 bits. Huffman = Σ count × length = 18 bits. CR = 72/18 = 4.00 > 1, so yes, compression is achieved.

Try it, step by step: SP22 Q5(b).

F21 B2aCLO2 · Huffman code + L_avg + entropy + CR + redundancy · 8
5x5 8-bit [[18,16,16,14,12],[11,11,12,14,12],[11,14,12,12,14],[12,16,16,17,17],[17,12,11,14,11]]. i) Derive entropy equation from information theory [2] ii) entropy [1] iii) Huffman codes [2] iv) compression ratio vs 8-bit fixed [2] v) relative data redundancy [1].
Worked answer (deck 13)
F21 B2(a)2 + 1 + 2 + 2 + 1 marks

5×5, 8-bit image. (i) Derive the entropy equation from information theory (ii) entropy (iii) Huffman codes (iv) compression ratio against 8-bit fixed length (v) relative data redundancy.

(i) An event E with probability P(E) carries I(E) = log2(1/P(E)) = −log2P(E) bits of information: the rarer the event, the more information. For a source with symbols a1 … aJ, symbol aj occurs with probability P(aj) and carries −log2P(aj) bits, so the average information per symbol is the weighted sum

H = Σ P(aj) I(aj) = −Σj=1J P(aj) log2 P(aj)   bits/symbol

For an image, P(aj) is the normalized histogram p(rk) = nk/N.

(ii)–(v) N = 25.

Symbol (gray level)CountProbability = count/N
1277/25 = 0.28
1155/25 = 0.2
1455/25 = 0.2
1644/25 = 0.16
1733/25 = 0.12
1811/25 = 0.04

Step 1, sort the probabilities largest first. Steps 2–4, add the two smallest and re-sort, until two values are left (a sum that ties with an existing value goes below it). Step 5, walk back from the last column: upper branch 0, lower branch 1.

SymbolProbabilityCodeReduction 1Reduction 2Reduction 3Reduction 4
120.28010.28  010.32  000.4  10.6  0
110.2100.2  100.28  010.32  000.4  1
140.2110.2  110.2  100.28  01
160.160000.16  0000.2  11
170.1200100.16  001
180.040011
SymbolProbability pCodeLength lp × l
120.280120.56
110.21020.4
140.21120.4
160.1600030.48
170.12001040.48
180.04001140.16

Average length Lavg = Σ l·p = (0.28)(2) + (0.2)(2) + (0.2)(2) + (0.16)(3) + (0.12)(4) + (0.04)(4) = 2.48 bits/symbol.

Entropy H = −Σ p log2 p = −(0.28)log2(0.28) + −(0.2)log2(0.2) + −(0.2)log2(0.2) + −(0.16)log2(0.16) + −(0.12)log2(0.12) + −(0.04)log2(0.04) = 2.4188 bits/symbol.

Coding efficiency η = H / Lavg = 2.4188 / 2.48 = 97.53%.

Compression ratio CR = n1/n2 = 8 / 2.48 = 3.2258. Relative redundancy RD = 1 − 1/CR = 1 − 1/3.2258 = 0.6900 → 69.00% of the data was redundant.

Total bits: Huffman = 62 bits; fixed = 25 × 8 = 200 bits.

Try it, step by step: F21 (iii)–(v).

F21 B2bCLO2 · RMSE fidelity criterion · 3CLO2 · Golomb code G_m · 3
i) Image [1,4,3,2,3,4,2,4,5] 3x3 3 bits lossy, +2 after decompression: RMSE as fidelity criteria [3]. ii) Golomb code G4(9) [3].
Worked answer (deck 13)

Same answer as SP24 3b (SP24 Q3(b)ii · F23 Q2(c)ii · F21 B2(b)ii · slide 49).

Same answer as SP24 3b (SP24 Q3(b)i · F23 Q2(c)i · F21 B2(b)i · SP25 Q3(b)ii).

More worked examples: slides and other examiners (16)
Slides 6–7 · why images must be compressed (deck 13)
Slides 6–7 · why images must be compressedworked

SD TV, 2 hours, 720×480 colour frames at 30 frames/s, 3 bytes per pixel:

One second = 30 × (720 × 480) × 3 = 31,104,000 bytes ≈ 31 MB.

Two hours = 31,104,000 × 60² × 2 ≈ 2.24 × 1011 bytes = 224 GB. That needs twenty-seven 8.5 GB dual-layer DVDs, so the video must be compressed by a factor of about 26.3 to fit on one DVD.

HD TV (1920×1080) needs even higher compression. Web images of 128×128 colour take 7.0 to 0.03 s to download over 56 kbps to 12 Mbps links. A 1 GB flash memory holds only about 41 uncompressed 8-megapixel photos.

Slides 13–15 · fixed vs variable length (deck 13)
Slides 13–15 · fixed vs variable lengthworked

A computer-generated 256×256, 8-bit image uses only four gray levels:

rkpr(rk)Code 1l1Code 2l2
870.25010101118012
1280.4710000000811
1860.251100010080003
2550.031111111180013
others0—8—0

Code 1 (fixed): 8 bits/pixel, total 256 × 256 × 8 bits.

Code 2 (variable): Lavg = 0.25×2 + 0.47×1 + 0.25×3 + 0.03×3 = 1.81 bits; total 256 × 256 × 1.81 bits.

C = 8/1.81 ≈ 4.42; RD = 1 − 1/4.42 = 0.774: 77.4% of the data in the first image was redundant.

Slide 16 · second example (deck 13)
Slide 16 · second exampleworked
rkpr(rk)Code 1l1Code 2l2
r0 = 00.190003112
r1 = 1/70.250013012
r2 = 2/70.210103102
r3 = 3/70.1601130013
r4 = 4/70.08100300014
r5 = 5/70.061013000015
r6 = 6/70.0311030000016
r7 = 7/70.0211130000006

Lavg = 2(0.19) + 2(0.25) + 2(0.21) + 3(0.16) + 4(0.08) + 5(0.06) + 6(0.03) + 6(0.02) = 2.7 bits.

CR = 3/2.7 = 1.11; RD = 1 − 1/1.11 = 0.099: 9.9% of the data was redundant.

Slides 26–34 · 3-bit image, six gray levels (deck 13)
Slides 26–34 · 3-bit image, six gray levelsworked

Probabilities: a1 = 0.1, a2 = 0.4, a3 = 0.06, a4 = 0.1, a5 = 0.04, a6 = 0.3. Find the Huffman code.

Step 1, sort the probabilities largest first. Steps 2–4, add the two smallest and re-sort, until two values are left (a sum that ties with an existing value goes below it). Step 5, walk back from the last column: upper branch 0, lower branch 1.

SymbolProbabilityCodeReduction 1Reduction 2Reduction 3Reduction 4
a20.410.4  10.4  10.4  10.6  0
a60.3000.3  000.3  000.3  000.4  1
a10.10110.1  0110.2  0100.3  01
a40.101000.1  01000.1  011
a30.06010100.1  0101
a50.0401011
SymbolProbability pCodeLength lp × l
a20.4110.4
a60.30020.6
a10.101130.3
a40.1010040.4
a30.060101050.3
a50.040101150.2

Average length Lavg = Σ l·p = (0.4)(1) + (0.3)(2) + (0.1)(3) + (0.1)(4) + (0.06)(5) + (0.04)(5) = 2.2 bits/symbol.

Entropy H = −Σ p log2 p = −(0.4)log2(0.4) + −(0.3)log2(0.3) + −(0.1)log2(0.1) + −(0.1)log2(0.1) + −(0.06)log2(0.06) + −(0.04)log2(0.04) = 2.1435 bits/symbol.

Coding efficiency η = H / Lavg = 2.1435 / 2.2 = 97.43%.

Compression ratio CR = n1/n2 = 3 / 2.2 = 1.3636. Relative redundancy RD = 1 − 1/CR = 1 − 1/1.3636 = 0.2667 → 26.67% of the data was redundant.

As on slide 34: n1 = 3, n2 = 2.2, C = 3/2.2 = 1.364, RD = 0.267. 26.7% fewer bits: coding redundancy eliminated. Yes, it reduces the total bit count.

Try it, step by step: every sort, merge and re-sort, then the codes passed back column by column (slides 26–33).

Slide 36 · Class work (deck 13)
Slide 36 · Class worksolved

A 3-bit grayscale image has A (dark gray) 0.45, B (medium gray) 0.13, C (light gray) 0.12, D (white) 0.16, E (black) 0.09, F (very light gray) 0.05. Find the Huffman code.

Step 1, sort the probabilities largest first. Steps 2–4, add the two smallest and re-sort, until two values are left (a sum that ties with an existing value goes below it). Step 5, walk back from the last column: upper branch 0, lower branch 1.

SymbolProbabilityCodeReduction 1Reduction 2Reduction 3Reduction 4
A0.4510.45  10.45  10.45  10.55  0
D0.160000.16  0000.25  010.3  000.45  1
B0.130100.14  0010.16  0000.25  01
C0.120110.13  0100.14  001
E0.0900100.12  011
F0.050011
SymbolProbability pCodeLength lp × l
A0.45110.45
B0.1301030.39
C0.1201130.36
D0.1600030.48
E0.09001040.36
F0.05001140.2

Average length Lavg = Σ l·p = (0.45)(1) + (0.13)(3) + (0.12)(3) + (0.16)(3) + (0.09)(4) + (0.05)(4) = 2.24 bits/symbol.

Entropy H = −Σ p log2 p = −(0.45)log2(0.45) + −(0.13)log2(0.13) + −(0.12)log2(0.12) + −(0.16)log2(0.16) + −(0.09)log2(0.09) + −(0.05)log2(0.05) = 2.2199 bits/symbol.

Coding efficiency η = H / Lavg = 2.2199 / 2.24 = 99.10%.

Compression ratio CR = n1/n2 = 3 / 2.24 = 1.3393. Relative redundancy RD = 1 − 1/CR = 1 − 1/1.3393 = 0.2533 → 25.33% of the data was redundant.

Try it, step by step: the slide 36 class work.

Slide 41 · entropy of the 4-level image (deck 13)
Slide 41 · entropy of the 4-level imageworked

H = −[0.25 log2 0.25 + 0.47 log2 0.47 + 0.25 log2 0.25 + 0.03 log2 0.03]

≈ −[0.25(−2) + 0.47(−1.09) + 0.25(−2) + 0.03(−5.06)] ≈ 1.6614 bits/pixel.

Compare: the variable-length code used 1.81 bits, close to this limit.

Slides 44–45 · two extreme cases (deck 13)
Slides 44–45 · two extreme casesworked

Uniform distribution, 3-bit image (L = 8), every level has p = 1/8: H = −Σ (1/8) log2(1/8) = 3 bits/pixel. The natural 3-bit code is already optimal; a uniform distribution has the highest entropy.

Certain event, every pixel is 2: p(2) = 1, so H = −(1) log2(1) = 0 bits/pixel. The gray level is known in advance, so one value codes the whole image.

Slide 50 · G4(7) and G5(12) (deck 13)
Slide 50 · G4(7) and G5(12)worked

G4(7): q = 1 → 10; k = 2, c = 0, r = 3 ≥ 0 → r + c = 3 in 2 bits = 11 → 1011.

G5(12): q = ⌊12/5⌋ = 2 → 110; k = ⌈log2 5⌉ = 3, c = 8 − 5 = 3, r = 12 mod 5 = 2. r = 2 < c = 3 → r in k − 1 = 2 bits = 10. So G5(12) = 110 + 10 = 11010.

Slide 51 · "Fill the chart at home" (deck 13)
Slide 51 · "Fill the chart at home"solved
nG1(n)G2(n)G4(n)
0000000
11001001
2110100010
31110101011
41111011001000
511111011011001
61111110111001010
711111110111011011

m = 1: k = 0, so the code is just the unary code of n. m = 2: k = 1, c = 0, the remainder is 1 bit. m = 4: k = 2, c = 0, the remainder is 2 bits. Row 7 matches the slide (11111110, 11101, 1011).

Slides 53–64 · encode a1 a2 a3 a3 a4 (deck 13)
Slides 53–64 · encode a1 a2 a3 a3 a4worked

Four-symbol source a1 = 0.2, a2 = 0.2, a3 = 0.4, a4 = 0.2. Encode the message a1 a2 a3 a3 a4.

SymbolProbabilityInitial sub-interval
a10.2[0, 0.2)
a20.2[0.2, 0.4)
a30.4[0.4, 0.8)
a40.2[0.8, 1)

For each symbol: width = high − low; new low = low + width × (symbol's low end); new high = low + width × (symbol's high end).

SymbolCurrent intervalWidthNew lowNew high
a1[0, 1)10 + 1×0 = 00 + 1×0.2 = 0.2
a2[0, 0.2)0.20 + 0.2×0.2 = 0.040 + 0.2×0.4 = 0.08
a3[0.04, 0.08)0.040.04 + 0.04×0.4 = 0.0560.04 + 0.04×0.8 = 0.072
a3[0.056, 0.072)0.0160.056 + 0.016×0.4 = 0.06240.056 + 0.016×0.8 = 0.0688
a4[0.0624, 0.0688)0.00640.0624 + 0.0064×0.8 = 0.067520.0624 + 0.0064×1 = 0.0688

Final interval: [0.06752, 0.0688). Any number in it encodes the message. Shortest decimal: 0.068.

The slide picks 0.068: the arithmetic code for a1a2a3a3a4.

Try it, step by step: slides 55–63: watch each symbol zoom into its slice.

Slide 74 · Class work (deck 13)
Slide 74 · Class worksolved

Six-symbol source A 0.45, B 0.13, C 0.12, D 0.16, E 0.09, F 0.05 with sub-intervals A [0, 0.45), B [0.45, 0.58), C [0.58, 0.70), D [0.70, 0.86), E [0.86, 0.95), F [0.95, 1.00). Encode the message "ACEF". (The Huffman code of the same source is the slide 36 class work above.)

SymbolProbabilityInitial sub-interval
A0.45[0, 0.45)
B0.13[0.45, 0.58)
C0.12[0.58, 0.7)
D0.16[0.7, 0.86)
E0.09[0.86, 0.95)
F0.05[0.95, 1)

For each symbol: width = high − low; new low = low + width × (symbol's low end); new high = low + width × (symbol's high end).

SymbolCurrent intervalWidthNew lowNew high
A[0, 1)10 + 1×0 = 00 + 1×0.45 = 0.45
C[0, 0.45)0.450 + 0.45×0.58 = 0.2610 + 0.45×0.7 = 0.315
E[0.261, 0.315)0.0540.261 + 0.054×0.86 = 0.307440.261 + 0.054×0.95 = 0.3123
F[0.30744, 0.3123)0.004860.30744 + 0.00486×0.95 = 0.3120570.30744 + 0.00486×1 = 0.3123

Final interval: [0.312057, 0.3123). Any number in it encodes the message. Shortest decimal: 0.3121.

Try it, step by step: the slide 74 class work 'ACEF'.

Slides 77–95 · 4×4, 8-bit image of 39 39 126 126 rows (deck 13)
Slides 77–95 · 4×4, 8-bit image of 39 39 126 126 rowsworked
3939126126
3939126126
3939126126
3939126126
image

Dictionary locations 0–255 already hold the 256 single gray levels; new sequences go from location 256. Read the pixels left to right, top to bottom. For each pixel: if currently recognized sequence + pixel is in the dictionary, keep growing it; otherwise output the code of the recognized sequence, add the longer sequence to the dictionary, and restart from the pixel.

Currently recognized sequencePixel being processedEncoded outputDictionary location (code word)Dictionary entry
39
39393925639-39
391263925739-126
126126126258126-126
12639126259126-39
3939
39-3912625626039-39-126
126126
126-12639258261126-126-39
3939
39-39126
39-39-12612626026239-39-126-126
12639
126-3939259263126-39-39
39126
39-12612625726439-126-126
126126

Encoded output: 39 39 126 126 256 258 260 259 257 126 (10 code words).

Original = 16 pixels × 8 bits = 128 bits. Compressed = 10 codes × 9 bits = 90 bits. CR = 128/90 = 1.4222 (compression achieved).

Try it, step by step: the slide example, one pixel at a time (slides 78–95).

Slides 110–112 · Example 10.2.9, 8×8 4-bit image (deck 13)
Slides 110–112 · Example 10.2.9, 8×8 4-bit imageworked
1010101010101010
1010101010121212
1010101010121212
00010101000
55500000
55510109910
55544400
00000000
image
Row(gray level, run length) pairs
Row 1(10,8)
Row 2(10,5) (12,3)
Row 3(10,5) (12,3)
Row 4(0,3) (10,3) (0,2)
Row 5(5,3) (0,5)
Row 6(5,3) (10,2) (9,2) (10,1)
Row 7(5,3) (4,3) (0,2)
Row 8(0,8)

18 pairs = 36 numbers, each stored in 4 bits. Original = 8×8×4 = 256 bits. RLE = 36×4 = 144 bits. CR = 256/144 = 1.7778.

As on slide 112: original 64 × 4 = 256 bits, compressed 36 × 4 = 144 bits, compression ratio 64:36 = 16:9.

Try it, step by step: Example 10.2.9, every run.

Slides 116–117 · Example 10.2.7, 8×8 binary image (deck 13)
Slides 116–117 · Example 10.2.7, 8×8 binary imageworked
00000000
11110000
01100000
01111100
01110010
00100110
11110100
00000000
binary image (1 = white, 0 = black)

Convention: the first number of each row is the number of zeros (black) in the first run, so a row that starts with 1 begins with 0.

RowRLC numbers
Row 18
Row 20, 4, 4
Row 31, 2, 5
Row 41, 5, 2
Row 51, 3, 2, 1, 1
Row 62, 1, 2, 2, 1
Row 70, 4, 1, 1, 2
Row 88

Rows 2 and 7 begin with 0 because they start with a white run.

Slide 125 · 2×2 example (deck 13)
Slide 125 · 2×2 exampleworked

Original [100 150; 200 250], reconstructed [102 148; 198 255].

Step 1, differences: [−2 2; 2 −5]. Step 2, squares: [4 4; 4 25]. Step 3, mean: (4 + 4 + 4 + 25)/4 = 37/4 = 9.25. Step 4, root: RMSE = √9.25 ≈ 3.04.

Slide 132 · Class work (deck 13)
Slide 132 · Class worksolved

A 3×3, 3-bit image [1,4,3,5,2,3,4,2,1] is lossy compressed; after decompression it becomes [3,6,5,7,4,5,6,4,3]. Calculate the RMSE.

Every pixel increased by 2, so e = 2 everywhere: erms = √((9 × 4)/9) = √4 = 2.

Try it, step by step: slide 132 class work.

9. Colour: RGB, CMY(K), HSI

deck 14 · 6 past questions
The pattern: how she asks it
  • Q1 / Q3 theory: define a colour image, additive vs subtractive mixing, why colour models exist, which model is used for printing (CMYK) and for segmentation (HSI) (SP25 1c, SP24 1d, F23 1c).
  • Numeric: RGB → CMY (1 − RGB), RGB → HSI (θ, S, I) (SP22 7b).

Connects to: HSI separates intensity from colour, so the gray-level methods (HE, filters) run on I alone.

typeCLOtimeswhere
Colour: RGB cube, additive/subtractive, CMYCLO16SP25 1c, SP24 1d, F23 1c, SP23 3d, SP22 7a, SP22 7b
Formula wall: every formula with a worked line
whatformula / fact
RGB → CMYC = 1 − R, M = 1 − G, Y = 1 − B (normalise to 0–1 first)
θcos⁻¹ { ½[(R − G) + (R − B)] / √[(R − G)² + (R − B)(G − B)] }
Hθ if B ≤ G, else 360° − θ (red 0°, green 120°, blue 240°)
S1 − 3·min(R, G, B) / (R + G + B)
I(R + G + B) / 3
24-bit colours2²⁴ = 16,777,216

Worked (R, G, B) = (0.8, 0.4, 0.2): numerator ½(0.4 + 0.6) = 0.5; denominator √(0.16 + 0.6 × 0.2) = √0.28 = 0.5292; θ = cos⁻¹(0.9449) = 19.1°. B ≤ G, so H = 19.1°. S = 1 − 3 × 0.2 / 1.4 = 0.5714. I = 1.4 / 3 = 0.4667.

  • Additive (light, RGB): R + G + B = white. Subtractive (pigment, CMY): C + M + Y = black (K added because CMY black is muddy).
  • RGB cube: black at the origin, white at the far corner, grays on the diagonal R = G = B.
  • HSI separates colour (H, S) from brightness (I): good for segmentation and for enhancing I without changing hues.

Past questions: look, recall, then open the answer

SP25 1cCLO1 · Colour: RGB cube, additive/subtractive, CMY · 4
Define a color image. What is the difference between additive and subtractive color mixing? What is the purpose of color models in image processing? Which model is appropriate or representative for color printing and segmentation?
Worked answer (deck 14)
SP25 Final Q1(c)4 marks

Define a colour image. What is the difference between additive and subtractive colour mixing? What is the purpose of colour models in image processing? Which model is appropriate for colour printing and for segmentation?

Colour image: an image whose every pixel holds a colour vector, usually three components (R, G, B), each 8 bits, i.e. 24 bits per pixel.

Additive vs subtractive: additive mixing adds light (R, G, B; all three = white; screens). Subtractive mixing uses pigments that absorb light (C, M, Y; all three = black; printing).

Purpose of colour models: to specify colours in a standard way, as points in a coordinate system, so that hardware (RGB for displays, CMYK for printers) and processing (HSI for describing and analysing colour) can use them.

Printing: CMY/CMYK. Segmentation: HSI/HSV, because hue is kept apart from intensity, so an object can be picked out by its hue even when lighting changes.

SP24 1dCLO1 · Colour: RGB cube, additive/subtractive, CMY · 5
Which color model (RGB, CMY, CMYK, HSI) is appropriate or representative for: i) Color Printing ii) Computer Displaying? Explain the reasons. [2+3]
Worked answer (deck 14)
SP24 Final Q1(d)2 + 3 marks

Which colour model (RGB, CMY, CMYK, HSI) is appropriate for (i) colour printing, (ii) computer displaying? Explain.

(i) Colour printing: CMYK. A printer puts inks on white paper. Inks work by absorbing light, so a subtractive model is needed: cyan, magenta and yellow each remove one primary. Mixing C + M + Y gives only a muddy dark brown, so a separate black (K) ink is added for true black, better contrast and less ink use.

(ii) Computer display: RGB. A monitor emits light. Each pixel has red, green and blue sub-pixels whose light adds up (additive model): (255, 0, 0) is red, (255, 255, 255) is white, (0, 0, 0) is black. RGB also matches the eye's three cone types.

F23 1cCLO1 · Colour: RGB cube, additive/subtractive, CMY · 4
Draw the RGB color cube using the Cartesian coordinate system. How are different colors and gray scales formed in the RGB color model? [2+2]
Worked answer (deck 14)
F23 Final Q1(c) · SP22 Q7(a) · F22 B4(c)4 · 5 · 1 + 4 marks

Draw the RGB colour cube using the Cartesian coordinate system. How are different colours and gray scales formed in the RGB colour model? (SP22: describe the RGB model with a schematic of the cube.)

Same answer as SP23 (i) and (ii). For the 5-mark SP22 version add: RGB is additive; primaries red, green, blue; each channel 0–255 in an 8-bit image; used in monitors, cameras and image editing; 24-bit RGB gives 16,777,216 colours. (F22 Section B is from another examiner's paper.)

SP23 3dCLO1 · Colour: RGB cube, additive/subtractive, CMY · 8
i) Draw the schematic of the RGB color cube (Cartesian). ii) How are colors and gray scale formed in RGB? iii) How many colors in a standard RGB image? [3+3+2]
Worked answer (deck 14)
SP23 Final Q3(d)3 + 3 + 2 marks

(i) Draw the schematic of the RGB colour cube (Cartesian). (ii) How are colours and gray scale formed in RGB? (iii) How many colours in a standard RGB image?

(i) Draw the cube above: axes R, G, B from the origin; black at (0,0,0); red (1,0,0), green (0,1,0), blue (0,0,1); yellow (1,1,0), cyan (0,1,1), magenta (1,0,1); white (1,1,1); dashed gray-scale diagonal.

(ii) Every colour is a point (R, G, B) on or inside the cube: the three primaries of light are added in those amounts (e.g. (1,1,0) = red + green = yellow). When R = G = B the point lies on the diagonal from black to white, so equal amounts of the three give a shade of gray; (0.5, 0.5, 0.5) is mid-gray.

(iii) 8 bits per channel × 3 channels = 24 bits per pixel, so (28)3 = 224 = 16,777,216 colours.

SP22 7aCLO1 · Colour: RGB cube, additive/subtractive, CMY · 5
Describe the RGB color model with schematic of RGB color cube. [5]
Worked answer (deck 14)

Same answer as F23 1c (F23 Final Q1(c) · SP22 Q7(a) · F22 B4(c)).

SP22 7bCLO1 · Colour: RGB cube, additive/subtractive, CMY · 8
i) Why is RGB called 'Additive' whereas CMYK 'Subtractive'? [4] ii) Basic differences between RGB image and Indexed color image? Which is preferred for storage and transmission? [4]
Worked answer (deck 14)
SP22 Final Q7(b)(i)4 marks

Why is RGB called “additive” whereas CMYK is “subtractive”?

RGB builds a colour by adding light of the three primaries on a black (no light) background, as a monitor does: each primary adds energy, and R + G + B at full strength gives white. CMY(K) builds a colour with pigments on white paper: each ink absorbs (subtracts) one primary from the white light, e.g. cyan = white − red, magenta = white − green, yellow = white − blue. Adding more ink removes more light, so C + M + Y gives black (in practice dark brown, so black ink K is added for depth and contrast).

AnswerRGB adds light (all three = white); CMY(K) subtracts light from white (all three = black).
SP22 Final Q7(b)(ii)4 marks

Basic differences between an RGB image and an indexed colour image? Which is preferred for storage and transmission?

(Not on the Fall 2025 slides; a short answer is enough.)

RGB (true-colour) image: every pixel stores its own three values R, G, B, usually 24 bits per pixel, so it can show any of 16.7 million colours.

Indexed image: every pixel stores only an index (e.g. 8 bits) into a small colour map (palette) that lists the actual RGB colours, e.g. 256 entries.

Storage/transmission: the indexed image is preferred: about 8 bits/pixel plus a small table instead of 24 bits/pixel (GIF uses this). The cost is a limited number of colours.

More worked examples: slides and other examiners (5)
Slide 20 · worked example (deck 14)
Slide 20 · worked exampleworked

Convert RGB (98, 35, 146) to CMY.

Step 1 · normalise: R = 98/255 = 0.384, G = 35/255 = 0.137, B = 146/255 = 0.573.

Step 2 · subtract from 1: C = 1 − 0.384 = 0.616, M = 1 − 0.137 = 0.863, Y = 1 − 0.573 = 0.427.

AnswerCMY = (0.616, 0.863, 0.427); in 8-bit form (255 − R, 255 − G, 255 − B) = (157, 220, 109)
Worked · RGB(200, 100, 50) → HSI (deck 14)
Worked · RGB(200, 100, 50) → HSIworked

Step 1 · normalise (divide by 255): R = 200/255 = 0.7843, G = 100/255 = 0.3922, B = 50/255 = 0.1961.

Step 2 · θ

numerator = ½[(R − G) + (R − B)] = ½[(0.3922) + (0.5882)] = 0.4902
denominator = √[(R − G)² + (R − B)(G − B)] = √[0.1538 + (0.5882)(0.1961)] = 0.5188
θ = cos⁻¹(0.4902 / 0.5188) = cos⁻¹(0.9449) = 19.11°

Step 3 · hue: B = 0.1961 ≤ G = 0.3922, so H = θ = 19.11°.

S = 1 − 3·min(R,G,B)/(R + G + B) = 1 − 3 × 0.1961 / 1.3725 = 0.5714
I = (R + G + B)/3 = 1.3725/3 = 0.4575
AnswerRGB(200, 100, 50) → H = 19.11°, S = 0.5714, I = 0.4575

Try it, step by step: RGB → HSI, one quantity at a time (θ top, θ bottom, θ, H, S, I).

Worked · slide 20 colour RGB(98, 35, 146) → HSI (deck 14)
Worked · slide 20 colour RGB(98, 35, 146) → HSIpractice

Step 1 · normalise (divide by 255): R = 98/255 = 0.3843, G = 35/255 = 0.1373, B = 146/255 = 0.5725.

Step 2 · θ

numerator = ½[(R − G) + (R − B)] = ½[(0.2471) + (-0.1882)] = 0.0294
denominator = √[(R − G)² + (R − B)(G − B)] = √[0.0610 + (-0.1882)(-0.4353)] = 0.3781
θ = cos⁻¹(0.0294 / 0.3781) = cos⁻¹(0.0778) = 85.54°

Step 3 · hue: B = 0.5725 > G = 0.1373, so H = 360° − θ = 274.46°.

S = 1 − 3·min(R,G,B)/(R + G + B) = 1 − 3 × 0.1373 / 1.0941 = 0.6237
I = (R + G + B)/3 = 1.0941/3 = 0.3647
AnswerRGB(98, 35, 146) → H = 274.46°, S = 0.6237, I = 0.3647

Here B > G, so the hue is measured the long way round: 360° − θ. That puts it between blue (240°) and magenta (300°), which matches a purple colour.

F22 Final B5(c) (another examiner's paper) (deck 14)
F22 Final B5(c) (another examiner's paper)3 + 2 + 3 marks

Explain the HSI colour model with an example. Differences between additive and subtractive colour? How does the combination of cyan, magenta and yellow pigments create different colours?

HSI: describes colour as Hue (angle from red: red 0°, green 120°, blue 240°), Saturation (purity, 0–1) and Intensity (brightness, 0–1). Example: RGB(200, 100, 50) gives H ≈ 19.1° (an orange-red), S ≈ 0.57, I ≈ 0.46.

Additive vs subtractive: see the table in the additive/subtractive section (light vs pigment; all three = white vs black; screens vs printers).

CMY combinations: each pigment removes one primary from white light: cyan + magenta removes red and green, leaving blue; magenta + yellow leaves red; yellow + cyan leaves green; all three remove everything: black.

F21 Final A3(a) (another examiner's paper) (deck 14)
F21 Final A3(a) (another examiner's paper)8 marks

Inkjet printer and computer: describe the colour model used in a monitor and in a printer. Among red, green and blue, which appears brightest, followed by the others? Justify. Discuss the two major areas of colour image processing.

Monitor: RGB (additive, emits light). Printer: CMYK (subtractive inks on white paper, K added for true black).

Brightest: green, then red, then blue. The eye's overall brightness sensitivity peaks near 555 nm (green), where the red- and green-sensitive cones overlap; very few cones (about 2%) respond to blue, so equal-energy blue looks darkest. (The luminance formula agrees: Y = 0.299R + 0.587G + 0.114B.)

Two major areas (Gonzalez, chapter 6): full-colour processing (images from a full-colour sensor, e.g. a colour camera; process the colour vectors) and pseudo-colour processing (assign colours to gray levels or ranges of intensity to make features easier to see).

Q1 theory points (CLO1)

In plain wordsEach Q1 answer is a definition, a formula or sketch, and three short points. Below are the questions she repeats, with the points to write.
if askedwrite
sampling vs quantization; why more levels look finercoordinates vs amplitude; few pixels → blocky, few levels → false contours; bits = M·N·k
4-, 8-, m-connectivity; distance propertiesthe three adjacency rules; m removes double paths; D ≥ 0, symmetric, triangle inequality
image enhancement; spatial vs frequency domainmake an image more useful for a task; spatial works on pixels directly (point, neighbourhood), frequency on the Fourier transform
piecewise linear: identity, threshold, stretchthe 3-piece formula with (r₁,s₁),(r₂,s₂) and the 3 settings, each with a graph
histogram; 4 types; can you rebuild the image?h(rk) = nk; dark / bright / low / high contrast sketches; no, it holds no positions
why is the equalized histogram not flat?discrete levels: several r map to one s, some s stay empty
smoothing vs sharpeningaveraging (integration) blurs, removes noise; derivatives (differentiation) bring out edges
1st vs 2nd derivativethick vs thin double edge; ramp non-zero vs zero; 2nd strong on points, noise-sensitive
edge detection steps; Cannysmooth → gradient → threshold; Canny's 3 criteria and 5 steps
segmentation approachesdiscontinuity (points, lines, edges) and similarity (threshold, region growing, split & merge)
structuring element; erosion vs dilationsmall shape with an origin; fit vs hit; shrink vs grow; one application each
opening vs closingequations, order of operations, specks vs holes, idempotent
chain code; why CFD and NCFDboundary as direction numbers; CFD removes rotation, smallest shift removes start point
data vs information; three redundanciesdata = what is stored, information = what it tells; coding, spatial, irrelevant (+ temporal)
lossless vs lossyexact rebuild (Huffman, LZW, RLE) vs some loss for higher CR (quantization); judge lossy with RMSE
why is Huffman optimal / entropyshortest codes for most likely symbols, prefix-free; H is the lower bound on Lavg
additive vs subtractive; which model for printing, display, segmentationlight vs pigment; CMYK, RGB, HSI
RGB cube; how gray is formedaxes R, G, B 0–1; black origin, white (1,1,1); gray on the diagonal

Q3 case bank (CLO3)

Problem (cue word) → Method → Why it works → Steps / equation → Result + one limitation

Write each case as 4–6 short lines in that order. Name the method in bold in the first line; the examiner looks for it.

SP23 Q1(b) · brain tumor pipeline2 + 2 + 3 + 2

(i) Remove noise: median filter (scan noise is impulse-like; median keeps tumor edges) or Gaussian/mean for random noise.

(ii) Partition by intensity: thresholding, g = 1 if f > T else 0; T found iteratively.

(iii) Highlight a range of values: gray-level slicing, [A, B] → L−1, others 0 (or unchanged).

(iv) Group similar pixels: region growing from a seed inside the tumor, add 8-neighbours with |difference| ≤ T.

SP25 Q3(a) · faded, unevenly lit document for OCR4 + 3 + 3

(i) Enhance: contrast stretching or histogram equalization (faded ink = low contrast) and median filter (background specks).

(ii) Laplacian over Sobel: text is thin strokes and fine detail; the 2nd derivative gives thin, sharp response and g = f − ∇²f sharpens in one mask. Sobel gives thick edges and needs two masks.

(iii) Segment: local (adaptive) thresholding, because illumination is uneven; then horizontal projection to split text lines.

SP25 Q3(c) · satellite roads with spots and gaps3 + 3 + 3

(i) Opening A ∘ B = (A ⊖ B) ⊕ B: erosion deletes spots smaller than B, dilation restores the roads' size.

(ii) Closing A • B = (A ⊕ B) ⊖ B: dilation bridges small gaps, erosion returns the road width. Use a line-shaped SE along the road.

(iii) Boundary β(A) = A − (A ⊖ B): subtract the eroded road from the road; one-pixel outline remains.

F23 Q3(b) · touching coins, small particles5 + 5

(i) Erosion with a disk SE larger than the necks: touching coins split into separate blobs; count with connected-component labeling. (Follow with dilation if size matters = opening.)

(ii) Opening with an SE just larger than the small particles: they cannot contain the SE, so erosion removes them; dilation restores the large particles to their size.

SP24 Q3(a) · salt-and-pepper only on the object4 + 4

(i) Median filter 3×3 on the object region (mask from thresholding), background untouched: impulses sort to the ends, the median ignores them, edges stay sharp.

(ii) Morphology: opening removes white (salt) specks, closing fills black (pepper) dots inside the object.

SP25 Q3(d) · bone fracture, four methods7

Power-law: γ < 1 brightens a dark X-ray so thin cracks show; limitation: also brightens noise.

Histogram analysis / equalization: spreads bone gray levels for contrast; limitation: may over-enhance soft tissue.

Hough transform: finds straight fracture lines by voting in (ρ, θ); limitation: curved cracks are missed.

Chain code: codes the bone outline; a break shows as an irregular CFD (sudden turns); limitation: noise-sensitive, needs a clean boundary.

Traps

Final checklist