繁体   English   中英

如何加速 numpy for 循环,根据强度为点云着色

[英]how to accelerate the numpy for-loop, for coloring point-cloud by its intensity

在此处输入代码我想根据强度为点云着色。

目前,我使用以下 for 循环将颜色图 function应用于点的强度(第 4 维):

import numpy as np
points = np.random.random([128*1200,4])
points_colors = np.zeros([points.shape[0], 3])

for idx, p_c in enumerate(points[:, 3]):
    points_colors[idx, :] = color_map(p_c)
points_colors /= 255.0

颜色映射 function 的示例:

def color_map( value, minimum=0, maximum=255):
    minimum, maximum = float(minimum), float(maximum)
    ratio = 2 * (value-minimum) / (maximum - minimum)
    b = int(max(0, 255*(1 - ratio)))
    r = int(max(0, 255*(ratio - 1)))
    g = 255 - b - r
    return r, g, b

给点云上色比直接使用open3d的原始colormap(即按点的x,y,z-pose上色)消耗的时间要多得多

我怎样才能通过它的强度来加速颜色映射点云的过程?

也欢迎其他不将 xyzi-point-cloud 转换为 xyzrgb-point-cloud 的解决方案。


附言。 我实际使用的 color_map 有点复杂,但具有相同的 output:

def rainbow_color_map(
    val,
    minval = 0
    maxval=256,
    normalize=False,
    colors=[(1, 1, 255), (1, 255, 1), (255, 1, 1)] * 10,
):
    i_f = float(val - minval) / float(maxval - minval) * (len(colors) - 1)

    i, f = int(i_f // 1), i_f % 1  # Split into whole & fractional parts.

    (r1, g1, b1), (r2, g2, b2) = colors[i], colors[i + 1]
    if normalize:
        return (
            (r1 + f * (r2 - r1)) / maxval,
            (g1 + f * (g2 - g1)) / maxval,
            (b1 + f * (b2 - b1)) / maxval,
        )
    else: 
        return r1 + f * (r2 - r1), g1 + f * (g2 - g1), b1 + f * (b2 - b1)

您可以修改 function 以在不使用循环的情况下计算整个数组:

def color_map(minimum, maximum, value):
    minimum, maximum = float(minimum), float(maximum)
    ratio = 2 * (value-minimum) / (maximum - minimum)
    
    b = 255*(1 - ratio)
    b[b<0] = 0
    b = b.astype(int)
    
    r = 255*(ratio - 1)
    r[r<0] = 0
    r = r.astype(int)
    
    g = 255 - b - r
    
    points_colors = np.c_[r, g, b]
    
    return points_colors

然后像这样调用 function:

import numpy as np
points = np.random.random([128*1200,4])
minimum, maximum = np.min(points[:, 3]), np.max(points[:, 3])
points_colors = color_map(minimum, maximum, points[:, 3])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM