惯性矩和偏心距描述器
這次我們將學(xué)會(huì)怎么使用pcl::MomentOfInertiaEstimation?這個(gè)類來獲取以慣性矩和偏心距為基礎(chǔ)的描述器。這個(gè)類也能提取坐標(biāo)對(duì)稱和定向包圍的方形盒子。但是記住導(dǎo)出的OBB不是最小可能性的盒子。
下面介紹了該種方法的特征提取方式。第一次先算出點(diǎn)云矩陣的協(xié)方差,計(jì)算它的特征值和特征向量。然后把特征向量進(jìn)行歸一化處理,并把它組成右手坐標(biāo)系。每一步都會(huì)迭代一次。每一次迭代特征向量都會(huì)旋轉(zhuǎn)。選轉(zhuǎn)的順序總是一樣的,總是被別的特征向量執(zhí)行。這提供了選擇不變性。我們把這個(gè)旋轉(zhuǎn)的主向量作為當(dāng)前的坐標(biāo)系。
對(duì)于每一個(gè)慣性矩都會(huì)被計(jì)算。此外,當(dāng)前的坐標(biāo)系還被用于偏心距的計(jì)算。出于這個(gè)原因,當(dāng)前的向量被當(dāng)成一個(gè)平面的法線向量同時(shí)點(diǎn)云被投射到這個(gè)向量上。對(duì)于這個(gè)投射,偏心距會(huì)被計(jì)算。
完成上述實(shí)現(xiàn)的類還提供了方法來獲得AABB和OBB。旋轉(zhuǎn)的方形盒子被當(dāng)做AABB和特征向量一起計(jì)算。
下面是一段代碼
#include <pcl/features/moment_of_inertia_estimation.h> #include <vector> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> #include <pcl/visualization/cloud_viewer.h> #include <boost/thread/thread.hpp>int main (int argc, char** argv) {if (argc != 2)return (0); boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));viewer->setBackgroundColor (0, 0, 0);viewer->addCoordinateSystem (1.0);viewer->initCameraParameters ();viewer->addPointCloud<pcl::PointXYZ> (cloud, "sample cloud");viewer->addCube (min_point_AABB.x, max_point_AABB.x, min_point_AABB.y, max_point_AABB.y, min_point_AABB.z, max_point_AABB.z, 1.0, 1.0, 0.0, "AABB"); pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ> ()); if (pcl::io::loadPCDFile (argv[1], *cloud) == -1) return (-1); pcl::MomentOfInertiaEstimation <pcl::PointXYZ> feature_extractor; feature_extractor.setInputCloud (cloud); feature_extractor.compute (); std::vector <float> moment_of_inertia; std::vector <float> eccentricity; pcl::PointXYZ min_point_AABB; pcl::PointXYZ max_point_AABB; pcl::PointXYZ min_point_OBB; pcl::PointXYZ max_point_OBB; pcl::PointXYZ position_OBB; Eigen::Matrix3f rotational_matrix_OBB; float major_value, middle_value, minor_value; Eigen::Vector3f major_vector, middle_vector, minor_vector; Eigen::Vector3f mass_center; feature_extractor.getMomentOfInertia (moment_of_inertia); feature_extractor.getEccentricity (eccentricity); feature_extractor.getAABB (min_point_AABB, max_point_AABB); feature_extractor.getOBB (min_point_OBB, max_point_OBB, position_OBB, rotational_matrix_OBB); feature_extractor.getEigenValues (major_value, middle_value, minor_value); feature_extractor.getEigenVectors (major_vector, middle_vector, minor_vector); feature_extractor.getMassCenter (mass_center); boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("3D Viewer")); viewer->setBackgroundColor (0, 0, 0); viewer->addCoordinateSystem (1.0); viewer->initCameraParameters (); viewer->addPointCloud<pcl::PointXYZ> (cloud, "sample cloud"); viewer->addCube (min_point_AABB.x, max_point_AABB.x, min_point_AABB.y, max_point_AABB.y, min_point_AABB.z, max_point_AABB.z, 1.0, 1.0, 0.0, "AABB"); Eigen::Vector3f position (position_OBB.x, position_OBB.y, position_OBB.z); Eigen::Quaternionf quat (rotational_matrix_OBB); viewer->addCube (position, quat, max_point_OBB.x - min_point_OBB.x, max_point_OBB.y - min_point_OBB.y, max_point_OBB.z - min_point_OBB.z, "OBB"); pcl::PointXYZ center (mass_center (0), mass_center (1), mass_center (2)); pcl::PointXYZ x_axis (major_vector (0) + mass_center (0), major_vector (1) + mass_center (1), major_vector (2) + mass_center (2)); pcl::PointXYZ y_axis (middle_vector (0) + mass_center (0), middle_vector (1) + mass_center (1), middle_vector (2) + mass_center (2)); pcl::PointXYZ z_axis (minor_vector (0) + mass_center (0), minor_vector (1) + mass_center (1), minor_vector (2) + mass_center (2)); viewer->addLine (center, x_axis, 1.0f, 0.0f, 0.0f, "major eigen vector"); viewer->addLine (center, y_axis, 0.0f, 1.0f, 0.0f, "middle eigen vector"); viewer->addLine (center, z_axis, 0.0f, 0.0f, 1.0f, "minor eigen vector"); //Eigen::Vector3f p1 (min_point_OBB.x, min_point_OBB.y, min_point_OBB.z); //Eigen::Vector3f p2 (min_point_OBB.x, min_point_OBB.y, max_point_OBB.z); //Eigen::Vector3f p3 (max_point_OBB.x, min_point_OBB.y, max_point_OBB.z); //Eigen::Vector3f p4 (max_point_OBB.x, min_point_OBB.y, min_point_OBB.z); //Eigen::Vector3f p5 (min_point_OBB.x, max_point_OBB.y, min_point_OBB.z); //Eigen::Vector3f p6 (min_point_OBB.x, max_point_OBB.y, max_point_OBB.z); //Eigen::Vector3f p7 (max_point_OBB.x, max_point_OBB.y, max_point_OBB.z); //Eigen::Vector3f p8 (max_point_OBB.x, max_point_OBB.y, min_point_OBB.z); //p1 = rotational_matrix_OBB * p1 + position; //p2 = rotational_matrix_OBB * p2 + position; //p3 = rotational_matrix_OBB * p3 + position; //p4 = rotational_matrix_OBB * p4 + position; //p5 = rotational_matrix_OBB * p5 + position; //p6 = rotational_matrix_OBB * p6 + position; //p7 = rotational_matrix_OBB * p7 + position; //p8 = rotational_matrix_OBB * p8 + position; //pcl::PointXYZ pt1 (p1 (0), p1 (1), p1 (2)); //pcl::PointXYZ pt2 (p2 (0), p2 (1), p2 (2)); //pcl::PointXYZ pt3 (p3 (0), p3 (1), p3 (2)); //pcl::PointXYZ pt4 (p4 (0), p4 (1), p4 (2)); //pcl::PointXYZ pt5 (p5 (0), p5 (1), p5 (2)); //pcl::PointXYZ pt6 (p6 (0), p6 (1), p6 (2)); //pcl::PointXYZ pt7 (p7 (0), p7 (1), p7 (2)); //pcl::PointXYZ pt8 (p8 (0), p8 (1), p8 (2)); //viewer->addLine (pt1, pt2, 1.0, 0.0, 0.0, "1 edge"); //viewer->addLine (pt1, pt4, 1.0, 0.0, 0.0, "2 edge"); //viewer->addLine (pt1, pt5, 1.0, 0.0, 0.0, "3 edge"); //viewer->addLine (pt5, pt6, 1.0, 0.0, 0.0, "4 edge"); //viewer->addLine (pt5, pt8, 1.0, 0.0, 0.0, "5 edge"); //viewer->addLine (pt2, pt6, 1.0, 0.0, 0.0, "6 edge"); //viewer->addLine (pt6, pt7, 1.0, 0.0, 0.0, "7 edge"); //viewer->addLine (pt7, pt8, 1.0, 0.0, 0.0, "8 edge"); //viewer->addLine (pt2, pt3, 1.0, 0.0, 0.0, "9 edge"); //viewer->addLine (pt4, pt8, 1.0, 0.0, 0.0, "10 edge"); //viewer->addLine (pt3, pt4, 1.0, 0.0, 0.0, "11 edge"); //viewer->addLine (pt3, pt7, 1.0, 0.0, 0.0, "12 edge"); while(!viewer->wasStopped()) { viewer->spinOnce (100); boost::this_thread::sleep (boost::posix_time::microseconds (100000)); } return (0);}讓我們來對(duì)此解釋一下
pcl::MomentOfInertiaEstimation <pcl::PointXYZ> feature_extractor;feature_extractor.setInputCloud (cloud);feature_extractor.compute ();上面的代碼加載了點(diǎn)云文件
std::vector <float> moment_of_inertia;std::vector <float> eccentricity;pcl::PointXYZ min_point_AABB;pcl::PointXYZ max_point_AABB;pcl::PointXYZ min_point_OBB;pcl::PointXYZ max_point_OBB;pcl::PointXYZ position_OBB;Eigen::Matrix3f rotational_matrix_OBB;float major_value, middle_value, minor_value;Eigen::Vector3f major_vector, middle_vector, minor_vector;Eigen::Vector3f mass_center;上面是?pcl::MomentOfInertiaEstimation這個(gè)類實(shí)例化的代碼。
feature_extractor.getMomentOfInertia (moment_of_inertia);feature_extractor.getEccentricity (eccentricity);feature_extractor.getAABB (min_point_AABB, max_point_AABB);feature_extractor.getOBB (min_point_OBB, max_point_OBB, position_OBB, rotational_matrix_OBB);feature_extractor.getEigenValues (major_value, middle_value, minor_value);feature_extractor.getEigenVectors (major_vector, middle_vector, minor_vector);feature_extractor.getMassCenter (mass_center);上面是我們聲明所有需要用來存儲(chǔ)描述器和方形盒子的變量。
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));viewer->setBackgroundColor (0, 0, 0);viewer->addCoordinateSystem (1.0);viewer->initCameraParameters ();viewer->addPointCloud<pcl::PointXYZ> (cloud, "sample cloud");viewer->addCube (min_point_AABB.x, max_point_AABB.x, min_point_AABB.y, max_point_AABB.y, min_point_AABB.z, max_point_AABB.z, 1.0, 1.0, 0.0, "AABB");上面展示了怎么獲取描述器和其它特征。
pcl::PointXYZ center (mass_center (0), mass_center (1), mass_center (2));pcl::PointXYZ x_axis (major_vector (0) + mass_center (0), major_vector (1) + mass_center (1), major_vector (2) + mass_center (2));pcl::PointXYZ y_axis (middle_vector (0) + mass_center (0), middle_vector (1) + mass_center (1), middle_vector (2) + mass_center (2));pcl::PointXYZ z_axis (minor_vector (0) + mass_center (0), minor_vector (1) + mass_center (1), minor_vector (2) + mass_center (2));viewer->addLine (center, x_axis, 1.0f, 0.0f, 0.0f, "major eigen vector");viewer->addLine (center, y_axis, 0.0f, 1.0f, 0.0f, "middle eigen vector");viewer->addLine (center, z_axis, 0.0f, 0.0f, 1.0f, "minor eigen vector");上面簡(jiǎn)單的創(chuàng)建了PCLVisualizer這個(gè)類,并把點(diǎn)云和AABB加入到可視化里面。
//Eigen::Vector3f p1 (min_point_OBB.x, min_point_OBB.y, min_point_OBB.z);//Eigen::Vector3f p2 (min_point_OBB.x, min_point_OBB.y, max_point_OBB.z);//Eigen::Vector3f p3 (max_point_OBB.x, min_point_OBB.y, max_point_OBB.z);//Eigen::Vector3f p4 (max_point_OBB.x, min_point_OBB.y, min_point_OBB.z);//Eigen::Vector3f p5 (min_point_OBB.x, max_point_OBB.y, min_point_OBB.z);//Eigen::Vector3f p6 (min_point_OBB.x, max_point_OBB.y, max_point_OBB.z);//Eigen::Vector3f p7 (max_point_OBB.x, max_point_OBB.y, max_point_OBB.z);//Eigen::Vector3f p8 (max_point_OBB.x, max_point_OBB.y, min_point_OBB.z);//p1 = rotational_matrix_OBB * p1 + position;//p2 = rotational_matrix_OBB * p2 + position;//p3 = rotational_matrix_OBB * p3 + position;//p4 = rotational_matrix_OBB * p4 + position;//p5 = rotational_matrix_OBB * p5 + position;//p6 = rotational_matrix_OBB * p6 + position;//p7 = rotational_matrix_OBB * p7 + position;//p8 = rotational_matrix_OBB * p8 + position;//pcl::PointXYZ pt1 (p1 (0), p1 (1), p1 (2));//pcl::PointXYZ pt2 (p2 (0), p2 (1), p2 (2));//pcl::PointXYZ pt3 (p3 (0), p3 (1), p3 (2));//pcl::PointXYZ pt4 (p4 (0), p4 (1), p4 (2));//pcl::PointXYZ pt5 (p5 (0), p5 (1), p5 (2));//pcl::PointXYZ pt6 (p6 (0), p6 (1), p6 (2));//pcl::PointXYZ pt7 (p7 (0), p7 (1), p7 (2));//pcl::PointXYZ pt8 (p8 (0), p8 (1), p8 (2));//viewer->addLine (pt1, pt2, 1.0, 0.0, 0.0, "1 edge");//viewer->addLine (pt1, pt4, 1.0, 0.0, 0.0, "2 edge");//viewer->addLine (pt1, pt5, 1.0, 0.0, 0.0, "3 edge");//viewer->addLine (pt5, pt6, 1.0, 0.0, 0.0, "4 edge");//viewer->addLine (pt5, pt8, 1.0, 0.0, 0.0, "5 edge");//viewer->addLine (pt2, pt6, 1.0, 0.0, 0.0, "6 edge");//viewer->addLine (pt6, pt7, 1.0, 0.0, 0.0, "7 edge");//viewer->addLine (pt7, pt8, 1.0, 0.0, 0.0, "8 edge");//viewer->addLine (pt2, pt3, 1.0, 0.0, 0.0, "9 edge");//viewer->addLine (pt4, pt8, 1.0, 0.0, 0.0, "10 edge");//viewer->addLine (pt3, pt4, 1.0, 0.0, 0.0, "11 edge");//viewer->addLine (pt3, pt7, 1.0, 0.0, 0.0, "12 edge");上面是可以用來顯示特征向量的代碼。
這些大量的代碼展示了選擇的方形盒子是怎么工作的。記住你需要旋轉(zhuǎn)OBB的每一個(gè)頂點(diǎn)。這個(gè)代碼和PCLViser::addCube()方法一樣。
然后運(yùn)行代碼
./moment_of_inertia lamppost.pcd?
?
總結(jié)
以上是生活随笔為你收集整理的惯性矩和偏心距描述器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【OpenCV 例程200篇】14. 图
- 下一篇: python里面的list