繁体   English   中英

从 Python 中的一组点构建超平面

[英]Building an hyperplane from a set of points in Python

我有一个看起来像这样的超平面实现:


class Hyperplane:
    """This is a generalization of the plane in n dimensions
    
    @param unit_normal_vector: the vector that is normal to the plane.
    @param distance_to_origin: the minimal distance in meters of the plane from the origin.
    """

    def __init__(
        self,
        unit_normal_vector: np.ndarray,
        distance_to_origin: float,
    ):
        self.unit_normal_vector: np.ndarray = unit_normal_vector
        self.distance_to_origin: float = distance_to_origin
    
    @classmethod
    def from_n_points(
        cls,
        points: Union[np.ndarray,List[np.ndarray],List[List[float]]],
    ) -> 'Hyperplane':
        """Build an hyperplane from a set of points
        We need exactly n points to build the hyperplane where n is the dimension of the space.
        """
        X = np.array(points)
        k = np.ones(X.shape[0])
        a=np.dot(np.linalg.inv(X), k)
        
        unit_normal_vector: np.ndarray = a / np.linalg.norm(a)
        distance_to_origin: float = -1 / np.linalg.norm(a)
        
        return cls(
            unit_normal_vector=unit_normal_vector,
            distance_to_origin=distance_to_origin,
        )

我希望没有错误... ^_^'

我现在需要的是找到一种方法,将超平面拟合到点云中有超过 n 个点的一组点。

它可能是 Ransac 算法或类似的东西。

是否有可以为此实施的行之有效的方法?

谢谢

您可以使用点矩阵的奇异值分解 (SVD)。

points = ...   # An n-dim point per row
centroid = np.mean(points, axis=0)
u, s, vh = np.linalg.svd(points - centroid, full_matrices=False)

超平面的法线是对应于最小奇异值的轴。 只要np.linalg.svd()将它们从最大到最小排序,轴就是vh的最后一个单一向量。

normal = vh[-1]

然后超平面由centroidnormal定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM