批量彩色图+深度图转点云文件(ply & pcd)

1、获取各图路径
2、生成点云文件
代码:

// C++ 标准库
#include <iostream>
#include <string>
#include <io.h>
#include <vector>
using namespace std;

// OpenCV 库
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

// PCL 库
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>

// 定义点云类型
typedef pcl::PointXYZRGBA PointT;
typedef pcl::PointCloud<PointT> PointCloud;

// 相机内参
const double camera_factor = 1000;
const double camera_cx = 325.5;
const double camera_cy = 253.5;
const double camera_fx = 518.0;
const double camera_fy = 519.0;

void getFiles(std::string path, std::vector<std::string>& files)
{
	intptr_t   hFile = 0;//intptr_t和uintptr_t的类型:typedef long int; typedef unsigned long int
	struct _finddata_t fileinfo;
	std::string p;
	if ((hFile = _findfirst(p.assign(path).append("/*").c_str(), &fileinfo)) != -1)//assign方法:先将原字符串清空,然后赋予新的值作替换。
	{
		do
		{
			if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
			{
				files.push_back(p.assign(path).append("/").append(fileinfo.name));
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}

// 主函数 
int main(int argc, char** argv)
{
	vector <string> files1;
	vector <string> files2;
	//需要读取的文件夹路径,使用单右斜杠“/”
	string filePath1 = "C:/Users/Administrator/Desktop/PointCloudPic/color";
	string filePath2 = "C:/Users/Administrator/Desktop/PointCloudPic/depth";
	//string filePath = "./datas"; //相对路径也可以
	getFiles(filePath1, files1);
	getFiles(filePath2, files2);
	for (int i = 0; i < files1.size(); i++) {
		// 图像矩阵
		cv::Mat rgb, depth;
		// 使用cv::imread()来读取图像
		// rgb 图像是8UC3的彩色图像
		// depth 是16UC1的单通道图像,注意flags设置-1,表示读取原始数据不做任何修改
		rgb = cv::imread(files1[i]);
		depth = cv::imread(files2[i], -1);

		// 点云变量
		// 使用智能指针,创建一个空点云。这种指针用完会自动释放。
		PointCloud::Ptr cloud(new PointCloud);
		// 遍历深度图
		for (int m = 0; m < depth.rows; m++)
			for (int n = 0; n < depth.cols; n++)
			{
				// 获取深度图中(m,n)处的值
				ushort d = depth.ptr<ushort>(m)[n];
				// d 可能没有值,若如此,跳过此点
				if (d == 0)
					continue;
				// d 存在值,则向点云增加一个点
				PointT p;

				// 计算这个点的空间坐标
				p.z = double(d) / camera_factor;
				p.x = (n - camera_cx) * p.z / camera_fx;
				p.y = (m - camera_cy) * p.z / camera_fy;

				// 从rgb图像中获取它的颜色
				// rgb是三通道的BGR格式图,所以按下面的顺序获取颜色
				p.b = rgb.ptr<uchar>(m)[n * 3];
				p.g = rgb.ptr<uchar>(m)[n * 3 + 1];
				p.r = rgb.ptr<uchar>(m)[n * 3 + 2];

				// 把p加入到点云中
				cloud->points.push_back(p);
			}
		int size = cloud->points.size();
		// 设置并保存点云
		cloud->height = 1;
		cloud->width = cloud->points.size();
		cloud->is_dense = false;
		string name = "C:/Users/Administrator/Desktop/PointCloudPic/pointcloud/" + to_string(i);

		pcl::io::savePLYFile(name + ".ply", *cloud);   //将点云数据保存为ply文件
		pcl::io::savePCDFile(name + ".pcd", *cloud);   //将点云数据保存为pcd文件
		// 清除数据并退出
		cloud -> points.clear();
		cout << "point cloud size = " << size << endl;
		cout << "NO." << i << " Done!" << "\n" << endl;
	}
	cout << "End" << endl;
	return 0;
}
点云换为深度图需要进行以下步骤: 1. 读取ply文件并将其换为点云数据结构。 2. 确定深度图的分辨率和范围。 3. 将点云中的每个点换为深度图像素坐标。 4. 将深度值分配给每个深度图像素。 以下是一个Python示例代码,可以将ply文件换为深度图: ```python import numpy as np import open3d as o3d from PIL import Image # 读取PLY文件并将其换为点云数据结构 pcd = o3d.io.read_point_cloud('input_cloud.ply') # 设置深度图的分辨率和范围 resolution = (512, 512) z_min = 0 z_max = 5 # 将点云中的每个点换为深度图像素坐标 depth_image = np.zeros(resolution, dtype=np.float32) for point in np.asarray(pcd.points): x, y, z = point i = int((x / z) * resolution[0] / 2 + resolution[0] / 2) j = int((y / z) * resolution[1] / 2 + resolution[1] / 2) if i >= 0 and i < resolution[0] and j >= 0 and j < resolution[1]: depth_image[j, i] = z # 将深度值分配给每个深度图像素 depth_image = (depth_image - z_min) / (z_max - z_min) * 255 depth_image = depth_image.astype(np.uint8) # 保存深度图 Image.fromarray(depth_image).save('output_depth.png') ``` 在此示例代码中,我们使用了Open3D库来读取PLY文件并将其换为点云数据结构。然后,我们定义了深度图的分辨率和范围,并将点云中的每个点换为深度图像素坐标。最后,我们将深度值分配给每个深度图像素,并将其保存为PNG文件。请注意,此示例代码仅适用于点云中没有重叠的情况。如果点云中存在重叠区域,则需要进行额外处理以避免深度图中的深度信息丢失。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值