#include <pcl/visualization/cloud_viewer.h>
#include <iostream>//标准C++库中的输入输出类相关头文件。
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>//pcd 读写类相关的头文件。
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h> //PCL中支持的点类型头文件。
#include <pcl/octree/octree.h>
#include<fstream>
#include <string>
#include <vector>
//#include <LasOperator.h>
#include <liblas/liblas.hpp>
#include <pcl/filters/passthrough.h>
#include <pcl/segmentation/region_growing.h>
#include <pcl/search/search.h>
#include <pcl/search/kdtree.h>
#include <pcl/features/normal_3d.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/radius_outlier_removal.h>
using namespace std;
void loadLasFile(string s, pcl::PointCloud<pcl::PointXYZ>& cloud) {
/*
*读取las文件
*/
std::ifstream ifs(s, std::ios::in | std::ios::binary); // 打开las文件
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs); // 读取las文件
unsigned long int nbPoints = reader.GetHeader().GetPointRecordsCount();//获取las数据点的个数
cloud.width = nbPoints; //保证与las数据点的个数一致
cloud.height = 1;
cloud.is_dense = false;
cloud.points.resize(cloud.width * cloud.height);
int i = 0;
uint16_t r1, g1, b1;
int r2, g2, b2;
uint32_t rgb;
while (reader.ReadNextPoint()) {
// 获取las数据的x,y,z信息
cloud.points[i].x = (reader.GetPoint().GetX());
cloud.points[i].y = (reader.GetPoint().GetY());
cloud.points[i].z = (reader.GetPoint().GetZ());
//获取las数据的r,g,b信息
/*r1 = (reader.GetPoint().GetColor().GetRed());
g1 = (reader.GetPoint().GetColor().GetGreen());
b1 = (reader.GetPoint().GetColor().GetBlue());
r2 = ceil(((float)r1 / 65536) * (float)256);
g2 = ceil(((float)g1 / 65536) * (float)256);
b2 = ceil(((float)b1 / 65536) * (float)256);
rgb = ((int)r2) << 16 | ((int)g2) << 8 | ((int)b2);
cloud.points[i].rgb = *reinterpret_cast<float*>(&rgb);*/
i++;
}
}
float computeRange(pcl::PointCloud<pcl::PointXYZ>& trail, float r, int index, int k) {
float x1, y1, x2, y2, a1, a2, a3, b1, b2, b3;//向量1,a,b为轨迹线上2点
x2 = 0; y2 = 1;
a1 = trail.points[index].x;
a2 = trail.points[index].y;
a3 = trail.points[index].z;
b1 = trail.points[index + k].x;
b2 = trail.points[index + k].y;
b3 = trail.points[index + k].z;
x1 = a1 - b1;
y1 = a2 - b2;
float cosa = (x1 * x2 + y1 * y2) / (sqrt(x1 * x1 + y1 * y1) + sqrt(x2 * x2 + y2 * y2));
float sina = sqrt(1 - cosa * cosa);
float range = r / sina;
//float radian = acos(cosa);
//float angle = radian * 180.0 / M_PI;
//if (angle > 90)
// angle = 180 - angle;
//cout << "angle: " << angle << "\t";
return range;
}
int main() {
//输入点云,轨迹线,保存种子
pcl::PointCloud<pcl::PointXYZ>::Ptr input_cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr trail(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr midle(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr midle_filtered(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr result(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr tempCloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointXYZ tempPnt;
std::vector<int>idx;
std::vector<int>midle_idx;
//变量
float r = 10; //o1,使用+ 2.5,5,10偏小,5刚好,10覆盖
int k = 6;//20
//读取输入点云
cout << "读取点云" << endl;
loadLasFile("o.las", *input_cloud);
//pcl::io::loadPCDFile("o.pcd", *input_cloud);
//读取轨迹线
cout << "读取轨迹线" << endl;
pcl::io::loadPCDFile("trail.pcd", *trail);
cout << "原始点数: " << input_cloud->width << " \t" << "轨迹线点数: " << trail->width << endl;
//cloud构建八叉树
cout << "构建八叉树" << endl;
float resolution = 128.0f;
pcl::octree::OctreePointCloudSearch<pcl::PointXYZ> octree(resolution);
octree.setInputCloud(input_cloud);
octree.addPointsFromInputCloud();
cout << "构建完成" << endl;
/**
切割宽于路面部分
*/
cout << "切割宽于路面部分" << endl;
for (size_t i = 0;i + k < trail->points.size(); i += k) {
//for (size_t i = 8000;i<8500; i += k) {
cout << "-------------------------------------------------------------" << endl;
float range = computeRange(*trail, r, i, k);
float center = trail->points[i].x;
float Ymin = min(trail->points[i].y, trail->points[i + k].y);
float Ymax = max(trail->points[i].y, trail->points[i + k].y);
float Zmin = -10;
float Zmax = max(trail->points[i].z, trail->points[i + k].z);
cout << "range: " << range << endl;
//分段切割种子点
Eigen::Vector3f Emin(center - range, Ymin, Zmin);//0xc0c0c0c0 0x3f3f3f3f
Eigen::Vector3f Emax(center + range, Ymax, Zmax);
octree.boxSearch(Emin, Emax, idx);
cout << i << "\t这段种子数:\t" << idx.size() << endl;
midle_idx.insert(midle_idx.end(), idx.begin(), idx.end());
}
//提取midle
boost::shared_ptr<std::vector<int>> midle_ptr = boost::make_shared<std::vector<int>>(midle_idx);
pcl::ExtractIndices<pcl::PointXYZ> extract;
extract.setInputCloud(input_cloud);
extract.setIndices(midle_ptr);
extract.setNegative(false);//如果设为true,可以提取指定index之外的点云
extract.filter(*midle);
//去杂点
pcl::RadiusOutlierRemoval<pcl::PointXYZ> ror;
ror.setInputCloud(midle);
ror.setRadiusSearch(0.03);
ror.setMinNeighborsInRadius(2);
ror.filter(*midle_filtered);
cout << "滤波后的点云: o_midle_filtered.pcd" << endl;
pcl::io::savePCDFileASCII("o_midle_filtered.pcd", *midle_filtered);
cout << "保存成功" << endl;
return 0;
}
切割midle,滤波
最新推荐文章于 2023-11-11 16:30:20 发布