跳到正文
Hacker News· nickponline·· 6 天前AI 评分38

Shapefit:用神经网络把不规则形状掩码拟合为干净多边形

Shapefit: Turn irregular shape masks into clean polygons

AI 导读

Shapefit 是一个用神经网络将边缘粗糙的形状掩码拟合为干净多边形的开源工具,支持单形状和重叠矩形并集(如建筑轮廓、房间布局),可输出正交或平行约束的角点。

正文 · AI 翻译

用神经网络将干净的多边形拟合到不规则形状。

shapefit 接收一个边缘质量很差的掩码(凸起、锯齿状台阶、缺口、孔洞、圆角),并返回一个具有正确角点的多边形。它适用于单个形状以及重叠矩形的并集,例如建筑轮廓和房间布局。

pipeline

安装

git clone git@github.com:nickponline/shapefit.git && cd shapefit
pip install -e ".[dev]"      # numpy, torch, opencv-python-headless, shapely, scipy (+ pytest, matplotlib)
pytest                       # 29 tests, approximately 15 s

训练好的检查点包含在 shapefit/weights/shapefit.pt 中(15 MB)。

用法

from shapefit import ShapeFitter, fit_polygon

fitter = ShapeFitter("shapefit/weights/shapefit.pt", device="cpu")   # or "cuda" / "mps"

poly = fitter.fit(mask)                             # mask of any size -> (N, 2) array of (x, y) corners
poly = fitter.fit(noisy_points)                     # or an (M, 2) point list, or a shapely Polygon
poly = fitter.fit(mask, regularize="orthogonal")    # all corners 90° (buildings, rooms)
poly = fitter.fit(mask, regularize="parallel")      # almost-parallel edges become parallel

poly = fit_polygon(mask)                            # short form, uses the shipped checkpoint

角点位于输入像素坐标中(x = 列,y = 行),在屏幕上按逆时针顺序排列。完整示例请运行 python examples/quickstart.py。

结果

测试形状来自训练未使用的种子。绿色:shapefit。橙色虚线:cv2.approxPolyDP(Douglas–Peucker),ε = 周长的 2%。

results

unions

IoU ↑ 正确角点数量 ↑ 顶点 F1 ↑
shapefit,500 个各种类型的形状 0.960 78.8 % 0.845
Douglas–Peucker,每列的最佳 ε(1%、2% 或 3%) 0.910 50.6 % 0.617
shapefit,200 个矩形并集 0.947 67.0 % 0.818
Douglas–Peucker,ε = 1%(最佳),并集 0.898 28.0 % 0.588

更多细节

图解说明(也在 docs/explainer.html 中)涵盖了训练数据、网络、矢量化器、完整结果表、56 个新形状的交互式图库、训练以及局限性。

许可证

MIT

来源:Hacker News · github.com