栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

批量把彩色图和深度图转换为彩色点云文件(C++)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

批量把彩色图和深度图转换为彩色点云文件(C++)

1、单个彩色图和深度图转换为彩色点云文件(C++):

全部代码如下:

// C++ 标准库
#include 
#include 
using namespace std;
 
// OpenCV 库
#include 
#include 
 
// PCL 库
#include 
#include 
#include 
 
// 定义点云类型
typedef pcl::PointXYZRGBA PointT;
typedef pcl::PointCloud 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;
 
// 主函数 
int main(int argc, char ** argv)
{
	// 图像矩阵
	cv::Mat rgb, depth;
	// 使用cv::imread()来读取图像
	// rgb 图像是8UC3的彩色图像
	// depth 是16UC1的单通道图像,注意flags设置-1,表示读取原始数据不做任何修改
	rgb = cv::imread("C:\Users\Administrator\Desktop\color.png");
	depth = cv::imread("C:\Users\Administrator\Desktop\depth.png", -1);  //在cv::imread参数中加入-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(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(m)[n * 3];
			p.g = rgb.ptr(m)[n * 3 + 1];
			p.r = rgb.ptr(m)[n * 3 + 2];
 
			// 把p加入到点云中
			cloud->points.push_back(p);
		}
	// 设置并保存点云
	cloud->height = 1;
	cloud->width = cloud->points.size();
	cout << "point cloud size = " << cloud->points.size() << endl;
	cloud->is_dense = false;
	pcl::io::savePLYFile("C:\Users\Administrator\Desktop\ply.ply", *cloud);   //将点云数据保存为ply文件
	pcl::io::savePCDFile("C:\Users\Administrator\Desktop\pcd.pcd", *cloud);   //将点云数据保存为pcd文件
	// 清除数据并退出
	cloud->points.clear();
	cout << "Point cloud saved." << endl;
	return 0;
}

2、批量把彩色图和深度图转换为彩色点云文件(C++):

全部代码如下:

#include 
#include 
using namespace std;
#include 
#include 
#include "dirent.h"

#include 
#include 


// OpenCV 库
#include 
#include 

// PCL 库
#include 
#include 
#include 

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

// 相机内参
const double camera_factor = 256;
const double camera_cx = 989.6663020916587;
const double camera_cy = 594.9904177802305;
const double camera_fx = 1400.660521866737;
const double camera_fy = 1397.477295771064;

void getFiles(string path, vector& files)
{
	//文件句柄  
	//在 win7 下定义的文件句柄类型是 long 型,在 win10 下要改为 intptr_t 类型
    //long hFile = 0;  //win7
	intptr_t hFile = 0;  //win10
	//文件信息,声明一个存储文件信息的结构体  
	struct _finddata_t fileinfo;
	string p;//字符串,存放路径
	if ((hFile = _findfirst(p.assign(path).append("\*").c_str(), &fileinfo)) != -1)//若查找成功,则进入
	{
		do
		{
			//如果是目录,迭代之(即文件夹内还有文件夹)  
			if ((fileinfo.attrib & _A_SUBDIR))
			{
				//文件名不等于"."&&文件名不等于".."
				//.表示当前目录
				//..表示当前目录的父目录
				//判断时,两者都要忽略,不然就无限递归跳不出去了!
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
					getFiles(p.assign(path).append("\").append(fileinfo.name), files);
			}
			//如果不是,加入列表  
			else
			{
				files.push_back(p.assign(path).append("\").append(fileinfo.name));
			}
		} while (!_findnext(hFile, &fileinfo));
		//_findclose函数结束查找
		_findclose(hFile);
	}
}

// 主函数 
int main(int argc, char** argv)
{
	std::string path_1 = "F:\shu_ju\selection\2021-03-03-15-17-24\image2";
	std::string path_2 = "F:\shu_ju\selection\2021-03-03-15-17-24\buquan_cropped";
	std::vector files_1;
	std::vector files_2;
	getFiles(path_1, files_1);  //获取指定目录内的所有文件的路径
	getFiles(path_2, files_2);
	cout << "files rgb size: " << files_1.size() << ", files depth size: " << files_2.size() << endl;
	
	for (int i = 0; i < files_1.size(); ++i) {
		std::cout << files_1[i] << std::endl;
		std::cout << files_2[i] << std::endl;

		// 图像矩阵
		cv::Mat rgb, depth;
		// 使用cv::imread()来读取图像
		// rgb 图像是8UC3的彩色图像
		// depth 是16UC1的单通道图像,注意flags设置-1,表示读取原始数据不做任何修改
		//rgb = cv::imread("F:\shu_ju\selection\2021-03-03-15-17-24\image2\1614755848.902324.png");
		//depth = cv::imread("F:\shu_ju\selection\2021-03-03-15-17-24\buquan_cropped\0000000000.png", -1);
		//rgb = cv::imread(path_1 + "\" + files_1[i]);
		//depth = cv::imread(path_2 + "\" + files_2[i], -1);
		rgb = cv::imread(files_1[i]);
		depth = cv::imread(files_2[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(m)[n];
				// d 可能没有值,若如此,跳过此点
				bool flag = (d > 0);
				if (!flag)
					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(m)[n * 3];
				p.g = rgb.ptr(m)[n * 3 + 1];
				p.r = rgb.ptr(m)[n * 3 + 2];

				// 把p加入到点云中
				cloud->points.push_back(p);
			}
		// 设置并保存点云
		cloud->height = 1;
		cloud->width = cloud->points.size();
		cout << "point cloud size = " << cloud->points.size() << endl;
		cloud->is_dense = false;
		//pcl::io::savePLYFile("C:\Users\Administrator\Desktop\ply.ply", *cloud);   //将点云数据保存为ply文件
		pcl::io::savePCDFile("F:\shu_ju\selection\2021-03-03-15-17-24\RGBD\" + files_2[i].substr(55,10) +".pcd", *cloud);   //将点云数据保存为pcd文件
		// 清除数据并退出
		cloud->points.clear();
		cout << "Point cloud saved." << endl;
	}
	system("pause");
	return 0;
}

参考文献:
1、三维重建_彩色图和深度图转点云文件、ply和pcd相互转换、点云合并
2、C++ 获取文件夹下的所有文件名

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/429750.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号