赞
踩
一般情况下,有python的三方库的软件,可以读RGB二进制的Raw数据,生成Numpy数据。比如说OpenCV之类的。但是,我们也可以使用python的裸代码,得到Numpy。
知识点:
代码例子如下:
#!/usr/bin/env python # encoding: utf-8 import os import numpy as np def read_raw_to_rgb(raw_path, width, height, channel): """ 读取raw数据转换成 RGB 的numpy 格式的数据 """ assert os.path.exists(raw_path), r'输入的文件必须存在' raw_data = open(raw_path, 'rb').read() raw_data_shape = (width, height, channel) np_raw_data = np.frombuffer(raw_data, dtype=np.uint8) reshaped_data = np_raw_data.reshape(raw_data_shape) return reshaped_data def convert_rgb_to_rgba(rgb_data, alpha): """ 将 RGB 数据扩充成 RGBA 的数据""" r = rgb_data[:, :, 0] g = rgb_data[:, :, 1] b = rgb_data[:, :, 2] a = np.zeros(r.shape, np.uint8) a[:, :] = alpha rgba_data = np.dstack((r, g, b, a)) return rgba_data def create_alpha_data_according_rgb_data(rgb_data, alpha): """ 根据rgb_data的维度创建alpha_data""" r = rgb_data[:, :, 0] a = np.zeros(r.shape, np.double) a[:, :] = alpha return a vr_path = r'D:\temp\result\vr_512_512.raw' sr_path = r'D:\temp\result\sr_512_512.raw' vr_rgb_data = read_raw_to_rgb(vr_path, 512, 512, 3) vr_alpha_data = create_alpha_data_according_rgb_data(vr_rgb_data, 0.1) sr_rgb_data = read_raw_to_rgb(sr_path, 512, 512, 3) sr_alpha_data = create_alpha_data_according_rgb_data(sr_rgb_data, 0.9) print(512*512*4) vr_rgb_data_image_idx = vr_rgb_data[np.newaxis, :, :, :] sr_rgb_data_image_idx = sr_rgb_data[np.newaxis, :, :, :] rgb_data_array = np.concatenate((vr_rgb_data_image_idx, sr_rgb_data_image_idx), axis=0) print(rgb_data_array.shape) vr_alpha_data_image_idx = vr_alpha_data[np.newaxis, :, :] sr_alpha_data_image_idx = sr_alpha_data[np.newaxis, :, :] alpha_data_array = np.concatenate((vr_alpha_data_image_idx, sr_alpha_data_image_idx), axis=0) print(alpha_data_array.shape)
测试环境中的图像,可以使用 OpenCV读取一张图, 将numpy保存到文件中。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。