C++利用PCL点云库操作txt文件详解

 更新时间:2024年01月23日 10:39:13   作者:玖玉ww  
这篇文章主要为大家详细介绍了C++如何利用PCL点云库操作txt文件,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以了解一下

读取txt点云文件

#include <fstream>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>

// 从txt文件中读取三维坐标
void create_cloud_from_txt(const std::string& file_path, pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
{
	std::ifstream file(file_path.c_str());
	std::string line;
	pcl::PointXYZ point;
	while (getline(file, line)) {
		std::stringstream ss(line);
		ss >> point.x;
		ss >> point.y;
		ss >> point.z;
		cloud->push_back(point);
	}
	file.close();
}

int main() {
	/******* 加载点云 ********/
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	create_cloud_from_txt("拉伸.txt", cloud);
	return 0;
}

读取txt点云文件&点云可视化

CloudViewer和PCLVisualizer 是 PCL 中两个不同的类,都用于创建和管理点云数据的可视化窗口。

CloudViewer头文件:<pcl/visualization/cloud_viewer.h>

PCLVisualizer头文件:<pcl/visualization/pcl_visualizer.h>

pcl::visualization::PCL_VISUALIZER_POINT_SIZE是点云属性之点的大小。

#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp>
#include <pcl/visualization/cloud_viewer.h>

void create_cloud_from_txt(const std::string& file_path, pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
{
	std::ifstream file(file_path.c_str());
	std::string line;
	pcl::PointXYZ point;
	while (getline(file, line)) {
		std::stringstream ss(line);
		ss >> point.x;
		ss >> point.y;
		ss >> point.z;
		cloud->push_back(point);
	}
	file.close();
}


// 可视化,使用PCLVisualizer类
void visualization(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
{
	boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("viewer"));
	// 添加需要显示的点云数据
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 50, 127, 220);
	viewer->addPointCloud<pcl::PointXYZ>(cloud, single_color, "example");
	// 设置显示点云时,点的大小为2(以大小为2的点显示点云)
	viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "example");

	while (!viewer->wasStopped()) { // 直到窗口关闭才结束循环
		viewer->spinOnce(100);
		boost::this_thread::sleep(boost::posix_time::microseconds(100000));
	}
}
void visualization2(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
{
	pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("viewer"));
	// 添加需要显示的点云数据
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 50, 127, 220);
	viewer->addPointCloud<pcl::PointXYZ>(cloud, single_color, "example");
	viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "example");

	while (!viewer->wasStopped()) {
		viewer->spinOnce(); // 调用内部的重绘函数
	}
}
void visualization3(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
{
	// pcl::visualization::PCLVisualizer viewer;  // 不设置窗口的名字
	// pcl::visualization::PCLVisualizer viewer("viewer3");
	pcl::visualization::PCLVisualizer viewer;
	viewer.setWindowName("viewer3");
	// 添加需要显示的点云数据
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 50, 127, 220);
	viewer.addPointCloud<pcl::PointXYZ>(cloud, single_color, "example");
	viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "example");

	while (!viewer.wasStopped()) {
		//viewer.spinOnce(); // 重绘函数
		viewer.spinOnce(100);
		boost::this_thread::sleep(boost::posix_time::microseconds(100000));
	}
}
// 可视化,使用CloudViewer类
void  visualization_cv(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
{
	pcl::visualization::CloudViewer viewer("simple cloud viewer");
	viewer.showCloud(cloud);
	while (!viewer.wasStopped())
	{
		// todo::
	}
}

int main() {
	// 加载点云
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1(new pcl::PointCloud<pcl::PointXYZ>);
	create_cloud_from_txt("拉伸.txt", cloud1);
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud2(new pcl::PointCloud<pcl::PointXYZ>);
	create_cloud_from_txt("拉伸2.txt", cloud2);

	// 可视化点云
	visualization(cloud1);
	visualization_cv(cloud2);

	return 0;
}

读取彩色txt点云&可视化

使用PCLVisualizer 类进行可视化。

#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp>


/*
* 从txt文件中读取三维坐标、rgb颜色
* 要注意txt文件中坐标数据 和 颜色数据 的位置,以不同方式保存的txt文件其数据顺序可能不同
* 以坐标在前,坐标(x,y,z)、颜色在后,颜色(r,g,b)为例
*/
void create_cloud_from_txt_rgb(const std::string& file_path, pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud)
{
	std::ifstream file(file_path.c_str());
	std::string line;
	pcl::PointXYZRGB point;
	while (getline(file, line)) {
		std::stringstream ss(line);
		ss >> point.x;
		ss >> point.y;
		ss >> point.z;
		float f;
		std::uint8_t r = 0, g = 0, b = 0;    // Example: Red color
		ss >> f; r = f;
		ss >> f; g = f;
		ss >> f; b = f;
		std::uint32_t rgb = ((std::uint32_t)r << 16 | (std::uint32_t)g << 8 | (std::uint32_t)b);
		point.rgb = *reinterpret_cast<float*>(&rgb);
		// 可以使用下面注释的三行,替换上面两行
		/*point.r = r;
		point.g = g;
		point.b = b;*/
		cloud->push_back(point);
	}
	file.close();
}


// 可视化
void visualization_rgb(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud)
{
	boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("viewer"));
	/** 添加需要显示的点云数据 **/
	//pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> single_color(cloud, 50, 100, 255);
	//viewer->addPointCloud<pcl::PointXYZRGB>(cloud, single_color, "example");
	// 下面一行以点云本身的颜色进行显示,上面两行修改点云颜色进行显示
	viewer->addPointCloud<pcl::PointXYZRGB>(cloud, "example");
	viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "example");

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


int main(int argc, char** argv) {
	// 加载点云
	pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
	create_cloud_from_txt_rgb("拉伸2 - Cloud - red.txt", cloud);
	// 可视化点云
	visualization_rgb(cloud);

	return 0;
}

截面 - Cloud - red.txt是从一个点云截取的一部分,以红色(255, 0, 0)保存到txt文件。

显示结果:

到此这篇关于C++利用PCL点云库操作txt文件详解的文章就介绍到这了,更多相关C++ PCL操作txt内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解C++中的指针结构体数组以及指向结构体变量的指针

    详解C++中的指针结构体数组以及指向结构体变量的指针

    这篇文章主要介绍了C++中的指针结构体数组以及指向结构体变量的指针的用法,是C++入门学习中的基础知识,需要的朋友可以参考下
    2015-09-09
  • c语言实现简易版三子棋(附完整代码)

    c语言实现简易版三子棋(附完整代码)

    大家好,本篇文章主要讲的是c语言实现简易版三子棋(附完整代码),感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • C语言位运算符:与、或、异或、取反、左移与右移详细介绍

    C语言位运算符:与、或、异或、取反、左移与右移详细介绍

    以下是对C语言中的位运算符:与、或、异或、取反、左移与右移进行了详细的分析介绍,需要的朋友可以过来参考下
    2013-08-08
  • C++指针、引用与取地址运算符对比分析

    C++指针、引用与取地址运算符对比分析

    文章解释了C++中指针、引用和取地址运算符(&)的概念,并分析了它们的区别,文章还讨论了函数传参、右值引用以及智能指针等,感兴趣的朋友跟随小编一起看看吧
    2026-04-04
  • C语言动态数组详解

    C语言动态数组详解

    本文给大家分享的是一则使用C语言实现动态数组的代码,完美解决内存溢出以及内存回收问题,有需要的小伙伴可以参考下
    2021-09-09
  • 利用C++实现⾃然连接操作算法

    利用C++实现⾃然连接操作算法

    这篇文章主要介绍了利用C++实现⾃然连接操作算法,文章围绕主题展开详细的内容介绍,具有一定参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • C++11 constexpr使用详解

    C++11 constexpr使用详解

    constexpr是一种比const 更严格的束缚, 它修饰的表达式本身在编译期间可知, 并且编译器会尽可能的 evaluate at compile time,本文重点给大家介绍C++11 constexpr使用,需要的朋友可以参考下
    2021-12-12
  • C语言数据结构之线索二叉树及其遍历

    C语言数据结构之线索二叉树及其遍历

    这篇文章主要介绍了C语言数据结构之线索二叉树及其遍历的相关资料,为了加快查找节点的前驱和后继。对二叉树的线索化就是对二叉树进行一次遍历,在遍历的过程中检测节点的左右指针是否为空,如果是空,则将他们改为指向前驱和后继节点的线索,需要的朋友可以参考下
    2017-08-08
  • IOS开发之UIScrollView实现图片轮播器的无限滚动

    IOS开发之UIScrollView实现图片轮播器的无限滚动

    这篇文章主要介绍了IOS开发之UIScrollView实现图片轮播器的无限滚动的相关资料,需要的朋友可以参考下
    2017-07-07
  • C语言之循环语句详细介绍

    C语言之循环语句详细介绍

    大家好,本篇文章主要讲的是C语言之循环语句详细介绍,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12

最新评论