我有一个使用 SimpleITK 库读取 NIFTI 图像的 python 代码。然后它将图像转换为 Numpy 数组。然后,我将 Numpy 数组扩展为一个列表。
我有 20 个 FLAIR.nii.gz 文件。它们每个都有 48 片。
当我拥有所有 20 个患者的所有 48 个切片时,我将列表转换为 Numpy 数组。
我这样做是因为我是 Python 的新手,而且我不知道有任何其他方法可以做到这一点。
代码是:
import os
import SimpleITK as sitk
import numpy as np
flair_dataset = []
# For each patient directory
# data_path is a list with all of the patient's directory.
for i in data_path:
img_path = os.path.join(file_path, i, 'pre')
mask_path = os.path.join(file_path, i)
for name in glob.glob(img_path+'/FLAIR*'):
# Reads images using SimpleITK.
brain_image = sitk.ReadImage(name)
# Get a numpy array from a SimpleITK Image.
brain_array = sitk.GetArrayFromImage(brain_image)
flair_dataset.extend(brain_array)
if debug:
print('brain_image size: ', brain_image.GetSize())
print('brain_array Shape: ', brain_array.shape)
print('flair_dataset length:', len(flair_dataset))
print('flair_dataset length: ', len(flair_dataset))
print('flair_dataset[1] type: ', print(type(flair_dataset[1])))
print('flair_dataset[1] shape: ', print(flair_dataset[1].shape))
flair_array = np.array(flair_dataset)
print('flair_array.shape: ', flair_array.shape)
print('flair_array.dtype: ', flair_array.dtype)
此代码生成此输出(所有 FLAIR.nii.gz 文件具有相同的形状):
data_path = ['68', '55', '50', '61', '63', '52', '51', '60', '67', '58', '59', '53', '69', '64', '56', '65', '54', '62', '66', '57']
patient_data_path = 68
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 48
Mask list length: 48
patient_data_path = 55
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 96
Mask list length: 96
patient_data_path = 50
brain_image size: (256, 232, 48)
brain_array Shape: (48, 232, 256)
flair_dataset length: 144
WMH image Size: (256, 232, 48)
WMH array Shape: (48, 232, 256)
Mask list length: 144
patient_data_path = 61
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 192
Mask list length: 192
patient_data_path = 63
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 240
Mask list length: 240
patient_data_path = 52
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 288
Mask list length: 288
patient_data_path = 51
brain_image size: (256, 232, 48)
brain_array Shape: (48, 232, 256)
flair_dataset length: 336
WMH image Size: (256, 232, 48)
WMH array Shape: (48, 232, 256)
Mask list length: 336
patient_data_path = 60
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 384
Mask list length: 384
patient_data_path = 67
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 432
Mask list length: 432
patient_data_path = 58
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 480
Mask list length: 480
patient_data_path = 59
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 528
Mask list length: 528
patient_data_path = 53
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 576
Mask list length: 576
patient_data_path = 69
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 624
Mask list length: 624
patient_data_path = 64
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 672
Mask list length: 672
patient_data_path = 56
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 720
Mask list length: 720
patient_data_path = 65
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 768
Mask list length: 768
patient_data_path = 54
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 816
Mask list length: 816
patient_data_path = 62
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 864
Mask list length: 864
patient_data_path = 66
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 912
Mask list length: 912
patient_data_path = 57
brain_image size: (232, 256, 48)
brain_array Shape: (48, 256, 232)
flair_dataset length: 960
Mask list length: 960
代码的最终输出是:
flair_dataset length: 960
mask_dataset length: 960
flair_dataset[1] type: <class 'numpy.ndarray'>
flair_dataset[1] shape: (256, 232)
flair_array.shape: (960,)
flair_array.dtype: object
我的问题:
我不明白为什么 flair_array 有这种形状:(960,)
。
flair_array dtype
是object
。
我尝试了相同的代码,没有更改任何内容,并且效果很好。它也有 20 名患者,每个 FLAIR.nii.gz 文件也有 48 个切片。
它的输出:
data_path = ['39', '31', '2', '23', '35', '29', '17', '49', '27', '8', '33', '4', '19', '41', '37', '11', '25', '6', '0', '21']
patient_data_path = 39
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 48
Mask list length: 48
patient_data_path = 31
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 96
Mask list length: 96
patient_data_path = 2
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 144
Mask list length: 144
patient_data_path = 23
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 192
Mask list length: 192
patient_data_path = 35
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 240
Mask list length: 240
patient_data_path = 29
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 288
Mask list length: 288
patient_data_path = 17
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 336
Mask list length: 336
patient_data_path = 49
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 384
Mask list length: 384
patient_data_path = 27
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 432
Mask list length: 432
patient_data_path = 8
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 480
Mask list length: 480
patient_data_path = 33
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 528
Mask list length: 528
patient_data_path = 4
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 576
Mask list length: 576
patient_data_path = 19
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 624
Mask list length: 624
patient_data_path = 41
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 672
Mask list length: 672
patient_data_path = 37
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 720
Mask list length: 720
patient_data_path = 11
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 768
Mask list length: 768
patient_data_path = 25
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 816
Mask list length: 816
patient_data_path = 6
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 864
Mask list length: 864
patient_data_path = 0
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 912
Mask list length: 912
patient_data_path = 21
brain_image size: (240, 240, 48)
brain_array Shape: (48, 240, 240)
flair_dataset length: 960
Mask list length: 960
这是此数据集的最终输出:
flair_dataset length: 960
mask_dataset length: 960
flair_dataset[1] type: <class 'numpy.ndarray'>
flair_dataset[1] shape: (240, 240)
flair_array.shape: (960, 240, 240)
flair_array.dtype: float32
有了这第二个数据集,flair_array
就是float32
.
为什么第一个flair_array
形状是(960,)
?
更新:
在两个数据集中,brain_array.dtype
总是float32
。