16位深度图(depth map)伪彩色化(pseudo colorize)的代码

本文记录将 16 位的 深度图伪彩色化的代码。
注意,DepthMapPseudoColorize 函数第一个参数可以是 图像的 data,也可以是 path。
在函数内部都可以。
在第二篇参考文献中,有 不同的色彩参数。

import numpy as np
import cv2
from PIL import Image

def DepthMapPseudoColorize(depth_map,output_path, max_depth = None, min_depth = None):
    # Load 16 units depth_map(element ranges from 0~65535),
    # Convert it to 8 units (element ranges frome 0~255) and pseudo colorize
    if isinstance(depth_map, np.ndarray):
        uint16_img =  depth_map
    # Indicate the argument load mode -1, otherwise loading default 8 units
    if isinstance(depth_map, str):
        uint16_img = cv2.imread(depth_map, -1)
    if None == max_depth:
        max_depth = uint16_img.max()
    if None == min_depth:
        min_depth = uint16_img.min()

    uint16_img -= min_depth
    uint16_img = uint16_img / (max_depth - min_depth)
    uint16_img *= 255

    # cv2.COLORMAP_JET, blue represents a higher depth value, and red represents a lower depth value
    # The value of alpha in the cv.convertScaleAbs() function is related to the effective distance in the depth map. If like me, all the depth values
    # in the default depth map are within the effective distance, and the 16-bit depth has been manually converted to 8-bit depth. , then alpha can be set to 1.
    im_color=cv2.applyColorMap(cv2.convertScaleAbs(uint16_img,alpha=1),cv2.COLORMAP_JET)
    #convert to mat png
    im=Image.fromarray(im_color)
    im.save(output_path)

调用实例:

input_dir = 'D:/Application/NonAcademic/Desktop/MC_Result/4_0214MC_loc/'
input_file_name = '1024_4_depth.png'

output_dir = 'D:/Application/NonAcademic/Desktop/MC_Result/4_0214MC_loc/0215depth_colorized/'
output_file_name = input_file_name

input_path = input_dir+input_file_name
output_path = output_dir+output_file_name
DepthMapPseudoColorize(input_path,output_path)

参考文章

[1] python 将16位 png 深度图转化为伪彩色图

[2] 深度图转伪彩色图(python)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

培之

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

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

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

打赏作者

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

抵扣说明:

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

余额充值