簡體   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