PCL: Point Cloud Coloring | Customize the color of any point in the point cloud

1 Point cloud of type PointXYZRGB in PCL

1.1 r\g\b type of PointXYZRGB type point cloud in PCL

The r\g\b type of the PointXYZRGB type point cloud in PCL is unsigned char, and the code is verified below.

Code:

#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;
}

Output result:

insert image description here
In C++, the stdint.h file defines unsigned charthe alias asuint8_t

insert image description here

1.2 The numerical range of r\g\b of PointXYZRGB type point cloud in PCL

We already know that the r\g\b type of the PointXYZRGB type point cloud is unsigned char, if you directly output r\g\b, the result is a character;

To see its color range, you need to cast

Code:

#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;
}

Output result:

->加载点云详情:
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

As can be seen from the output results, the color range is 0~255

2 Customize the color of each point in the point cloud

Similarly, when passing a color value of type int to r\g\b, a forced type conversion is also required, (uint8_t)just .

2.1 Single color assignment

Make the point cloud red.

Code:

#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;
}

Results show:
insert image description here

2.2 Color any point according to the given color array

A custom color array to assign color to any point in the point cloud. Only 10 colors are given here, if you need more colors, please refer to the RGB color comparison table

Code:

#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;
}

Results show:
insert image description here


Related Links:

PCL point cloud data processing basics

Guess you like

Origin blog.csdn.net/weixin_46098577/article/details/122330201