Beispiel #1
0
def crop_nifti(input_img, ref_crop):
    """Crop input image based on the reference.

    It uses nilearn `resample_to_img` function.

    Args:
        input_img (str): image to be processed
        ref_crop (str): template used to crop the image

    Returns:
       output_img (NIfTI image): crop image on disk.
       crop_template: (NIfTI image): output template on disk.
    """
    import os

    from nilearn.image import crop_img, resample_to_img

    basedir = os.getcwd()
    # crop_ref = crop_img(ref_img, rtol=0.5)
    # crop_ref.to_filename(os.path.join(basedir, os.path.basename(input_img).split('.nii')[0] + '_cropped_template.nii.gz'))
    # crop_template = os.path.join(basedir, os.path.basename(input_img).split('.nii')[0] + '_cropped_template.nii.gz')

    # resample the individual MRI into the cropped template image
    crop_img = resample_to_img(input_img, ref_crop, force_resample=True)
    crop_img.to_filename(
        os.path.join(
            basedir,
            os.path.basename(input_img).split(".nii")[0] + "_cropped.nii.gz"))

    output_img = os.path.join(
        basedir,
        os.path.basename(input_img).split(".nii")[0] + "_cropped.nii.gz")
    crop_template = ref_crop

    return output_img, crop_template
Beispiel #2
0
def crop_nifti(input_img, ref_crop):
    """Crop input image based on the reference. It uses nilearn
    `resample_to_img` function.
    Args:
       input_img (str): image to be processed
       ref_img (str): template used to crop the image
    Returns:
       output_img (nifty image): crop image on disk.
       crop_template: (nifty image): output template on disk.
    """

    import nibabel as nib
    import os
    import numpy as np
    from nilearn.image import resample_img, crop_img, resample_to_img
    from nibabel.spatialimages import SpatialImage

    basedir = os.getcwd()
    # crop_ref = crop_img(ref_img, rtol=0.5)
    # crop_ref.to_filename(os.path.join(basedir, os.path.basename(input_img).split('.nii')[0] + '_cropped_template.nii.gz'))
    # crop_template = os.path.join(basedir, os.path.basename(input_img).split('.nii')[0] + '_cropped_template.nii.gz')

    # resample the individual MRI into the cropped template image
    crop_img = resample_to_img(input_img, ref_crop)
    crop_img.to_filename(
        os.path.join(
            basedir,
            os.path.basename(input_img).split('.nii')[0] + '_cropped.nii.gz'))

    output_img = os.path.join(
        basedir,
        os.path.basename(input_img).split('.nii')[0] + '_cropped.nii.gz')
    crop_template = ref_crop

    return output_img, crop_template
Beispiel #3
0
def crop_nifti(input_img, ref_img):
    """

    :param input_img:
    :param crop_sagittal:
    :param crop_coronal:
    :param crop_axial:
    :return:
    """

    import nibabel as nib
    import os
    import numpy as np
    from nilearn.image import resample_img, crop_img, resample_to_img
    from nibabel.spatialimages import SpatialImage

    basedir = os.getcwd()
    crop_ref = crop_img(ref_img, rtol=0.5)
    crop_ref.to_filename(
        os.path.join(
            basedir,
            os.path.basename(input_img).split('.nii')[0] +
            '_cropped_template.nii.gz'))
    crop_template = os.path.join(
        basedir,
        os.path.basename(input_img).split('.nii')[0] +
        '_cropped_template.nii.gz')

    ## resample the individual MRI onto the cropped template image
    crop_img = resample_to_img(input_img, crop_template)
    crop_img.to_filename(
        os.path.join(
            basedir,
            os.path.basename(input_img).split('.nii')[0] + '_cropped.nii.gz'))

    output_img = os.path.join(
        basedir,
        os.path.basename(input_img).split('.nii')[0] + '_cropped.nii.gz')

    return output_img, crop_template