python:点云pcd与图像根据时间戳匹配

背景

在做激光点云方面任务时,总会遇到点云数据的解析清洗等相关的工作。对于激光点云通常为10HZ,而摄像头采集视频为30HZ,也就是说,点云与图像不能每一帧都能刚好对齐。

因此,引出我们今天的主题,如何去做到点云与图像的帧对齐?

  • 通过硬件时钟对齐,触发式同时采集点云跟图像。
  • 通过后处理,进行软同步。

python代码实现

这里给出一个解决方案,通过代码进行软同步,可以做到近似对齐。思想很简单,主要是根据每一帧点云的时间戳去找到最接近的图像时间戳。

class FileMatch:
    def __init__(self, dir_path):
        file_dir = glob.glob(dir_path + "/*")
        self.filepath = []
        for f in file_dir:
            if f.endswith(".bag"):
                pass
            else:
                self.filepath.append(f)

    def match(self):
        for i in self.filepath:
            pcd_file = os.listdir(i + '/pcd')
            img_file = os.listdir(i + "/jpg")
            # print(img_file)
            for pcd in pcd_file:
                print(i, pcd)
                pcd_prefix = os.path.splitext(pcd)[0]
                dict = {k:eval(pcd_prefix) - eval(os.path.splitext(k)[0]) for k in img_file}

                value = 0
                img_key, diff_val = min(dict.items(), key=lambda x: abs(value - x[1]))
                print(img_key, diff_val, pcd)
                src_img = os.path.join(i ,"jpg", img_key)
                new_img_path = i+"/img_new"
                if not os.path.exists(new_img_path):
                    os.makedirs(new_img_path)
                new_img = os.path.join(new_img_path, pcd_prefix + '.jpg')
                # print(src_img)
                # print(new_img)
                shutil.copyfile(src_img, new_img)

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
实时点数据转深度图需要使用到相机的内参矩阵和外参矩阵,以及点数据的坐标系和深度图像的坐标系之间的变换关系。如果你已经有了这些信息,可以使用 Python 中的 NumPy 和 OpenCV 库来实现实时点数据转深度图的功能。 以下是一个示例代码,假设你已经安装了 NumPy 和 OpenCV 库: ```python import numpy as np import cv2 # 相机内参矩阵 K = np.array([[fx, 0, cx], [0, fy, cy], [0, 0, 1]]) # 相机外参矩阵 R = np.array([[r11, r12, r13], [r21, r22, r23], [r31, r32, r33]]) t = np.array([tx, ty, tz]) T = np.hstack((R, t.reshape(3, 1))) P = np.dot(K, T) # 点数据坐标系到深度图像坐标系的变换矩阵 M = np.array([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]]) # 创建深度图像窗口 cv2.namedWindow('depth', cv2.WINDOW_NORMAL) # 循环读取点数据并转换为深度图像 while True: # 读取点数据 pcd_data = np.loadtxt('input.pcd', skiprows=10) # 将点数据转换为深度图像 pcd_data_homo = np.hstack((pcd_data, np.ones((pcd_data.shape[0], 1)))) pcd_data_cam = np.dot(P, pcd_data_homo.T).T pcd_data_img = np.dot(M, pcd_data_cam.T).T[:, :3] / pcd_data_cam[:, 2:] depth_img = np.zeros((h, w), dtype=np.float32) for i in range(pcd_data_img.shape[0]): x, y, z = pcd_data_img[i] if x >= 0 and x < w and y >= 0 and y < h and z > 0: depth_img[int(y), int(x)] = z # 显示深度图像 cv2.imshow('depth', depth_img) cv2.waitKey(1) ``` 这里假设你已经有了相机的内参矩阵和外参矩阵,以及点数据的坐标系和深度图像的坐标系之间的变换关系,分别存储在 K、R、t 和 M 矩阵中。代码中的循环读取点数据并转换为深度图像的过程可以实现实时转换。注意,在代码中我们使用了 OpenCV 库的 namedWindow 函数来创建深度图像窗口,并使用 waitKey 函数来等待按键事件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ywfwyht

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值