pcl 直通滤波
处理某一个坐标轴方向上的点云
头文件等
#include<pcl/filters/passthrough.h>
typedef pcl::PointXYZ PointT;
typedef pcl::PointCloud<PointT> CloudT;
typedef CloudT::Ptr CP;
主要代码
CP cloudTo(new CloudT);
pcl::PassThrough<pcl::PointXYZ> pass; //定义滤波器
pass.setInputCloud(cloudFrom); //设置待滤波的点云
pass.setFilterFieldName("x"); //设置需要操作的坐标轴
pass.setFilterLimits(46.0, 54.0); // 设置滤波坐标范围
pass.setFilterLimitsNegative(true); // true为保留范围内的数据,false为舍弃范围内的数据
pass.filter(*cloudTo); // 进行滤波输出
代码(x轴)
void lxr_vision(CP cloud1) {
pcl::visualization::PCLVisualizer viewer;
viewer.setBackgroundColor(0.05, 0.05, 0.05);
//viewer.addPointCloud(cloudFrom, pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(cloudFrom, 255, 0, 0), "cloudForm");
viewer.addPointCloud(cloud1, pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(cloud1, 0, 255, 0), "cloud1");
//viewer.addPointCloud(cloud2, pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(cloud2, 255, 0, 0), "cloud2");
while (!viewer.wasStopped()) {
viewer.spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
}
void process(string path) {
//读取点云
CP cloudFrom(new CloudT);
if (pcl::io::loadPCDFile<PointT>(path, *cloudFrom) == -1) {
cout << "读取失败:" << path << endl;
return;
}
//直通滤波
CP cloudTo(new CloudT);
pcl::PassThrough<pcl::PointXYZ> pass;
pass.setInputCloud(cloudFrom);
pass.setFilterFieldName("x"); //设置需要操作的坐标轴
pass.setFilterLimits(46.0, 54.0); // 设置坐标范围
pass.setFilterLimitsNegative(true); // true为保留范围内的数据,false为舍弃范围内的数据
pass.filter(*cloudTo); // 进行滤波输出
//可视化一下
cout << "原始点云大小:" << cloudFrom->size() << endl;
cout << "滤后点云大小:" << cloudTo->size() << endl;
lxr_vision(cloudFrom);
lxr_vision(cloudTo);
return;
}
结果
滤波前
滤波后
代码(y轴)
void lxr_vision(CP cloud1) {
pcl::visualization::PCLVisualizer viewer;
viewer.setBackgroundColor(0.05, 0.05, 0.05);
//viewer.addPointCloud(cloudFrom, pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(cloudFrom, 255, 0, 0), "cloudForm");
viewer.addPointCloud(cloud1, pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(cloud1, 0, 255, 0), "cloud1");
//viewer.addPointCloud(cloud2, pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(cloud2, 255, 0, 0), "cloud2");
while (!viewer.wasStopped()) {
viewer.spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
}
void process(string path) {
//读取点云
CP cloudFrom(new CloudT);
if (pcl::io::loadPCDFile<PointT>(path, *cloudFrom) == -1) {
cout << "读取失败:" << path << endl;
return;
}
//直通滤波
CP cloudTo(new CloudT);
pcl::PassThrough<pcl::PointXYZ> pass;
pass.setInputCloud(cloudFrom);
pass.setFilterFieldName("x"); //设置需要操作的坐标轴
pass.setFilterLimits(46.0, 54.0); // 设置坐标范围
pass.setFilterLimitsNegative(true); // true为保留范围内的数据,false为舍弃范围内的数据
pass.filter(*cloudTo); // 进行滤波输出
//可视化一下
cout << "原始点云大小:" << cloudFrom->size() << endl;
cout << "滤后点云大小:" << cloudTo->size() << endl;
lxr_vision(cloudFrom);
lxr_vision(cloudTo);
return;
}
结果
滤波前
滤波后