导入所需库
#include
#include
#include
#include using namespace std;
using namespace pcl;
using namespace Eigen;
核心代码
void pcaNormal(const PointCloud<PointXYZ>::Ptr& cloud, const int& k_neighbors, PointCloud<Normal>::Ptr& normals)
{KdTreeFLANN<PointXYZ>::Ptr kdtree(new KdTreeFLANN<PointXYZ>);kdtree->setInputCloud(cloud);if (normals == NULL)normals = PointCloud<Normal>::Ptr(new PointCloud<Normal>);if (!normals->empty())normals->resize(0);for (const auto& point : *cloud){PointXYZ searchPoint = point; vector<int> KNNIndices(k_neighbors);vector<float> KNNSquareDistance(k_neighbors);if (kdtree->nearestKSearch(searchPoint, k_neighbors, KNNIndices, KNNSquareDistance) > 0){PointCloud<PointXYZ>::Ptr neighborPoints(new PointCloud<PointXYZ>);pcl::copyPointCloud(*cloud, KNNIndices, *neighborPoints);Eigen::Vector4f pcaCentroid;Eigen::Matrix3f covariance;Eigen::Matrix3f eigenVectorsPCA;Eigen::Vector3f eigenValuesPCA;compute3DCentroid(*neighborPoints, pcaCentroid);computeCovarianceMatrixNormalized(*neighborPoints, pcaCentroid, covariance);Eigen::SelfAdjointEigenSolver<Eigen::Matrix3f> eigen_solver(covariance, Eigen::ComputeEigenvectors);eigenVectorsPCA = eigen_solver.eigenvectors();eigenValuesPCA = eigen_solver.eigenvalues();Normal normal(eigenVectorsPCA(0), eigenVectorsPCA(1), eigenVectorsPCA(2));normals->push_back(normal);}elsenormals->push_back(Normal());}}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!