将读取的.raw文件转换为tensor张量送入网络

# coding:utf-8
import numpy as np
import torch

# 首先确定原图片的基本信息:数据格式,行数列数,通道数
rows=886#图像的行数
cols=492#图像的列数
patchsx = rows
patchsy = cols
batchsz = 16
channels =1# 图像的通道数,灰度图为1
path = r"C:\Users\wpx\Desktop\111.raw"
# 读取.raw文件到nparray类型
content = open(path, 'rb').read()
samples_ref = np.frombuffer(content, dtype='uint16').reshape((-1, 886, 492))
#创建一个类型为floap32的nparray类型,方便之后转换为tensor张量送入深度学习网络当中
batch_inp_np = np.zeros((1, patchsx, patchsy), dtype = 'float32')
# 将我们读取出来的raw文件内容放入到我们创建的文件当中
batch_inp_np[0, :, :] = np.float32(samples_ref[0,:, :]) * np.float32(1 / 65536)
# nparray -> torch转换类型
print("img",batch_inp_np.shape)
img_tensor = torch.from_numpy(batch_inp_np)
print("image_tensor",img_tensor.shape)

结果:

 

posted @ 2022-03-19 15:45  九叶草  阅读(212)  评论(0编辑  收藏  举报