PCL:交互点选点云

Posted 没事就要敲代码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PCL:交互点选点云相关的知识,希望对你有一定的参考价值。

1 选点方式

Shift+鼠标左键 实现点选

2 代码实现

#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>

using namespace std;

typedef pcl::PointXYZ PointT;
typedef pcl::PointCloud<PointT> PointCloudT;
typedef pcl::visualization::PCLVisualizer Visualizer;

//Mutex 
boost::mutex cloud_mutex;

//用于向回调函数传递参数的结构体
struct callback_args 

	PointCloudT::Ptr clicked_points_3d;
	pcl::visualization::PCLVisualizer::Ptr viewerPtr;
;

//点选函数
void pointPick_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);

	//显示当前点
	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");
	cout << current_point << endl;


int main()

	cout << "->正在加载点云..." << endl;
	PointCloudT::Ptr cloud(new PointCloudT);
	if (pcl::io::loadPCDFile("1.pcd", *cloud) < 0)
	
		PCL_ERROR("\\a点云文件不存在!\\n");
		system("pause");
		return -1;
	
	cout << "->加载点的个数为:" << cloud->points.size() << endl;
	
	//创建可视化对象
	boost::shared_ptr<Visualizer> viewer(new Visualizer("viewer"));

	cloud_mutex.lock();    // for not overwriting the point cloud

	viewer->addPointCloud(cloud, "pick");
	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(pointPick_callback, (void*)&cb_args);
	cout << "->Shift + 鼠标左键选点,按 ‘Q’结束选点" << endl;

	// Spin until 'Q' is pressed:
	viewer->spin();
	cout << "->选点结束" << endl;

	cloud_mutex.unlock();

	while (!viewer->wasStopped())
	
		viewer->spinOnce(100);
		boost::this_thread::sleep(boost::posix_time::microseconds(100000));
	

	return 0;

3 结果展示

->正在加载点云...
->加载点的个数为:41049
->Shift + 鼠标左键选点,按 ‘Q’结束选点
(-0.364254,-0.54109,-1.45432)
(0.0056356,-0.487013,-1.55463)
(-0.364254,-0.54109,-1.45432)
(0.115426,-0.608053,-1.33675)
(-0.554544,0.267087,-1.44627)
(0.255615,0.260605,-1.4394)
(-0.335707,0.119625,-1.19228)
->选点结束

4 boost::mutex 详细解说

参考链接:https://www.cnblogs.com/flyinggod/p/13570390.html

5 注意

  • 目前不清除boost::mutex 的作用,点选过程中,将boost::mutex的相关代码注释掉,仍可以正常选点,没有明显区别。

6 参考链接

https://pointclouds.org/documentation/classpcl_1_1visualization_1_1_point_picking_event.html

以上是关于PCL:交互点选点云的主要内容,如果未能解决你的问题,请参考以下文章

PCL OcTree——点云压缩

PCL:CloudViewer 简单点云可视化

PCL:计算指定点到点云的最远一点坐标

PCL:投影滤波将点云投影至球面

PCL:多平面分割

PCL OcTree——点云压缩