Exercise: hit-or-miss and thinning

Goal

By the end of this exercise you should be able to:

  • apply a hit-or-miss template (1, 0, don’t care) by hand, and choose templates for end points, isolated pixels and junctions,
  • decide from a 3 × 3 neighbourhood whether a pixel is simple, and explain why,
  • carry out template thinning on a small shape, and explain why the templates are applied one at a time and why end points are kept,
  • (math) write hit-or-miss as two erosions, and use Yokoi’s connectivity number.

Do the pen-and-paper part first, then the demo. Pixels are written (row, column), counted from 0 at the top left, as in the demo.

Background

Idea What it means
Template 3 × 3 cells: 1 = must be object, 0 = must be background, · = don’t care
Hit-or-miss marks the pixels where the template, centred there, fits completely; pixels outside the image are background
Rotations turning a template by 45° moves each outer cell one step around the centre; 8 rotations of the end-point template cover all 8 directions
Thinning templates L1 = rows 000 / ·1· / 111 (an edge pixel), L2 = rows ·00 / 110 / ·1· (a corner pixel), each turned by 0°, 90°, 180°, 270°, applied in the order L1, L2, L1 ↻90°, L2 ↻90°, …
One thinning pass for each template in turn: remove every pixel where it fits; repeat passes until nothing changes
Simple pixel among its 8 neighbours: exactly one object part (8-connected) and exactly one background part touching the pixel’s 4 neighbours (4-connected)
End point exactly one object neighbour; simple, but kept by thinning

By hand

B1. The 7 × 7 image (1 = object):

\begin{matrix} 0&0&0&0&0&0&0\\ 0&0&0&0&0&0&0\\ 0&1&1&1&1&0&0\\ 0&0&0&0&0&1&0\\ 0&0&0&0&0&0&0\\ 0&1&0&0&0&0&0\\ 0&0&0&0&0&0&0 \end{matrix}

  1. Apply the end-point template with a 1 above the centre (rows 010 / 010 / 000). Which pixels does it find?
  2. Apply all 8 rotations of it. Which pixels are found, and by which rotation (say where the single neighbour is)?
  3. Is the pixel at (5, 1) found? Which template finds it?
  4. Which neighbours does pixel (2, 4) have? Is it an end point, a regular line pixel or a junction?

B2. Five neighbourhoods, centre pixel = 1 (rows top to bottom). For each: count the object parts and the background parts touching the centre, decide simple or not, and say in words what removing the centre would do. (a) 010 / 010 / 010 (b) 110 / 110 / 000 (c) 111 / 111 / 111 (d) 010 / 110 / 000 (e) 101 / 010 / 000

B3. A 3 × 7 rectangle (rows 1–3, columns 1–7 of a 5 × 9 image, background around it). (a) Where does L1 (rows 000 / ·1· / 111) fit in the first pass? Why not at the corner (1, 1)? (b) Carry out the first pass with the 8 templates in order (you may use the table: only L1, L2 ↻90° and L1 ↻180° remove anything). Which pixels go? (c) Continue until nothing changes. Draw the skeleton and count its pixels. Is it in the middle of the rectangle? What happens at the two ends?

B4. A horizontal line two pixels thick (rows 1–2, columns 1–10). (a) Which template removes the top row, which the bottom row? (b) What is left if all 8 templates are tested on the same image and all matches are removed together? And with one template at a time?

B5. A square ring (a 7 × 7 square with a 3 × 3 hole). (a) What are its numbers of objects and holes, and its Euler number (objects − holes)? (b) Thinning removes only simple pixels. What can you say about the skeleton’s number of objects and holes, without computing it? (c) A thinning that also removed end points: what would a solid 3 × 7 rectangle become? And the ring?

B1. The line runs along row 2 from (2, 1) to (2, 4) and then diagonally down to (3, 5); (5, 1) is a lone pixel. (a) Nothing: no pixel has its only neighbour directly above it. (b) Two pixels. (2, 1): its only neighbour is to the right (2, 2), found by the rotation 000 / 011 / 000. (3, 5): its only neighbour is up-left (2, 4), found by 100 / 010 / 000. (c) No: it has no neighbour at all, so no end-point template fits (each needs one 1). The isolated-pixel template 000 / 010 / 000 finds it, and only it. (d) (2, 4) has the neighbours (2, 3) and (3, 5): two neighbours, a regular line pixel. (2, 2) and (2, 3) also have two each. There is no junction.

B2.

object parts background parts simple? removing the centre …
(a) 010/010/010 2 (above, below) 2 (left, right) no cuts the line in two
(b) 110/110/000 1 1 yes shaves a corner off a block
(c) 111/111/111 1 0 no makes a hole
(d) 010/110/000 1 (left and above touch diagonally) 1 yes the two neighbours stay connected through their corner
(e) 101/010/000 2 (the two diagonals do not touch) 2 (above, and the rest) no cuts the shape in two

In (d) the background cell at the top left is cut off from the other background cells (its 4-neighbours in the ring, above and left of the centre, are object), but it is not a 4-neighbour of the centre, so it is not counted: one background part.

B3. (a) L1 needs background in the row above and object in the whole row below: it fits at (1, 2) to (1, 6). At (1, 1) the row below would be (2, 0), (2, 1), (2, 2), and (2, 0) is background. (b) L1 removes (1, 2)–(1, 6) (5 pixels). L2 ↻90° (rows ·1· / 110 / ·00) removes the corner (3, 7). L1 ↻180° (rows 111 / ·1· / 000) removes (3, 2)–(3, 6). The other templates find nothing. (c) Pass 2: L2 ↻90° removes (2, 7). Pass 3 removes nothing. The skeleton has 9 pixels: row 2 from (2, 1) to (2, 6), plus (1, 1), (3, 1) and (1, 7):

\begin{matrix} 0&0&0&0&0&0&0&0&0\\ 0&1&0&0&0&0&0&1&0\\ 0&1&1&1&1&1&1&0&0\\ 0&1&0&0&0&0&0&0&0\\ 0&0&0&0&0&0&0&0&0 \end{matrix}

It runs along the middle row, but the ends are not tidy: at the left the corners are kept (a short fork), at the right one corner pixel survives. Corners of a wide shape behave like the ends of lines.

B4. (a) L1 (background above, object below) removes the top row except its ends; L1 ↻180° (object above, background below) the bottom row. (b) In parallel: both rows match at once (each was tested on the original image), all 20 pixels go, nothing is left. One template at a time: after L1 has removed the top row, the bottom-row pixels have background above and below; they are the middle of a one-pixel line and no template fits them. A one-pixel line survives: (2, 2) to (2, 9), plus the two end pixels (1, 1) and (1, 10) (10 pixels).

B5. (a) One object and one hole: Euler number 1 − 1 = 0. (b) Removing simple pixels never changes the number of objects or holes, so the skeleton is still one object with one hole: a closed loop around the hole. (c) A rectangle would shrink, pixel by pixel from the ends, to a single pixel (one object, no hole, the same topology). The ring cannot: once it is a one-pixel loop, every pixel has two object parts (its two neighbours along the loop) and is not simple, so the loop stays.

Math questions

B6. (math) Show that the hit-or-miss transform with B_1 (the 1-cells) and B_2 (the 0-cells) is (A \ominus B_1) \cap (A^c \ominus B_2), and that it is empty for every image if B_1 \cap B_2 \neq \emptyset. Show also that (A^c \ominus B_2) = (A \oplus \check B_2)^c.

B7. (math) Yokoi’s connectivity number for 8-connected objects is N = \sum_{k \in \{0, 2, 4, 6\}} (\bar x_k - \bar x_k \bar x_{k+1} \bar x_{k+2}), with x_0, \dots, x_7 the neighbours going round from the top (N, NE, E, SE, S, SW, W, NW), \bar x = 1 - x and x_8 = x_0. Compute N for the five neighbourhoods of B2. For which is N = 1?

B8. (math) Why do we pair 8-connectivity for the object with 4-connectivity for the background? Consider a diagonal line of pixels ((0,0), (1,1), (2,2), \dots) running across an image: is it connected, and does it separate the image into two background parts, with (a) 8/4, (b) 8/8, (c) 4/4?

B6. The template fits at x if x + b \in A for all b \in B_1 (that is x \in A \ominus B_1) and x + b \notin A, i.e. x + b \in A^c, for all b \in B_2 (that is x \in A^c \ominus B_2). Both must hold: the intersection. If b \in B_1 \cap B_2, then x + b would have to be in A and in A^c: impossible, so the result is empty. Duality of erosion and dilation: x \in A^c \ominus B_2 iff no x + b (b \in B_2) lies in A iff x \notin A \oplus \check B_2.

B7. (a) x = (1,0,0,0,1,0,0,0): the terms for k = 0, 2, 4, 6 are 0, 1, 0, 1: N = 2. (b) neighbours N, W, NW: x = (1,0,0,0,0,0,1,1): terms 0, 1 - 1 = 0, 1 - 0 = 1, 0: N = 1. (c) all 1: N = 0. (d) N and W: x = (1,0,0,0,0,0,1,0): terms 0, 0, 1, 0: N = 1. (e) NE and NW: x = (0,1,0,0,0,0,0,1): terms 1 - 1\cdot 0 \cdot 1 = 1, 1 - 1\cdot1\cdot1 = 0, 0, 1 - 1 \cdot 0 \cdot 1 = 1: N = 2. N = 1 exactly for (b) and (d), the simple ones.

B8. (a) 8/4: the diagonal is one connected object (diagonal steps allowed), and the background on its two sides cannot cross it with 4-steps: two background parts. Consistent, like a real curve (the digital Jordan curve theorem holds). (b) 8/8: the line is connected, but the background also leaks through the diagonal gaps: one background part, so a closed curve would not separate inside from outside. (c) 4/4: the diagonal pixels are not connected to each other, yet the background cannot pass between them: neither a curve nor a gap. Only the mixed pairs 8/4 and 4/8 behave like curves in the plane.

Explore

The demo works on a small grid: click cells of the image to change them. The left column holds the template (click a cell: 1 → 0 → ·), template presets and four switches. The algorithms: hit-or-miss (the template at every object pixel: matching cells green, failing cells red; the result on the right), thin: 8 templates (one frame per template application; the right grid numbers each pixel by the pass that removed it, dark = kept), thin: simple points (visits the border pixels side by side and removes the simple ones), and simple? (every object pixel with its object parts a, b, … and background parts 1, 2, …). Step, play, or jump to the end; next stage jumps a row or a pass.

E1. Preset find the end points is the image of B1. Step through and check your answers to B1(a)–(c): untick all 8 rotations for (a), tick it for (b), and choose the isolated template for (c).

E2. Preset isolated pixels. Which pixels does the isolated template find? Switch to the end point template with all rotations: what does it find now, and why are the pixels of the 3 × 3 block found by neither? Try the corner template (no rotations).

E3. Preset simple or not? holds the five neighbourhoods of B2 side by side (centres in row 2, columns 2, 7, 12, 17, 22). Step to each centre and compare with your table. At the end, how many of the 22 object pixels are simple, how many are end points, how many are not simple? Tick click inspects a pixel and click on other pixels.

E4. Preset thin a rectangle (B3). Step through the first pass template by template (the box under the switches shows the template in use). Compare with B3(b) and (c).

E5. Preset parallel removal cuts the line (B4). What is left? Untick parallel (no subiterations): what is left now?

E6. Preset a ring keeps its hole: simple-point thinning without keeping end points. What remains, and why does it stop there? Then preset no end points: a line shrinks (the rectangle of B3). Where does it end? Tick keep end points: what is the result now, and how does it differ from the template result of E4?

E7. Preset thin an L. How many passes, and how many skeleton pixels? Where are the short forks, and why there? Draw your own shape (or start from empty 12 × 12) with a hole, thin it, and check that the hole survives.

E1. Without rotations: nothing is found. With all 8: (2, 1) by rotation 3 (neighbour to the right) and (3, 5) by rotation 8 (neighbour up-left), as in B1(b). The isolated template finds only (5, 1).

E2. Isolated pixels: (1, 1), (3, 3) and (6, 8). The end-point templates find (6, 1) and (6, 2), the two ends of the two-pixel segment; the isolated pixels have no neighbour, so they are not end points. A pixel of the 3 × 3 block has at least three object neighbours, so no template with exactly one 1 fits. The corner template (rows 000 / 011 / 011) finds the block’s top-left corner (2, 5).

E3. Centres: (2, 2) not simple (2 object parts), (2, 7) simple, (2, 12) not simple (no background part: a hole would appear), (2, 17) simple, (2, 22) not simple. Over all 22 object pixels: 15 simple, 4 end points, 3 not simple (the three centres).

E4. As in B3: pass 1, L1 removes 5 pixels of the top row, L2 ↻90° the corner (3, 7), L1 ↻180° 5 pixels of the bottom row; pass 2, L2 ↻90° removes (2, 7); pass 3 removes nothing. 9 pixels remain.

E5. In parallel all 20 pixels are removed in the first pass: nothing remains. Sequentially a one-pixel line of 10 pixels remains: (2, 2) to (2, 9) plus the end pixels (1, 1) and (1, 10).

E6. The ring (36 pixels) becomes a one-pixel loop of 16 pixels around the hole in one pass (the demo reports 2 passes: the second removes nothing). On a one-pixel loop every pixel has two object parts, so none is simple, and thinning stops even though end points are not protected. The rectangle without end points shrinks to the single pixel (2, 6). With keep end points, the middle row from (2, 1) to (2, 6) remains: a straight centred line without the forks of the template result (the order and rules of removal change the details of the skeleton).

E7. 3 passes remove pixels (a fourth finds nothing); 18 of the 49 pixels remain. Short forks appear at the outer corners of the L: there, two sides of the shape meet at a right angle, and thinning treats the corner like a line end. A hole is never closed: its border pixels have two background parts or two object parts once the wall is one pixel thick, so they are not removed.

Code

Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in skeleton/src/skel_thinning/exercise.rs. A template is written row by row, with 1, 0 and DONT_CARE. The code indexes pixels as img[(x, y)] = (column, row), while this page writes (row, column).

C1. hit_or_miss(img, t): mark the pixels where the template fits:

for every pixel p:
    fits = true
    for every cell c of the template that is not "don't care":
        v = the image at p + (offset of c)          # outside the image: 0
        if v ≠ the value of c:  fits = false
    out(p) = fits

The check uses B1(a) and (c).

C2. rotate45(t): turn a template by 45° clockwise: each of the 8 outer cells moves one step around the centre. With it, hit_or_miss_all_turns (given) finds all end points; the check is B1(b).

C3. is_simple(img, x, y): count the parts among the 8 neighbours:

object parts     = groups of object neighbours, 8-connected (within the 3 × 3 window)
background parts = groups of background neighbours, 4-connected,
                   counting only groups that contain a 4-neighbour of the centre
simple = (object parts = 1) and (background parts = 1)

The checks are the five neighbourhoods of B2.

C4. thin(img, parallel): template thinning with L1 and L2 in 4 turns each:

repeat until nothing changes:
    for each of the 8 templates, in the order L1, L2, L1 ↻90°, L2 ↻90°, …:
        remove every pixel where the template fits     # sequential: on the current image

In parallel, all 8 templates are tested on the same image and their matches removed together. The checks are the rectangle of B3 (9 pixels after 2 passes) and the two-pixel line of B4.

C5. thin_simple(img, keep_end_points): thinning by simple points, side by side:

repeat until nothing changes:
    for side in above, below, right, left:
        B = the object pixels with background on that side
        for p in B:
            if p is simple now (and, if end points are kept, p has more than one neighbour):
                remove p

The checks are B5: the ring keeps its hole, the rectangle shrinks to one pixel without end points, and stays a line along the middle row with them.

Then cargo run -p skeleton -- skel-thinning and open the pictures in out/skel-thinning/ (grey: mask, white: skeleton, blue: end points, red: pixels with 3 or more skeleton neighbours). The program thins a rectangle, a ring, the letter F and a synthetic neuron in three ways (templates, simple points with end points kept, simple points without) and prints the pixel and end-point counts.

C6. Compare the end points of the template skeletons and the simple-point skeletons of the letter F and the neuron (letter-f-templates.png, letter-f-simple.png). Which count is right, and where do the extra end points come from?

C7. neuron-parallel.png is the neuron thinned with all 8 templates at once. What is left? Explain with B4.

C8. Look at the column “no ends”. What happened to each shape, and why is the ring different?

The model solution is skeleton/src/skel_thinning/solution.rs in the download.

C6. The letter F has three tips (top arm, middle arm, foot of the stem). Simple-point thinning with end points kept finds 3 end points; template thinning finds 7. Template thinning leaves short forks at the square ends and corners of wide strokes (the rectangle of B3 in large: 4 end points for the rectangle), because a corner of a wide shape is thinned like the end of a line. Each fork adds an end point and a junction, and the forked skeletons are larger (rectangle 45 against 22 pixels). The neuron has 8 dendrite tips. Simple points give 9 end points: the 8 tips and a short spur at the elbow near the bottom, where the thick dendrite bends into a thinner one (the corner of a wide stroke again). Templates give 11: the same 9, and two tips that end in a fork.

C7. Of 4973 pixels, 392 are left, broken into short pieces: 140 end points and 37 isolated pixels. Wherever a stroke is two pixels thick at some step, opposite templates remove both sides at once and cut the line, as in B4. Sequentially the same templates give a connected skeleton.

C8. Without keeping end points, the rectangle, the letter F and the neuron each shrink to a single pixel: every end point is simple, so lines shorten from their ends until one pixel is left (one object, no hole: the topology is kept). The ring keeps a closed loop of 64 pixels: on a one-pixel loop every pixel has two object parts and is not simple, so it cannot shrink further without opening the hole (B5).

Where this leads

Thinning gives a connected one-pixel line, but “the middle of the shape” was never defined. The next unit defines it: the medial axis, the centres of the largest discs that fit inside the shape, which are the ridges of the distance transform. The unit after that turns the skeleton into a graph of branches and junctions that we can measure.