深度图上色,深度图raw文件转为png,如何读取深度图raw文件?

发布于:2024-05-01 ⋅ 阅读:(219) ⋅ 点赞:(0)

raw文件,要知道宽、高、通道数、数据类型,就能顺利转化,下面是转化代码:


import numpy as np
import cv2

# 图像的基本信息
width = 640  # 图像宽度
height = 480  # 图像高度
channels = 1  # 图像通道数,例如3表示RGB
dtype = 'uint16'  # 数据类型,根据实际情况可能是'uint8'或'uint16'等

# 使用numpy从RAW文件读取数据
with open('Depth_1714481422987_0.raw', 'rb') as f:
    img_data = np.fromfile(f, dtype=np.uint16)

# 根据图像尺寸重塑数组
img = img_data.reshape(height, width, channels)

# 转换为 uint8
img_uint8 = (img / 256).astype('uint8')

# 转换为灰度图像
# apply colormap on depth image(image must be converted to 8-bit per pixel first)
im_color = cv2.applyColorMap(cv2.convertScaleAbs(img_uint8, alpha=15), cv2.COLORMAP_JET)

# 保存图像
cv2.imwrite('Depth_1714481422987_0_color.png', im_color)

# 显示图像
cv2.imshow('Colored Depth Image', im_color)
cv2.waitKey(0)
cv2.destroyAllWindows()

一般jpg图的转换:

import numpy as np
import cv2

# 图像的基本信息
width = 640  # 图像宽度
height = 480  # 图像高度
channels = 3  # 图像通道数,例如3表示RGB
dtype = 'uint8'  # 数据类型,根据实际情况可能是'uint8''uint16'等

# 使用numpy从RAW文件读取数据
with open('Color_1714481423037_1.raw', 'rb') as f:
    img_data = np.fromfile(f, dtype=dtype)

# 根据图像尺寸重塑数组
img = img_data.reshape(height, width, channels)

# 如果图像数据是16位但cv2.imshow只支持8位,需要转换
if dtype == 'uint16' and cv2.__version__.startswith('3'):
    img = (img / 256).astype('uint8')

cv2.imwrite('Color_1714481423037_1.png', img)

# 显示图像
cv2.imshow('RAW Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()


网站公告

今日签到

点亮在社区的每一天
去签到