我有 .xyz 文件中的一组点,我需要对已识别平面的顶点进行参数化。我尝试使用凸包函数,但只返回周长。
我的代码是这样开始的:
#importing data
data = np.loadtxt("Asm.xyz", dtype=float, comments="L")
x = data[:, 0]
y = data[:, 1]
z = data[:, 2]
#user selects plane of interest
plane_max = float(input("Enter the largest z-value for the plane range: "))
plane_min = float(input("Enter the smallest z-value for the plane range: "))
plane = (z >= plane_min) & (z <= plane_max)
#using linear regression to parametrize plane
X = np.column_stack((x[plane], y[plane]))
Y = z[plane]
model = LinearRegression()
model.fit(X, Y)
a, b = model.coef_
c = model.intercept_
#grab points coinciding with plane
predicted_z = a * x + b * y + c
threshold = 0.001
on_plane_indices = np.where(np.abs(predicted_z - z) < threshold)[0]
points_on_plane = data[on_plane_indices]
#attempting to use convex hull
hull = ConvexHull(points_on_plane[:,:2])
corner_points = points_on_plane[hull.vertices]
绘制“顶点”和我的数据显示凸包正在返回平面周围的周长,而不仅仅是像我想要的那样的角:
凸包和我绘制的点:
我只需要矩形的角/顶点。我的同事建议我使用
sklearn.decomposition.PCA
函数,但我不确定这是否是正确的方法。
凸包肯定不会给你一个矩形,但可以从那里开始并按照本答案中所述进行一些几何操作,以获得最小面积的矩形,它有一个Python实现发布在这里。
幸运的是,甚至还有一个可立即安装的 python 模块MinimumBoundingBox
,源代码可在https://bitbucket.org/william_rusnack/minimumboundingbox/ 获得。