An implementation of 2D EDT (Euclidian distance transform) with Saito's algorithm in pure Rust
There are also other crates that implements EDT, but I would like to reinvent a wheel that has these advantages:
Performance was not the priority, but I would like to explore more optimizations.
EDT is the basis of many algorithms, but it is hard to find in a general purpose image processing library, probably because the algorithm is not trivial to implement efficiently. This crate provides an implementation of EDT in fairly efficient algorithm presented in the literature.
The algorithm used in this crate (Saito's algorithm) is O(n^3), where n is the number of pixels along one direction. Naive computation of EDT would be O(n^4), so it is certainly better than that, but there is also fast-marching based algorithm that is O(n^2).
Add dependency
toml
[dependencies]
edt = "0.1.1"
Prepare a binary image as a flattened vec. This library assumes that the input is a flat vec for 2d image. You can specify the dimensions with a tuple.
rust
let mut vec: Vec<bool> = vec![/*...*/];
let dims = (32, 32);
If you want to read input from an image, you can use image crate. Make sure to put it to your project's dependencies in that case.
rust
use image::GenericImageView;
let img = image::open("Rust_logo.png").unwrap();
let dims = img.dimensions();
let img2 = img.into_luma8();
let flat_samples = img2.as_flat_samples();
let vec = flat_samples.image_slice().unwrap();
Call edt with given shape
```rust use edt::edt;
let edt_image = edt(&vec, (dims.0 as usize, dims.1 as usize), true); ```
Save to a file if you want. The code below normalizes the value with maximum value to 8 bytes grayscale image.
```rust use image::{ImageBuffer, Luma};
let maxvalue = edtimage.iter().map(|p| p).reduce(f64::max).unwrap(); let edt_img = edt_image .iter() .map(|p| (p / max_value * 255.) as u8) .collect();
let edtimg: ImageBuffer
// Write the contents of this image to the Writer in PNG format. edt_img.save("edt.png").unwrap(); ```
This paper is a great summary of this field of research.
doi=10.1.1.66.2644
https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.66.2644&rep=rep1&type=pdf
Section 7.7
https://www.cs.jhu.edu/~misha/ReadingSeminar/Papers/Saito94.pdf