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

python 将16位 png 深度图转化为伪彩色图:

全部代码如下:

import cv2
import os.path
import glob
import numpy as np
from PIL import Image

PNG_FILE = 'F:\\shendu_buquan_jieguo\\1\\kitti\\*.png'
TARGET_FILE = 'F:\\shendu_buquan_jieguo_color\\1\\kitti'
def convertPNG(pngfile,outdir):
    #读取16位深度图(像素范围0~65535),并将其转化为8位(像素范围0~255)
    uint16_img = cv2.imread(pngfile, -1)    #在cv2.imread参数中加入-1,表示不改变读取图像的类型直接读取,否则默认的读取类型为8位。
    uint16_img -= uint16_img.min()
    uint16_img = uint16_img / (uint16_img.max() - uint16_img.min())
    uint16_img *= 255
    #使得越近的地方深度值越大,越远的地方深度值越小,以达到伪彩色图近蓝远红的目的。
    uint16_img = 255 - uint16_img

    # cv2 中的色度图有十几种,其中最常用的是 cv2.COLORMAP_JET,蓝色表示较高的深度值,红色表示较低的深度值。
    # cv.convertScaleAbs() 函数中的 alpha 的大小与深度图中的有效距离有关,如果像我一样默认深度图中的所有深度值都在有效距离内,并已经手动将16位深度转化为了8位深度,则 alpha 可以设为1。
    im_color=cv2.applyColorMap(cv2.convertScaleAbs(uint16_img,alpha=1),cv2.COLORMAP_JET)
    #convert to mat png
    im=Image.fromarray(im_color)
    #save image
    im.save(os.path.join(outdir,os.path.basename(pngfile)))
    print(os.path.basename(pngfile))

for pngfile in glob.glob(PNG_FILE):#C:/Users/BAMBOO/Desktop/source pics/rgbd_6/depth/*.png
    convertPNG(pngfile, TARGET_FILE)#C:/Users/BAMBOO/Desktop/source pics/rgbd_6/color

参考教程:

1、深度图转伪彩色图(python)
2、Python OpenCV读取16位单通道图像并转换为8位灰度图
3、python opencv 4.1.0 cv2.convertScaleAbs()函数
4、cv2.convertScaleAbs(depth_image,alpha=0.03)

  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值