Quiz: connectivity and labelling
Before this unit: Day 13: Morphology.
Short questions to test your understanding. Try each on paper first, then open the answer. Objects are 8-connected and the background 4-connected unless a question says otherwise.
Q1. Two 2 × 2 blocks of object pixels touch only at one corner. How many objects are there with 8-connectivity, and with 4-connectivity?
Which neighbourhood contains the diagonal neighbour?
One with 8-connectivity (the corner pixels are diagonal neighbours), two with 4-connectivity.
Q2. Can a mask have more objects with 8-connectivity than with 4-connectivity?
Is a 4-path also an 8-path?
No. Every 4-path is an 8-path, so every 4-component lies inside an 8-component. 8-connectivity can only merge objects, never split them.
Q3. A closed curve drawn with diagonal steps only. Why is it a bad idea to use 8-connectivity for both the curve and the background?
Can a background path pass between two diagonal curve pixels?
With 8-connected background, a path can step diagonally between two diagonally touching curve pixels, so the inside is connected to the outside. The curve is closed (one object) but does not separate: the digital version of Jordan’s theorem fails. With background 4 the inside is cut off, as it should be.
Q4. You flood-fill a large square object from its top-left corner, once with a queue and once with a stack. Describe how the labelled region grows in each case.
Which pixel is taken next: the oldest or the newest?
Queue (oldest first): breadth-first, the region grows in rings of equal path distance from the seed (diagonal fronts with 4-connectivity), like a geodesic dilation. Stack (newest first): depth-first, the fill runs along one direction as far as it can, then backtracks: long arms. Both label the same component.
Q5. Why should a flood fill not be written as a recursive function on large images?
How deep can the recursion go?
Each recursive call waits on the call stack until its neighbours are done. For a big object the depth can approach the number of object pixels (a million for a 1000 × 1000 object), far beyond the call stack. An explicit queue or stack in ordinary memory has no such limit.
Q6. In the first pass of the two-pass algorithm with 8-connectivity, which neighbours of the current pixel are examined, and why only these?
Which pixels already have a label in raster order?
Up-left, up, up-right and left: the 8-neighbours that come earlier in raster order and therefore already carry a provisional label. Every pair of 8-neighbours is looked at exactly once, when the later of the two is visited.
Q7. A U-shaped object is labelled with the two-pass algorithm. How many provisional labels does it get, and where is the equivalence recorded?
The scan meets the two arms before it meets the bottom.
Two: one per arm, because the arms are not yet connected when the scan reaches their top rows. The equivalence is recorded at the first pixel of the bottom part whose visited neighbours carry both labels. The second pass gives both arms the same final label.
Q8. What do union by rank and path compression each prevent?
What makes a find expensive?
A find costs the depth of the node. Union by rank prevents deep trees from being built (height at most \log_2 n). Path compression repairs the trees during finds: every node visited now points directly to the root, so the next find from there takes one step. Together, the cost per operation is almost constant.
Q9. (math) Show that with union by rank, a root of rank r has at least 2^r nodes in its tree.
Induction on r: when does a rank grow?
Rank 0: one node. A root gets rank r + 1 only when two roots of rank r are united; by induction each tree has \ge 2^r nodes, so the new tree has \ge 2^{r+1}. Unions with a lower-ranked tree add nodes but do not change the rank. Hence the rank, and the height, is at most \log_2 n.