问题描述
我正在尝试读取 3D 图像二进制文件并在 python 中显示 3D 图像的切片。格式为 int16 和 big endian。文件扩展名是 .rec
。我有以下一段代码:
struct_fmt = '>h' # big-endian,signed int16
struct_len = struct.calcsize(struct_fmt)
struct_unpack = struct.Struct(struct_fmt).unpack_from
results = []
with open('test.rec',"rb") as f:
while True:
data = f.read(struct_len)
if not data: break
s = struct_unpack(data)
results.append(s)
img3d = np.array(results)
img3d = img3d.reshape(401,401,326)
np.save('output.npy',img3d)
image = np.load('output.npy')
fig,ax=plt.subplots()
output_slice=image[:,190,:]
ax.imshow(output_slice,cmap='gray')
ax.axis('off')
plt.show()
但我得到所有体素值都为 0。图像是黑色的。我错过了什么?
解决方法
我找到了解决方案。在 matplotlib 中,约定是 (z,x,y),这意味着较小的分辨率优先。所以这意味着有 326 个 401x401 2D 图像的投影。所以实际上体素值被放错了位置。所以我有一个扭曲的图像。绘制体素值的直方图也有很大帮助。刚刚使用 plt.hist(img_arry.ravel(),bins=50) 绘制直方图。