# skullstrip anatomy print("Skullstrip INV2") skullstrip_spm12(fileINV2, pathSPM12, path) # bring skullstrip_mask in conformed space (mri_vol2vol, NN) transmask = ApplyVolTransform() transmask.inputs.source_file = os.path.join(path_skull, "skullstrip_mask.nii") transmask.inputs.target_file = os.path.join(path, sub, "mri", "orig.mgz") transmask.inputs.reg_header = True transmask.inputs.interp = "nearest" transmask.inputs.transformed_file = os.path.join(path, sub, "mri", "skullstrip_mask.nii") transmask.inputs.args = "--no-save-reg" transmask.run() # apply skullstrip mask to T1 and save as brainmask multiply_images(os.path.join(path, sub, "mri", "T1.mgz"), os.path.join(path, sub, "mri", "skullstrip_mask.nii"), os.path.join(path, sub, "mri", "brainmask.mgz")) multiply_images(os.path.join(path, sub, "mri", "T1.mgz"), os.path.join(path, sub, "mri", "skullstrip_mask.nii"), os.path.join(path, sub, "mri", "brainmask.auto.mgz")) # autorecon2 and autorecon3 print("Autorecon2 and Autorecon3") os.system("recon-all" + \ " -hires" + \ " -autorecon2" + " -autorecon3" + \
def skullstrip_refined(file_mask1, file_mask2): """ The purpose of the following function is to enhance the skullstrip mask in native space. It uses a second mask which was manually corrected during the freesurfer segmentation. This corrected brainmask is converted to native space and multiplied with the initial brainmask. Inputs: *file_mask1: brainmask in original space. *file_mask2: manually corrected brainmask in freesurfer space (brain.finalsurfs.mgz). Outputs: *file_out: filename of enhanced brainmask. created by Daniel Haenelt Date created: 31-01-2020 Last modified: 31-01-2020 """ import os import nibabel as nb from nipype.interfaces.freesurfer import ApplyVolTransform from lib.io.get_filename import get_filename # get output path and basename path_output, name_output, _ = get_filename(file_mask1) # filename of temporary and enhanced brainmask file_temp = os.path.join(path_output, "temp.nii") file_out = os.path.join(path_output, name_output + "_enhanced.nii") # bring skullstrip_mask from conformed space into original space transmask = ApplyVolTransform() transmask.inputs.source_file = file_mask2 transmask.inputs.target_file = file_mask1 transmask.inputs.reg_header = True transmask.inputs.interp = "nearest" transmask.inputs.transformed_file = file_temp transmask.inputs.args = "--no-save-reg" transmask.run() # load first brainmask in original space mask1 = nb.load(file_mask1) mask1_array = mask1.get_fdata() # load second brainmask transformed into original space mask2 = nb.load(file_temp) mask2_array = mask2.get_fdata() # make second brainmask binary mask2_array[mask2_array == 1] = 0 mask2_array[mask2_array > 0] = 1 # multiply both masks mask_enhanced_array = mask1_array * mask2_array # write enhancec brainmask mask_enhanced = nb.Nifti1Image(mask_enhanced_array, mask1.affine, mask1.header) nb.save(mask_enhanced, file_out) # remove temporary file os.remove(file_temp) return file_out
# binarise and overwrite mask mask = nb.load(os.path.join(path_t1, "mask.nii")) mask_array = mask.get_fdata() mask_array[mask_array <= mask_threshold] = 0 mask_array[mask_array != 0] = 1 output = nb.Nifti1Image(mask_array, mask.affine, mask.header) nb.save(output, os.path.join(path_t1, "mask.nii")) # transform to mp2rage space via scanner coordinates applyreg = ApplyVolTransform() applyreg.inputs.source_file = os.path.join(path_t1, "mask.nii") applyreg.inputs.target_file = os.path.join(path_t1, "T1.nii") applyreg.inputs.transformed_file = os.path.join(path_t1, "mask.nii") applyreg.inputs.reg_header = True applyreg.inputs.interp = "nearest" applyreg.run() """ convert orig to nifti """ mc = MRIConvert() mc.inputs.in_file = os.path.join(path_orig, "orig.mgz") mc.inputs.out_file = os.path.join(path_orig, "orig.nii") mc.inputs.in_type = "mgz" mc.inputs.out_type = "nii" mc.run() """ scanner transformation """ get_scanner_transform(os.path.join(path_orig, "orig.nii"), os.path.join(path_t1, "T1.nii"), path_scanner, False) get_scanner_transform(os.path.join(path_t1, "T1.nii"),