计算点云的法向量

1、1.先mark一个文件操作:遍历(或者迭代遍历)指定目录,boost::filesystem可真好用

for (const auto& it : boost::filesystem::directory_iterator("/your/path")) {
    if (it.path().extension() == ".pcd") {
       std::cout << it.path() << ", " << it.path().filename() << ", " << it.path().stem() << ", " << it.path().extension() << std::endl;
    }
}

for (const auto& it : boost::filesystem::recursive_directory_iterator("/your/path")) {
    if (it.path().extension() == ".pcd") {
       std::cout << it.path() << ", " << it.path().filename() << ", " << it.path().stem() << ", " << it.path().extension() << std::endl;
    }
}// it.path()                  it.path().filename()    it.path().stem()    it.path().extension() // "/path/pairs_12_7.pcd",    "pairs_12_7.pcd",       "pairs_12_7",       ".pcd"

2.用pcl::NormalEstimation简直就是坑爹,计算出的点云法向量有40~50%都是有问题的

pcl::search::KdTree<PointS>::Ptr kdtree_submap(new pcl::search::KdTree<PointS>);
    kdtree_submap->setInputCloud(cloud_submap);// Make sure the tree searches the surface
    pcl::NormalEstimation<PointS, PointS>::Ptr ne(new pcl::NormalEstimation<PointS, PointS>);
    ne->setInputCloud(cloud_ds_angle_);
    ne->setSearchSurface(cloud_submap);
    ne->setSearchMethod(kdtree_submap);
    ne->setRadiusSearch(search_r_ne);
    ne->compute(*cloud_ds_angle_);

3、用pca和kdtree自己计算,效果赞赞赞,而且效率与上面的一样

void my_normal_estimation(const KdTreePtr &kdtree, PCloudTPtr &cloud, double search_r) {
        for (auto &pt : cloud->points) {
            std::vector<int> k_indices;
            std::vector<float> k_sqr_distances;
            if (kdtree->radiusSearch(pt, search_r, k_indices, k_sqr_distances) < 3) {
                continue;
            }
            PCloudTPtr cloud_search(new PCloudT);
            pcl::copyPointCloud(*(kdtree->getInputCloud()), k_indices, *cloud_search);
            pcl::PCA<PointT> pca;
            pca.setInputCloud(cloud_search);
            Eigen::Matrix3f eigen_vector = pca.getEigenVectors();
            Eigen::Vector3f vect_2 = eigen_vector.col(2);// please fit to your own coordinate
            pt.normal_x = vect_2[0];
            pt.normal_y = vect_2[1];
            pt.normal_z = vect_2[2];
        }
    }
计算点云向量点云处理中重要的一步,可以用于分析点云的曲率、表面变化以及三维重建等应用。以下是一个简单的手写计算点云向量的程序示例: 1. 首先,我们需要导入必要的库,如numpy和sklearn等: import numpy as np from sklearn.neighbors import NearestNeighbors 2. 然后,我们定义一个函数来计算点云向量: def compute_normals(point_cloud, k=20): # 计算最近邻点 nbrs = NearestNeighbors(n_neighbors=k).fit(point_cloud) distances, indices = nbrs.kneighbors(point_cloud) # 初始化向量 normals = np.zeros_like(point_cloud) for i in range(len(indices)): # 计算协方差矩阵 covariance_matrix = np.cov(point_cloud[indices[i]].T) # 计算特征值和特征向量 eigenvalues, eigenvectors = np.linalg.eig(covariance_matrix) # 找到最小特征值的索引 min_eigenvalue_index = np.argmin(eigenvalues) # 提取对应的特征向量作为向量 normals[i] = eigenvectors[:, min_eigenvalue_index] return normals 3. 然后,我们读取点云数据,调用计算向量的函数,并保存结果: point_cloud = np.loadtxt('point_cloud.txt') # 读取点云数据(如果点云数据文件存在的话) normals = compute_normals(point_cloud) # 计算向量 np.savetxt('normals.txt', normals) # 保存向量数据 以上是一个简单的手写计算点云向量的程序示例。注:此示例基于最近邻点计算向量,只是其中的一种方,实际应用中可能会根据具体情况选择其他算
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值