【解析rosbag可视化】二维图像可视化点云

前言

  • 一般我们把采集的原始数据放在 rosbag 中。我们要可视化需要对 bag 包解析
  • 二维图像可视化点云思路:在二维图片显示投影的点云就行了

一、环境配置

  • 我用的 python 3.7
  • pip install --extra-index-url https://rospypi.github.io/simple/ rosbag ---- 安装 rosbag 库
  • pip install sensor_msgs --extra-index-url https://rospypi.github.io/simple/ ---- 安装 sensor_msgs 库 网络不稳定有可能中断 多安装几次就好了

二、代码

整体代码很简单,直接上代码。

import cv2
import numpy as np
import rosbag
import sensor_msgs.point_cloud2 as pc2


def pointsToBev(points, imgW=540, imgH=1080, minY=-16, maxY=16, minX=-20, maxX=34, show=False):
    img = np.zeros((imgH, imgW, 3), np.uint8)
    ratioW = imgW / (maxY - minY)
    ratioH = imgH / (maxX - minX)
    for i, pt in enumerate(points):
        x, y, *other = pt
        v = imgH - (x - minX) * ratioH
        u = imgW - (y - minY) * ratioW
        u, v = int(u), int(v)
        if (u >= 0 and u < imgW) and (v >= 0 and v < imgH):
            img[v, u] = [255, 255, 255]  # 点云显示白色
    if show:
        cv2.imwrite("test.jpg", img)
        cv2.imshow("bev", img)
        cv2.waitKey(0)
    return img


def get_iter(path, topic):
    bag_data = rosbag.Bag(path, "r")
    iter_points_data = bag_data.read_messages(topics=topic)
    return iter_points_data


if __name__ == '__main__':
    bag_path = "test.bag"  # bag包路径
    topic_name = "/lidar_points"  # 话题名
    points_iter = get_iter(bag_path, topic_name)  # 生成器 可迭代对象
    while True:
        try:
            topic, msg, t = next(points_iter)  # 每次取一帧,节省内存空间
            pcd = pc2.read_points(msg)
            pcd = np.array(list(pcd))
            pcd = list(np.delete(pcd, np.where(np.isnan(pcd))[0], axis=0))
            data = np.array(pcd)
            pointsToBev(data, show=True)
        except StopIteration:
            # 遇到 StopIteration 停止迭代
            print("over")
            break

三、效果图

在这里插入图片描述

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

读书猿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值