如何将 3D 点云转换为深度图像?

How to convert a 3D point cloud to a depth image?

对于我的工作,我必须将点云转换为灰度(深度)图像,这意味着云中每个 XYZ 点的 z 坐标代表一个灰色阴影。为了将 Z 坐标从 [z_min、z_max] 区间映射到 [0..255] 区间,我使用了 Arduino 的映射函数:

float map(float x, float in_min, float in_max, float out_min, float out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; }

完成后,我需要将结果写入图像,问题是我拥有的云可以有数百万个点,所以我不能按顺序将它们 1 1 1 写入图像。假设我有 3000x1000 个订购的 XY 点。如果我想将它们写入 700x300 像素的图像,我该怎么办?我希望问题很清楚,提前感谢您的回答。

我已经设法找到解决问题的方法。这是一个很长的堆栈溢出算法,但请耐心等待。这个想法是将 XY 灰度点的矢量写为 pgm 文件。

第 1 步: cloud_to_greyscale - 将 XYZ 点云 转换为 矢量的函数XY灰度点数,接收一个cloud作为参数:

 for each point pt in cloud
     point_xy_greyscale.x <- pt.x
     point_xy_greyscale.y <- pt.y
     point_xy_greyscale.greyscale <- map(pt.z, z_min, z_max, 0, 255)
     greyscale_vector.add(point_xy_greyscale)
 loop

 return greyscale_vector

第 2 步: greyscale_to_image - 将先前返回的向量写入 greyscale_image 的函数,class 具有 width,一个height和一个_pixels成员通常对应一个unsigned short的二维数组。该函数接收以下参数:一个 greyscale_vector(将转换为图像)和一个 x_epsilon,这将帮助我们为我们的点划定 x 像素坐标,知道 x 点坐标是浮点数(因此不适合作为数组索引)。

一些背景信息:我在研究一种叫做 widop 的云,所以在我的 3D 中 space xwidthydepthzheight。另外值得注意的是,y 是一个 integer,所以对于我的问题,图像的 height 很容易找到:它是 y_max - y_min。要找到图像的 width,请按照下面的算法进行操作,如果不清楚,我会回答任何问题,我愿意接受建议。

img_width <- 0;    // image width
img_height <- y_max - y_min + 1    // image height

// determining image width
for each point greyscale_xy_point in greyscale_vector
    point_x_cell <- (pt.x - x_min) * x_epsilon * 10

    if point_x_cell > img_width
        img_width <- point_x_cell + 1
loop

// defining and initializing image with the calculated height and width
greyscale_img(img_width, img_height)

// initializing greyscale image points
for y <- 0 to greyscale_img.height
    for x <- 0 to greyscale_img.width
        greyscale_img[y][x] = 0
    loop
loop

// filling image with vector data
for each point point_xy_greyscale in greyscale_vector
    image_x = (point_xy_greyscale.x - x_min) * x_epsilon * 10
    image_y = point_xy_greyscale.y - y_min

    greyscale_image[image_y][image_x] = point_xy_greyscale.greyscale
loop

return greyscale_image

剩下要做的就是将图像写入文件,不过这很容易做到,只要找到前面link中与pgm格式相关的格式规则即可。我希望这对某人有所帮助。

EDIT_1: 我加了一张结果图。它应该是一条铁路,它相当暗的原因是有些物体很高,所以地面物体更暗。

depth image of railway