Skip to content

Pruning

Fact Value
Also called Spur removal
Status Implemented and tested
Family Morphological filters
Engine Browser
Type Nonlinear, spatial, end point removal and regrowth
Output Mask
Operator shown as algorithm (exact)
Support radius global
Code web/src/core/filters/morphology/binary.ts, pruningFilter
Operator code web/src/core/filters/morphology/binary.ts, prune
Channels Brightness only, RGB

Overview

Pruning removes short branches, or spurs, from thin lines such as the result of thinning. It shortens every line end, which removes the spurs, and then lets the ends of the main lines grow back to their full length.

Two panels: thinned shapes with a few short branches, and the pruned result, in which the short branches are gone and the main lines remain. Two panels: thinned shapes with a few short branches, and the pruned result, in which the short branches are gone and the main lines remain.
Pruning with length 3 removes the short branches that thinning left, and keeps the main lines.

How it works

  1. Remove the end points of every line, all at once, n times.
  2. Find the end points that are left.
  3. Grow them back n times with the 3 x 3 square, but only along the original lines.
  4. Join the shortened lines and the regrown ends.

Mathematical foundation

\[ X_4 = X_1 \cup \left( (X_2 \oplus H) \cap A \right)^{(n)} \]

The formula shows the default parameter values.

where

  • \(n\): The spur length
  • \(H\): The 3 x 3 square

X_1 is A with its end points removed n times. This deletes every branch up to n pixels long and shortens the main lines by n. X_2 holds the end points of X_1. They grow back n times with the 3 x 3 square H, but only inside A, which restores the ends of the main lines. X_4 joins both. The four steps follow Gonzalez and Woods. The end point rule is this filter's own. A pixel is an end point when its neighbors are one corner pixel alone, or one side pixel with none, one or both of the corners next to it. All end points go at once in each removal step.

The four steps follow Gonzalez and Woods. The end point rule is this filter's own. A pixel is an end point when its neighbors are one corner pixel alone, or one side pixel with none, one or both of the corners next to it. A spur next to the end of a main line can grow back with that end.

Kernel or operator

There is no kernel. The Inspector lists the four steps with the chosen length.

Algorithm

  1. Make the image binary.
  2. Remove the end points, all at once, n times.
  3. Find the end points that are left.
  4. Grow them back n times with the 3 x 3 square, inside the original image.
  5. Join the two results.

Parameters

Parameter Default Values Meaning
Spur length length 3 1 to 50 px Branches up to this many pixels long are removed.
Channels channelScope Brightness only RGB, Brightness only Which part of the color image the filter changes.

Implementation

prune in web/src/core/filters/morphology/binary.ts runs the four steps. 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0
2 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 0 0 0 0 0 0 0 0
3 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

A small binary image, 1 for white and 0 for black. The thin line has a spur of two pixels above it. Pruning with length 3 removes the spur and grows the line ends back to full length.

Complexity

  • Time: \(O(N n)\)
  • Memory: \(O(N)\)

N is the number of pixels per channel and n the spur length.

Artifacts and limitations

Limitations

  • A spur next to the end of a main line can grow back with it.

Artifacts

  • Restored spur pixels close to line ends.

Tests

The reference runs the same steps with NumPy. Before the cases are written, the script checks that a line with a short spur loses the spur and keeps its full length. The cases cover two lengths on a thinned image.

References

  • Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 9, morphological image processing.