Exemplo n.º 1
0
def convert_labels_to_nnunet(source_nifti: str, target_nifti: str):
    img = sitk.ReadImage(source_nifti)
    img_npy = sitk.GetArrayFromImage(img)
    nnunet_seg = np.zeros(img_npy.shape, dtype=np.uint8)
    # why are they not using normal labels and instead use random numbers???
    nnunet_seg[img_npy == 500] = 1  # left ventricular (LV) blood pool (500)
    nnunet_seg[img_npy == 600] = 2  # right ventricular blood pool (600)
    nnunet_seg[img_npy == 200] = 3  # LV normal myocardium (200)
    nnunet_seg[img_npy == 1220] = 4  # LV myocardial edema (1220)
    nnunet_seg[img_npy == 2221] = 5  # LV myocardial scars (2221)
    nnunet_seg_itk = sitk.GetImageFromArray(nnunet_seg)
    nnunet_seg_itk = copy_geometry(nnunet_seg_itk, img)
    sitk.WriteImage(nnunet_seg_itk, target_nifti)
Exemplo n.º 2
0
def convert_labels_back_to_myops(source_nifti: str, target_nifti: str):
    nnunet_itk = sitk.ReadImage(source_nifti)
    nnunet_npy = sitk.GetArrayFromImage(nnunet_itk)
    myops_seg = np.zeros(nnunet_npy.shape, dtype=np.uint8)
    # why are they not using normal labels and instead use random numbers???
    myops_seg[nnunet_npy == 1] = 500  # left ventricular (LV) blood pool (500)
    myops_seg[nnunet_npy == 2] = 600  # right ventricular blood pool (600)
    myops_seg[nnunet_npy == 3] = 200  # LV normal myocardium (200)
    myops_seg[nnunet_npy == 4] = 1220  # LV myocardial edema (1220)
    myops_seg[nnunet_npy == 5] = 2221  # LV myocardial scars (2221)
    myops_seg_itk = sitk.GetImageFromArray(myops_seg)
    myops_seg_itk = copy_geometry(myops_seg_itk, nnunet_itk)
    sitk.WriteImage(myops_seg_itk, target_nifti)
def load_remove_save(input_file: str, output_file: str, for_which_classes: list,
                     minimum_valid_object_size: dict = None):
    # Only objects larger than minimum_valid_object_size will be removed. Keys in minimum_valid_object_size must
    # match entries in for_which_classes
    img_in = sitk.ReadImage(input_file)
    img_npy = sitk.GetArrayFromImage(img_in)
    volume_per_voxel = float(np.prod(img_in.GetSpacing(), dtype=np.float64))

    image, largest_removed, kept_size = remove_all_but_the_largest_connected_component(img_npy, for_which_classes,
                                                                                       volume_per_voxel,
                                                                                       minimum_valid_object_size)
    # print(input_file, "kept:", kept_size)
    img_out_itk = sitk.GetImageFromArray(image)
    img_out_itk = copy_geometry(img_out_itk, img_in)
    sitk.WriteImage(img_out_itk, output_file)
    return largest_removed, kept_size
Exemplo n.º 4
0
        seg = convert_MR_seg(load_png_stack(gt_dir)[::-1])

        img_dir = join(d, p, "T1DUAL", "DICOM_anon", "InPhase")
        img_outfile = join(output_images, patient_name + "_0000.nii.gz")
        _ = dicom2nifti.convert_dicom.dicom_series_to_nifti(
            img_dir, img_outfile, reorient_nifti=False)

        img_dir = join(d, p, "T1DUAL", "DICOM_anon", "OutPhase")
        img_outfile = join(output_images, patient_name + "_0001.nii.gz")
        _ = dicom2nifti.convert_dicom.dicom_series_to_nifti(
            img_dir, img_outfile, reorient_nifti=False)

        img_sitk = sitk.ReadImage(img_outfile)
        img_sitk_npy = sitk.GetArrayFromImage(img_sitk)
        seg_itk = sitk.GetImageFromArray(seg.astype(np.uint8))
        seg_itk = copy_geometry(seg_itk, img_sitk)
        sitk.WriteImage(seg_itk, join(output_labels, patient_name + ".nii.gz"))
        patient_ids.append(patient_name)

    # Process T1 test
    d = join(root_test, "MR")
    patients = subdirs(d, join=False)
    for p in patients:
        patient_name = "T1_" + p

        img_dir = join(d, p, "T1DUAL", "DICOM_anon", "InPhase")
        img_outfile = join(output_imagesTs, patient_name + "_0000.nii.gz")
        _ = dicom2nifti.convert_dicom.dicom_series_to_nifti(
            img_dir, img_outfile, reorient_nifti=False)

        img_dir = join(d, p, "T1DUAL", "DICOM_anon", "OutPhase")