读取raw格式数据并展示

目的:读取X光的raw数据,用于图像处理

RAW数据保存的是深度图,数值的范围为[0, 65535]即2byte。正常图像的范围是[0-255]。

切记不要直接读取为uint8!!

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt


raw_file_path = '003RU11-1.raw'
# 从缓冲区中读取原始 16 位深度数据
with open(raw_file_path, 'rb') as f:
    raw_data = f.read()

width, height = 2560, 3072  # 图像的宽度和高度

# 将原始数据转换为 NumPy 数组
image = np.frombuffer(raw_data, dtype=np.uint16).reshape((height, width))

# 归一化到 0-255 的范围
image_normalized = ((image - image.min()) * (255.0 / (image.max() - image.min()))).astype(np.uint8)

# 使用 PIL 创建图像对象
image_8bit = Image.fromarray(image_normalized)

# 显示图像
plt.figure()
plt.title("Normalized 8-bit Image")
plt.imshow(image_8bit, cmap='gray')
plt.axis('off')
plt.show()

参考:读取raw格式数据,OpenCV显示_opencv查看raw图尺寸-CSDN博客