#!/usr/bin/env python import sys,os,os.path inputfile = '' outputfile = '' refmask='' reforiginal='' threshold='' if len(sys.argv)==1: print "Usage: python skullstripper.py -in -ref -refmask -out \nOptional:\n\t-thr " sys.exit() for x in range(1,len(sys.argv)): if x%2 != 0: if sys.argv[x]=='-in': inputfile=sys.argv[x+1] elif sys.argv[x]=='-ref': reforiginal=sys.argv[x+1] elif sys.argv[x]=='-refmask': refmask=sys.argv[x+1] elif sys.argv[x]=='-out': outputfile=sys.argv[x+1] elif sys.argv[x]=='-thr': threshold=sys.argv[x+1] elif sys.argv[x]=='-h': print "Usage: python skullstripper.py -in -ref -refmask -out \nOptional:\n\t-thr " sys.exit() else: print "\nError invalid input.\n" print "Usage: python skullstripper.py -in -ref -refmask -out \nOptional:\n\t-thr " sys.exit() if os.path.exists(inputfile)!=True: print 'Inputfile does not exist in this directory' sys.exit() if os.path.exists(refmask)!=True: print 'Reference mask does not exist in this directory' sys.exit() if os.path.exists(reforiginal)!=True: print 'Reference file does not exist in this directory' sys.exit() #create output folders masks_folder = "masks" stripped_folder = "stripped_images" if not os.path.exists(masks_folder): os.makedirs(masks_folder) if not os.path.exists(stripped_folder): os.makedirs(stripped_folder) #path to niftyreg programs: reg_aladin, reg_f3d, reg_tools registeraffinepath="reg_aladin" registerf3dpath="reg_f3d" regtoolspath="reg_tools" #finds all files and folders workingdir=os.getcwd() os.system(registeraffinepath+" -target "+inputfile+" -source "+ reforiginal+" -aff affinetransformcurrent.txt") os.system(registerf3dpath+" -target "+inputfile+" -source "+ reforiginal+" -aff affinetransformcurrent.txt -cpp ccptemp.nii") #cleanup files os.remove("outputResult.nii") os.remove("affinetransformcurrent.txt") os.system(registerf3dpath+" -target "+inputfile+" -source "+refmask+" -ln 0 -maxit 0 -incpp ccptemp.nii -res "+masks_folder+'/'+outputfile) #binarizes images and converts it to a char so it can be edited in brainsuite os.remove("outputCPP.nii") os.remove("ccptemp.nii") #add -thr 800 if needed if threshold !='': print 'Starting Thresholding' # this command places the mask in the masks/ folder. os.system(regtoolspath+" -in "+masks_folder+'/'+outputfile+" -out "+masks_folder+'/'+outputfile+" -thr "+threshold) print 'Done Thresholding' print 'Starting Masking' # this command places the stripped images in the stripped_images/ folder. os.system(regtoolspath+" -in "+inputfile+" -nan "+masks_folder+'/'+outputfile+ " -out " + stripped_folder+'/'+outputfile + "_stripped") print 'Done Masking'