Esempio n. 1
0
def fs_seg2mask(parc_file,out_file=None):
    import nibabel as nb, numpy as np, os
    import scipy.ndimage
    from generic_pipelines.utils import fname_presuffix_basename
    nii = nb.load(parc_file)
    op = ((np.mgrid[:5,:5,:5]-2.0)**2).sum(0)<=4
    mask = scipy.ndimage.binary_closing(nii.get_data()>0,op,iterations=2)
    scipy.ndimage.binary_fill_holes(mask,output=mask)
    if out_file==None:
        out_file=fname_presuffix_basename(parc_file,suffix='_mask')
    out_file = os.path.abspath(os.path.join(os.getcwd(),out_file))
    nb.save(nb.Nifti1Image(mask.astype(np.uint8),nii.get_affine()),out_file)
    del nii, mask, op
    return out_file
Esempio n. 2
0
def fs_seg2mask(parc_file, out_file=None):
    import nibabel as nb, numpy as np, os
    import scipy.ndimage
    from generic_pipelines.utils import fname_presuffix_basename
    nii = nb.load(parc_file)
    op = ((np.mgrid[:5, :5, :5] - 2.0)**2).sum(0) <= 4
    mask = scipy.ndimage.binary_closing(nii.get_data() > 0, op, iterations=2)
    scipy.ndimage.binary_fill_holes(mask, output=mask)
    if out_file == None:
        out_file = fname_presuffix_basename(parc_file, suffix='_mask')
    out_file = os.path.abspath(os.path.join(os.getcwd(), out_file))
    nb.save(nb.Nifti1Image(mask.astype(np.uint8), nii.get_affine()), out_file)
    del nii, mask, op
    return out_file
Esempio n. 3
0
def seg2mask(tissues_files,out_file=None):
    import nibabel as nb, numpy as np, os
    import scipy.ndimage
    from generic_pipelines.utils import fname_presuffix_basename
    niis = [nb.load(f) for f in tissues_files]
    data = [n.get_data() for n in niis]
    mask = np.logical_or(data[0]>.5,data[1]>.5)
    np.logical_or(data[2]>.9,mask,mask)
    np.logical_or(np.logical_and(data[0]+data[1]>.2,data[2]>.5),mask,mask)
    scipy.ndimage.binary_fill_holes(mask,None,mask)
    if out_file==None:
        out_file=fname_presuffix_basename(tissues_files[0],suffix='_mask')
    out_file = os.path.abspath(os.path.join(os.getcwd(),out_file))
    nb.save(nb.Nifti1Image(mask.astype(np.uint8),niis[0].get_affine()),
            out_file)
    del niis, data, mask
    return out_file
Esempio n. 4
0
def seg2mask(tissues_files, out_file=None):
    import nibabel as nb, numpy as np, os
    import scipy.ndimage
    from generic_pipelines.utils import fname_presuffix_basename
    niis = [nb.load(f) for f in tissues_files]
    data = [n.get_data() for n in niis]
    mask = np.logical_or(data[0] > .5, data[1] > .5)
    np.logical_or(data[2] > .9, mask, mask)
    np.logical_or(np.logical_and(data[0] + data[1] > .2, data[2] > .5), mask,
                  mask)
    scipy.ndimage.binary_fill_holes(mask, None, mask)
    if out_file == None:
        out_file = fname_presuffix_basename(tissues_files[0], suffix='_mask')
    out_file = os.path.abspath(os.path.join(os.getcwd(), out_file))
    nb.save(nb.Nifti1Image(mask.astype(np.uint8), niis[0].get_affine()),
            out_file)
    del niis, data, mask
    return out_file