Exemple #1
0
def tissue_classification(img,
                          mask=None,
                          niters=25,
                          beta=0.5,
                          ngb_size=6,
                          probc=None,
                          probg=None,
                          probw=None):
    import numpy as np

    from nipy import load_image, save_image
    from nipy.core.image.image_spaces import (make_xyz_image, xyz_affine)
    from nipy.algorithms.segmentation import BrainT1Segmentation
    import os
    # Input image
    img = load_image(img)

    # Input mask image
    mask_img = mask
    if mask_img == None:
        mask_img = img
    else:
        mask_img = load_image(mask_img)

    # Other optional arguments
    #niters = int(get_argument('niters', 25))
    #beta = float(get_argument('beta', 0.5))
    #ngb_size = int(get_argument('ngb_size', 6))

    # Perform tissue classification
    mask = mask_img.get_data() > 0
    S = BrainT1Segmentation(img.get_data(),
                            mask=mask,
                            model='5k',
                            niters=niters,
                            beta=beta,
                            ngb_size=ngb_size)

    # Save label image
    outfile = os.path.abspath('hard_classif.nii')
    save_image(make_xyz_image(S.label, xyz_affine(img), 'scanner'), outfile)
    print('Label image saved in: %s' % outfile)

    # Compute fuzzy Dice indices if a 3-class fuzzy model is provided
    if not probc == None and\
       not probg == None and\
       not probw == None:
        print('Computing Dice index')
        gold_ppm = np.zeros(S.ppm.shape)
        gold_ppm_img = (probc, probg, probw)
        for k in range(3):
            img = load_image(gold_ppm_img[k])
            gold_ppm[..., k] = img.get_data()
        d = fuzzy_dice(gold_ppm, S.ppm, np.where(mask_img.get_data() > 0))
        print('Fuzzy Dice indices: %s' % d)
def tissue_classification(img,mask=None,niters=25,beta=0.5,ngb_size=6,probc=None,probg=None,probw=None):
    import numpy as np

    from nipy import load_image, save_image
    from nipy.core.image.image_spaces import (make_xyz_image,
                                              xyz_affine)
    from nipy.algorithms.segmentation import BrainT1Segmentation
    import os
    # Input image
    img = load_image(img)

    # Input mask image
    mask_img = mask
    if mask_img == None:
        mask_img = img
    else:
        mask_img = load_image(mask_img)

    # Other optional arguments
    #niters = int(get_argument('niters', 25))
    #beta = float(get_argument('beta', 0.5))
    #ngb_size = int(get_argument('ngb_size', 6))

    # Perform tissue classification
    mask = mask_img.get_data() > 0
    S = BrainT1Segmentation(img.get_data(), mask=mask, model='5k',
        niters=niters, beta=beta, ngb_size=ngb_size)

    # Save label image
    outfile = os.path.abspath('hard_classif.nii')
    save_image(make_xyz_image(S.label, xyz_affine(img), 'scanner'),
        outfile)
    print('Label image saved in: %s' % outfile)

    # Compute fuzzy Dice indices if a 3-class fuzzy model is provided
    if not probc == None and\
       not probg == None and\
       not probw == None:
        print('Computing Dice index')
        gold_ppm = np.zeros(S.ppm.shape)
        gold_ppm_img = (probc, probg, probw)
        for k in range(3):
            img = load_image(gold_ppm_img[k])
            gold_ppm[..., k] = img.get_data()
        d = fuzzy_dice(gold_ppm, S.ppm, np.where(mask_img.get_data() > 0))
        print('Fuzzy Dice indices: %s' % d)
Exemple #3
0
def change_vox(t_shape, img):
    t_aff = adjust_affine(xyz_affine(img), img.shape, t_shape)
    img2 = resample(img, reference=(t_shape, t_aff))
    zooms = np.asarray(img.header.get_zooms())*np.asarray(img.shape)/np.asarray(t_shape)
    return img2, t_aff, zooms
else:
    mask_img = load_image(mask_img)

# Other optional arguments
niters = int(get_argument('niters', 25))
beta = float(get_argument('beta', 0.5))
ngb_size = int(get_argument('ngb_size', 6))

# Perform tissue classification
mask = mask_img.get_data() > 0
S = BrainT1Segmentation(img.get_data(), mask=mask, model='5k',
                        niters=niters, beta=beta, ngb_size=ngb_size)

# Save label image
outfile = 'hard_classif.nii'
save_image(make_xyz_image(S.label, xyz_affine(img), 'scanner'),
           outfile)
print('Label image saved in: %s' % outfile)

# Compute fuzzy Dice indices if a 3-class fuzzy model is provided
if args.probc is not None and \
        args.probg is not None and \
        args.probw is not None:
    print('Computing Dice index')
    gold_ppm = np.zeros(S.ppm.shape)
    gold_ppm_img = (args.probc, args.probg, args.probw)
    for k in range(3):
        img = load_image(gold_ppm_img[k])
        gold_ppm[..., k] = img.get_data()
    d = fuzzy_dice(gold_ppm, S.ppm, np.where(mask_img.get_data() > 0))
    print('Fuzzy Dice indices: %s' % d)
Exemple #5
0
niters = int(get_argument('niters', 25))
beta = float(get_argument('beta', 0.5))
ngb_size = int(get_argument('ngb_size', 6))

# Perform tissue classification
mask = mask_img.get_data() > 0
S = BrainT1Segmentation(img.get_data(),
                        mask=mask,
                        model='5k',
                        niters=niters,
                        beta=beta,
                        ngb_size=ngb_size)

# Save label image
outfile = 'hard_classif.nii'
save_image(make_xyz_image(S.label, xyz_affine(img), 'scanner'), outfile)
print('Label image saved in: %s' % outfile)

# Compute fuzzy Dice indices if a 3-class fuzzy model is provided
if args.probc is not None and \
        args.probg is not None and \
        args.probw is not None:
    print('Computing Dice index')
    gold_ppm = np.zeros(S.ppm.shape)
    gold_ppm_img = (args.probc, args.probg, args.probw)
    for k in range(3):
        img = load_image(gold_ppm_img[k])
        gold_ppm[..., k] = img.get_data()
    d = fuzzy_dice(gold_ppm, S.ppm, np.where(mask_img.get_data() > 0))
    print('Fuzzy Dice indices: %s' % d)