Python读取和转换原始3D图像文件

3

我正在处理原始格式的CT扫描医学图像。它基本上是一个由体素组成的三维矩阵(512 * 512 * 切片数)。我想将文件的每个切片提取到单独的文件中。

import numpy as np
import matplotlib.pyplot as plt

# reading the raw image into a string. The image files can be found at:
# https://grand-challenge.org/site/anode09/details/
f = open('test01.raw', 'rb')
img_str = f.read()

# converting to a uint16 numpy array
img_arr = np.fromstring(img_str, np.uint16)

# get the first image and plot it
im1 = img_arr[0:512*512]
im1 = np.reshape(im1, (512, 512))
plt.imshow(im1, cmap=plt.cm.gray_r)
plt.show()

结果看起来肯定像是胸部CT扫描,但图像的质地很奇怪,好像像素错位了一样。
相关信息可能在关联的.mhd信息文件中,但我不确定应该在哪里查找:
ObjectType = Image
NDims = 3
BinaryData = True
BinaryDataByteOrderMSB = False
CompressedData = False
TransformMatrix = 1 0 0 0 1 0 0 0 1
Offset = 0 0 0
CenterOfRotation = 0 0 0
AnatomicalOrientation = RPI
ElementSpacing = 0.697266 0.697266 0.7
DimSize = 512 512 459
ElementType = MET_SHORT
ElementDataFile = test01.raw

你说的“错位”是什么意思?它看起来像一张腹部CT切片。你尝试过调整图像的窗宽吗? - john elemans
1个回答

4

试试这种方式:

Dim_size=np.array((512,512,459),dtype=np.int) #Or read that from your mhd info File

f = open(FileName,'rb') #only opens the file for reading
img_arr=np.fromfile(f,dtype=np.uint16)
img_arr=img_arr.reshape(Dim_size[0],Dim_size[1],Dim_size[2])

如果您的内存受限,请分块读取文件。

f = open(FileName,'rb') #only opens the file for reading
for i in range(0,Dim_size[2]):
    img_arr=np.fromfile(f,dtype=np.uint16,count=Dim_size[0]*Dim_size[1])
    img=img.reshape(Dim_size[0],Dim_size[1])
    #Do something with the Slice

一个展示原始文件实际内容的好方法是在ImageJ中读取它。对于读取这种ITK兼容文件,甚至有一个插件可用,但直接导入原始文件也应该可以工作。 https://imagej.net/Welcome http://ij-plugins.sourceforge.net/plugins/3d-io/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接