python读取并转换原始3D图像文件

标签 python medical-imaging

我正在使用原始格式的 ct 扫描医学图像。它基本上是一个 3d 体素矩阵(512*512*nb 切片)。我想将文件的每个部分提取到单独的文件中。

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 扫描,但图像的纹理很奇怪,就好像像素放错了位置一样。

enter image description here

一些相关信息可能位于关联的 .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

最佳答案

尝试这样:

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/

关于python读取并转换原始3D图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41651998/

相关文章:

python - 使用共享 C 库的 3D numpy 数组迭代

python ImportError Openvino 通过脚本和 shell

Python OrderedDict 迭代

python - 如何使用索引将 Pandas 数据框写入 sqlite

image-processing - 如果整个地面实况是黑色的,则医学图像分割

geometry - 如何计算附加路径长度(APL)图像分割指标?

python - 如何减少 MRI(.nii 格式)图像中的 channel 数?

python - Python opencv和dicom文件

python - 如何使用 selenium (Python) 单击 Alertify 对话框按钮

python nltk 从句子中提取关键字