Thinning¶
| Fact | Value |
|---|---|
| Also called | Zhang-Suen thinning, Guo-Hall thinning |
| Status | Implemented and tested |
| Family | Morphological filters |
| Engine | Browser |
| Type | Nonlinear, spatial, parallel thinning in two sub-iterations |
| Output | Mask |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | web/src/core/filters/morphology/binary.ts, thinningFilter |
| Operator code | web/src/core/filters/morphology/binary.ts, thin |
| Channels | Brightness only, RGB |
Overview¶
Thinning peels white shapes layer by layer until only lines one pixel thick remain. It never breaks a shape into pieces or joins two shapes, and it keeps line ends. Two classic algorithms are offered: Zhang-Suen and Guo-Hall.
How it works¶
- Look at every white pixel on the outline of a shape.
- Remove it if that keeps the shape in one piece and does not shorten a line.
- Do this in two sub-iterations that favor opposite sides, and repeat until nothing changes.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(A_k\): The foreground after k iterations
- \(D_1, D_2\): The pixels the two sub-iterations remove
D_1 and D_2 are the pixels that the first and the second sub-iteration may remove. Each is decided from the 8 neighbors of a pixel, so that removing it keeps shapes connected and line ends in place. All pixels marked in one sub-iteration go at once. Zhang-Suen follows the rules of the 1984 paper. The zhang method of skimage.morphology.skeletonize uses a modified table and gives other results. Guo-Hall follows the tables of skimage.morphology.thin and matches it.
Both algorithms decide from the 8 neighbors of a pixel whether it may go. Removing all such pixels at once could erase a line two pixels thick, so each iteration has two sub-iterations with slightly different rules. Zhang and Suen count the neighbors and the black-to-white changes around the pixel. Guo and Hall use a connectivity count and two neighbor counts. The zhang method of skimage.morphology.skeletonize uses a modified table, so it gives other results than the rules of the paper used here.
Kernel or operator¶
There is no kernel. The Inspector lists the thinning step with the chosen algorithm.
Algorithm¶
- Make the image binary.
- In the first sub-iteration, remove at once every pixel the first rule allows.
- In the second, do the same with the second rule.
- Repeat until nothing changes, or for the chosen number of iterations.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Method method |
Zhang-Suen | Zhang-Suen, Guo-Hall | The thinning algorithm. Both remove pixels in two sub-iterations per iteration. |
Iterations iterations |
0 | 0 to 100 | The largest number of iterations. 0 runs until nothing changes. |
Channels channelScope |
Brightness only | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
thin in web/src/core/filters/morphology/binary.ts builds a table of 256 neighbor codes for each sub-iteration and removes all marked pixels at once. Pixels outside the image count as black. It is a global filter, so it always runs on the whole image in one tile, and a selection is cut out of the result. The filter runs in the browser engine through the shared filter framework in web/src/core/filters/run.ts, which handles the channel scope and alpha. Values at or above 128 count as white, and the result is 255 or 0.
Worked example¶
| Row | Input | Result |
|---|---|---|
| 0 | 1 0 0 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 0 0 0 0 0 0 0 0 |
| 2 | 0 1 1 1 1 1 1 0 0 0 |
0 0 0 0 0 0 0 0 0 0 |
| 3 | 0 1 1 1 1 1 1 0 0 0 |
0 0 1 1 1 0 0 0 0 0 |
| 4 | 0 1 1 1 1 1 1 0 0 0 |
0 0 0 0 0 0 0 0 0 0 |
| 5 | 0 0 0 0 0 0 0 0 0 0 |
0 0 0 0 0 0 0 0 0 0 |
| 6 | 0 0 1 0 0 0 0 0 0 0 |
0 0 1 0 0 0 0 0 0 0 |
| 7 | 0 0 1 0 0 0 0 0 0 0 |
0 0 1 0 0 0 0 0 0 0 |
| 8 | 0 0 1 1 1 0 0 0 0 0 |
0 0 1 1 1 0 0 0 0 0 |
A small binary image, 1 for white and 0 for black. The bar of three rows thins to one row. Thinning also shortens the ends a little.
Complexity¶
- Time: \(O(N k)\)
- Memory: \(O(N)\)
N is the number of pixels per channel and k the number of iterations, about half the thickness of the widest shape.
Artifacts and limitations¶
Limitations
- Small bumps on the outline become short branches.
- Not rotation invariant.
Artifacts
- Short spurs from bumps on the outline.
Tests¶
For Guo-Hall the reference is skimage.morphology.thin. For Zhang-Suen it is a NumPy version of the rules of the paper. Before the cases are written, the script checks that a thick bar thins to a line one pixel thick, and that both algorithms keep the number of shapes. The cases cover both methods, with and without an iteration limit.
References¶
- Zhang, T. Y. and Suen, C. Y. A fast parallel algorithm for thinning digital patterns. Communications of the ACM 27(3), 236 to 239, 1984. DOI 10.1145/357994.358023.
- Guo, Z. and Hall, R. W. Parallel thinning with two-subiteration algorithms. Communications of the ACM 32(3), 359 to 373, 1989. DOI 10.1145/62065.62074.
- scikit-image documentation, skimage.morphology.thin. https://scikit-image.org/docs/stable/api/skimage.morphology.html#skimage.morphology.thin