1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! 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](#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`](crate::ForegroundColor) parameter on
//! [`edge_detection::sobel`][sobel], [`edge_detection::sobel4`][sobel4], and
//! [`thin_image_edges`](crate::thin_image_edges) specifies what foreground and
//! background colors the resulting
//! [`thin_image_edges`](crate::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.
//!
//! [sobel]: crate::edge_detection::sobel
//! [sobel4]: crate::edge_detection::sobel4
//!
//! An example program can be viewed at `/examples/skeletonize.rs`.
//!
//! #### No preprocessing
//!
//! The image is already binarized so the edges can be thinned immediately.
//!
//! ```
//! # fn main() -> Result<(), skeletonize::error::SkeletonizeError> {
//! use skeletonize::{foreground, thin_image_edges, MarkingMethod};
//!
//! # let image_buffer = image::ImageBuffer::from_pixel(1, 1, image::Rgb([255, 255, 255]));
//! # let mut img = image::DynamicImage::ImageRgb8(image_buffer).grayscale();
//! let method = MarkingMethod::Modified;
//!
//! thin_image_edges::<foreground::Black>(&mut img, method, None)?;
//! # Ok(())
//! # }
//! ```
//!
//! 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.
//!
//! ```
//! # fn main() -> Result<(), skeletonize::error::SkeletonizeError> {
//! use skeletonize::edge_detection::sobel4;
//! use skeletonize::{foreground, thin_image_edges, MarkingMethod};
//!
//! # let image_buffer = image::ImageBuffer::from_pixel(2, 2, image::Rgb([255, 255, 255]));
//! # let img = image::DynamicImage::ImageRgb8(image_buffer).grayscale();
//! 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)?;
//! # Ok(())
//! # }
//! ```
//!
//! #### Thresholding
//!
//! Threshold the image before thinning, e.g., cleaning up a grayscale image.
//!
//! ```
//! # fn main() -> Result<(), skeletonize::error::SkeletonizeError> {
//! use skeletonize::{foreground, thin_image_edges, threshold, MarkingMethod};
//!
//! # let image_buffer = image::ImageBuffer::from_pixel(2, 2, image::Rgb([255, 255, 255]));
//! # let mut img = image::DynamicImage::ImageRgb8(image_buffer).grayscale();
//! let method = MarkingMethod::Modified;
//! let threshold = 0.1;
//!
//! skeletonize::threshold(&mut img, threshold)?;
//! thin_image_edges::<foreground::Black>(&mut img, method, None)?;
//! # Ok(())
//! # }
//! ```
//!
//! ## 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](https://doi.org/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](https://doi.org/10.1016/0167-8655(88)90124-9)
use ;
pub use thin_image_edges;
/// Represents the color of the foreground or features in a binary image. For
/// example, white text on a black background has a white foreground color and
/// black background color.
/// Implementations of [`ForegroundColor`](crate::ForegroundColor).
/// Classification of pixels in an image used for edge thinning.
/// The algorithm that determines which pixels are removed during the edge
/// thinning process.
///
/// ### Reference
///
/// <span id="standard"></span>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](https://doi.org/10.1145/357994.358023)
///
/// <span id="modified"></span>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](https://doi.org/10.1016/0167-8655(88)90124-9)
/// Create a binary image where values below `threshold` become black and above
/// become white. `threshold` ranges from 0.0 to 1.0.