【问题标题】:Reading binary file prints only zeros读取二进制文件只打印零
【发布时间】:2021-05-25 03:47:37
【问题描述】:

我正在尝试读取 3D 图像二进制文件并在 python 中显示 3D 图像的切片。格式为 int16 大端。文件扩展名为.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。而且图像是黑色的。我错过了什么?

【问题讨论】:

    标签: python numpy matplotlib 3d voxel


    【解决方案1】:

    我找到了解决方案。在 matplotlib 中,约定是 (z,x,y),这意味着较小的分辨率优先。所以这意味着有 326 个 401x401 2D 图像的投影。所以实际上体素值错位了。所以我有一个扭曲的形象。绘制体素值的直方图也很有帮助。刚刚使用 plt.hist(img_arry.ravel(),bins=50) 绘制直方图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 2023-03-03
      相关资源
      最近更新 更多