1

我是深度学习的新手,目前正在研究图像分割网络。我设法训练了网络,但问题是将网络响应转换为 nii 格式。我将 CT 图像中的训练样本切割成 512X512 切片,然后切片成 128X128 块。因此,我将补丁转移到网络的输入端,并在输出端获得掩码 128x128。我设法将掩码分组到一个 numpy 数组中。但是当从一个数组转移到 nii 并试图将得到的掩码施加到原始 CT 上时,我的比例不匹配。请告诉我可能是什么问题?我真的很感激任何帮助。

例如,为了简单起见,我从训练样本中提取了一个掩码。


input_path = PatchPathOutput
ls = os.listdir(input_path)#dir with patches(128) of inital mask
slices = []

for i in range(0, len(ls), 16):
    line1 = np.array(Image.open(input_path + '/'+ ls[i]))
    #here I get the first slice line from patches
    for j in range(1, 4):
        a = np.array(Image.open(input_path + '/'+ ls[i+j]))
        line1 = np.concatenate((line1, a), axis = 1)
    
    line2 = np.array(Image.open(input_path + '/'+ ls[i + 4]))
    for j in range(5, 8):
        a = np.array(Image.open(input_path + '/'+ ls[i+j]))
        line2 = np.concatenate((line2, a), axis = 1)

    line3 = np.array(Image.open(input_path + '/'+ ls[i + 8]))
    for j in range(9, 12):
        a = np.array(Image.open(input_path + '/'+ ls[i+j]))
        line3 = np.concatenate((line3, a), axis = 1)
        
    line4 = np.array(Image.open(input_path + '/'+ ls[i + 12]))
    for j in range(13, 16):
        a = np.array(Image.open(input_path + '/'+ ls[i+j]))
        line4 = np.concatenate((line4, a), axis = 1)
    
    #all lines to slice (512 x 512)
    slice_ = np.concatenate((line1, line2), axis = 0)
    slice_ = np.concatenate((slice_, line3), axis = 0)
    slice_ = np.concatenate((slice_, line4), axis = 0)
    
    slices.append(slice_)

    slices_ = np.asarray(slices)#shape (137, 512, 512)
    slices_ = np.swapaxes(slices_,0,2)#shape (512, 512, 137)

    import nibabel as nib

    new_image = nib.Nifti1Image(slices_, affine=np.eye(4))
    nib.save(new_image, 'new_image.nii')


4

1 回答 1

1

好吧,我意识到问题出在哪里,在保存过程中,您需要使用原始nii图像的仿射变换。

ct_scan = nib.load(CtPathInput + '/019.nii')

import nibabel as nib

new_image = nib.Nifti1Image(slices_, ct_scan.affine)
nib.save(new_image, 'new_image.nii')
于 2021-05-05T10:43:10.000 回答