【自用】点云学习记录——PCL屏幕选点

1  原理

PCL屏幕选点

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

2  代码

#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
 
typedef pcl::PointXYZRGBA PointT;
typedef pcl::PointCloud<PointT> PointCloudT;
 
// Mutex: //进程锁
boost::mutex cloud_mutex;
 
//用于给回调函数的结构体定义
// structure used to pass arguments to the callback function
struct 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<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 << " " << current_point.y << " " << current_point.z << std::endl;
 
}
 
void main()
{
	std::string filename("rabbit.pcd");
	//visualizer,定义可视化窗口
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());
	boost::shared_ptr<pcl::visualization::PCLVisualizer> 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));
	}
}

3  结果

 4  存在问题

4.1  点云的坐标系是什么坐标系?

4.2  为什么坐标数值差别那么小?

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值