深度图转伪彩色图(python)

kinect得到的深度图灰不拉几,人眼很难识别出其中的物体,感知深度的变化。

在做展示的时候,我们往往需要可视化,使用OpenCV的预定义的颜色映射来将灰度图像伪彩色化,在OpenCV中使用applycolormap(伪彩色函数)。

 

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

def convertPNG(pngfile,outdir):
    # READ THE DEPTH
    im_depth = cv2.imread(pngfile)
    #apply colormap on deoth image(image must be converted to 8-bit per pixel first)
    im_color=cv2.applyColorMap(cv2.convertScaleAbs(im_depth,alpha=15),cv2.COLORMAP_JET)
    #convert to mat png
    im=Image.fromarray(im_color)
    #save image
    im.save(os.path.join(outdir,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

代码里写得挺清楚了,深度图转换伪彩色图的scale可以通过alpha的数值调整。

得到的结果如下

转换之前:

转换之后

 

posted @ 2018-11-07 11:16  Bamboo123  阅读(14465)  评论(1编辑  收藏  举报