Here is the Python code that does what you need. I changed the calculation of the alpha shape (concave body) here so that it does not insert inner edges ( only_outer parameter). I also made it self-sufficient so that it does not depend on an external library.
from scipy.spatial import Delaunay import numpy as np def alpha_shape(points, alpha, only_outer=True): """ Compute the alpha shape (concave hull) of a set of points. :param points: np.array of shape (n,2) points. :param alpha: alpha value. :param only_outer: boolean value to specify if we keep only the outer border or also inner edges. :return: set of (i,j) pairs representing edges of the alpha-shape. (i,j) are the indices in the points array. """ assert points.shape[0] > 3, "Need at least four points" def add_edge(edges, i, j): """ Add a line between the i-th and j-th points, if not in the list already """ if (i, j) in edges or (j, i) in edges:
If you run it with the following test code, you will get the following digit :
from matplotlib.pyplot import * # Constructing the input point data np.random.seed(0) x = 3.0 * np.random.rand(2000) y = 2.0 * np.random.rand(2000) - 1.0 inside = ((x ** 2 + y ** 2 > 1.0) & ((x - 3) ** 2 + y ** 2 > 1.0) & ((x - 1.5) ** 2 + y ** 2 > 0.09)) points = np.vstack([x[inside], y[inside]]).T # Computing the alpha shape edges = alpha_shape(points, alpha=0.25, only_outer=True) # Plotting the output figure() axis('equal') plot(points[:, 0], points[:, 1], '.') for i, j in edges: plot(points[[i, j], 0], points[[i, j], 1]) show()
source share