找了半天网上都没有开源的根据距离进行点云渐变色上色的代码,所以写一个方便自己和大家的使用~
渐变色生成代码
- 输入点云shape为
[N,3]
,我这里使用的是nuScenes数据集,生成的点云使用到中心点的距离作为参数,进行渐变,输出的为[x,y,z,r,g,b]
格式的数组,shape为[N,6]
,其余的代码通过各种demo即可实现。
def gradient_point_cloud_color_map(points):
# 根据距离生成色彩
colors = np.zeros([points.shape[0], 3])
# 使用x,y计算到中心点的距离
dist = np.sqrt(np.square(points[:,0]) + np.square(points[:,1]))
dist_max = np.max(dist)
print(f"dist_max: {dist_max}")
# 调整渐变半径
dist = dist / 51.2 # 我这里的半径是51.2m,
# dist = dist / 2
# RGB
min = [127,0,255] # 紫色
max = [255,255,0] # 黄色
# 最近处为紫色
# colors[:,0] = 127
# colors[:,2] = 255
# 减R(127 -> 0),加G(0->255),再减B(255->0),再加R(0 -> 255)
# 127+255+255+255
all_color_value = 127+255+255+255
dist_color = dist * all_color_value
# 减R (127 -> 0)
clr_1 = 127
dy_r = 127-dist_color
tmp = np.zeros([colors[dist_color<clr_1].shape[0], 3])
tmp[:, 0] = dy_r[dist_color<clr_1]
tmp[:, 1] = 0
tmp[:, 2] = 255
colors[dist_color<clr_1] = tmp
# 加G (0->255)
clr_2 = 127+255
dy_g = dist_color-clr_1
tmp = np.zeros([colors[(dist_color>=clr_1) & (dist_color<clr_2)].shape[0], 3])
tmp[:, 0] = 0
tmp[:, 1] = dy_g[(dist_color>=clr_1) & (dist_color<clr_2)]
tmp[:, 2] = 255
colors[(dist_color>=clr_1) & (dist_color<clr_2)] = tmp
# 减B (255->0)
clr_3 = 127+255+255
dy_b = dist_color-clr_2
tmp = np.zeros([colors[(dist_color>=clr_2) & (dist_color<clr_3)].shape[0], 3])
tmp[:, 0] = 0
tmp[:, 1] = 255
tmp[:, 2] = dy_b[(dist_color>=clr_2) & (dist_color<clr_3)]
colors[(dist_color>=clr_2) & (dist_color<clr_3)] = tmp
# 加R(0 -> 255)
clr_4 = 127+255+255+255
dy_r = dist_color-clr_3
tmp = np.zeros([colors[(dist_color>=clr_3) & (dist_color<clr_4)].shape[0], 3])
tmp[:, 0] = dy_r[(dist_color>=clr_3) & (dist_color<clr_4)]
tmp[:, 1] = 255
tmp[:, 2] = 0
colors[(dist_color>=clr_3) & (dist_color<clr_4)] = tmp
'''
'''
# 外围都为黄色
tmp = np.zeros([colors[dist_color>clr_4].shape[0], 3])
tmp[:, 0] = 255
tmp[:, 1] = 255
tmp[:, 2] = 0
colors[dist_color>clr_4] = tmp
points = np.concatenate((points[:,:3], colors),axis=1)
return points
最终效果
参考文章:
- 根据点云高度赋色(附open3d python代码)