开发环境:Ubuntu18.04 PCL1.9.1
#include<iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/filters/passthrough.h>
#include <pcl/filters/radius_outlier_removal.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <Eigen/Dense>
void split(const std::string_input &s,std::vector<std::string> &string_output,const std::string &delim)
//s为输入的字符串,完成分割后的子字符串tokens存储到向量中,delim为分隔符字符串
//形参是引用类型时,它对应的实参被引用传递,引用形参是它对应的实参的别名,形参的数值就等于实参,不同于值传递的拷贝
{
//find_first_not_of(r,n,m) 从下标n的字符开始寻找第一个不是r的字符串,m表示搜索范围截止到下标为m的的字符
//这句代码从下标为0的字符开始,寻找第一个不是delim的字符,即我们要的第一个数据的第一个数字,start为该数字的下标
std::string::size_type start = string_input.find_first_not_of(delim,0);
//pose为第一个delim的下标
std::string::size_type pose = string_input.find_first_of(delim,start)
while(start!=std::string::nops||pose!=std::string::nops)//当既没有字符也没有delim的时候结束
{ //push_back用于向容器的末尾添加元素
string_output.push_back(string_input.substr(strat,pose-start));//提取出一段字符添加到输出字符串的末尾,这段字符下标以start开始,以pose-1结束。
start=string_input.find_first_not_of(delim,pose);//更新start
pose=string_output.find_first_of(delim,start);//更新pose
}
}
bool get_cloud_xyzrgb(std::string path,pcl::PointCloud<pcl::PointXYZRGB>&cloud)
{
cloud.clear();//清空引用对象cloud里的所有元素
std::ifstream inf;//创建了一个名为inf的输入文件流对象
bool flag = false;
try
{
inf.open(path);
}
catch (const std::exception&)//std::exception是C++标准库中所有异常类的基类。因此,catch (const std::exception&)可以捕获大多数由标准库抛出的异常。
{
return false;
}
std::string sline;//定义字符串
std::vector<std::string>string_output;
std::string Delema = " ";
pcl::PointXYZRGB point_message;
while(getline(inf,sline))//从inf文件中逐行读取,并存储在sline中
{
int i=0;
split(sline,string_output,Delema);
if(string_output.size()!=6) cout<<"don't match"<<endl;
point_message.x=stold(string_output[i]);
point_message.y=stold(string_output[i+1]);
point_message.z=stold(string_output[i+2]);
point_message.r = static_cast<uint8_t>(std::stoi(string_output[i+3]));
point_message.g = static_cast<uint8_t>(std::stoi(string_output[i+4]));
point_message.b = static_cast<uint8_t>(std::stoi(string_output[i+5]));
string_output.clear();
cloud.push_back(point_message);
}
inf.close();
}
void viewerOneoff(pcl::visualization::PCLVisualizer& viewer)
{
viewer.setBackgroundColor(0.0,0.0,0.0)//创建可视化窗口,将背景设置为白色
}
int main(int argc,char **argv)
{
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>)
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZRGB>)
//pcl::PointCloud<pcl::PointXYZRGB>: 表示点云对象的数据结构,每个点包含 XYZ 坐标和 RGB 颜色信息。
//::Ptr: 是指针类型后缀,表示指向该数据结构的指针。
//cloud: 是指向点云对象的智能指针的变量名。
//new pcl::PointCloud<pcl::PointXYZRGB>:在堆内存上创建一个新的pcl::PointCloud<pcl::PointXYZRGB>对象
string path = "file_path"//直接在引号内复制粘贴路径名即可
get_cloud_xyzrgb(path,*cloud);
std::cout<<"loaded"
<<cloud->width*cloud->height
<<std::endl;
//创建直通滤波器
pcl::PassThrough<pcl::PointXYZRGB>pass;
pass.setInputCloud(cloud);
pass.setFilterFieldName("z"); // 在 Z 轴方向进行滤波
pass.setFilterLimits(0.0, 40); // 设置滤波范围
pass.filter(*cloud_filtered);
// 可视化结果
pcl::visualization::PCLVisualizer viewer("Point Cloud Viewer");
viewer.createViewPort(0.0, 0.0, 0.5, 1.0, "original_cloud");
viewer.createViewPort(0.5, 0.0, 1.0, 1.0, "filtered_cloud");
viewer.setBackgroundColor(0, 0, 0); // 设置背景颜色为黑色
// 显示原始点云
viewer.addPointCloud(cloud, "original_cloud", 0);
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1.0, 0.0, 0.0, "original_cloud");
// 显示滤波后的点云
viewer.addPointCloud(filtered_cloud, "filtered_cloud", 1);
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0.0, 1.0, 0.0, "filtered_cloud");
// 开始可视化
while (!viewer.wasStopped()) {
viewer.spinOnce();
}
return 0;
}