我使用下面的代码获得了簇点云的特征向量。
for (size_t j = 0; j < cluster_indices.size (); j++){
static int line=0;
Eigen::Vector4f centroid;
Eigen::Matrix3f covariance_matrix;
// Extract the eigenvalues and eigenvectors
Eigen::Vector3f eigen_values;
Eigen::Matrix3f eigen_vectors;
pcl::compute3DCentroid(*cloud_filtered,cluster_indices[j],centroid);
// Compute the 3x3 covariance matrix
pcl::computeCovarianceMatrix (*cloud_filtered, centroid, covariance_matrix);
pcl::eigen33 (covariance_matrix, eigen_vectors, eigen_values);
//~ _viewer->addLine<pcl::PointXYZRGB> (centroid, eigen_vectors, "line");
std::cout << "centroid:"<<centroid<<std::endl;
cloud_cluster->points.push_back(centroidToPoints(centroid));
for(int i=0; i<3;i++){
std::cout << "eigenvector "<<i<<":"<<eigen_vectors.col(i)<<std::endl;
cloud_cluster->points.push_back(matrixToPoints(eigen_vectors.col(i)));
_viewer->addLine<pcl::PointXYZRGB> (centroidToPoints(centroid), matrixToPoints(eigen_vectors.col(i)), "line"+line);
line++;
}
}
然后我将质心和特征向量分别转换为点。在下面的代码的帮助下。
pcl::PointXYZRGB centroidToPoints(Eigen::Vector4f& c){
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_eigen (new pcl::PointCloud<pcl::PointXYZRGB>);
cloud_eigen->width = 15;
cloud_eigen->height = 1;
cloud_eigen->points.resize (cloud_eigen->width * cloud_eigen->height);
cloud_eigen->points[0].x=c[0];
cloud_eigen->points[0].y=c[1];
cloud_eigen->points[0].z=c[2];
//~ pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZRGB> cloud_handler2 (cloud_eigen, "z");
//~ _viewer->addPointCloud<pcl::PointXYZRGB> (cloud_eigen, cloud_handler2, "second cloud");
//~ _viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "second cloud");
return cloud_eigen->points[0];
}
pcl::PointXYZRGB EigenToPoints(Eigen::DenseBase<Eigen::Matrix<float, 3, 3> >::ColXpr ev){
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_eigen (new pcl::PointCloud<pcl::PointXYZRGB>);
cloud_eigen->width = 15;
cloud_eigen->height = 1;
cloud_eigen->points.resize (cloud_eigen->width * cloud_eigen->height);
cloud_eigen->points[0].x=ev[0];
cloud_eigen->points[0].y=ev[1];
cloud_eigen->points[0].z=ev[2];
return cloud_eigen->points[0];
}
但是特征向量不是垂直的。如下所示(绿色和红色点是特征向量)。
谁能告诉我什么是错的?。