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

C++利用PCL點(diǎn)云庫(kù)操作txt文件詳解

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

讀取txt點(diǎn)云文件

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

// 從txt文件中讀取三維坐標(biāo)
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() {
	/******* 加載點(diǎn)云 ********/
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	create_cloud_from_txt("拉伸.txt", cloud);
	return 0;
}

讀取txt點(diǎn)云文件&點(diǎn)云可視化

CloudViewer和PCLVisualizer 是 PCL 中兩個(gè)不同的類,都用于創(chuàng)建和管理點(diǎn)云數(shù)據(jù)的可視化窗口。

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

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

pcl::visualization::PCL_VISUALIZER_POINT_SIZE是點(diǎn)云屬性之點(diǎn)的大小。

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

	// 可視化點(diǎn)云
	visualization(cloud1);
	visualization_cv(cloud2);

	return 0;
}

讀取彩色txt點(diǎn)云&可視化

使用PCLVisualizer 類進(jìn)行可視化。

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


/*
* 從txt文件中讀取三維坐標(biāo)、rgb顏色
* 要注意txt文件中坐標(biāo)數(shù)據(jù) 和 顏色數(shù)據(jù) 的位置,以不同方式保存的txt文件其數(shù)據(jù)順序可能不同
* 以坐標(biāo)在前,坐標(biāo)(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"));
	/** 添加需要顯示的點(diǎn)云數(shù)據(jù) **/
	//pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> single_color(cloud, 50, 100, 255);
	//viewer->addPointCloud<pcl::PointXYZRGB>(cloud, single_color, "example");
	// 下面一行以點(diǎn)云本身的顏色進(jìn)行顯示,上面兩行修改點(diǎn)云顏色進(jìn)行顯示
	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) {
	// 加載點(diǎn)云
	pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
	create_cloud_from_txt_rgb("拉伸2 - Cloud - red.txt", cloud);
	// 可視化點(diǎn)云
	visualization_rgb(cloud);

	return 0;
}

截面 - Cloud - red.txt是從一個(gè)點(diǎn)云截取的一部分,以紅色(255, 0, 0)保存到txt文件。

顯示結(jié)果:

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

相關(guān)文章

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

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

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

    C語(yǔ)言詳細(xì)分析講解流程控制語(yǔ)句用法

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

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

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

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

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

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

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

    C++實(shí)現(xiàn)簡(jiǎn)易萬(wàn)年歷

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

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

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

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

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

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

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

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

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

最新評(píng)論