下午闲来无事,在网上找了几个pcl点云的教程,简单拼凑,做了一点儿小功能,希望能对您有所启发!
主要功能为:
(1)将txt格式的点云转化为pcd格式的点云
(2)根据设定的高程阈值,即Z字段,将范围之内的点坐标滤掉
(3)从滤波后的点云可视化结果中,获取鼠标点击位置的三维点坐标,将其打印在控制台
1 txt点云转pcd点云
///------------------------------点云数据的格式转化模块------------------------------
//函数功能:将如下格式的txt点云转换为pcd点云
/*
PointNum: 13346
X Y Z Intensity ClassType ReturnNumberType
437354.62000000 2557954.96980469 -3.51999817 1476 2
*/
//函数参数:txtPath txt点云文件路径
//函数参数:pcdPath pcd点云的文件路径
bool txt2pcd(string txtPath, string pcdPath)
{
ifstream in;
in.open(txtPath);
string widthLine;
getline(in, widthLine);
//第一行标记了点云的行数,以字符串形式读入然后再转化为数值
string width = widthLine.substr(widthLine.find_first_of(':') + 2, widthLine.size() - 1);
int pclWidth;
sscanf(width.c_str(), "%d", &pclWidth);
cout << width << endl;
pcl::PointCloud<pcl::PointXYZ> pcdCloud;
pcdCloud.width = pclWidth;
pcdCloud.height = 1;
pcdCloud.is_dense = false;
pcdCloud.points.resize(pcdCloud.width * pcdCloud.height);
//跳过第二行
getline(in, widthLine);
float num[6];
for(size_t j = 0; j < pclWidth; ++j)
{
for(size_t i = 0; i < 6; ++i)
{
in >> num[i];
}
pcdCloud.points[j].x = num[0];
pcdCloud.points[j].y = num[1];
pcdCloud.points[j].z = num[2];
}
pcl::io::savePCDFileASCII(pcdPath, pcdCloud);
return true;
}
2 根据设定高程阈值进行简单点云滤波
//根据z字段范围进行点云滤波
//参数:cloud 待滤波点云数据
//参数:minZ 高程下界
//参数:maxZ 高程上界
//参数:is_inner 为真取区间外点,为假取区间外点
//返回值:根据设定高程界限滤波后的点云结果
pcl::PointCloud<pcl::PointXYZ>::Ptr filterByZ(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, double minZ, double maxZ, bool is_inner)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloudFilter(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PassThrough<pcl::PointXYZ> pass;
pass.setInputCloud(cloud);
pass.setFilterFieldName("z");
pass.setFilterLimits(minZ, maxZ);
pass.setFilterLimitsNegative(is_inner);//参数为true表示取范围之外的点;为false表示取设定范围之内的点,默认为false
//该函数在未来可能会被setNegative所替代
pass.filter(*cloudFilter);
return cloudFilter;
}
3 屏幕三维点坐标拾取
boost::mutex cloud_mutex;
typedef pcl::PointXYZRGBA PointT;
typedef pcl::PointCloud<PointT> PointCloudT;
//定义回调参数结构体
struct callback_args{
// structure used to pass arguments to the callback function
PointCloudT::Ptr clicked_points_3d;
pcl::visualization::PCLVisualizer viewerPtr;
};
// 回调处理事件 这里是点选取事件
void pp_callback(const pcl::visualization::PointPickingEvent& event, void *args)
{
struct callback_args * data = (struct callback_args *)args;//点云数据 & 可视化窗口
if (event.getPointIndex() == -1)
return;
PointT current_point;
event.getPoint(current_point.x, current_point.y, current_point.z);
data->clicked_points_3d->points.push_back(current_point);
//Draw clicked points in red:
pcl::visualization::PointCloudColorHandlerCustom<PointT> red(data->clicked_points_3d, 255, 0, 0);
data->viewerPtr.removePointCloud("clicked_points");
data->viewerPtr.addPointCloud(data->clicked_points_3d, red, "clicked_points");
data->viewerPtr.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 10, "clicked_points");
std::cout << current_point.x << " " << setiosflags(ios::fixed) << current_point.y << " " << current_point.z << std::endl;
}
//参数:待显示点云
void getPointCoordinate(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
{
pcl::visualization::PCLVisualizer viewer;
std::cout << cloud->points.size() << std::endl;
cloud_mutex.lock(); // for not overwriting the point cloud
// Display pointcloud:
viewer.addPointCloud(cloud, "bunny_source");
viewer.setCameraPosition(0, 0, -2, 0, -1, 0, 0);
// Add point picking callback to viewer:
struct callback_args cb_args;
PointCloudT::Ptr clicked_points_3d(new PointCloudT);
cb_args.clicked_points_3d = clicked_points_3d;
cb_args.viewerPtr = pcl::visualization::PCLVisualizer(viewer);
viewer.registerPointPickingCallback(pp_callback, (void*)&cb_args);
std::cout << "Shift+click on three floor points, then press 'Q'..." << std::endl;
// Spin until 'Q' is pressed:
viewer.spin();
std::cout << "done." << std::endl;
cloud_mutex.unlock();
while (!viewer.wasStopped())
{
viewer.spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
}
4 测试代码
int main(int argc, char* argv[])
{
//测试1:将规定的txt格式的点云转换为pcd格式的点云数据
//txt2pcd("block.txt", "pointA.pcd");
//测试3:根据Z坐标滤掉给定阈值以外的点云坐标
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1(new pcl::PointCloud<pcl::PointXYZ>);
int ret = pcl::io::loadPCDFile("pointA.pcd", *cloud1);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud2 = filterByZ(cloud1, 0, 6, false);
getPointCoordinate(cloud2);
cout << "End" << endl;
system("pause");
return 0;
}
滤波之前的点云:
滤波之后的点云
完整源码参见【运筹优化与图像处理算法编程】公众号微文:根据高程阈值滤波及三维屏幕坐标提取