PCL:点云赋色 | 自定义点云中任意一点的颜色

1 PCL中PointXYZRGB类型的点云

1.1 PCL中PointXYZRGB类型点云的r\g\b类型

PCL中PointXYZRGB类型点云的r\g\b类型为unsigned char,下面通过代码进行验证。

代码:

#include <pcl/io/pcd_io.h>

using namespace std;

typedef pcl::PointXYZRGB PointT;
typedef pcl::PointCloud<PointT> PointCloudT;

int main()
{
    
    
	//------------------------ 加载点云 ------------------------
	PointCloudT::Ptr cloud(new PointCloudT);
	if (pcl::io::loadPCDFile("bunny.pcd", *cloud) < 0)
	{
    
    
		PCL_ERROR("->\a点云文件不存在!\n");
		return -1;
	}
	cout << "->加载点云详情:\n" << *cloud << endl;
	//======================== 加载点云 ========================

	cout << "->PCL中PointXYZRGB类型点云的r\g\b类型为:" << typeid(cloud->points[0].r).name() << endl;
	
	return 0;
}

输出结果:

在这里插入图片描述
C++中,stdint.h文件将unsigned char的别名定义为 uint8_t

在这里插入图片描述

1.2 PCL中PointXYZRGB类型点云的r\g\b的数值范围

我们已经知道PointXYZRGB类型点云的r\g\b类型为unsigned char,如果直接输出r\g\b,其结果为字符;

要想查看其颜色范围,需要进行强制类型转换

代码:

#include <pcl/io/pcd_io.h>

using namespace std;

typedef pcl::PointXYZRGB PointT;
typedef pcl::PointCloud<PointT> PointCloudT;

int main()
{
    
    
	//------------------------ 加载点云 ------------------------
	PointCloudT::Ptr cloud(new PointCloudT);
	if (pcl::io::loadPCDFile("colorfulBunny.pcd", *cloud) < 0)
	{
    
    
		PCL_ERROR("->\a点云文件不存在!\n");
		return -1;
	}
	cout << "->加载点云详情:\n" << *cloud << endl;
	//======================== 加载点云 ========================

	cout << "->第9个点的rgb为:"
		<< cloud->points[8].r << ", "
		<< cloud->points[8].g << ", "
		<< cloud->points[8].b << endl;

	cout << "->第9个点的rgb为:(强制类型转换)"
		<< (int)cloud->points[8].r << ", "
		<< (int)cloud->points[8].g << ", "
		<< (int)cloud->points[8].b << endl;

	return 0;
}

输出结果:

->加载点云详情:
points[]: 35947
width: 35947
height: 1
is_dense: 1
sensor origin (xyz): [0, 0, 0] / orientation (xyzw): [0, 0, 0, 1]

->9个点的rgb为:, ?->9个点的rgb为:(强制类型转换)255, 153, 255

从输出结果中可以看出,颜色范围为 0~255

2 自定义点云中每一点的颜色

同样的,将int类型的颜色值传给r\g\b时,也要进行强制类型转换,在数值前加 (uint8_t) 即可。

2.1 单一赋色

将点云赋为红色。

代码:

#include <pcl/io/pcd_io.h>

using namespace std;

typedef pcl::PointXYZRGB PointT;
typedef pcl::PointCloud<PointT> PointCloudT;

int main()
{
    
    
	//------------------------ 加载点云 ------------------------
	PointCloudT::Ptr cloud(new PointCloudT);
	if (pcl::io::loadPCDFile("bunny.pcd", *cloud) < 0)
	{
    
    
		PCL_ERROR("->\a点云文件不存在!\n");
		return -1;
	}
	cout << "->加载点云详情:\n" << *cloud << endl;
	//======================== 加载点云 ========================

	//-------------- 将所有点设置为红色(255,0,0)--------------
	size_t max_i = cloud->points.size();
	for (size_t i = 0; i < max_i; i++)
	{
    
    
		//需要强制类型转换
		cloud->points[i].r = (uint8_t)255;
		cloud->points[i].g = (uint8_t)0;
		cloud->points[i].b = (uint8_t)0;
	}
	//=============== 将所有点设置为红色(255,0,0)==============

	//------------------------ 保存点云 ------------------------
	pcl::io::savePCDFileBinary("redBunny.pcd", *cloud);
	//======================== 保存点云 ========================

	return 0;
}

结果展示:
在这里插入图片描述

2.2 根据给定的颜色数组为任意点赋色

自定义颜色数组,为点云任意点赋色。这里只给出10种颜色,需要更多颜色,请参考 RGB颜色对照表

代码:

#include <pcl/io/pcd_io.h>

using namespace std;

typedef pcl::PointXYZRGB PointT;
typedef pcl::PointCloud<PointT> PointCloudT;

int main()
{
    
    
	//------------------------ 加载点云 ------------------------
	PointCloudT::Ptr cloud(new PointCloudT);
	if (pcl::io::loadPCDFile("bunny.pcd", *cloud) < 0)
	{
    
    
		PCL_ERROR("->\a点云文件不存在!\n");
		return -1;
	}
	cout << "->加载点云详情:\n" << *cloud << endl;
	//======================== 加载点云 ========================

	//----------------------- 定义颜色数组 -----------------------
	const int colors[][3]=
	{
    
    
		255, 0,   0,	// red 			1
		0,   255, 0,	// green		2
		0,   0,   255,	// blue			3
		255, 255, 0,	// yellow		4
		0,   255, 255,	// light blue	5
		255, 0,   255,	// magenta		6
		255, 255, 255,	// white		7
		255, 128, 0,	// orange		8
		255, 153, 255,	// pink			9
		155, 48, 255,	// purple		10
	};
	//====================== 定义颜色数组 ======================

	//-------------- 遍历所有点,根据颜色数组循环赋色 --------------
	size_t max_i = cloud->points.size();
	for (size_t i = 0; i < max_i; i++)
	{
    
    
		//需要强制类型转换
		cloud->points[i].r = (uint8_t)colors[i % 10][0];
		cloud->points[i].g = (uint8_t)colors[i % 10][1];
		cloud->points[i].b = (uint8_t)colors[i % 10][2];
	}
	//=============== 遍历所有点,根据颜色数组循环赋色==============

	//------------------------ 保存点云 ------------------------
	pcl::io::savePCDFileBinary("colorfulBunny.pcd", *cloud);
	//======================== 保存点云 ========================

	return 0;
}

结果展示:
在这里插入图片描述


相关链接:

PCL 点云数据处理基础

猜你喜欢

转载自blog.csdn.net/weixin_46098577/article/details/122330201