简体   繁体   English

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

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

I have an hyperplane implementation that looks like this:我有一个看起来像这样的超平面实现:


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,
        )

I hope there is no error... ^_^'我希望没有错误... ^_^'

What I need now is to find a way to fit a hyperplane to a set of points where there are more than n points in the points cloud.我现在需要的是找到一种方法,将超平面拟合到点云中有超过 n 个点的一组点。

It may be a Ransac algorithm or something like this.它可能是 Ransac 算法或类似的东西。

Is there a proven method that can be implemented for that?是否有可以为此实施的行之有效的方法?

Thanks谢谢

You can use a singular value decomposition (SVD) of the point matrix.您可以使用点矩阵的奇异值分解 (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)

The normal to the hyperplane is the axis corresponding to the smallest singular value.超平面的法线是对应于最小奇异值的轴。 As long as np.linalg.svd() sorts them from largest to smallest, the axis is the last unitary vector of vh .只要np.linalg.svd()将它们从最大到最小排序,轴就是vh的最后一个单一向量。

normal = vh[-1]

The hyperplane is then defined by the centroid and the normal .然后超平面由centroidnormal定义。

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

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