一、原理
将点投影到一个参数化模型上,这个参数化模型可以是平面、圆球、圆柱、锥形等进行投影滤波。
二、代码显示
重点代码
// 我们的想法是创建一个平面 ax+by+cz+d=0; c!=0
pcl::ModelCoefficients::Ptr c(new pcl::ModelCoefficients());
c->values.resize(4); //ax + by + cz + d = 0
c->values[0] = 1.0; // ax
c->values[1] = 1.0;// by
c->values[2] = 1.0; //cz
c->values[3] = 0;//d
// 开始投影滤波
pcl::ProjectInliers<pcl::PointXYZ> pro; // 定义一个投影对象
pro.setInputCloud(cloud);
pro.setModelType(pcl::SACMODEL_PLANE); // 平面作为投影的模型
pro.setModelCoefficients(c); // 系数
pro.filter(*cloud_filtered);
整体代码:
#if 1 // 投影滤波
using namespace std;
int main()
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); //待滤波点云
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>); //滤波后点云
///读入点云数据
cout << "->正在读入点云..." << endl;
pcl::PCDReader reader;
reader.read("desktop.pcd", *cloud);
cout << "\t\t<读入点云信息>\n" << *cloud << endl;
// 我们的想法是创建一个平面 ax+by+cz+d=0; c!=0
pcl::ModelCoefficients::Ptr c(new pcl::ModelCoefficients());
c->values.resize(4); //ax + by + cz + d = 0
c->values[0] = 1.0; // ax
c->values[1] = 1.0;// by
c->values[2] = 1.0; //cz
c->values[3] = 0;//d
// 开始投影滤波
pcl::ProjectInliers<pcl::PointXYZ> pro; // 定义一个投影对象
pro.setInputCloud(cloud);
pro.setModelType(pcl::SACMODEL_PLANE); // 平面作为投影的模型
pro.setModelCoefficients(c); // 系数
pro.filter(*cloud_filtered);
//================================= 滤波前后对比可视化 ================================= ↓
pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("滤波前后对比"));
/*-----视口1-----*/
int v1(0);
viewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1); //设置第一个视口在X轴、Y轴的最小值、最大值,取值在0-1之间
viewer->setBackgroundColor(0, 0, 0, v1); //设置背景颜色,0-1,默认黑色(0,0,0)
viewer->addText("befor_filtered", 10, 10, "v1_text", v1);
viewer->addPointCloud<pcl::PointXYZ>(cloud, "befor_filtered_cloud", v1);
/*-----视口2-----*/
int v2(0);
viewer->createViewPort(0.5, 0.0, 1.0, 1.0, v2);
viewer->setBackgroundColor(0.3, 0.3, 0.3, v2);
viewer->addText("after_filtered", 10, 10, "v2_text", v2);
viewer->addPointCloud<pcl::PointXYZ>(cloud_filtered, "after_filtered_cloud", v2);
/*-----设置相关属性-----*/
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "befor_filtered_cloud", v1);
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1, 0, 0, "befor_filtered_cloud", v1);
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "after_filtered_cloud", v2);
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0, 1, 0, "after_filtered_cloud", v2);
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
//================================= 滤波前后对比可视化 ================================= ↑
return 0;
}
#endif