在我的工作中,我必须将点云转换为灰度(深度)图像,这意味着点云中每个 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)
{ 返回 (x - in_min) * (out_max - out_min)/(in_max - in_min) + out_min; }
完成后,我需要将结果写入图像,问题是我拥有的云可以有数百万个点,所以我不能按顺序将它们 1 1 1 写入图像。假设我有 3000x1000 个订购的 XY 点。如果我想将它们写入 700x300 像素的图像,我该怎么办?我希望问题很清楚,提前感谢您的回答。
最佳答案
我已经设法找到解决问题的方法。这是一个很长的堆栈溢出算法,但请耐心等待。这个想法是写一个 XY 灰度点 vector 作为 pgm文件。
第 1 步: cloud_to_greyscale
- 将 XYZ 点云 转换为 XY 灰度点 vector 的函数> 并接收一个 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
- 将先前返回的 vector 写入 greyscale_image
的函数,该类具有 width
,一个height
和一个_pixels
成员,通常对应一个unsigned short
的二维数组。该函数接收以下参数:一个 greyscale_vector
(将转换为图像)和一个 x_epsilon
,它将帮助我们界定 x
像素坐标对于我们的点,知道 x
点坐标是 float (因此不适合作为数组索引)。
一些背景信息:我在研究一种叫做 widop 的云,所以在我的 3D 空间中 x
是 width
,y
是深度
,z
是高度
。另外值得注意的是 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
剩下要做的就是将图像写入文件,但这很容易做到,您可以在前面与pgm格式相关的链接中找到格式规则。我希望这对某人有所帮助。
EDIT_1:我添加了一张结果图片。它应该是一条铁路,它相当暗的原因是有些物体很高,所以地面物体更暗。
关于c++ - 如何将 3D 点云转换为深度图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37023162/