3个版本
0.6.3 | 2019年2月13日 |
---|---|
0.6.2 | 2019年2月5日 |
0.6.0 | 2019年2月4日 |
#658 在 图像
每月下载量 57次
34KB
513 行
SIGNED-DISTANCE-FIELD-RS
Rust的快速签距离场
此crates根据二进制图像近似签距离场。该算法灵感来源于George J. Grevara在2004年发表的论文《The dead reckoning signed distance transform》。不要忘记以发布模式编译!
特性
在计算签距离场的过程中,该算法构建了一个图像,每个像素包含指向最近边缘的向量。该向量距离场在计算普通距离场后可用,可用于进一步处理。此外,该库还提供了从距离场到图像的简单转换,具有整数精度。
入门
更新您的 Cargo.toml
signed-distance-field = { version = "0.6.3", features = [ "piston_image" ] }
use signed_distance_field::prelude::*;
fn main(){
// load data using piston image
let mut gray_image = image::open("sketch.jpg").unwrap().to_luma();
// interpret grayscale image as binary image with any pixel brighter than 80 being 'on'
let binary_image = binary_piston_image::of_gray_image_with_threshold(&gray_image, 80);
// convert the binary image to a distance field
let distance_field = compute_f32_distance_field(&binary_image);
// clip all distances greater than 10px and compress them into a byte array
// so that a distance of -10px is 0 and a distance of 10px is 255
// (edges, having a distance of 0px, will be 128)
let distance_image = distance_field
.normalize_clamped_distances(-10.0, 10.0)
// convert f32 distance field to u8 piston image
.unwrap().to_gray_u8_image();
// save the piston image as png
distance_image.save("sketch_distance.png").unwrap();
}
Piston Images
此库可以配置为提供一些简单的piston图像转换。功能标志 piston_image
解锁这些功能。image crate不是计算签距离场所必需的,包括piston image实际上是可选的。
缺点(目前)
- 仅单核
- 可能不如直观方法准确
- 未显式使用GPU或SIMD加速
接下来是什么?
- 考虑针对SIMD和多线程进行优化
- 考虑添加其他算法,可能包含GPU利用
依赖关系
~0.2–3MB
~19K SLoC