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

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()

相关推荐

  1. 深度学习中读取索引并转成tensor

    2024-05-01 15:58:01       39 阅读
  2. 基于深度学习的深度预测

    2024-05-01 15:58:01       30 阅读
  3. 【0261】pg内核 raw parsetree 深入分析(一)

    2024-05-01 15:58:01       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-01 15:58:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-01 15:58:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-01 15:58:01       87 阅读
  4. Python语言-面向对象

    2024-05-01 15:58:01       96 阅读

热门阅读

  1. 【QT教程】QT6图形渲染与OpenGL编程

    2024-05-01 15:58:01       26 阅读
  2. Podman与Docker有何不同?

    2024-05-01 15:58:01       37 阅读
  3. uni框架下的前端小知识

    2024-05-01 15:58:01       31 阅读
  4. 代码随想录学习Day 32

    2024-05-01 15:58:01       32 阅读
  5. 学习使用js给指定日期加减指定天数

    2024-05-01 15:58:01       31 阅读
  6. 关于react native文件路径的烦心事

    2024-05-01 15:58:01       37 阅读
  7. 认识产品经理

    2024-05-01 15:58:01       30 阅读
  8. 软设之进程的状态

    2024-05-01 15:58:01       26 阅读