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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
use crate::{BinaryImage, MonoImage, MonoImageItem, SampleStat, SampleStatBuilder, Shape};
/// The skeleton of a binary image (aka medial axis)
pub struct Skeleton {
pub image: BinaryImage,
pub stat: SampleStat,
}
impl Shape {
pub fn to_skeleton(&self) -> Skeleton {
self.image.to_skeleton()
}
}
impl BinaryImage {
/// An extremely fast implementation of skeletonization
#[allow(clippy::many_single_char_names)]
pub fn to_skeleton(&self) -> Skeleton {
if self.width > MonoImageItem::MAX as usize ||
self.height > MonoImageItem::MAX as usize {
panic!(
"image too large: width={}, height={}",
self.width, self.height
);
}
let boundary = Shape::image_boundary(self);
let mut centerx = BinaryImage::new_w_h(self.width, self.height);
let mut centery = BinaryImage::new_w_h(self.width, self.height);
let mut centerxy = BinaryImage::new_w_h(self.width, self.height);
let mut centeryx = BinaryImage::new_w_h(self.width, self.height);
let mut spanx = MonoImage::new_w_h(self.width, self.height);
let mut spany = MonoImage::new_w_h(self.width, self.height);
let mut spanxy = MonoImage::new_w_h(self.width, self.height);
let mut spanyx = MonoImage::new_w_h(self.width, self.height);
let mut result = BinaryImage::new_w_h(self.width, self.height);
// span width for each horizontal scan line
for y in 0..self.height {
let mut u = false;
let mut span = 0;
for x in 0..self.width + 1 {
let mut new_span = false;
if x < self.width {
let v = self.get_pixel(x, y);
if !u && v {
// 0 -> 1
span = 0;
} else if u && !v {
// 1 -> 0
new_span = true;
}
if v {
span += 1;
}
u = v;
} else if u {
new_span = true;
}
if new_span {
for b in x - span..x {
if b == x - span / 2 - 1 {
centerx.set_pixel(b, y, true);
}
spanx.set_pixel(b, y, span as MonoImageItem);
}
}
}
}
// span height for each vertical scan line
for x in 0..self.width {
let mut u = false;
let mut span = 0;
for y in 0..self.height + 1 {
let mut new_span = false;
if y < self.height {
let v = self.get_pixel(x, y);
if !u && v {
// 0 -> 1
span = 0;
} else if u && !v {
// 1 -> 0
new_span = true;
}
if v {
span += 1;
}
u = v;
} else if u {
new_span = true;
}
if new_span {
for b in y - span..y {
if b == y - span / 2 - 1 {
centery.set_pixel(x, b, true);
}
spany.set_pixel(x, b, span as MonoImageItem);
}
}
}
}
// diagonal scanline go towards north east
let mut x: i32 = 0;
let mut y: i32 = 0;
let mut xx = 0;
let mut yy = 0;
while x < self.width as i32 && y < self.height as i32 {
let mut u = false;
let mut span = 0;
while x <= self.width as i32 && y >= -1 {
let mut new_span = false;
if x < self.width as i32 && y >= 0 {
let v = self.get_pixel(x as usize, y as usize);
if !u && v {
// 0 -> 1
span = 0;
} else if u && !v {
// 1 -> 0
new_span = true;
}
if v {
span += 1;
}
u = v;
} else if u {
new_span = true;
}
if new_span {
let mut bx = x - span;
let mut by = y + span;
while bx < x && by > y {
if bx == x - span / 2 - 1 {
centerxy.set_pixel(bx as usize, by as usize, true);
}
spanxy.set_pixel(bx as usize, by as usize, span as MonoImageItem);
bx += 1;
by -= 1;
}
}
x += 1;
y -= 1;
}
if yy < self.height - 1 {
yy += 1;
} else {
xx += 1;
}
x = xx as i32;
y = yy as i32;
}
// diagonal scanline go towards south east
let mut x: i32 = 0;
let mut y: i32 = 0;
let mut xx = self.width - 1;
let mut yy = 0;
while x < self.width as i32 && y < self.height as i32 {
let mut u = false;
let mut span = 0;
while x <= self.width as i32 && y <= self.height as i32 {
let mut new_span = false;
if x < self.width as i32 && y < self.height as i32 {
let v = self.get_pixel(x as usize, y as usize);
if !u && v {
// 0 -> 1
span = 0;
} else if u && !v {
// 1 -> 0
new_span = true;
}
if v {
span += 1;
}
u = v;
} else if u {
new_span = true;
}
if new_span {
let mut bx = x - span;
let mut by = y - span;
while bx < x && by < y {
if bx == x - span / 2 - 1 {
centeryx.set_pixel(bx as usize, by as usize, true);
}
spanyx.set_pixel(bx as usize, by as usize, span as MonoImageItem);
bx += 1;
by += 1;
}
}
x += 1;
y += 1;
}
if xx > 0 {
xx -= 1;
} else {
yy += 1;
}
x = xx as i32;
y = yy as i32;
}
// filter speckle
for y in 0..self.height {
for x in 0..self.width {
let u = centerx.get_pixel(x, y);
let v = centery.get_pixel(x, y);
let w = centerxy.get_pixel(x, y);
let z = centeryx.get_pixel(x, y);
if u || v || w || z {
let mut on = false;
let sx = spanx.get_pixel(x, y);
let sy = spany.get_pixel(x, y);
let sxy = spanxy.get_pixel(x, y);
let syx = spanyx.get_pixel(x, y);
if u && sx <= sy && sx <= sxy && sx <= syx {
on = true;
}
if v && sy < sx && sy <= sxy && sy <= syx {
on = true;
}
if w && sxy < sx && sxy < sy && sxy <= syx {
on = true;
}
if z && syx < sx && syx < sy && syx < sxy {
on = true;
}
if on {
let min = std::cmp::min(std::cmp::min(sx, sy), std::cmp::min(sxy, syx));
if boundary.get_pixel(x, y) && min > 1 {
on = false;
}
}
if on {
result.set_pixel(x, y, true);
}
}
}
}
// final aggregation
let mut stat = SampleStatBuilder::new();
for y in 0..self.height as i32 {
for x in 0..self.width as i32 {
if result.get_pixel(x as usize, y as usize) {
if result.get_pixel_safe(x-1, y) ||
result.get_pixel_safe(x+1, y) ||
result.get_pixel_safe(x, y-1) ||
result.get_pixel_safe(x, y+1) ||
result.get_pixel_safe(x-1, y-1) ||
result.get_pixel_safe(x-1, y+1) ||
result.get_pixel_safe(x+1, y-1) ||
result.get_pixel_safe(x+1, y+1) {
let mut dd = 0;
let x = x as usize;
let y = y as usize;
let sx = spanx.get_pixel(x, y);
let sy = spany.get_pixel(x, y);
let sxy = spanxy.get_pixel(x, y);
let syx = spanyx.get_pixel(x, y);
if centerx.get_pixel(x, y) && sx <= sy && sx <= sxy && sx <= syx {
dd = sx;
}
if centery.get_pixel(x, y) && sy < sx && sy <= sxy && sy <= syx {
dd = sy;
}
if centerxy.get_pixel(x, y) && sxy < sx && sxy < sy && sxy <= syx {
dd = sxy;
}
if centeryx.get_pixel(x, y) && syx < sx && syx < sy && syx < sxy {
dd = syx;
}
if dd > 0 {
stat.add(dd as i32);
}
} else {
result.set_pixel(x as usize, y as usize, false);
}
}
}
}
Skeleton {
image: result,
stat: stat.build(),
}
}
}