点云obb盒显示 python

我感觉注释写的挺全的,就不多写说明了
原本是用来做无人机拍摄点的,需要的话自己拿走改吧改吧,分享给刚入门的兄弟们

class Obb_show(object):
    def __init__(self, point, cls=None, clusters=None):
        """
        参数:
            point:[x,y,z]     shape[n,3]
            cls:分类码          shape[n]
            clusters:实例码    shape[n]
        """
        self.point = point
        
        self.cls = cls 
        if self.cls is None:
            self.cls = point[:,2]         
           
        self.clusters = clusters
     
        if self.clusters is None:
            self.clusters = np.ones((point.shape[0]))
        # 求一下最大边长,画图的时候外边界用
        xl = np.max(self.point[:,0]) - np.min(self.point[:,0])
        yl = np.max(self.point[:,1]) - np.min(self.point[:,1])
        zl = np.max(self.point[:,2]) - np.min(self.point[:,2])
        self.l = max(xl,yl,zl)/2
        self.median = np.array([xl,yl,zl])/2 + np.array([np.min(self.point[:,0]),np.min(self.point[:,1]),np.min(self.point[:,2])])
        
    def obb(self, point):
        point1 = point.transpose(1,0) 
        cov_m = np.cov(point1)  # 求协方差矩阵
        vector, matrix = np.linalg.eig(cov_m)    # 将协方差矩阵生成特征值和特征向量 取特征向量
        return vector, matrix

    def show(self, cls=None, vector_show=False, line=1, mode='box', camrea=False):
        """
        参数:
            cls:当cls不为None的时候,只显示当前分类码的obb盒
            vector:为Ture时显示特征向量
            line: 特征向量的长度 默认1mode: 两种模式 ‘box’,‘mend’
        returm:
            mend_list: list 根据obb盒补充的点           [m,n,3]
            camera_list: list 拍摄点 每个实例三个拍摄点   [m,3,3]
        """
        mp.figure('3D Scatter')
        ax3d = mp.gca(projection='3d') 
        # ax3d.set_aspect("equal")
        ax3d.set_xlabel('X', fontsize=14
首先,需要理解obb的概念和计算方法。obb是一种用于包围物体的子,它的面朝物体的表面,可以用来快速计算物体的碰撞和运动。obb的计算方法通常是通过求解物体的协方差矩阵,然后求解矩阵的特征值和特征向量,进而计算obb的位置和方向。 在Eigen库中,可以使用Matrix类来表示矩阵,并使用SelfAdjointEigenSolver类来求解特征值和特征向量。下面是一个示例代码,用于计算obb的位置、方向和大小。 ```c++ #include <Eigen/Dense> #include <Eigen/Eigenvalues> using namespace Eigen; void computeOBB(const MatrixXf& points, Vector3f& position, Matrix3f& orientation, Vector3f& halfExtents) { int numPoints = points.cols(); // Compute the center of mass Vector3f center = points.rowwise().mean(); // Compute the covariance matrix Matrix3f cov = Matrix3f::Zero(); for (int i = 0; i < numPoints; i++) { Vector3f p = points.col(i) - center; cov += p * p.transpose(); } cov /= numPoints; // Compute the eigenvectors and eigenvalues SelfAdjointEigenSolver<Matrix3f> solver(cov); Vector3f eigenvalues = solver.eigenvalues(); Matrix3f eigenvectors = solver.eigenvectors(); // Set the position to the center of mass position = center; // Set the orientation to the eigenvectors orientation = eigenvectors; // Set the half extents to the square roots of the eigenvalues halfExtents = eigenvalues.array().sqrt(); } ``` 在这个示例代码中,MatrixXf表示一个矩阵,其中每一列代表一个点的坐标。Vector3f和Matrix3f分别表示一个三维向量和一个三维矩阵。SelfAdjointEigenSolver类用于求解协方差矩阵的特征值和特征向量。 在函数的实现中,首先计算点云的中心点,然后计算协方差矩阵。接着,使用SelfAdjointEigenSolver类求解特征值和特征向量。最后,将中心点、特征向量和特征值的平方根设置为obb的位置、方向和大小。 需要注意的是,这个示例代码中没有考虑obb的旋转角度。如果需要计算obb的旋转角度,可以使用Quaternion类来表示四元数,并计算特征向量之间的旋转矩阵。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值