如何找到2D点云的Alpha形状(凹多边形)?

2024-04-27 06:31:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一个实现,计算二维阿尔法形状。我在运行ubuntu。我希望此任务使用命令行实用程序,但也可以使用python库。

在Google中,我发现了许多计算alpha形状的实现。但没有一个输出我想要的。作为输入,我有一个二维点列表(例如,文本文件中每行一对浮点)。作为输出,我需要另一个具有相同比例的二维点列表。

我试过安装最新的python绑定cgal,但这些绑定已经有一段时间不受支持了,并且不再在Ubuntu 11.04上编译(我也试过Ubuntu 10.04,但没有成功)。Clustr,由aron Straup Cope在flickr开发的一个项目也不会在Ubuntu 11.04上编译(可能是因为它也绑定到了旧的CGAL库)。

我还试过贝尔实验室的肯·克拉克森写的this implementation。它输出的几乎是我想要的,输出似乎在另一个尺度上,它将浮点转换为int。

我还尝试了dionysus的python绑定。这些都已编译,但当我用点列表给函数fill_alpha2D_complex(points, f)提供数据时,输出结果并不是我所期望的。它不是一个二维点的列表,而是一个“持久性图”,我不知道这是什么意思。

有人知道解决这个问题的简单方法吗?

更新:我想打印出与alpha形状相关的点,在该形状即将不再连接。我认为这意味着“给我与最小alpha值相关联的点,这样形状就连接起来了。”

更新我现在了解了如何从Ken Clarkson's implementationdionysus implementation中获取所需内容(或多或少是所需内容)。克拉克森的实现是正确的,它只是输出点的索引,而不是点本身(与狄俄尼索斯的故事相同),我需要一些可选的标志正确。我写的包装纸在下面。这个解决方案是理想的,因为它产生了一个既连接又不包含孔的alpha形状。Alpha自动设置。另一方面,狄俄尼索斯不会自动发现阿尔法的这个值。另外,Clarkson的实现可以设置为输出形状的ps图像(带有-afps标志)。要使用非古代版本的GCC编译Clarkson的代码,您需要遵循here概述的步骤。以下代码可以用作库或独立包装:

#!/usr/bin/python -O

import sys, os
import subprocess
import tempfile

hull_path = "./hull.exe"

def get_alpha_shape(points):
    # Write points to tempfile
    tmpfile = tempfile.NamedTemporaryFile(delete=False)
    for point in points:
        tmpfile.write("%0.7f %0.7f\n" % point)
    tmpfile.close()

    # Run hull
    command = "%s -A -m1000000 -oN < %s" % (hull_path, tmpfile.name)
    print >> sys.stderr, "Running command: %s" % command
    retcode = subprocess.call(command, shell=True)
    if retcode != 0:
        print >> sys.stderr, "Warning: bad retcode returned by hull.  Retcode value:" % retcode
    os.remove(tmpfile.name)

    # Parse results
    results_file = open("hout-alf")
    results_file.next() # skip header
    results_indices = [[int(i) for i in line.rstrip().split()] for line in results_file]
#    print "results length = %d" % len(results_indices)
    results_file.close()
    os.remove(results_file.name)

    return [(points[i], points[j]) for i,j in results_indices]

if __name__ == "__main__":
    points = [tuple([float(i) for i in line.rstrip().split()]) for line in sys.stdin]
    for point_i, point_j in get_alpha_shape(points):
        sys.stdout.write("%0.7f,%0.7f\t%0.7f,%0.7f\n" % (point_i[0], point_i[1], point_j[0], point_j[1]))
    sys.exit(0)

Tags: nameinalpha列表forsysresultscommand
1条回答
网友
1楼 · 发布于 2024-04-27 06:31:48

我在狄俄尼索斯的文献中发现了this可能给你一个阿尔法形状:

complex = Filtration()
fill_alpha2D_complex(points, complex)
alphashape = [s for s in complex if s.data[0] <= .5]

那么我相信你需要做些什么:

for simplex in alphashape:
    print [v for v in simplex.vertices]

相关问题 更多 >