RGB to Depth映射

//相机内参
struct Intrinsic{
  int rows, cols;
  float cx, cy, fx, fy;
};

//相机外参R,T
struct Extrinsic{
  glm::mat3 rotation;
  glm::vec3 translation;
};

//图像帧
struct Frame{
  int rows, cols;
  int size;
  void *data;
};

//BGR像素
struct BGR888Pixel{
  uint8_t b, g, r;
};

//利用相机内参先计算出索引表,为后续点云计算提供预计算。
float* GenerateXyTable(const Intrinsic& intrinsic) {
	if (intrinsic.fx == 0 || intrinsic.fy == 0) {
		printf("camera intrinsic parameters error.");
		return nullptr;
	}

	float* xy_table = new float[intrinsic.rows * intrinsic.cols * 2];
	float fx_recip = 1.0f / intrinsic.fx;
	float fy_recip = 1.0f / intrinsic.fy;

	for (int h = 0; h < intrinsic.rows; h++){
		for (int w = 0; w < intrinsic.cols; w++) {
			int idx = h * intrinsic.cols + w;
			xy_table[2 * idx] = ((float)w - intrinsic.cx) * fx_recip;
			xy_table[2 * idx + 1] = ((float)h - intrinsic.cy) * fy_recip;
		}
	}
	return xy_table;
}

//彩色图映射到深度图空间,结果彩色图大小跟深度图大小一致
int AlignColorToDepthSpace(Frame& aligned_color,
	const Frame& depth_map,
	const Frame& color_img,
	const Intrinsic& rgb_intri,
	const glm::mat3& rotation,
	const glm::vec3& translation,
	float* xy_table){
	if (xy_table == nullptr || aligned_color.data == nullptr || depth_map.data == nullptr) 
	  return 3;
	memset(aligned_color.data, 0, aligned_color.size);
	
        //提供zbuf缓存,用于处理深度值冲突:取深度值小的像素为有效像素
	int* zbuf = new int[color_img.cols * color_img.rows];
	memset(zbuf, 0, color_img.cols * color_img.rows* sizeof(int));
	for (int idx = 0; idx <depth_map.cols * depth_map.rows; idx++) {
		float depth = static_cast<float>(((uint16_t*)depth_map.data)[idx]);
		if (depth == 0) continue;
		glm::vec3 point{ xy_table[2 * idx] * depth, xy_table[2 * idx + 1] * depth, depth };
		glm::vec3 point_world = rotation * point + translation;
		if (point_world.z == 0) continue;
		int u = static_cast<int>((point_world.x / point_world.z) * rgb_intri.fx + rgb_intri.cx);
		int v = static_cast<int>((point_world.y / point_world.z) * rgb_intri.fy + rgb_intri.cy);
		if (u < 0 || u >= color_img.cols || v < 0 || v >= color_img.rows)
			continue;

		int zbuf_idx = v * color_img.cols + u;
		if (zbuf[zbuf_idx] == 0) zbuf[zbuf_idx] = idx + 1;
		else if (depth < static_cast<float>(((uint16_t*)depth_map.data)[zbuf[zbuf_idx] - 1])) zbuf[zbuf_idx] = idx + 1;
	}

	BGR888Pixel* aligned_color_ptr = static_cast<BGR888Pixel*>(aligned_color.data);
	BGR888Pixel* color_ptr = static_cast<BGR888Pixel*>(color_img.data);
	for (int i = 0; i < color_img.cols * color_img.rows; i++) {
		if (zbuf[i] == 0) continue;
                //从索引取出对应的彩色像素用于填充原深度图尺寸的彩色图映射图
		aligned_color_ptr[zbuf[i] - 1] = color_ptr[i];
	}
	delete[] zbuf;
	return 0;
}

"depth-to-image" 是将深度图转换为图像的过程,其中深度图包含了场景中每个像素点的距离信息。这个过程通常涉及将距离值映射到对应的图像像素值,以便可视化或进行其他图像处理任务。 以下是一些 depth-to-image 的应用举例: 1. 深度感知和虚拟现实:将深度图转换为图像可以帮助感知场景的距离和深度信息。这对于虚拟现实、增强现实等应用非常重要,可以用于生成虚拟物体的遮挡效果、景深效果或深度感知交互。 2. 三维重建和场景理解:通过将深度图转换为图像,可以更直观地理解场景的三维结构和形状。这对于三维重建、场景理解、机器人导航等任务非常有用,可以将深度信息转化为可视化的图像表示,帮助分析和处理场景数据。 3. 物体检测和分割:将深度图转换为图像可以辅助物体检测和分割任务。通过将深度信息与RGB图像结合起来,可以提供更准确的物体边界、形状和深度信息,有助于改善物体检测和分割的结果。 4. 深度图像处理和修复:深度图转换为图像后,可以应用各种图像处理技术,如滤波、增强、纹理合成等。这些处理可以用于深度图像的去噪、边缘增强、缺失数据填充等任务,提高深度图像的质量和可用性。 5. 人机交互和手势识别:通过将深度图转换为图像,可以实现基于深度的人机交互和手势识别。通过分析深度信息,可以捕捉用户的姿态、手势等信息,实现自然的人机交互和手势控制。 这些只是 depth-to-image 的一些应用举例,深度图转换为图像可以在多个领域中发挥重要作用,包括计算机视觉、虚拟现实、机器人技术等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值