我希望使用CGAL来获得alpha形状。
@sloriot提供an extremely relevant script并在我的自定义后:
from sys import *
path.append("../../cgal_package")
from CGAL.Alpha_shapes_2 import *
from CGAL.Triangulations_2 import Delaunay_triangulation_2
from CGAL.Kernel import *
from random import *
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import constants
def Point_2_str(self):
return "Point_2"+str((self.x(), self.y()))
# now we turn it into a member function
Point_2.__str__ = Point_2_str
def show_alpha_values(AS):
print "Alpha spectrum"
for alpha_value in AS.alpha:
print alpha_value
def read_points(file):
result = []
dataFile = open(file, 'r')
for i, line in enumerate(dataFile):
coordinateList = [float(x) for x in line.strip().split()]
for j in range(0, len(coordinateList), 2):
result.append(Point_2(coordinateList[j], coordinateList[j+1]))
dataFile.close()
return result
def getAlphaShape():
L =[]
verbose = True
list_of_points = read_points(constants.PROJECT_PATH + '\\data\\clusters.txt')
a = Alpha_shape_2()
a.make_alpha_shape(list_of_points)
a.set_mode(Alpha_shape_2.Mode.REGULARIZED)
a.set_alpha(1000)
alpha_shape_edges = []
alpha_shape_vertices = []
for it in a.alpha_shape_edges:
alpha_shape_edges.append(a.segment(it))
for it in a.alpha_shape_vertices:
alpha_shape_vertices.append(it)
_showAlphaShape(list_of_points, alpha_shape_vertices)
print "alpha_shape_edges"
print len(alpha_shape_edges)
print "alpha_shape_vertices"
print len(alpha_shape_vertices)
print "Optimal alpha: "
print a.find_optimal_alpha(2).next()
def _showAlphaShape(points, vertices):
fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # left, bottom, width, height (range 0 to 1)
axes.axis('equal')
axes.set_xlabel(r'$x (m)$')
axes.set_ylabel(r'$y (m)$')
axes.set_title(r'$\alpha$-shape of clusters')
# draw cluster points
x = [pt.x() for pt in points]
y = [pt.y() for pt in points]
axes.scatter(x, y, s=1)
# draw alpha shape
for i in range(len(vertices)-1):
x1 = vertices[i].point()[0]
x2 = vertices[i+1].point()[0]
y1 = vertices[i].point()[1]
y2 = vertices[i+1].point()[1]
axes.plot([[x1, x2], [y1, y2]], color='b')
fontP = FontProperties()
fontP.set_size('7')
axes.legend(loc=3, prop=fontP)
fig.savefig(constants.PROJECT_PATH + '\\data\\1.svg')
运行后,我
蓝色部分显然是不的alpha形状。 哪里出错?