Beispiel #1
0
def seg_pipeline4(rootdir, uid, save_name):
    # Read data
    t1ce_img, affine_t1ce, hdr_t1ce = file_io.read_nii(os.path.join(rootdir, uid, uid + '_t1ce.nii.gz'))
    t2_img, affine_t2, hdr_t2 = file_io.read_nii(os.path.join(rootdir, uid, uid + '_t2.nii.gz'))
    flair_img, affine_flair, hdr_flair = file_io.read_nii(os.path.join(rootdir, uid, uid + '_flair.nii.gz'))
    seg_img, affine_seg, hdr_seg = file_io.read_nii(os.path.join(rootdir, uid, uid + '_seg.nii.gz'))

    # Preprocess
    st1, et1 = img_utils.get_bbox(flair_img)
    flair_roi = img_utils.normalize_0_1(img_utils.crop_img(flair_img, st1, et1))

    # full tumor segmentation
    full_tumor = img_utils.clustering_img([flair_roi], 5, method='Kmeans')
    full_tumor = img_utils.rm_small_cc(full_tumor.astype(np.uint8), rate=0.3)
    nc_tumor_1 = img_utils.get_holes(full_tumor)
    full_tumor = img_utils.fill_full(full_tumor, )
    st_2, et_2 = img_utils.get_bbox(full_tumor)
    full_tumor_roi = img_utils.crop_img(full_tumor, st_2, et_2)
    nc_tumor_1 = img_utils.crop_img(img_utils.get_holes(nc_tumor_1), st_2, et_2)

    # ec tumor segmentation
    t1ce_roi = img_utils.normalize_0_1(img_utils.crop_img(img_utils.crop_img(t1ce_img, st1, et1), st_2, et_2))
    t1ce_roi[full_tumor_roi != 1] = 0
    ec_tumor = img_utils.clustering_img([t1ce_roi, ], 3, method='Kmeans')

    # necrotic segmentation
    nc_tumor_2 = img_utils.segment_necrotic(ec_tumor, 3, 1)
    nc_tumor = nc_tumor_1 + nc_tumor_2
    nc_tumor[nc_tumor == 2] = 1

    # Calculate metrics
    final_mask = np.zeros(full_tumor_roi.shape)
    final_mask[full_tumor_roi == 1] = 2
    final_mask[ec_tumor == 1] = 4
    final_mask[nc_tumor == 1] = 1
    final_mask = img_utils.crop2raw(seg_img.shape, img_utils.crop2raw(full_tumor.shape, final_mask, st_2, et_2), st1,
                                    et1)
    ec_result = metrics.test_result(final_mask, seg_img, mode='ec')
    core_result = metrics.test_result(final_mask, seg_img, mode='core')
    full_result = metrics.test_result(final_mask, seg_img, mode='full')

    if save_name is not None:
        file_io.save_nii(final_mask, affine_seg, hdr_seg, os.path.join(rootdir, uid, uid + save_name))
    return ec_result, core_result, full_result
Beispiel #2
0
def seg_pipeline(flair_dir, save_dir, f_cluster=5,  c_method='Kmeans', ):
    """
    segment the full tumor by clustering.
    :param flair_dir: flair image.
    :param save_dir: the dir to save the segmentation result.
    :param f_cluster: The cluster number of clustering.
    :param c_method: The cluster method, such as Kmeans, GMM, minibatchkmeans et al.
    :return: None
    """
    assert os.path.isfile(flair_dir), print('There is not a file at %s' % flair_dir)
    # Read data
    flair_img, affine_flair, hdr_flair = file_io.read_nii(flair_dir)

    # Preprocess
    st1, et1 = img_utils.get_bbox(flair_img)
    flair_roi = img_utils.normalize_0_1(img_utils.crop_img(flair_img, st1, et1))

    # full tumor segmentation
    full_tumor = img_utils.clustering_img([flair_roi], int(f_cluster), method=c_method)
    full_tumor = img_utils.rm_small_cc(full_tumor.astype(np.uint8), rate=0.3)
    full_tumor = img_utils.fill_full(full_tumor)
    final_mask = img_utils.crop2raw(flair_img.shape, full_tumor, st1, et1)
    if save_dir is not None:
        file_io.save_nii(final_mask, affine_flair, hdr_flair, save_dir)
Beispiel #3
0
def seg_pipeline(
    t1ce_dir,
    flair_dir,
    save_dir,
    bbox=None,
    nc_seg_mode='cc',
    t2_dir=None,
    t2_cluster=3,
    f_cluster=5,
    ec_cluster=4,
    c_method='Kmeans',
):
    assert os.path.isfile(t1ce_dir), print('There is not a file at %s' %
                                           t1ce_dir)
    assert os.path.isfile(flair_dir), print('There is not a file at %s' %
                                            flair_dir)
    # if not os.path.isdir(save_dir):
    #     os.mkdir(save_dir)
    # Read data
    t1ce_img, affine_t1ce, hdr_t1ce = file_io.read_nii(t1ce_dir)
    flair_img, affine_flair, hdr_flair = file_io.read_nii(flair_dir)
    if bbox is not None:
        st1, et1 = bbox[0], bbox[1]
    else:
        st1, et1 = img_utils.get_bbox(flair_img)

    # Preprocess
    flair_roi = img_utils.normalize_0_1(img_utils.crop_img(
        flair_img, st1, et1))

    # full tumor segmentation
    full_tumor = img_utils.clustering_img([flair_roi],
                                          int(f_cluster),
                                          method=c_method)
    full_tumor = img_utils.rm_small_cc(full_tumor.astype(np.uint8), rate=0.3)
    full_tumor = img_utils.fill_full(full_tumor)
    st_2, et_2 = img_utils.get_bbox(full_tumor)
    full_tumor_roi = img_utils.crop_img(full_tumor, st_2, et_2)

    # ec tumor segmentation
    t1ce_roi = img_utils.normalize_0_1(
        img_utils.crop_img(img_utils.crop_img(t1ce_img, st1, et1), st_2, et_2))
    t1ce_roi[full_tumor_roi != 1] = 0
    ec_tumor = img_utils.clustering_img([
        t1ce_roi,
    ],
                                        int(ec_cluster),
                                        method=c_method)

    # necrotic segmentation
    if nc_seg_mode == 'cc':
        nc_tumor = img_utils.segment_necrotic(ec_tumor, 3, 1)
        # integrate mask
        final_mask = np.zeros(full_tumor_roi.shape)
        final_mask[full_tumor_roi == 1] = 2
        final_mask[ec_tumor == 1] = 4
        final_mask[nc_tumor == 1] = 1
    elif nc_seg_mode == 't2':
        # core segmentation
        assert os.path.isfile(t2_dir), print('There is not a file at %s' %
                                             t2_dir)
        t2_img, affine_t2, hdr_t2 = file_io.read_nii(t2_dir)
        t2_roi = img_utils.normalize_0_1(
            img_utils.crop_img(img_utils.crop_img(t2_img, st1, et1), st_2,
                               et_2))
        t2_roi[full_tumor_roi != 1] = 0
        core_tumor = img_utils.clustering_img([
            t2_roi,
        ],
                                              int(t2_cluster),
                                              method=c_method)
        # integrate mask
        final_mask = np.zeros(full_tumor_roi.shape)
        final_mask[full_tumor_roi == 1] = 2
        final_mask[core_tumor == 1] = 1
        final_mask[ec_tumor == 1] = 4
    else:
        raise Exception(
            'This necrotic region segmentation method have not been defined!')
    final_mask = img_utils.crop2raw(
        flair_img.shape,
        img_utils.crop2raw(full_tumor.shape, final_mask, st_2, et_2), st1, et1)
    if save_dir is not None:
        file_io.save_nii_sitk(final_mask, save_dir)
    return final_mask
Beispiel #4
0
def seg_pipeline_adjusted(rootdir, uid, save_name, mode):
    # Read data
    t1ce_img, affine_t1ce, hdr_t1ce = file_io.read_nii(
        os.path.join(rootdir, uid, uid + '_t1ce.nii.gz'))
    t2_img, affine_t2, hdr_t2 = file_io.read_nii(
        os.path.join(rootdir, uid, uid + '_t2.nii.gz'))
    flair_img, affine_flair, hdr_flair = file_io.read_nii(
        os.path.join(rootdir, uid, uid + '_flair.nii.gz'))
    seg_img, affine_seg, hdr_seg = file_io.read_nii(
        os.path.join(rootdir, uid, uid + '_seg.nii.gz'))

    # Preprocess
    st1, et1 = img_utils.get_bbox(flair_img)
    flair_roi = img_utils.normalize_0_1(img_utils.crop_img(
        flair_img, st1, et1))
    seg_roi = img_utils.crop_img(seg_img, st1, et1)

    # full tumor segmentation
    full_re = [None, -1, 0]
    for n_c in range(3, 25):
        full_tumor = img_utils.clustering_img([flair_roi], n_c, method=mode)
        full_tumor = img_utils.rm_small_cc(full_tumor.astype(np.uint8),
                                           rate=0.3)
        full_tumor = img_utils.fill_full(full_tumor)
        temp_seg = seg_roi.copy()
        temp_seg[seg_roi != 0] = 1
        full_re_metric = metrics.Seg_metric(temp_seg, full_tumor)
        # print('full_re_metric {}: dice:{}'.format(n_c, full_re_metric.dice()))
        if full_re_metric.dice() > full_re[-1]:
            full_re[0] = full_tumor
            full_re[1] = n_c
            full_re[2] = full_re_metric.dice()
        if full_re_metric.dice() < full_re[-1] - 0.1:
            break

    st_2, et_2 = img_utils.get_bbox(full_re[0])
    full_tumor_roi = img_utils.crop_img(full_re[0], st_2, et_2)

    # ec tumor segmentation
    t1ce_roi = img_utils.normalize_0_1(
        img_utils.crop_img(img_utils.crop_img(t1ce_img, st1, et1), st_2, et_2))
    t1ce_roi[full_tumor_roi != 1] = 0
    EC_re = [None, -1, 0]
    for n_c in range(3, 6):
        ec_tumor = img_utils.clustering_img([
            t1ce_roi,
        ], n_c, method=mode)
        ec_tumor_de = img_utils.crop2raw(full_re[0].shape, ec_tumor, st_2,
                                         et_2)
        temp_seg = np.zeros(seg_roi.shape)
        temp_seg[seg_roi == 4] = 1
        ec_re_metric = metrics.Seg_metric(temp_seg, ec_tumor_de)
        # print('ec_re_metric {}: dice:{}'.format(n_c, ec_re_metric.dice()))
        if ec_re_metric.dice() >= EC_re[-1]:
            EC_re = [ec_tumor, n_c, ec_re_metric.dice()]
    ec_tumor = EC_re[0]

    # necrotic segmentation
    nc_tumor_1 = img_utils.segment_necrotic(ec_tumor, 3, 1)
    final_mask = np.zeros(full_tumor_roi.shape)
    final_mask[full_tumor_roi == 1] = 2
    final_mask[ec_tumor == 1] = 4
    final_mask[nc_tumor_1 == 1] = 1
    final_mask = img_utils.crop2raw(
        seg_img.shape,
        img_utils.crop2raw(full_re[0].shape, final_mask, st_2, et_2), st1, et1)
    core_re = [
        final_mask, 'cc',
        metrics.test_result(final_mask, seg_img, mode='core')[0]
    ]
    # print('core_metric cc:dice:{}'.format(core_re[-1]))
    t2_roi = img_utils.normalize_0_1(
        img_utils.crop_img(img_utils.crop_img(t2_img, st1, et1), st_2, et_2))
    t2_roi[full_tumor_roi != 1] = 0
    for n_c in range(3, 6):
        core_tumor = img_utils.clustering_img([
            t2_roi,
        ], n_c, method=mode)
        final_mask = np.zeros(full_tumor_roi.shape)
        final_mask[full_tumor_roi == 1] = 2
        final_mask[core_tumor == 1] = 1
        final_mask[ec_tumor == 1] = 4
        final_mask = img_utils.crop2raw(
            seg_img.shape,
            img_utils.crop2raw(full_re[0].shape, final_mask, st_2, et_2), st1,
            et1)
        core_metric = metrics.test_result(final_mask, seg_img, mode='core')
        # print('core_metric {}:dice:{}'.format(n_c, core_metric[0]))
        if core_metric[0] >= core_re[-1]:
            core_re = [final_mask, n_c, core_metric[0]]

    final_mask = core_re[0]
    # Calculate metrics
    ec_result = metrics.test_result(final_mask, seg_img, mode='ec')
    core_result = metrics.test_result(final_mask, seg_img, mode='core')
    full_result = metrics.test_result(final_mask, seg_img, mode='full')

    if save_name is not None:
        file_io.save_nii_sitk(final_mask,
                              os.path.join(rootdir, uid, uid + save_name))
    return ec_result, core_result, full_result, [
        full_re[1], EC_re[1], core_re[1]
    ]