Crate skeletonize
source ·Expand description
A line thinning library for binary images, including edge detection and threshold functions for preprocessing images into binary images.
The goal of thinning is to remove excess pixels from the image until the lines present are one pixel wide, resembling a “skeleton” of the original pattern.
The thinning algorithms are based on the papers Zhang & Suen, 1984 and Chen & Hsu, 1988. See Reference.
Usage
There are three main workflows for thinning images with this library. The second and third workflows produce binarized images with library functions before thinning the image.
The generic ForegroundColor
parameter on
edge_detection::sobel
, edge_detection::sobel4
, and
thin_image_edges
specifies what foreground and
background colors the resulting
thin_image_edges
image will produce. The
foreground color is the color of the line to be thinned. A foreground color
of white will have a black background and a foreground of black will have a
white background. The generic parameters must match when using an edge
detection function in combination with the thinning function.
An example program can be viewed at /examples/skeletonize.rs
.
No preprocessing
The image is already binarized so the edges can be thinned immediately.
use skeletonize::{foreground, thin_image_edges, MarkingMethod};
let method = MarkingMethod::Modified;
thin_image_edges::<foreground::Black>(&mut img, method, None)?;
If this produces poor results and/or takes a long time to run:
- the incorrect foreground color may have been chosen - try using the opposite color, or
- the image may not be binary and needs to be thresholded.
Edge detection
Run an edge detection filter on the image and threshold those results before thinning the lines. Note that the foreground color parameters must match on the edge detection function and the thinning function.
use skeletonize::edge_detection::sobel4;
use skeletonize::{foreground, thin_image_edges, MarkingMethod};
let method = MarkingMethod::Modified;
let threshold = Some(0.1);
let mut filtered = sobel4::<foreground::White>(&img, threshold)?;
thin_image_edges::<foreground::White>(&mut filtered, method, None)?;
Thresholding
Threshold the image before thinning, e.g., cleaning up a grayscale image.
use skeletonize::{foreground, thin_image_edges, threshold, MarkingMethod};
let method = MarkingMethod::Modified;
let threshold = 0.1;
skeletonize::threshold(&mut img, threshold)?;
thin_image_edges::<foreground::Black>(&mut img, method, None)?;
Reference
Zhang, T. Y. & Suen, C. Y. (1984). A fast parallel algorithm for thinning digital patterns. Commun. ACM 27, 3 (March 1984), 236–239. DOI:10.1145/357994.358023
Chen, Yung-Sheng & Hsu, Wen-Hsing. (1988). A modified fast parallel algorithm for thinning digital patterns. Pattern Recognition Letters. 7. 99-106. DOI:10.1016/0167-8655(88)90124-9
Modules
ForegroundColor
.Enums
Traits
Functions
img
using one of the methods
in MarkingMethod
. Returns the number of iterations
needed for thinning on successful completion.threshold
become black and above
become white. threshold
ranges from 0.0 to 1.0.