Esempio n. 1
0
def preprocess_unlabeled(files_dir, preprocessed_dir):
    utils.makedir(preprocessed_dir)

    # bb_size = [80, 200, 200]  # will be extended by [10,20,20] per side for augmented

    # get_left_and_right_BB()
    cases = sorted(os.listdir(files_dir))
    cases = [x for x in cases if 'case' in x]

    for i in range(0, len(cases)):
        # if cases[i] == 'case_00223':
        out_dir = os.path.join(preprocessed_dir, cases[i])
        utils.makedir(out_dir)

        print(cases[i])

        img = sitk.ReadImage(
            os.path.join(files_dir, cases[i], 'imaging.nii.gz'))
        img = utils.resampleImage(img, SPACING, sitk.sitkLinear, 0,
                                  sitk.sitkInt16)

        # apply region of interest that is guided by bones
        bb, smaller_bb = get_threshold_bounding_box(img, 500)

        if img.GetSize()[0] > BB_SIZE[0]:

            # if img.GetSize()[0] > BB_SIZE[1]+50: # if x-axis is very long, it might be a whole body scan

            cropped_img = sitk.RegionOfInterest(
                img, [smaller_bb[3], smaller_bb[4], smaller_bb[5]],
                [smaller_bb[0], smaller_bb[1], smaller_bb[2]])
            sitk.WriteImage(cropped_img, 'cropped_' + str(i) + '.nrrd')

            lung_img = sitk.BinaryThreshold(cropped_img, -900, -700, 1,
                                            0)  # lung -900, -700
            sitk.WriteImage(lung_img, 'lung_img_thresh' + str(i) + '.nrrd')

            lung_img = utils.getLargestConnectedComponents(lung_img)
            sitk.WriteImage(lung_img, 'lung_img_cc' + str(i) + '.nrrd')

            # get boundingbox of lung:
            bb_lung = getBB(lung_img, 1)
            start_y_w_bb_lung = cropped_img.TransformIndexToPhysicalPoint(
                [bb_lung[0], bb_lung[1], bb_lung[2]])
            start_y_w_img = img.TransformPhysicalPointToIndex(
                start_y_w_bb_lung)

            # print('bb_lung: ', bb_lung)

            # values for final ROI
            start_X = max(
                0, bb_lung[0] + bb_lung[3]
            )  # - 0.1*BB_SIZE[0])  # start 20 voxel above bottom of lung
            size_X = BB_SIZE[0]
            if start_X + BB_SIZE[0] > img.GetSize()[0]:
                start_X = img.GetSize()[0] - size_X - 1

            start_y = max(0, (start_y_w_img[1] + bb_lung[4] -
                              BB_SIZE[1]))  ## according to bones bounding box
            # start_y = max(0, bb[1] + bb[4] - BB_SIZE[1])
            start_z_r = int((bb[2] + bb[5]) / 2)
            start_z_l = max(0, int((bb[2] + bb[5]) / 2) - BB_SIZE[2])

        else:
            ### pad volume in x direction
            img = utils.pad_volume(img,
                                   target_size_x=BB_SIZE[0],
                                   padValue=utils.getMinimum(img))

            # values for final ROI
            start_X = 0  # start 20 voxel above bottom of lung
            start_y = max(0, (bb[1] + bb[4] + 20 -
                              BB_SIZE[1]))  ## according to bones bounding box
            start_z_r = int((bb[2] + bb[5]) / 2)
            start_z_l = max(0, int((bb[2] + bb[5]) / 2) - BB_SIZE[2])

        left_img = get_final_roi(img, start_X, start_y, start_z_l, BB_SIZE[0],
                                 BB_SIZE[1], BB_SIZE[2])
        right_img = get_final_roi(img, start_X, start_y, start_z_r, BB_SIZE[0],
                                  BB_SIZE[1], BB_SIZE[2])

        sitk.WriteImage(left_img, os.path.join(out_dir, 'img_left.nrrd'))
        sitk.WriteImage(right_img, os.path.join(out_dir, 'img_right.nrrd'))
Esempio n. 2
0
def preprocess_centered_BB():
    utils.makedir(preprocessed_dir)

    # get_left_and_right_BB()
    cases = sorted(os.listdir(files_dir))
    cases = [x for x in cases if 'case' in x]

    # bbs_left = np.load('bbs_left.npy')
    # bbs_right = np.load('bbs_right.npy')

    for i in range(0, len(cases)):
        # if cases[i] == 'case_00002':
        out_dir = os.path.join(preprocessed_dir, cases[i])
        utils.makedir(out_dir)

        print(cases[i])

        img = sitk.ReadImage(
            os.path.join(files_dir, cases[i], 'imaging.nii.gz'))
        img = utils.resampleImage(img, SPACING, sitk.sitkLinear, 0,
                                  sitk.sitkInt16)
        segm = sitk.ReadImage(
            os.path.join(files_dir, cases[i], 'segmentation.nii.gz'))
        # threhsold segmentation such that no tumor tissue is available anymore
        segm = sitk.BinaryThreshold(segm, 1, 2, 1, 0)

        ## split image in left and right half
        [size_x, size_y, size_z] = img.GetSize()

        img_left = sitk.RegionOfInterest(
            img, [size_x, size_y, int(size_z / 2)], [0, 0, 0])
        img_right = sitk.RegionOfInterest(
            img, [size_x, size_y, int(size_z / 2)],
            [0, 0, int(size_z / 2) - 1])

        img_left, img_right = normalizeIntensities(img_left, img_right)

        segm_left = utils.resampleToReference(segm,
                                              img_left,
                                              sitk.sitkNearestNeighbor,
                                              defaultValue=0,
                                              out_dType=sitk.sitkUInt8)

        segm_right = utils.resampleToReference(segm,
                                               img_right,
                                               sitk.sitkNearestNeighbor,
                                               defaultValue=0,
                                               out_dType=sitk.sitkUInt8)

        sitk.WriteImage(segm_left, 'segm_left.nrrd')
        sitk.WriteImage(segm_right, 'segm_right.nrrd')

        size_l, centroid_l = get_segmentation_statistics(
            segm_left, False, False)
        size_r, centroid_r = get_segmentation_statistics(
            segm_right, False, False)

        start_l_x = max(0, centroid_l[0] - int(BB_SIZE[0] / 2))
        start_l_y = max(0, centroid_l[1] - int(BB_SIZE[1] / 2))
        start_l_z = max(0, centroid_l[2] - int(BB_SIZE[2] / 2))

        start_r_x = max(0, centroid_r[0] - int(BB_SIZE[0] / 2))
        start_r_y = max(0, centroid_r[1] - int(BB_SIZE[1] / 2))
        start_r_z = max(0, centroid_r[2] - int(BB_SIZE[2] / 2))

        segm_l_final = get_final_roi(segm_left, start_l_x, start_l_y,
                                     start_l_z, BB_SIZE[0], BB_SIZE[1],
                                     BB_SIZE[2])
        segm_r_final = get_final_roi(segm_right, start_r_x, start_r_y,
                                     start_r_z, BB_SIZE[0], BB_SIZE[1],
                                     BB_SIZE[2])

        img_l_final = get_final_roi(img_left, start_l_x, start_l_y, start_l_z,
                                    BB_SIZE[0], BB_SIZE[1], BB_SIZE[2])
        img_r_final = get_final_roi(img_right, start_r_x, start_r_y, start_r_z,
                                    BB_SIZE[0], BB_SIZE[1], BB_SIZE[2])

        print(size_l, size_r)

        # write images
        sitk.WriteImage(img_l_final, os.path.join(out_dir, 'img_left.nrrd'))
        sitk.WriteImage(img_r_final, os.path.join(out_dir, 'img_right.nrrd'))
        sitk.WriteImage(segm_l_final, os.path.join(out_dir, 'segm_left.nrrd'))
        sitk.WriteImage(segm_r_final, os.path.join(out_dir, 'segm_right.nrrd'))
Esempio n. 3
0
    print(labelled_files_lst[0:10])

    np.random.seed(5)
    # np.random.seed(1234) for fold 1 0.25
    np.random.shuffle(labelled_files_lst)

    labelled_num_considrd = labelled_files_lst[:int(labelled_train_num * p)]
    remaining_labelled = set(labelled_files_lst).difference(
        set(labelled_num_considrd))

    counter = 0

    data_path = '/cache/suhita/data/hippocampus/fold_' + str(
        fold_num) + '_P' + str(p)

    utils.makedir(data_path)
    utils.makedir(os.path.join(data_path, 'imgs'))
    utils.makedir(os.path.join(data_path, 'GT'))
    for i in labelled_num_considrd:
        # name = labelled_files_lst[i]
        print(i, counter)
        np.save(
            os.path.join(data_path, 'imgs',
                         str(counter) + '.npy'),
            np.expand_dims(
                np.load(
                    os.path.join(labelled_imgs_path,
                                 i.replace('.nii.gz', '.npy'))), -1))
        np.save(
            os.path.join(data_path, 'GT',
                         str(counter) + '.npy'),
Esempio n. 4
0
def preprocess():
    utils.makedir(preprocessed_dir)

    bb_size = [80, 200,
               200]  # will be extended by [10,20,20] per side for augmented

    # get_left_and_right_BB()
    cases = sorted(os.listdir(files_dir))
    cases = [x for x in cases if 'case' in x]

    bbs_left = np.load('bbs_left.npy')
    bbs_right = np.load('bbs_right.npy')

    for i in range(0, len(cases)):
        # if cases[i] == 'case_00002':
        out_dir = os.path.join(preprocessed_dir, cases[i])
        utils.makedir(out_dir)

        print(cases[i])

        img = sitk.ReadImage(
            os.path.join(files_dir, cases[i], 'imaging.nii.gz'))
        img = utils.resampleImage(img, [3.0, 0.75, 0.75], sitk.sitkLinear, 0,
                                  sitk.sitkInt16)
        segm = sitk.ReadImage(
            os.path.join(files_dir, cases[i], 'segmentation.nii.gz'))

        # apply region of interest that is guided by bones
        bb, smaller_bb = get_threshold_bounding_box(img, 500)
        if bb[3] > 100:

            cropped_img = sitk.RegionOfInterest(
                img, [smaller_bb[3], smaller_bb[4], smaller_bb[5]],
                [smaller_bb[0], smaller_bb[1], smaller_bb[2]])
            # sitk.WriteImage(cropped_img, 'cropped_' + str(i) + '.nrrd')

            lung_img = sitk.BinaryThreshold(cropped_img, -900, -700, 1,
                                            0)  # lung -900, -700
            # sitk.WriteImage(lung_img, 'lung_img_thresh' + str(i) + '.nrrd')

            lung_img = utils.getLargestConnectedComponents(lung_img)
            # sitk.WriteImage(lung_img, 'lung_img_cc' + str(i) + '.nrrd')

            # get boundingbox of lung:
            bb_lung = getBB(lung_img, 1)
            # print('bb_lung: ', bb_lung)

            # values for final ROI
            start_X = max(0, bb_lung[0] + bb_lung[3] -
                          20)  # start 20 voxel above bottom of lung
            size_X = 100
            if start_X + 100 > img.GetSize()[0]:
                start_X = img.GetSize()[0] - size_X - 1

            img = sitk.RegionOfInterest(
                img, [size_X, img.GetSize()[1],
                      img.GetSize()[2]], [start_X, 0, 0])
            # img = sitk.RegionOfInterest(img, [size_X, bb[4], bb[5]],
            #                                   [start_X, bb[1], bb[2]])

            sitk.WriteImage(img, 'final_cropped_' + str(i) + '.nrrd')

        else:
            ### pad volume in x direction
            img = utils.pad_volume(img,
                                   target_size_x=100,
                                   padValue=utils.getMinimum(img))

        ## split image in left and right half
        [size_x, size_y, size_z] = img.GetSize()

        img_left = sitk.RegionOfInterest(
            img, [size_x, size_y, int(size_z / 2)], [0, 0, 0])
        img_right = sitk.RegionOfInterest(
            img, [size_x, size_y, int(size_z / 2)],
            [0, 0, int(size_z / 2) - 1])

        sitk.WriteImage(img_left, 'img_left_temp.nrrd')
        sitk.WriteImage(img_right, 'img_right_temp.nrrd')

        # get ROI for left and right kidney
        roi_left_start = [
            max(0, bbs_left[i, 0] - 10),
            max(0, bbs_left[i, 1] - 10),
            max(0, bbs_left[i, 2] - 10)
        ]
        roi_right_start = [
            max(0, bbs_right[i, 0] - 10),
            max(0, bbs_right[i, 1] - 10),
            max(0, bbs_right[i, 2] - 10)
        ]

        # apply ROI to image
        img_left_final = get_final_roi(img_left, roi_left_start[0],
                                       roi_left_start[1], roi_left_start[2],
                                       bb_size[0] + 20, bb_size[1] + 20,
                                       bb_size[2] + 20)
        img_right_final = get_final_roi(img_right, roi_right_start[0],
                                        roi_right_start[1], roi_right_start[2],
                                        bb_size[0] + 20, bb_size[1] + 20,
                                        bb_size[2] + 20)

        segm_left_final = utils.resampleToReference(segm,
                                                    img_left_final,
                                                    sitk.sitkNearestNeighbor,
                                                    defaultValue=0,
                                                    out_dType=sitk.sitkUInt8)

        segm_right_final = utils.resampleToReference(segm,
                                                     img_right_final,
                                                     sitk.sitkNearestNeighbor,
                                                     defaultValue=0,
                                                     out_dType=sitk.sitkUInt8)

        # write images
        sitk.WriteImage(img_left_final, os.path.join(out_dir, 'img_left.nrrd'))
        sitk.WriteImage(img_right_final, os.path.join(out_dir,
                                                      'img_right.nrrd'))
        sitk.WriteImage(segm_left_final, os.path.join(out_dir,
                                                      'segm_left.nrrd'))
        sitk.WriteImage(segm_right_final,
                        os.path.join(out_dir, 'segm_right.nrrd'))
Esempio n. 5
0
                     str(fold_num) + '.npy')
print(train_fold[0:10])
nr_samples = train_fold.shape[0]

# np.random.seed(5)
np.random.seed(5)
np.random.shuffle(train_fold)
print(train_fold[0:10])

labelled_num_considrd = train_fold[:int(nr_samples * perc)]
remaining_labelled = set(train_fold).difference(set(labelled_num_considrd))

counter = 0

data_path = '/cache/suhita/data/kits/fold_' + str(fold_num) + '_P' + str(perc)
utils.makedir(data_path)
for i in labelled_num_considrd:
    # name = labelled_files_lst[i]
    print(i, counter)
    fold_name = 'case_' + str(counter)
    utils.makedir(os.path.join(data_path, fold_name))
    copyfile(os.path.join(labelled_path, i, 'img_left.npy'),
             os.path.join(data_path, fold_name, 'img_left.npy'))
    copyfile(os.path.join(labelled_path, i, 'img_right.npy'),
             os.path.join(data_path, fold_name, 'img_right.npy'))
    copyfile(os.path.join(labelled_path, i, 'segm_left.npy'),
             os.path.join(data_path, fold_name, 'segm_left.npy'))
    copyfile(os.path.join(labelled_path, i, 'segm_right.npy'),
             os.path.join(data_path, fold_name, 'segm_right.npy'))
    counter = counter + 1