将 unet 答案转换回 .nii 文件

Convert unet answer back to .nii file

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

为了举例和简单,我从训练样本中拿了一个mask。


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')


嗯,我知道问题出在哪里了,在保存的时候,需要对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')