CSE 4227 · Chapter 11 · Deck 10

Chain code, first difference and shape number

How to write a chain code, why it changes with start point and rotation, and the CC → CFD → NCFD fix. Every past question is solved in answer-script form, with a step-by-step simulator next to each one.

★ Must study on this page

Start here: representation and description

slides 3–9Know in 2 lines
In plain wordsAfter segmentation you have a blob of pixels: "this is the object". To compare it with other objects (is it a 0 or a 6? a bone or a crack?) you need a short, fixed way to write the shape down (representation) and a few numbers that describe it (description). A chain code writes the outline as a list of arrow directions.

Slide 3. Where it fits: segmentation → raw data (pixels) → representation → computation of descriptors. A representation uses either external characteristics (the boundary: shape) or internal characteristics (the pixels inside: regional properties such as colour and texture).

Slide 4. Image representation = how an image is stored and modelled for analysis. Image description = how we characterise and communicate its content.

representation (slides 5–7)what it isexample
1. pixel-basedordered arrays of pixel values (grayscale or RGB); each pixel = intensity/colour at a spatial locationconvolution, filtering (low-level operations)
2. region-basedafter segmentation the image is a set of regions, described by their boundary (external) or the pixels inside (internal)a tumour described by its boundary curve or its inside pixels
3. feature-basedextracted features (edges, corners, textures, shapes) instead of raw pixels; compact and robust for recognitionSIFT, HOG, deep-learning embeddings
descriptor (slide 8)what it holds
boundary descriptorschain codes: the boundary as a sequence of line segments with direction and length; useful for shape analysis
region descriptorsstatistics: mean intensity, variance, texture; colour histograms of a region
structural descriptorsgraph-based models: parts as nodes and their relationships as edges
semantic descriptionhigh-level meaning ("a cat sitting on a chair"), done by deep-learning models that map features to language

Chain code: what it is and how to write one

Must studyslides 10–13Came in past papers
In plain wordsStand on one pixel of the outline. Look at where the next outline pixel is: right, up-right, up… Write that direction as a number. Step there and repeat until you are back where you started. The list of numbers is the chain code. You no longer need the picture: start pixel + numbers redraw the whole outline.

Slide 10. A chain code is a boundary representation technique used to describe the shape of an object. Instead of storing all pixels of a boundary, it encodes the contour as a sequence of directional steps.

Slide 11. How it works: (1) boundary extraction: find the contour of the object; (2) starting point selection: pick one boundary pixel; (3) directional encoding: trace the boundary pixel by pixel and write each step as a direction number.

01234567
8-directional chain code (8-connectivity): 0 right, 1 up-right, 2 up, 3 up-left, 4 left, 5 down-left, 6 down, 7 down-right
0123
4-directional code (slide 11 calls it crack code): 0 right, 1 up, 2 left, 3 down
TrapDirection numbers go round counterclockwise starting from 0 = right, like angles in maths: code k means angle k × 45° (8-dir) or k × 90° (4-dir). Up is 2 in 8-dir but 1 in 4-dir. Draw the compass in every answer.

Slide 10 example. A square boundary traced counterclockwise from the bottom-left pixel: two steps right, two up, two left, two down.

01201200224466
slide 10 square: CC = 00224466

Slide 12. Chain code example (the digit "0"): start at the blue pixel at the top and walk clockwise. Code: 0, 0, 7, 7, 5, 6, 5, 5, 4, 3, 2, 2, 2, 1, 2. This is also the SP24 final figure (solved below).

01234560123456007756554322212
slide 12: CC = 007756554322212 (15 moves)

Try it, step by step: slide 12, the digit 0. Watch each move get its number, then the first difference and the normalisation.

Slide 12. Advantages: compact representation of boundaries; easy to store and manipulate; useful for shape analysis and recognition.

Slide 13. Simulation on a region of dots: start at the red dot (bottom-left of the bottom row) and walk counterclockwise. Chain code 001232446567.

0123401234001232446567
slide 13: CC = 001232446567

Try it, step by step: slide 13 simulation, counterclockwise from the red dot.

Slide 13. Is the chain code translation invariant? YES. Moving the whole shape somewhere else in the image does not change any step, because the code stores only relative moves, never absolute positions.

Why the plain chain code is not enough

slides 14–19Came in past papers
In plain wordsThe same shape can give many different chain codes: start at another pixel and the list is shifted; turn the shape and every digit changes; make it bigger and the list gets longer. For recognition we want one code per shape. That is what the next section fixes.

Slide 14. Is invariance needed? The letter F appears moved, scaled and rotated: we want translation, scale and rotation invariance so all of them are recognised as F.

Slide 15. Is it invariant to rotation, scaling or reflection? An L-shaped boundary with the 4-direction code:

case (slide 15)chain code
original03032211
start point changed03221103
reflected01033221
rotated10033212

Every case gives a different code even though it is the same L. Slide 16. Why invariance matters: shape recognition, classification and matching need a representation that stays the same despite rotation, scaling or reflection.

Slide 17. Problems in chain code representation (learn this list, it is a 3-mark theory question):

Slide 18. Starting point solution using shape number (same direction). A square traced clockwise from three different corners:

startchain code
top-left000666444222
top-right666444222000
bottom-left222000666444

The shape number is the smallest sequence among all circular shifts of the CC, here 000666444222. It is starting-point invariant.

Try it, step by step: slide 18 square. Change the start point, then normalise the CC: the smallest shift is the same every time.

Slide 19. Rotation and scaling comparison: rotating the square by 45° turns every 0/2/4/6 into 1/3/5/7 (different code); scaling it up makes the code longer. The shape number alone does not fix either.

TrapSlides 18, 19, 21 and 24 have AI-made pictures ("Made with AI") and a few printed digits are sloppy (e.g. slide 24 shows an 11-digit CFD for a 12-move square). Trust the method, not those digits. For the 12-move square 000222444666 the CFD is 002002002002.

The fix: CC → CFD → NCFD (shape number)

Must studyslides 20–24Came in past papers
In plain wordsRotation changes every direction by the same amount. So instead of storing the directions, store the turns: how much you turn at each corner. A turn does not care which way the shape faces. That is the first difference. Then, to forget the start point, read the ring of turns from the place that gives the smallest number.

Slide 20. Chain code changes with: (1) changing the start point, (2) reflection, (3) rotation. Solution is normalisation, in 3 steps:

1. Chain code (CC)  →  2. Circular First Difference (CFD)  →  3. Normalised CFD (NCFD, aka shape number)

Step 2: circular first difference

CFDi = (ci+1 − ci) mod n   for i = 1 … N,   with cN+1 = c1 (wrap round)

n = 8 for an 8-direction code, 4 for a 4-direction code. "Circular" means the last digit compares the last code with the first one, because the boundary is a closed loop. So the CFD has exactly as many digits as the CC.

Why it works: rotating the shape by 90° adds 2 (8-dir) to every code digit. In (ci+1 + 2) − (ci + 2) the 2 cancels. So the CFD is rotation invariant (for multiples of 45° in 8-dir, 90° in 4-dir).

Meaning of a CFD digit (8-dir): 0 = straight on, 1 = 45° left turn, 2 = 90° left, 4 = U-turn, 6 = 90° right, 7 = 45° right.

Step 3: normalise (minimum magnitude)

Write the CFD from every start (all circular shifts) and pick the smallest number. This is the NCFD, also called the shape number: invariant to start point and rotation.

Slide 21 example (4-direction, slide 15 L shapes): all four codes end at the same NCFD:

caseCCCFD (next − this) mod 4NCFD
original030322113133030303033133
start point changed032211033303033103033133
reflected010332211330303303033133
rotated100332123030331303033133

NCFD known as SHAPE NUMBER, an invariant code: 03033133 (slide 22).

Try it, step by step: slide 21–22 L shape, 4-direction code 03032211. Use the compass with 4 arrows.

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 23: Gonzalez's version (different subtraction!)

Three shapes (the third is rotated 90°) with chain codes 444201066, 066444201, 067644222. "Curvature = differences in chain code values", then "normalise curvature = mod 8", then "code is the cyclic permutation which produces the smallest number": 002271202 for all three.

chain codethis − next (slide 23)mod 8smallest shift
4442010660 0 2 2 -1 1 -6 0 2002271202002271202
066444201-6 0 2 0 0 2 2 -1 1202002271002271202
067644222-6 -1 1 2 0 2 0 0 2271202002002271202
TrapSlide 23 subtracts this − next; slide 22 subtracts next − this. Each digit of one is 8 minus the other (mod 8), so the final numbers differ (slide-22 rule on these shapes gives 006617606). Both are invariant. In the exam use slide 22's rule (next − this, the textbook rule), write the formula, and the marker can follow you.

Try it, step by step: slide 23 shape 1 (444201066). The difference rule is set to slide 23's this − next; switch it to see the slide-22 answer.

Slide 24. Comparison table:

representationstart-point invariantrotation invariantnotes
chain code (CC)✗✗basic, sensitive to start and rotation
shape number (smallest circular shift of CC)✓✗fixes start-point ambiguity only
CFD (differential chain code)✗✓encodes direction changes
NCFD (normalised CFD)✓✓fixes both start point and rotation
Trap"Shape number" means two things in this deck: slide 18/24 use it for the smallest shift of the CC; slides 21–22 use it for the NCFD. In an answer, write both lines with their names ("start-point invariant CC = …", "NCFD = …") and you cannot lose the mark.

Long codes, noise and resampling; applications; class work

slides 25–29On study list

Slides 25–26. Problems with a raw pixel boundary: (1) long chains of codes; (2) no invariance to rotation and scale; (3) sensitive to noise: easily disturbed by small bumps; plus the start point problem.

Solution: re-sample the boundary to a lower resolution (a coarser grid) before calculating the code. Each boundary point is moved to the nearest node of the coarse grid; the code is then written between those nodes.

Slide 27. Examples: object boundary → resampled boundary vertices → 4-directional or 8-directional chain code. Before resampling the code is long (000000766…1111); after resampling it is short (076666…12). Fewer digits, and tiny bumps disappear.

In plain wordsResampling is like tracing the outline with a thick marker on graph paper: you only keep the big turns, not every wiggle of the pen.

Slide 28. Applications:

fielduse casehow chain codes help
object detectionfind shapes (circles, squares, L-shapes) in documents or industrial partstrace the boundary; shape numbers match templates regardless of orientation
character recognition (OCR)handwritten or printed characterseach character's contour is a chain code; shape numbers recognise it even if rotated or slightly distorted
medical imagingtumours, organs, bones in scansthe code describes the boundary; shape numbers compare across patients or time
remote sensinglakes, buildings in satellite imagescontours as codes; shape numbers classify despite rotation or scale
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.

Past questions solved like the answer script

Must studyslides allCame in past papers

Every chain-code question so far is the same recipe: (1) write the CC with a labelled figure and compass, (2) smallest shift of the CC if they ask "invariant to starting point", (3) CFD, (4) smallest shift of the CFD if they ask "starting point and rotation". The figures below are redrawn from the question papers (row, column counted from 0 at the top-left unless the paper prints x–y axes).

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).

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.

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.

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 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).

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.

Traps and one-minute sheet

slides all
whathowinvariant to
chain code CCone direction number per boundary steptranslation
smallest shift of CCmin over circular shifts+ start point
CFD(ci+1 − ci) mod n, wrap round+ rotation (multiples of 45°/90°)
NCFD (shape number)min over circular shifts of CFDstart point + rotation
resamplingcoarser grid before codingless noise, shorter code (helps scale)