欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++利用PCL點云庫操作txt文件詳解

 更新時間:2024年01月23日 10:39:13   作者:玖玉ww  
這篇文章主要為大家詳細介紹了C++如何利用PCL點云庫操作txt文件,文中的示例代碼講解詳細,具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解一下

讀取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 中兩個不同的類,都用于創(chuàng)建和管理點云數(shù)據(jù)的可視化窗口。

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"));
	// 添加需要顯示的點云數(shù)據(jù)
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 50, 127, 220);
	viewer->addPointCloud<pcl::PointXYZ>(cloud, single_color, "example");
	// 設(shè)置顯示點云時,點的大小為2(以大小為2的點顯示點云)
	viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "example");

	while (!viewer->wasStopped()) { // 直到窗口關(guān)閉才結(jié)束循環(huán)
		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"));
	// 添加需要顯示的點云數(shù)據(jù)
	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(); // 調(diào)用內(nèi)部的重繪函數(shù)
	}
}
void visualization3(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
{
	// pcl::visualization::PCLVisualizer viewer;  // 不設(shè)置窗口的名字
	// pcl::visualization::PCLVisualizer viewer("viewer3");
	pcl::visualization::PCLVisualizer viewer;
	viewer.setWindowName("viewer3");
	// 添加需要顯示的點云數(shù)據(jù)
	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(); // 重繪函數(shù)
		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文件中坐標數(shù)據(jù) 和 顏色數(shù)據(jù) 的位置,以不同方式保存的txt文件其數(shù)據(jù)順序可能不同
* 以坐標在前,坐標(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"));
	/** 添加需要顯示的點云數(shù)據(jù) **/
	//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文件。

顯示結(jié)果:

到此這篇關(guān)于C++利用PCL點云庫操作txt文件詳解的文章就介紹到這了,更多相關(guān)C++ PCL操作txt內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C語言數(shù)據(jù)結(jié)構(gòu)之學(xué)生信息管理系統(tǒng)課程設(shè)計

    C語言數(shù)據(jù)結(jié)構(gòu)之學(xué)生信息管理系統(tǒng)課程設(shè)計

    這篇文章主要為大家詳細介紹了C語言數(shù)據(jù)結(jié)構(gòu)之學(xué)生信息管理系統(tǒng)課程設(shè)計,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • C語言詳細分析講解流程控制語句用法

    C語言詳細分析講解流程控制語句用法

    C語言語句的執(zhí)行默認順序執(zhí)行(從上往下依次執(zhí)行),編程語言一般除了默認的順序執(zhí)行以外,還提供分支執(zhí)行和循環(huán)執(zhí)行的語法,讓我們一起來看看
    2022-05-05
  • C++使用動態(tài)內(nèi)存分配的原因解說

    C++使用動態(tài)內(nèi)存分配的原因解說

    這篇文章主要介紹了C++使用動態(tài)內(nèi)存分配的原因解說,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • C++共享內(nèi)存刪除的陷阱

    C++共享內(nèi)存刪除的陷阱

    這篇文章主要介紹了C++共享內(nèi)存刪除的陷阱講解,當進程結(jié)束使用共享內(nèi)存區(qū)時,要通過函數(shù) shmdt 斷開與共享內(nèi)存區(qū)的連接。下面來看看具體問題都是怎么解決的吧
    2022-01-01
  • 使用C語言實現(xiàn)CRC校驗的方法

    使用C語言實現(xiàn)CRC校驗的方法

    本篇文章是對使用C語言實現(xiàn)CRC校驗的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C++實現(xiàn)簡易萬年歷

    C++實現(xiàn)簡易萬年歷

    這篇文章主要為大家詳細介紹了C++實現(xiàn)簡易萬年歷,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • C/C++中extern

    C/C++中extern "C" 的作用分析

    這篇文章主要介紹了C/C++中extern "C" 的作用,是在進行C/C++程序設(shè)計中非常常見的用法,需要的朋友可以參考下
    2014-09-09
  • C++與QML進行數(shù)據(jù)交互的常見方法總結(jié)

    C++與QML進行數(shù)據(jù)交互的常見方法總結(jié)

    這篇文章主要為大家詳細介紹了C++與QML進行數(shù)據(jù)交互的常見方法,文中 的示例代碼講解詳細,具有一定的參考價值,有需要的小伙伴可以跟隨小編一起了解一下
    2023-10-10
  • win32下進程間通信(共享內(nèi)存)實例分析

    win32下進程間通信(共享內(nèi)存)實例分析

    這篇文章主要介紹了win32下進程間通信(共享內(nèi)存)實例分析,對win32應(yīng)用程序及進程的原理做了較為深入的剖析,需要的朋友可以參考下
    2014-07-07
  • Qt獲取git版本信息的具體方法

    Qt獲取git版本信息的具體方法

    這篇文章主要介紹了Qt獲取git版本信息的具體方法,今天又碰到這個問題了,想根據(jù)具體的git版本信息做代碼問題確認,文中有詳細的解決方案,具有一定的參考價值,需要的朋友可以參考下
    2024-04-04

最新評論