Search code examples
c++cluster-analysispcapoint-cloud-librarypoint-clouds

Principal component analysis on PCL


I am using PCL with C++ and want to perform PCA on an already clustered pointcloud (that is on every individual cluster). The idea is to eliminate all clusters that are too large/small by measuring their size along the eigenvectors. So intended algorithm is :Get eigenvectors of every cluster, project the cluster points on the corresponding eigenvectors in order to measure maximum distances of points along these dimensions, and from there eliminate all clusters that have "bad" dimensions/ratio of dimensions. I am having difficulties with the implementation. The more help you could spare, the merrier. This is how my data is organized (just to be clear, these are snippets, cluster_indices have already been extracted properly which is checked), and what I started:

std::vector<pcl::PointIndices> cluster_indices;
pcl::PCA<pcl::PointXYZ> cpca = new pcl::PCA<pcl::PointXYZ>;
cpca.setInputCloud(input);
Eigen::Vector3f pca_vector(3,3);
// and now iterating with a for loop over the clusters, but having issues using setIndices already
for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it, n++)
{
    cpca.setIndices(it->indices); //not sure what to put in brackets, everything I thought of returns an error!
    pca_vector = cpca.getEigenVectors();
}

Solution

  • It seems that there is a general issue with this. So I found this solution: Instead of iterating with pointers, use a regular iterator, and then use these formulation. PointIndicesPtr pi_ptr(new PointIndices); pi_ptr->indices = cluster_indices[i].indices; //now can use pi_ptr as input