简体   繁体   中英

how to find principal components and orientation of a point cloud using point cloud library

I have a point cloud of a wooden block. I have found the centroid of that point cloud. Now I am trying to find the Principal components and orientation using point cloud library. Below is the code I have tried. Correct me if you havent understood something.

        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,centroid);

        // Compute the 3x3 covariance matrix
        pcl::computeCovarianceMatrix (*cloud_filtered, centroid, covariance_matrix);
        pcl::eigen33 (covariance_matrix, eigen_vectors, eigen_values);
        std::cout << "centroid-x:"<<centroid[0]<<"centroid-y:"<<centroid[1]<<"centroid-z:"<<centroid[2]<<std::endl;

If you need a rotation matrix representing an orientation, we can choose the axis in which the volume distribution of the object is highest (normalised first eigenvector - that is the eigenvector associated with the largest eigenvalue) as the first column of the matrix.

For the 2nd column of the matrix choose the 2nd eigenvector but you have to subtract from it its projection onto the 1st eigenvector so that it is orthogonal to the first. To calculate its projection you can use the dot product - if the eigenvectors are already normalised you can just use the dot product to calculate the length of the vector to subtract: so dot product the two vectors and multiply the 1st vector by the dot product, then subtract the resulting vector from the 1st eigenvector.

For the 3rd column there will only be one choice left - the cross product of the two calculated above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM