博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PCL:屏幕拾点得到坐标
阅读量:3948 次
发布时间:2019-05-24

本文共 3207 字,大约阅读时间需要 10 分钟。

转载他人,原文链接:https://blog.csdn.net/weixin_40863346/article/details/88049849

此系列旨在记录学习过程,及代码案例。

此文操作平台:win7+pcl1.8.0+vs2015

PCL中通过注册回调函数registerPointPickingCallback来实现点云的三维坐标选取。

以下为代码main.cpp:

#include 
#include
#include
#include
#include
typedef pcl::PointXYZRGBA PointT;typedef pcl::PointCloud
PointCloudT; // Mutex: //进程锁boost::mutex cloud_mutex; //用于给回调函数的结构体定义// structure used to pass arguments to the callback functionstruct callback_args { PointCloudT::Ptr clicked_points_3d; pcl::visualization::PCLVisualizer::Ptr 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); //TODO data->clicked_points_3d->clear();//将上次选的点清空 data->clicked_points_3d->points.push_back(current_point);//添加新选择的点 // Draw clicked points in red:将选中点用红色标记 pcl::visualization::PointCloudColorHandlerCustom
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 << " " << current_point.y << " " << current_point.z << std::endl; } void main(){ std::string filename("rabbit.pcd"); //visualizer,定义可视化窗口 pcl::PointCloud
::Ptr cloud(new pcl::PointCloud
()); boost::shared_ptr
viewer(new pcl::visualization::PCLVisualizer("viewer")); //读取点云数据 if (pcl::io::loadPCDFile(filename, *cloud)) { std::cerr << "ERROR: Cannot open file " << filename << "! Aborting..." << std::endl; return; } std::cout << cloud->points.size() << std::endl; // for not overwriting the point cloud //获得互斥体,期间不能修改点云 cloud_mutex.lock(); // Display pointcloud: //显示点云 viewer->addPointCloud(cloud, "rabbit"); //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::Ptr(viewer); //注册屏幕选点事件 viewer->registerPointPickingCallback(pp_callback, (void*)&cb_args); //Shfit+鼠标左键选择点 std::cout << "Shift+click on three floor points, then press 'Q'..." << std::endl; // Spin until 'Q' is pressed: //摁“Q”键结束 viewer->spin(); std::cout << "done." << std::endl; //释放互斥体 cloud_mutex.unlock(); while (!viewer->wasStopped()) { viewer->spinOnce(100); //100?? boost::this_thread::sleep(boost::posix_time::microseconds(100000)); }}

最终实现效果如下图:

代码中设计的类的理解/解释:

boost::mutex cloud_mutex:这是boost库中提供的的mutex类,可以和lock类通过组合轻易构建读写锁和互斥锁。其中mutex是对象类,lock类是模板类。mutex类主要有两种:boost::mutex,boost::shared_mutex,其中mutex有lock和unlock方法,shared_mutex除了提供lock和unlock方法外,还有shared_lock和shared_unlock方法。因此,boost::mutex为独占互斥类,boost::shared_mutex为共享互斥类。

参考链接:

                

pcl::visualization::PCLVisualizer:PCL中进行点云可视化的类。同样进行可视化的类还有CloudViewer类。后者不能用于多线程应用程序。

官方教程:

参考链接:

 

 

参考链接:

(1)

(2)

(3)PCL官方实例:

你可能感兴趣的文章
Manipulating Broadcast Receivers On Demand 按需操控广播接收
查看>>
Creating a View Class 创建一个视图类
查看>>
Custom Drawing 自定义绘制
查看>>
Making the View Interactive 视图互动
查看>>
Optimizing the View 优化视图
查看>>
Setting Up the Search Interface 设置搜索界面
查看>>
Storing and Searching for Data 数据存储和搜索
查看>>
Remaining Backward Compatible 保持向后兼容
查看>>
Remembering Your User 记住你的用户
查看>>
Authenticating to OAuth2 Services 验证OAuth2服务
查看>>
Creating a Custom Account Type 创建自定义帐户类型
查看>>
Sending Content to Other Apps 将内容发送到其他应用程序
查看>>
Receiving Content from Other Apps 接收来自其他应用程序的内容
查看>>
Adding an Easy Share Action 添加一个简单的共享行动
查看>>
Taking Photos Simply 简单地拍摄照片
查看>>
Recording Videos Simply 简单录制视频
查看>>
Controlling the Camera 控制相机
查看>>
Creating Multiple APKs for Different API Levels 创建多个不同的API级别的APK
查看>>
Creating Multiple APKs for Different Screen Sizes 创建多个APKs为不同的屏幕尺寸
查看>>
Creating Multiple APKs for Different GL Textures 创建多个APK给不同的GL结构
查看>>