0

我有一个使用 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 dtypeobject

我尝试了相同的代码,没有更改任何内容,并且效果很好。它也有 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

4

2 回答 2

3

在一种情况下

flair_array.shape:  (960,)
flair_array.dtype:  object

在另一个

flair_array.shape:  (960, 240, 240)
flair_array.dtype:  float32

你用:

flair_array = np.array(flair_dataset)

如果 的所有元素flair_dataset具有相同的形状,它可以从它们创建一个多维数组。

但是如果列表中的一个或多个数组的形状不同,它就不得不放弃多维目标,而只是创建一个对象 dtype 数组,这非常像一个列表 - 包含对原始数组的引用。

在原始列表中,大多数元素是

brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)

但我也看到一些

brain_image size:  (256, 232, 48)
brain_array Shape:  (48, 232, 256)

在第二组中都是

brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)

当人们询问 (n,) 形状时,当他们期望 (n,m,p) 时,我怀疑第一个具有object由元素形状混合引起的 dtype。这就是我问的原因dtype

于 2020-03-04T19:30:57.520 回答
1

我认为这flair_dataset.extend(brain_array)只是用加载的数组扩展 flair_dataset 列表。这样flair_dataset就可以在阵列中堆叠所有 960 幅图像。每个图像的大小为 240、240。

您是批量加载图像,还是在图像上包含 48 层的深度?如果是这样,请尝试附加数组而不是扩展它。然后,您将加载的数组放入其列表条目中。

于 2020-03-04T18:12:19.487 回答