Skip to content

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.

Two panels: white shapes on black, a bar, an L, a ring, a triangle with a bump and a few specks, and the thinned result, with every shape reduced to thin lines and the ring still a closed loop. Two panels: white shapes on black, a bar, an L, a ring, a triangle with a bump and a few specks, and the thinned result, with every shape reduced to thin lines and the ring still a closed loop.
Zhang-Suen thinning reduces every shape to lines one pixel thick. The ring stays a closed loop.

How it works

  1. Look at every white pixel on the outline of a shape.
  2. Remove it if that keeps the shape in one piece and does not shorten a line.
  3. Do this in two sub-iterations that favor opposite sides, and repeat until nothing changes.

Mathematical foundation

\[ A_{k+1} = A'_k \setminus D_2(A'_k),\quad A'_k = A_k \setminus D_1(A_k) \]

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

  1. Make the image binary.
  2. In the first sub-iteration, remove at once every pixel the first rule allows.
  3. In the second, do the same with the second rule.
  4. 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