PCL 1.8.1 最小二乘法拟合空间直线:点云处理教程
#include<iostream>\n#include<pcl/io/ply_io.h>\n#include<pcl/point_types.h>\n#include<pcl/common/pca.h>\n#include<pcl/segmentation/sac_segmentation.h>\n#include<pcl/visualization/pcl_visualizer.h>\n\nint\ main()\n{\n\ \t//------------------------------输入点云-----------------------------------\n\ \tpcl::PointCloudpcl::PointXYZ::Ptr\ cloud(new\ pcl::PointCloudpcl::PointXYZ);\n\ \tif\ (pcl::io::loadPLYFilepcl::PointXYZ("D:\DIANYUNWENJIANJIA\newkruskal2_ply.ply",\ *cloud)\ <\ 0)\n\ \t{\n\ \t\ \tPCL_ERROR("读取文件错误");\n\ \t\ \treturn\ -1;\n\ \t}\n\ \t//---------------------------------PCA-------------------------------------\n\ \tpcl::PCApcl::PointXYZ\ pca;\n\ \tpca.setInputCloud(cloud);\n\ \n\ \t//\ 获取特征值对应的特征向量\n\ \tEigen::Vector3f\ V1\ =\ pca.getEigenVectors().col(0);\n\ \tEigen::Vector3f\ V2\ =\ pca.getEigenVectors().col(1);\n\ \tEigen::Vector3f\ V3\ =\ pca.getEigenVectors().col(2);\n\ \n\ \t//---------------------------直线的点向式---------------------------------\n\ \tfloat\ m\ =\ V1[0],\ n\ =\ V1[1],\ p\ =\ V1[2];\n\ \tfloat\ x0\ =\ pca.getMean()[0],\ y0\ =\ pca.getMean()[1],\ z0\ =\ pca.getMean()[2];\n\ \tstd::cout\ <<\ "直线的方向向量为:"\ <<\ V1.transpose()\ <<\ std::endl;\n\ \tstd::cout\ <<\ "直线上一点的坐标为:"\ <<\ pca.getMean().head<3>().transpose()\ <<\ std::endl;\n\ \n\ \t//---------------------------直线的一般式---------------------------------\n\ \tEigen::Matrix<float,\ 2,\ 3>\ A;\n\ \tA.row(0)\ =\ V2.transpose();\n\ \tA.row(1)\ =\ V3.transpose();\n\ \tEigen::Vector3f\ sigma\ =\ pca.getMean().head<3>();\n\ \tEigen::Vector2f\ b\ =\ A\ *\ sigma;\n\ \n\ \tstd::cout\ <<\ "直线的一般式为:"\ <<\ std::endl;\n\ \tstd::cout\ <<\ V2[0]\ <<\ "x+("\ <<\ V2[1]\ <<\ "y)+("\ <<\ V2[2]\ <<\ "z)="\ <<\ b[0]\ <<\ std::endl;\n\ \tstd::cout\ <<\ V3[0]\ <<\ "x+("\ <<\ V3[1]\ <<\ "y)+("\ <<\ V3[2]\ <<\ "z)="\ <<\ b[1]\ <<\ std::endl;\n\ \n\ \t//----------------------------点云可视化----------------------------------\n\ \tpcl::visualization::PCLVisualizer\ viewer("Point\ Cloud\ Viewer");\n\ \tviewer.setBackgroundColor(0,\ 0,\ 0);\n\ \tviewer.addPointCloudpcl::PointXYZ(cloud,\ "cloud");\n\ \tviewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,\ 3,\ "cloud");\n\ \n\ \t//--------------------------拟合直线可视化--------------------------------\n\ \tpcl::ModelCoefficients::Ptr\ coefficients(new\ pcl::ModelCoefficients);\n\ \tcoefficients->values.resize(6);\n\ \tcoefficients->values[0]\ =\ x0;\n\ \tcoefficients->values[1]\ =\ y0;\n\ \tcoefficients->values[2]\ =\ z0;\n\ \tcoefficients->values[3]\ =\ m;\n\ \tcoefficients->values[4]\ =\ n;\n\ \tcoefficients->values[5]\ =\ p;\n\ \n\ \tviewer.addLine<pcl::PointXYZ,\ pcl::PointXYZ>(*cloud,\ *cloud,\ "line");\n\ \tviewer.setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR,\ 1.0,\ 0.0,\ 0.0,\ "line");\n\ \n\ \twhile\ (!viewer.wasStopped())\n\ \t{\n\ \t\ \tviewer.spinOnce();\n\ \t}\n\ \n\ \t//----------------------------输出点云-----------------------------------\n\ \tpcl::io::savePLYFile("D:\DIANYUNWENJIANJIA\最小二乘法拟合直线_ply.ply",\ *cloud);\n\ \n\ \treturn\ 0;\n\
原文地址: https://www.cveoy.top/t/topic/pZQl 著作权归作者所有。请勿转载和采集!