def pad_im(fname_im, nx_full, ny_full, nz_full, xi, xf, yi, yf, zi, zf): fname_im_pad = sct.add_suffix(fname_im, "_pad") pad_xi = str(xi) pad_xf = str(nx_full - (xf + 1)) pad_yi = str(yi) pad_yf = str(ny_full - (yf + 1)) pad_zi = str(zi) pad_zf = str(nz_full - (zf + 1)) pad = ",".join([pad_xi, pad_xf, pad_yi, pad_yf, pad_zi, pad_zf]) if len(Image(fname_im).data.shape) == 5: status, output = sct.run("sct_image -i " + fname_im + " -mcs") s = "Created file(s):\n-->" output_fnames = output[output.find(s) + len(s) :].split("\n")[0].split("'") fname_comp_list = [output_fnames[i] for i in range(1, len(output_fnames), 2)] fname_comp_pad_list = [] for fname_comp in fname_comp_list: fname_comp_pad = sct.add_suffix(fname_comp, "_pad") sct.run("sct_image -i " + fname_comp + " -pad-asym " + pad + " -o " + fname_comp_pad) fname_comp_pad_list.append(fname_comp_pad) components = ",".join(fname_comp_pad_list) sct.run("sct_image -i " + components + " -omc -o " + fname_im_pad) sct.check_file_exist(fname_im_pad, verbose=1) else: sct.run("sct_image -i " + fname_im + " -pad-asym " + pad + " -o " + fname_im_pad) return fname_im_pad
def pad_im(fname_im, nx_full, ny_full, nz_full, xi, xf, yi, yf, zi, zf): fname_im_pad = sct.add_suffix(fname_im, '_pad') pad_xi = str(xi) pad_xf = str(nx_full - (xf + 1)) pad_yi = str(yi) pad_yf = str(ny_full - (yf + 1)) pad_zi = str(zi) pad_zf = str(nz_full - (zf + 1)) pad = ','.join([pad_xi, pad_xf, pad_yi, pad_yf, pad_zi, pad_zf]) if len(Image(fname_im).data.shape) == 5: status, output = sct.run('sct_image -i ' + fname_im + ' -mcs') s = 'Created file(s):\n-->' output_fnames = output[output.find(s) + len(s):].split('\n')[0].split("'") fname_comp_list = [ output_fnames[i] for i in range(1, len(output_fnames), 2) ] fname_comp_pad_list = [] for fname_comp in fname_comp_list: fname_comp_pad = sct.add_suffix(fname_comp, '_pad') sct.run('sct_image -i ' + fname_comp + ' -pad-asym ' + pad + ' -o ' + fname_comp_pad) fname_comp_pad_list.append(fname_comp_pad) components = ','.join(fname_comp_pad_list) sct.run('sct_image -i ' + components + ' -omc -o ' + fname_im_pad) sct.check_file_exist(fname_im_pad, verbose=1) else: sct.run('sct_image -i ' + fname_im + ' -pad-asym ' + pad + ' -o ' + fname_im_pad) return fname_im_pad
def main(): # Initialization path_script = os.path.dirname(__file__) fsloutput = "export FSLOUTPUTTYPE=NIFTI; " # for faster processing, all outputs are in NIFTI # THIS DOES NOT WORK IN MY LAPTOP: path_sct = os.environ['SCT_DIR'] # path to spinal cord toolbox # path_sct = path_script[:-8] # TODO: make it cleaner! status, path_sct = commands.getstatusoutput("echo $SCT_DIR") fname_segmentation = "" name_process = "" processes = ["extract_centerline", "compute_CSA"] verbose = param.verbose start_time = time.time() remove_temp_files = param.remove_temp_files # Parameters for debug mode if param.debug: fname_segmentation = path_sct + "/testing/data/errsm_23/t2/t2_manual_segmentation.nii.gz" verbose = 1 remove_temp_files = 0 # Check input parameters try: opts, args = getopt.getopt(sys.argv[1:], "hi:p:v:") except getopt.GetoptError: usage() for opt, arg in opts: if opt == "-h": usage() elif opt in ("-i"): fname_segmentation = arg elif opt in ("-p"): name_process = arg elif opt in ("-v"): verbose = int(arg) # display usage if a mandatory argument is not provided if fname_segmentation == "" or name_process == "": usage() # display usage if the requested process is not available if name_process not in processes: usage() # check existence of input files sct.check_file_exist(fname_segmentation) # print arguments print "\nCheck parameters:" print ".. segmentation file: " + fname_segmentation if name_process == "extract_centerline": extract_centerline(fname_segmentation) if name_process == "compute_CSA": compute_CSA(fname_segmentation) # display elapsed time elapsed_time = time.time() - start_time print "\nFinished! Elapsed time: " + str(int(round(elapsed_time))) + "s"
def main(): # Initialization fname_input = '' fname_segmentation = '' if param.debug: print '\n*** WARNING: DEBUG MODE ON ***\n' status, path_sct_data = commands.getstatusoutput( 'echo $SCT_TESTING_DATA_DIR') fname_input = '' fname_segmentation = path_sct_data + '/t2/t2_seg.nii.gz' else: # Check input param try: opts, args = getopt.getopt(sys.argv[1:], 'hi:t:') except getopt.GetoptError as err: print str(err) usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ('-i'): fname_input = arg elif opt in ('-t'): fname_segmentation = arg # display usage if a mandatory argument is not provided if fname_segmentation == '' or fname_input == '': usage() # check existence of input files sct.check_file_exist(fname_input) sct.check_file_exist(fname_segmentation) # read nifti input file img = nibabel.load(fname_input) # 3d array for each x y z voxel values for the input nifti image data = img.get_data() # read nifti input file img_seg = nibabel.load(fname_segmentation) # 3d array for each x y z voxel values for the input nifti image data_seg = img_seg.get_data() X, Y, Z = (data > 0).nonzero() status = 0 for i in range(0, len(X)): if data_seg[X[i], Y[i], Z[i]] == 0: status = 1 break if status is not 0: sct.printv('ERROR: detected point is not in segmentation', 1, 'warning') else: sct.printv('OK: detected point is in segmentation') sys.exit(status)
def main(): # Initialization fname_input = '' fname_segmentation = '' if param.debug: sct.printv( '\n*** WARNING: DEBUG MODE ON ***\n') path_sct_data = os.path.join(sct.__data_dir__, "sct_testing_data") fname_input = '' fname_segmentation = os.path.join(path_sct_data, 't2', 't2_seg.nii.gz') else: # Check input param try: opts, args = getopt.getopt(sys.argv[1:], 'hi:t:') except getopt.GetoptError as err: logger.error(str(err)) usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ('-i'): fname_input = arg elif opt in ('-t'): fname_segmentation = arg # display usage if a mandatory argument is not provided if fname_segmentation == '' or fname_input == '': usage() # check existence of input files sct.check_file_exist(fname_input) sct.check_file_exist(fname_segmentation) # read nifti input file img = nibabel.load(fname_input) # 3d array for each x y z voxel values for the input nifti image data = img.get_data() # read nifti input file img_seg = nibabel.load(fname_segmentation) # 3d array for each x y z voxel values for the input nifti image data_seg = img_seg.get_data() X, Y, Z = (data > 0).nonzero() status = 0 for i in range(0, len(X)): if data_seg[X[i], Y[i], Z[i]] == 0: status = 1 break; if status is not 0: sct.printv('ERROR: detected point is not in segmentation', 1, 'warning') else: sct.printv('OK: detected point is in segmentation') sys.exit(status)
def main(): #Initialization fname = '' verbose = param.verbose start = '' end = '' try: opts, args = getopt.getopt(sys.argv[1:],'hi:e:s:v:') except getopt.GetoptError: usage() for opt, arg in opts : if opt == '-h': usage() elif opt in ("-i"): fname = arg elif opt in ('-s'): start = int(arg) elif opt in ('-e'): end = int(arg) elif opt in ('-v'): verbose = int(arg) # display usage if a mandatory argument is not provided if fname == '' : usage() # check existence of input files print'\nCheck if file exists ...' sct.check_file_exist(fname) # Display arguments print'\nCheck input arguments...' print' Input volume ...................... '+fname print' Verbose ........................... '+str(verbose) file = nibabel.load(fname) data = file.get_data() hdr = file.get_header() for i in range(start,end+1): data[:,:,i] = 0 print '\nSave volume ...' hdr.set_data_dtype('float32') # set imagetype to uint8 # save volume #data = data.astype(float32, copy =False) img = nibabel.Nifti1Image(data, None, hdr) file_name = 'centerline_erased.nii.gz' nibabel.save(img,file_name)
def main(): # Initialization fname_input = '' fname_segmentation = '' if param.debug: print '\n*** WARNING: DEBUG MODE ON ***\n' status, path_sct_data = commands.getstatusoutput('echo $SCT_TESTING_DATA_DIR') fname_input = '' fname_segmentation = path_sct_data+'/t2/t2_seg.nii.gz' else: # Check input param try: opts, args = getopt.getopt(sys.argv[1:],'hi:t:') except getopt.GetoptError as err: print str(err) usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ('-i'): fname_input = arg elif opt in ('-t'): fname_segmentation = arg # display usage if a mandatory argument is not provided if fname_segmentation == '' or fname_input == '': usage() # check existence of input files sct.check_file_exist(fname_input) sct.check_file_exist(fname_segmentation) # read nifti input file img = nibabel.load(fname_input) # 3d array for each x y z voxel values for the input nifti image data = img.get_data() # read nifti input file img_seg = nibabel.load(fname_segmentation) # 3d array for each x y z voxel values for the input nifti image data_seg = img_seg.get_data() X, Y, Z = (data>0).nonzero() status = 0 for i in range(0,len(X)): if data_seg(X[i],Y[i],Z[i]) == 0: status = 1 break; if status is not 0: sct.printv('ERROR: detected point is not in segmentation',1,'error') else: sct.printv('OK: detected point is in segmentation') sys.exit(0)
def main(): #Initialization fname = '' verbose = param.verbose try: opts, args = getopt.getopt(sys.argv[1:],'hi:v:') except getopt.GetoptError: usage() for opt, arg in opts : if opt == '-h': usage() elif opt in ("-i"): fname = arg elif opt in ('-v'): verbose = int(arg) # display usage if a mandatory argument is not provided if fname == '' : usage() # check existence of input files print'\nCheck if file exists ...' sct.check_file_exist(fname) # Display arguments print'\nCheck input arguments...' print' Input volume ...................... '+fname print' Verbose ........................... '+str(verbose) file = nibabel.load(fname) data = file.get_data() hdr = file.get_header() X,Y,Z = (data>0).nonzero() x_max,y_max = (data[:,:,max(Z)]).nonzero() x_max = x_max[0] y_max = y_max[0] z_max = max(Z) x_min,y_min = (data[:,:,min(Z)]).nonzero() x_min = x_min[0] y_min = y_min[0] z_min = min(Z) del data print 'Coords extrema : min [ ' + str(x_min) + ' ,' + str(y_min) + ' ,' + str(z_min) +' ] max [ ' + str(x_max) + ' ,' + str(y_max) + ' ,' + str(z_max) + ' ]' return z_min,z_max
def segmentation(fname_input, output_dir, image_type): # parameters path_in, file_in, ext_in = sct.extract_fname(fname_input) # define command cmd = 'sct_propseg_test -i ' + fname_input \ + ' -o ' + output_dir \ + ' -t ' + image_type \ + ' -detect-nii' \ status, output = sct.run(cmd) # check if spinal cord is correctly detected # sct_propseg return one point # check existence of input files segmentation_filename = path_in + file_in + '_seg' + ext_in manual_segmentation_filename = path_in + 'manual_' + file_in + ext_in detection_filename = path_in + file_in + '_detection' + ext_in sct.check_file_exist(detection_filename) sct.check_file_exist(segmentation_filename) # read nifti input file img = nibabel.load(detection_filename) # 3d array for each x y z voxel values for the input nifti image data = img.get_data() # read nifti input file img_seg = nibabel.load(manual_segmentation_filename) # 3d array for each x y z voxel values for the input nifti image data_seg = img_seg.get_data() X, Y, Z = (data > 0).nonzero() status = 0 for i in range(0, len(X)): if data_seg[X[i], Y[i], Z[i]] == 0: status = 1 break if status is not 0: sct.printv('ERROR: detected point is not in segmentation', 1, 'warning') else: sct.printv('OK: detected point is in segmentation') cmd_validation = 'sct_dice_coefficient ' + segmentation_filename \ + ' ' + manual_segmentation_filename \ + ' -bzmax' status_validation, output = sct.run(cmd_validation) print output return status
def segmentation(fname_input, output_dir, image_type): # parameters path_in, file_in, ext_in = sct.extract_fname(fname_input) # define command cmd = 'sct_propseg_test -i ' + fname_input \ + ' -o ' + output_dir \ + ' -t ' + image_type \ + ' -detect-nii' \ status, output = sct.run(cmd) # check if spinal cord is correctly detected # sct_propseg return one point # check existence of input files segmentation_filename = path_in + file_in + '_seg' + ext_in manual_segmentation_filename = path_in + 'manual_' + file_in + ext_in detection_filename = path_in + file_in + '_detection' + ext_in sct.check_file_exist(detection_filename) sct.check_file_exist(segmentation_filename) # read nifti input file img = nibabel.load(detection_filename) # 3d array for each x y z voxel values for the input nifti image data = img.get_data() # read nifti input file img_seg = nibabel.load(manual_segmentation_filename) # 3d array for each x y z voxel values for the input nifti image data_seg = img_seg.get_data() X, Y, Z = (data>0).nonzero() status = 0 for i in range(0,len(X)): if data_seg[X[i],Y[i],Z[i]] == 0: status = 1 break; if status is not 0: sct.printv('ERROR: detected point is not in segmentation',1,'warning') else: sct.printv('OK: detected point is in segmentation') cmd_validation = 'sct_dice_coefficient ' + segmentation_filename \ + ' ' + manual_segmentation_filename \ + ' -bzmax' status_validation, output = sct.run(cmd_validation) print output return status
def read_label_file(path_info_label): # file name of info_label.txt fname_label = path_info_label + param.file_info_label # Check info_label.txt existence sct.check_file_exist(fname_label) # Read file f = open(fname_label) # Extract all lines in file.txt lines = [lines for lines in f.readlines() if lines.strip()] # separate header from (every line starting with "#") lines = [lines[i] for i in range(0, len(lines)) if lines[i][0] != '#'] # read each line label_id = [] label_name = [] label_file = [] for i in range(0, len(lines) - 1): line = lines[i].split(',') label_id.append(int(line[0])) label_name.append(line[1]) label_file.append(line[2][:-1].strip()) # An error could occur at the last line (deletion of the last character of the .txt file), the 5 following code # lines enable to avoid this error: line = lines[-1].split(',') label_id.append(int(line[0])) label_name.append(line[1]) line[2] = line[2] + ' ' label_file.append(line[2].strip()) # check if all files listed are present in folder. If not, WARNING. sct.printv('\nCheck existence of all files listed in ' + param.file_info_label + ' ...') for fname in label_file: if os.path.isfile(path_info_label + fname) or os.path.isfile(path_info_label + fname + '.nii') or \ os.path.isfile(path_info_label + fname + '.nii.gz'): sct.printv(' OK: ' + path_info_label + fname) else: sct.printv(' WARNING: ' + path_info_label + fname + ' does not exist but is listed in ' + param.file_info_label + '.\n') # Close file.txt f.close() return [label_id, label_name, label_file]
def read_label_file(path_info_label): # file name of info_label.txt fname_label = path_info_label+param.file_info_label # Check info_label.txt existence sct.check_file_exist(fname_label) # Read file f = open(fname_label) # Extract all lines in file.txt lines = [lines for lines in f.readlines() if lines.strip()] # separate header from (every line starting with "#") lines = [lines[i] for i in range(0, len(lines)) if lines[i][0] != '#'] # read each line label_id = [] label_name = [] label_file = [] for i in range(0, len(lines)-1): line = lines[i].split(',') label_id.append(int(line[0])) label_name.append(line[1]) label_file.append(line[2][:-1].strip()) # An error could occur at the last line (deletion of the last character of the .txt file), the 5 following code # lines enable to avoid this error: line = lines[-1].split(',') label_id.append(int(line[0])) label_name.append(line[1]) line[2]=line[2]+' ' label_file.append(line[2].strip()) # check if all files listed are present in folder. If not, WARNING. print '\nCheck existence of all files listed in '+param.file_info_label+' ...' for fname in label_file: if os.path.isfile(path_info_label+fname) or os.path.isfile(path_info_label+fname + '.nii') or \ os.path.isfile(path_info_label+fname + '.nii.gz'): print(' OK: '+path_info_label+fname) pass else: print(' WARNING: ' + path_info_label+fname + ' does not exist but is listed in ' +param.file_info_label+'.\n') # Close file.txt f.close() return [label_id, label_name, label_file]
def loadFromPath(self, path, verbose): """ This function load an image from an absolute path using nibabel library :param path: path of the file from which the image will be loaded :return: """ from nibabel import load, spatialimages from sct_utils import check_file_exist, printv, extract_fname from sct_orientation import get_orientation check_file_exist(path, verbose=verbose) try: im_file = load(path) except spatialimages.ImageFileError: printv('Error: make sure ' + path + ' is an image.', 1, 'error') self.orientation = get_orientation(path) self.data = im_file.get_data() self.hdr = im_file.get_header() self.absolutepath = path self.path, self.file_name, self.ext = extract_fname(path)
def check_do_files_exist(fname_template, fname_template_vertebral_labeling, fname_template_seg, verbose): # TODO: no need to do that! sct.printv('\nCheck template files...') sct.check_file_exist(fname_template, verbose) sct.check_file_exist(fname_template_vertebral_labeling, verbose) sct.check_file_exist(fname_template_seg, verbose)
def loadFromPath(self, path, verbose): """ This function load an image from an absolute path using nibabel library :param path: path of the file from which the image will be loaded :return: """ from nibabel import load, spatialimages from sct_utils import check_file_exist, printv, extract_fname, get_dimension from sct_orientation import get_orientation check_file_exist(path, verbose=verbose) try: im_file = load(path) except spatialimages.ImageFileError: printv('Error: make sure ' + path + ' is an image.', 1, 'error') self.orientation = get_orientation(path) self.data = im_file.get_data() self.hdr = im_file.get_header() self.absolutepath = path self.path, self.file_name, self.ext = extract_fname(path) nx, ny, nz, nt, px, py, pz, pt = get_dimension(path) self.dim = [nx, ny, nz]
def get_or_set_orientation(): fsloutput = 'export FSLOUTPUTTYPE=NIFTI; ' # for faster processing, all outputs are in NIFTI # display usage if a mandatory argument is not provided if param.fname_data == '': sct.printv('ERROR: All mandatory arguments are not provided. See usage.', 1, 'error') # check existence of input files sct.printv('\ncheck existence of input files...', param.verbose) sct.check_file_exist(param.fname_data, param.verbose) # find what to do if param.orientation == '': todo = 'get_orientation' else: todo = 'set_orientation' # check if orientation is correct if check_orientation_input(): sct.printv('\nERROR in '+os.path.basename(__file__)+': orientation is not recognized. Use one of the following orientation: '+param.list_of_correct_orientation+'\n', 1, 'error') sys.exit(2) # display input parameters sct.printv('\nInput parameters:', param.verbose) sct.printv(' data ..................'+param.fname_data, param.verbose) # Extract path/file/extension path_data, file_data, ext_data = sct.extract_fname(param.fname_data) if param.fname_out == '': # path_out, file_out, ext_out = '', file_data+'_'+param.orientation, ext_data fname_out = path_data+file_data+'_'+param.orientation+ext_data else: fname_out = param.fname_out # create temporary folder sct.printv('\nCreate temporary folder...', param.verbose) path_tmp = sct.slash_at_the_end('tmp.'+time.strftime("%y%m%d%H%M%S"), 1) sct.run('mkdir '+path_tmp, param.verbose) # Copying input data to tmp folder and convert to nii # NB: cannot use c3d here because c3d cannot convert 4D data. sct.printv('\nCopying input data to tmp folder and convert to nii...', param.verbose) sct.run('cp '+param.fname_data+' '+path_tmp+'data'+ext_data, param.verbose) # go to tmp folder os.chdir(path_tmp) # convert to nii format sct.run('fslchfiletype NIFTI data', param.verbose) # Get dimensions of data sct.printv('\nGet dimensions of data...', param.verbose) nx, ny, nz, nt, px, py, pz, pt = sct.get_dimension('data.nii') sct.printv(' ' + str(nx) + ' x ' + str(ny) + ' x ' + str(nz)+ ' x ' + str(nt), param.verbose) # if 4d, loop across the data if nt == 1: if todo == 'set_orientation': # set orientation sct.printv('\nChange orientation...', param.verbose) set_orientation('data.nii', param.orientation, 'data_orient.nii') elif todo == 'get_orientation': # get orientation sct.printv('\nGet orientation...', param.verbose) sct.printv(get_orientation('data.nii'), 1) else: # split along T dimension sct.printv('\nSplit along T dimension...', param.verbose) sct.run(fsloutput+'fslsplit data data_T', param.verbose) if todo == 'set_orientation': # set orientation sct.printv('\nChange orientation...', param.verbose) for it in range(nt): file_data_split = 'data_T'+str(it).zfill(4)+'.nii' file_data_split_orient = 'data_orient_T'+str(it).zfill(4)+'.nii' set_orientation(file_data_split, param.orientation, file_data_split_orient) # Merge files back sct.printv('\nMerge file back...', param.verbose) cmd = fsloutput+'fslmerge -t data_orient' for it in range(nt): file_data_split_orient = 'data_orient_T'+str(it).zfill(4)+'.nii' cmd = cmd+' '+file_data_split_orient sct.run(cmd, param.verbose) elif todo == 'get_orientation': sct.printv('\nGet orientation...', param.verbose) sct.printv(get_orientation('data_T0000.nii'), 1) # come back to parent folder os.chdir('..') # Generate output files if todo == 'set_orientation': sct.printv('\nGenerate output files...', param.verbose) sct.generate_output_file(path_tmp+'data_orient.nii', fname_out) # Remove temporary files if param.remove_tmp_files == 1: sct.printv('\nRemove temporary files...', param.verbose) sct.run('rm -rf '+path_tmp, param.verbose) # to view results if todo == 'set_orientation': sct.printv('\nDone! To view results, type:', param.verbose) sct.printv('fslview '+fname_out+' &', param.verbose, 'code') print
def __init__(self, fname_src, fname_transfo, warp_atlas, warp_spinal_levels, folder_out, path_template, verbose): # Initialization self.fname_src = '' self.fname_transfo = '' self.folder_out = param.folder_out self.path_template = param.path_template self.folder_template = param.folder_template self.folder_atlas = param.folder_atlas self.folder_spinal_levels = param.folder_spinal_levels self.warp_template = param.warp_template self.warp_atlas = param.warp_atlas self.warp_spinal_levels = param.warp_spinal_levels self.verbose = param.verbose start_time = time.time() # Parameters for debug mode if param.debug: print '\n*** WARNING: DEBUG MODE ON ***\n' fname_src = path_sct+'/testing/sct_testing_data/data/mt/mtr.nii.gz' fname_transfo = path_sct+'/testing/sct_testing_data/data/mt/warp_template2mt.nii.gz' warp_atlas = 1 warp_spinal_levels = 1 verbose = 1 else: self.fname_src = fname_src self.fname_transfo = fname_transfo self.warp_atlas = warp_atlas self.warp_spinal_levels = warp_spinal_levels self.folder_out = folder_out self.path_template = path_template self.verbose = verbose # Check file existence sct.printv('\nCheck file existence...', self.verbose) sct.check_file_exist(self.fname_src) sct.check_file_exist(self.fname_transfo) # add slash at the end of folder name (in case there is no slash) self.path_template = sct.slash_at_the_end(self.path_template, 1) self.folder_out = sct.slash_at_the_end(self.folder_out, 1) self.folder_template = sct.slash_at_the_end(self.folder_template, 1) self.folder_atlas = sct.slash_at_the_end(self.folder_atlas, 1) self.folder_spinal_levels = sct.slash_at_the_end(self.folder_spinal_levels, 1) # print arguments print '\nCheck parameters:' print ' Destination image ........ '+self.fname_src print ' Warping field ............ '+self.fname_transfo print ' Path template ............ '+self.path_template print ' Output folder ............ '+self.folder_out+'\n' # Extract path, file and extension path_src, file_src, ext_src = sct.extract_fname(self.fname_src) # create output folder if os.path.exists(self.folder_out): sct.printv('WARNING: Output folder already exists. Deleting it...', self.verbose) sct.run('rm -rf '+self.folder_out) sct.run('mkdir '+self.folder_out) # Warp template objects if self.warp_template == 1: sct.printv('\nWarp template objects...', self.verbose) warp_label(self.path_template, self.folder_template, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # Warp atlas if self.warp_atlas == 1: sct.printv('\nWarp atlas of white matter tracts...', self.verbose) warp_label(self.path_template, self.folder_atlas, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # Warp spinal levels if self.warp_spinal_levels == 1: sct.printv('\nWarp spinal levels...', self.verbose) warp_label(self.path_template, self.folder_spinal_levels, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # to view results sct.printv('\nDone! To view results, type:', self.verbose) sct.printv('fslview '+self.fname_src+' '+self.folder_out+self.folder_template+'MNI-Poly-AMU_T2.nii.gz -b 0,4000 '+self.folder_out+self.folder_template+'MNI-Poly-AMU_level.nii.gz -l MGH-Cortical -t 0.5 '+self.folder_out+self.folder_template+'MNI-Poly-AMU_GM.nii.gz -l Red-Yellow -b 0.5,1 '+self.folder_out+self.folder_template+'MNI-Poly-AMU_WM.nii.gz -l Blue-Lightblue -b 0.5,1 &\n', self.verbose, 'info')
def __init__(self, fname_src, fname_transfo, warp_atlas, warp_spinal_levels, folder_out, path_template, verbose, qc): # Initialization self.fname_src = fname_src self.fname_transfo = fname_transfo self.warp_atlas = warp_atlas self.warp_spinal_levels = warp_spinal_levels self.folder_out = folder_out self.path_template = path_template self.folder_template = param.folder_template self.folder_atlas = param.folder_atlas self.folder_spinal_levels = param.folder_spinal_levels self.verbose = verbose self.qc = qc start_time = time.time() # Check file existence sct.printv('\nCheck file existence...', self.verbose) sct.check_file_exist(self.fname_src) sct.check_file_exist(self.fname_transfo) # add slash at the end of folder name (in case there is no slash) self.path_template = sct.slash_at_the_end(self.path_template, 1) self.folder_out = sct.slash_at_the_end(self.folder_out, 1) self.folder_template = sct.slash_at_the_end(self.folder_template, 1) self.folder_atlas = sct.slash_at_the_end(self.folder_atlas, 1) self.folder_spinal_levels = sct.slash_at_the_end(self.folder_spinal_levels, 1) # print arguments print '\nCheck parameters:' print ' Destination image ........ '+self.fname_src print ' Warping field ............ '+self.fname_transfo print ' Path template ............ '+self.path_template print ' Output folder ............ '+self.folder_out+'\n' # Extract path, file and extension path_src, file_src, ext_src = sct.extract_fname(self.fname_src) # create output folder if os.path.exists(self.folder_out): sct.printv('WARNING: Output folder already exists. Deleting it...', self.verbose, 'warning') sct.run('rm -rf '+self.folder_out) sct.run('mkdir '+self.folder_out) # Warp template objects sct.printv('\nWarp template objects...', self.verbose) warp_label(self.path_template, self.folder_template, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # Warp atlas if self.warp_atlas == 1: sct.printv('\nWarp atlas of white matter tracts...', self.verbose) warp_label(self.path_template, self.folder_atlas, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # Warp spinal levels if self.warp_spinal_levels == 1: sct.printv('\nWarp spinal levels...', self.verbose) warp_label(self.path_template, self.folder_spinal_levels, param.file_info_label, self.fname_src, self.fname_transfo, self.folder_out) # to view results sct.printv('\nDone! To view results, type:', self.verbose) sct.printv('fslview '+self.fname_src+' '+self.folder_out+self.folder_template+'MNI-Poly-AMU_T2.nii.gz -b 0,4000 '+self.folder_out+self.folder_template+'MNI-Poly-AMU_level.nii.gz -l MGH-Cortical -t 0.5 '+self.folder_out+self.folder_template+'MNI-Poly-AMU_GM.nii.gz -l Red-Yellow -b 0.5,1 '+self.folder_out+self.folder_template+'MNI-Poly-AMU_WM.nii.gz -l Blue-Lightblue -b 0.5,1 &\n', self.verbose, 'info') if self.qc: from msct_image import Image # output QC image im = Image(self.fname_src) im_wm = Image(self.folder_out+self.folder_template+'MNI-Poly-AMU_WM.nii.gz') im.save_quality_control(plane='axial', n_slices=4, seg=im_wm, thr=0.5, cmap_col='blue-cyan', path_output=self.folder_out)
def main(): # Initialization fname_src = '' fname_transfo = '' folder_out = param.folder_out path_template = param.path_template folder_template = param.folder_template folder_atlas = param.folder_atlas folder_spinal_levels = param.folder_spinal_levels file_info_label = param.file_info_label warp_template = param.warp_template warp_atlas = param.warp_atlas warp_spinal_levels = param.warp_spinal_levels verbose = param.verbose start_time = time.time() # Parameters for debug mode if param.debug: print '\n*** WARNING: DEBUG MODE ON ***\n' fname_src = path_sct+'/testing/sct_testing_data/data/mt/mtr.nii.gz' fname_transfo = path_sct+'/testing/sct_testing_data/data/mt/warp_template2mt.nii.gz' warp_atlas = 1 warp_spinal_levels = 1 verbose = 1 else: # Check input parameters try: opts, args = getopt.getopt(sys.argv[1:], 'ha:d:w:o:s:t:v:') except getopt.GetoptError: usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ("-a"): warp_atlas = int(arg) elif opt in ("-d"): fname_src = arg elif opt in ("-o"): folder_out = arg elif opt in ("-t"): path_template = arg elif opt in ("-s"): warp_spinal_levels = int(arg) elif opt in ('-v'): verbose = int(arg) elif opt in ("-w"): fname_transfo = arg # display usage if a mandatory argument is not provided if fname_src == '' or fname_transfo == '': usage() # Check file existence sct.printv('\nCheck file existence...', verbose) sct.check_file_exist(fname_src) sct.check_file_exist(fname_transfo) # add slash at the end of folder name (in case there is no slash) path_template = sct.slash_at_the_end(path_template, 1) folder_out = sct.slash_at_the_end(folder_out, 1) folder_template = sct.slash_at_the_end(folder_template, 1) folder_atlas = sct.slash_at_the_end(folder_atlas, 1) folder_spinal_levels = sct.slash_at_the_end(folder_spinal_levels, 1) # print arguments print '\nCheck parameters:' print ' Destination image ........ '+fname_src print ' Warping field ............ '+fname_transfo print ' Path template ............ '+path_template print ' Output folder ............ '+folder_out+'\n' # Extract path, file and extension path_src, file_src, ext_src = sct.extract_fname(fname_src) # create output folder if os.path.exists(folder_out): sct.printv('WARNING: Output folder already exists. Deleting it...', verbose) sct.run('rm -rf '+folder_out) sct.run('mkdir '+folder_out) # Warp template objects if warp_template == 1: sct.printv('\nWarp template objects...', verbose) warp_label(path_template, folder_template, param.file_info_label, fname_src, fname_transfo, folder_out) # Warp atlas if warp_atlas == 1: sct.printv('\nWarp atlas of white matter tracts...', verbose) warp_label(path_template, folder_atlas, param.file_info_label, fname_src, fname_transfo, folder_out) # Warp spinal levels if warp_spinal_levels == 1: sct.printv('\nWarp spinal levels...', verbose) warp_label(path_template, folder_spinal_levels, param.file_info_label, fname_src, fname_transfo, folder_out) # to view results sct.printv('\nDone! To view results, type:', verbose) sct.printv('fslview '+fname_src+' '+folder_out+folder_template+'MNI-Poly-AMU_T2.nii.gz -b 0,4000 '+folder_out+folder_template+'MNI-Poly-AMU_level.nii.gz -l MGH-Cortical -t 0.5 '+folder_out+folder_template+'MNI-Poly-AMU_GM.nii.gz -l Red-Yellow -b 0.5,1 '+folder_out+folder_template+'MNI-Poly-AMU_WM.nii.gz -l Blue-Lightblue -b 0.5,1 &\n', verbose, 'info')
def main(): # get path of the toolbox status, path_sct = getstatusoutput('echo $SCT_DIR') #print path_sct #Initialization fname = '' landmarks_native = '' landmarks_template = path_sct + '/dev/template_creation/template_landmarks-mm.nii.gz' reference = path_sct + '/dev/template_creation/template_shape.nii.gz' verbose = param.verbose interpolation_method = 'spline' try: opts, args = getopt.getopt(sys.argv[1:],'hi:n:t:R:v:a:') except getopt.GetoptError: usage() for opt, arg in opts : if opt == '-h': usage() elif opt in ("-i"): fname = arg elif opt in ("-n"): landmarks_native = arg elif opt in ("-t"): landmarks_template = arg elif opt in ("-R"): reference = arg elif opt in ('-v'): verbose = int(arg) elif opt in ('-a'): interpolation_method = str(arg) # display usage if a mandatory argument is not provided if fname == '' : usage() # check existence of input files print'\nCheck if file exists ...' sct.check_file_exist(fname) sct.check_file_exist(landmarks_native) sct.check_file_exist(landmarks_template) sct.check_file_exist(reference) path_input, file_input, ext_input = sct.extract_fname(fname) output_name = path_input + file_input + '_2temp' + ext_input print output_name transfo = 'native2temp.txt' # Display arguments print'\nCheck input arguments...' print' Input volume ...................... '+fname print' Landmarks in native space ...................... '+landmarks_native print' Landmarks in template space ...................... '+landmarks_template print' Reference ...................... '+reference print' Verbose ........................... '+str(verbose) print '\nEstimate rigid transformation between paired landmarks...' sct.run('ANTSUseLandmarkImagesToGetAffineTransform ' + landmarks_template + ' '+ landmarks_native + ' affine ' + transfo) # Apply rigid transformation print '\nApply affine transformation to native landmarks...' sct.run('sct_apply_transfo -i ' + fname + ' -o ' + output_name + ' -d ' + reference + ' -w ' + transfo +' -x ' + interpolation_method) # sct.run('WarpImageMultiTransform 3 ' + fname + ' ' + output_name + ' -R ' + reference + ' ' + transfo) print '\nFile created : ' + output_name
def crop_with_gui(self): import matplotlib.pyplot as plt import matplotlib.image as mpimg # Initialization fname_data = self.input_filename suffix_out = '_crop' remove_temp_files = self.rm_tmp_files verbose = self.verbose # Check file existence sct.printv('\nCheck file existence...', verbose) sct.check_file_exist(fname_data, verbose) # Get dimensions of data sct.printv('\nGet dimensions of data...', verbose) nx, ny, nz, nt, px, py, pz, pt = Image(fname_data).dim sct.printv('.. ' + str(nx) + ' x ' + str(ny) + ' x ' + str(nz), verbose) # check if 4D data if not nt == 1: sct.printv('\nERROR in ' + os.path.basename(__file__) + ': Data should be 3D.\n', 1, 'error') sys.exit(2) # sct.printv(arguments) sct.printv('\nCheck parameters:') sct.printv(' data ................... ' + fname_data) # Extract path/file/extension path_data, file_data, ext_data = sct.extract_fname(fname_data) path_out, file_out, ext_out = '', file_data + suffix_out, ext_data path_tmp = sct.tmp_create() + "/" # copy files into tmp folder from sct_convert import convert sct.printv('\nCopying input data to tmp folder and convert to nii...', verbose) convert(fname_data, os.path.join(path_tmp, "data.nii")) # go to tmp folder curdir = os.getcwd() os.chdir(path_tmp) # change orientation sct.printv('\nChange orientation to RPI...', verbose) Image('data.nii').change_orientation("RPI").save('data_rpi.nii') # get image of medial slab sct.printv('\nGet image of medial slab...', verbose) image_array = nibabel.load('data_rpi.nii').get_data() nx, ny, nz = image_array.shape scipy.misc.imsave('image.jpg', image_array[math.floor(nx / 2), :, :]) # Display the image sct.printv('\nDisplay image and get cropping region...', verbose) fig = plt.figure() # fig = plt.gcf() # ax = plt.gca() ax = fig.add_subplot(111) img = mpimg.imread("image.jpg") implot = ax.imshow(img.T) implot.set_cmap('gray') plt.gca().invert_yaxis() # mouse callback ax.set_title('Left click on the top and bottom of your cropping field.\n Right click to remove last point.\n Close window when your done.') line, = ax.plot([], [], 'ro') # empty line cropping_coordinates = LineBuilder(line) plt.show() # disconnect callback # fig.canvas.mpl_disconnect(line) # check if user clicked two times if len(cropping_coordinates.xs) != 2: sct.printv('\nERROR: You have to select two points. Exit program.\n', 1, 'error') sys.exit(2) # convert coordinates to integer zcrop = [int(i) for i in cropping_coordinates.ys] # sort coordinates zcrop.sort() # crop image sct.printv('\nCrop image...', verbose) nii = Image('data_rpi.nii') data_crop = nii.data[:, :, zcrop[0]:zcrop[1]] nii.data = data_crop nii.absolutepath = 'data_rpi_crop.nii' nii.save() # come back os.chdir(curdir) sct.printv('\nGenerate output files...', verbose) sct.generate_output_file(os.path.join(path_tmp, "data_rpi_crop.nii"), os.path.join(path_out, file_out + ext_out)) # Remove temporary files if remove_temp_files == 1: sct.printv('\nRemove temporary files...') sct.rmtree(path_tmp) sct.display_viewer_syntax(files=[os.path.join(path_out, file_out + ext_out)])
def main(): # Initialization path_script = os.path.dirname(__file__) fsloutput = 'export FSLOUTPUTTYPE=NIFTI; ' # for faster processing, all outputs are in NIFTI # THIS DOES NOT WORK IN MY LAPTOP: path_sct = os.environ['SCT_DIR'] # path to spinal cord toolbox path_sct = path_script[:-8] # TODO: make it cleaner! fname_data = '' fname_bvecs = '' verbose = param.verbose start_time = time.time() # Parameters for debug mode if param.debug: fname_data = os.path.expanduser("~")+'/code/spinalcordtoolbox_dev/testing/data/errsm_22/dmri/dmri.nii.gz' fname_bvecs = os.path.expanduser("~")+'/code/spinalcordtoolbox_dev/testing/data/errsm_22/dmri/bvecs.txt' verbose = 1 # Check input parameters try: opts, args = getopt.getopt(sys.argv[1:],'hb:i:v:') except getopt.GetoptError: usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ("-b"): fname_bvecs = arg elif opt in ("-i"): fname_data = arg elif opt in ('-v'): verbose = int(arg) # display usage if a mandatory argument is not provided if fname_data == '' or fname_bvecs == '': usage() # check existence of input files sct.check_file_exist(fname_data) sct.check_file_exist(fname_bvecs) # print arguments print '\nCheck parameters:' print '.. DWI data: '+fname_data print '.. bvecs file: '+fname_bvecs # Extract path, file and extension path_data, file_data, ext_data = sct.extract_fname(fname_data) # create temporary folder path_tmp = 'tmp.'+time.strftime("%y%m%d%H%M%S") sct.run('mkdir '+path_tmp) # copy files into tmp folder sct.run('cp '+fname_data+' '+path_tmp) sct.run('cp '+fname_bvecs+' '+path_tmp) # go to tmp folder os.chdir(path_tmp) # Get size of data print '\nGet dimensions data...' nx, ny, nz, nt, px, py, pz, pt = sct.get_dimension(fname_data) print '.. '+str(nx)+' x '+str(ny)+' x '+str(nz)+' x '+str(nt) # Open bvecs file bvecs = [] with open(fname_bvecs) as f: for line in f: bvecs_new = map(float, line.split()) bvecs.append(bvecs_new) # Check if bvecs file is nx3 if not len(bvecs[0][:]) == 3: print 'WARNING: bvecs file is 3xn instead of nx3. Consider using sct_dmri_transpose_bvecs' # transpose bvecs bvecs = zip(*bvecs) # Identify b=0 and DW images print '\nIdentify b=0 and DW images...' index_b0 = [] index_dwi = [] for it in xrange(0,nt): if math.sqrt(math.fsum([i**2 for i in bvecs[it]])) < 0.01: index_b0.append(it) else: index_dwi.append(it) nb_b0 = len(index_b0) nb_dwi = len(index_dwi) print '.. Number of b=0: '+str(nb_b0)+' '+str(index_b0) print '.. Number of DWI: '+str(nb_dwi)+' '+str(index_dwi) #TODO: check if number of bvecs and nt match # Split into T dimension print '\nSplit along T dimension...' sct.run(fsloutput+' fslsplit '+fname_data+' data_splitT') # retrieve output names status, output = sct.run('ls data_splitT*.*') file_data_split = output.split() # Remove .nii extension file_data_split = [file_data_split[i].replace('.nii','') for i in xrange (0,len(file_data_split))] # Merge b=0 images print '\nMerge b=0...' cmd = fsloutput+'fslmerge -t b0' for it in xrange(0,nb_b0): cmd += ' '+file_data_split[index_b0[it]] sct.run(cmd) # Merge DWI images print '\nMerge DWI...' cmd = fsloutput+'fslmerge -t dwi' for it in xrange(0,nb_dwi): cmd += ' '+file_data_split[index_dwi[it]] sct.run(cmd) # come back to parent folder os.chdir('..') # Generate output files print('\nGenerate output files...') sct.generate_output_file(path_tmp+'/b0.nii',path_data,'b0',ext_data) sct.generate_output_file(path_tmp+'/dwi.nii',path_data,'dwi',ext_data) # Remove temporary files print('\nRemove temporary files...') sct.run('rm -rf '+path_tmp) # display elapsed time elapsed_time = time.time() - start_time print '\nFinished! Elapsed time: '+str(int(round(elapsed_time)))+'s' # to view results print '\nTo view results, type:' print 'fslview b0 dwi &\n'
def main(args=None): # initializations param = Param() # check user arguments if not args: args = sys.argv[1:] # Get parser info parser = get_parser() arguments = parser.parse(args) fname_data = arguments['-i'] fname_seg = arguments['-s'] if '-l' in arguments: fname_landmarks = arguments['-l'] label_type = 'body' elif '-ldisc' in arguments: fname_landmarks = arguments['-ldisc'] label_type = 'disc' else: sct.printv('ERROR: Labels should be provided.', 1, 'error') if '-ofolder' in arguments: path_output = arguments['-ofolder'] else: path_output = '' param.path_qc = arguments.get("-qc", None) path_template = arguments['-t'] contrast_template = arguments['-c'] ref = arguments['-ref'] remove_temp_files = int(arguments['-r']) verbose = int(arguments['-v']) param.verbose = verbose # TODO: not clean, unify verbose or param.verbose in code, but not both if '-param-straighten' in arguments: param.param_straighten = arguments['-param-straighten'] # if '-cpu-nb' in arguments: # arg_cpu = ' -cpu-nb '+str(arguments['-cpu-nb']) # else: # arg_cpu = '' # registration parameters if '-param' in arguments: # reset parameters but keep step=0 (might be overwritten if user specified step=0) paramreg = ParamregMultiStep([step0]) if ref == 'subject': paramreg.steps['0'].dof = 'Tx_Ty_Tz_Rx_Ry_Rz_Sz' # add user parameters for paramStep in arguments['-param']: paramreg.addStep(paramStep) else: paramreg = ParamregMultiStep([step0, step1, step2]) # if ref=subject, initialize registration using different affine parameters if ref == 'subject': paramreg.steps['0'].dof = 'Tx_Ty_Tz_Rx_Ry_Rz_Sz' # initialize other parameters # file_template_label = param.file_template_label zsubsample = param.zsubsample # smoothing_sigma = param.smoothing_sigma # retrieve template file names file_template_vertebral_labeling = get_file_label( os.path.join(path_template, 'template'), 'vertebral labeling') file_template = get_file_label( os.path.join(path_template, 'template'), contrast_template.upper() + '-weighted template') file_template_seg = get_file_label(os.path.join(path_template, 'template'), 'spinal cord') # start timer start_time = time.time() # get fname of the template + template objects fname_template = os.path.join(path_template, 'template', file_template) fname_template_vertebral_labeling = os.path.join( path_template, 'template', file_template_vertebral_labeling) fname_template_seg = os.path.join(path_template, 'template', file_template_seg) fname_template_disc_labeling = os.path.join(path_template, 'template', 'PAM50_label_disc.nii.gz') # check file existence # TODO: no need to do that! sct.printv('\nCheck template files...') sct.check_file_exist(fname_template, verbose) sct.check_file_exist(fname_template_vertebral_labeling, verbose) sct.check_file_exist(fname_template_seg, verbose) path_data, file_data, ext_data = sct.extract_fname(fname_data) # sct.printv(arguments) sct.printv('\nCheck parameters:', verbose) sct.printv(' Data: ' + fname_data, verbose) sct.printv(' Landmarks: ' + fname_landmarks, verbose) sct.printv(' Segmentation: ' + fname_seg, verbose) sct.printv(' Path template: ' + path_template, verbose) sct.printv(' Remove temp files: ' + str(remove_temp_files), verbose) # check if data, segmentation and landmarks are in the same space # JULIEN 2017-04-25: removed because of issue #1168 # sct.printv('\nCheck if data, segmentation and landmarks are in the same space...') # if not sct.check_if_same_space(fname_data, fname_seg): # sct.printv('ERROR: Data image and segmentation are not in the same space. Please check space and orientation of your files', verbose, 'error') # if not sct.check_if_same_space(fname_data, fname_landmarks): # sct.printv('ERROR: Data image and landmarks are not in the same space. Please check space and orientation of your files', verbose, 'error') # check input labels labels = check_labels(fname_landmarks, label_type=label_type) vertebral_alignment = False if len(labels) > 2 and label_type == 'disc': vertebral_alignment = True path_tmp = sct.tmp_create(basename="register_to_template", verbose=verbose) # set temporary file names ftmp_data = 'data.nii' ftmp_seg = 'seg.nii.gz' ftmp_label = 'label.nii.gz' ftmp_template = 'template.nii' ftmp_template_seg = 'template_seg.nii.gz' ftmp_template_label = 'template_label.nii.gz' # ftmp_template_label_disc = 'template_label_disc.nii.gz' # copy files to temporary folder sct.printv('\nCopying input data to tmp folder and convert to nii...', verbose) sct.run([ 'sct_convert', '-i', fname_data, '-o', os.path.join(path_tmp, ftmp_data) ]) sct.run([ 'sct_convert', '-i', fname_seg, '-o', os.path.join(path_tmp, ftmp_seg) ]) sct.run([ 'sct_convert', '-i', fname_landmarks, '-o', os.path.join(path_tmp, ftmp_label) ]) sct.run([ 'sct_convert', '-i', fname_template, '-o', os.path.join(path_tmp, ftmp_template) ]) sct.run([ 'sct_convert', '-i', fname_template_seg, '-o', os.path.join(path_tmp, ftmp_template_seg) ]) sct_convert.main(args=[ '-i', fname_template_vertebral_labeling, '-o', os.path.join(path_tmp, ftmp_template_label) ]) if label_type == 'disc': sct_convert.main(args=[ '-i', fname_template_disc_labeling, '-o', os.path.join(path_tmp, ftmp_template_label) ]) # sct.run('sct_convert -i '+fname_template_label+' -o '+os.path.join(path_tmp, ftmp_template_label)) # go to tmp folder curdir = os.getcwd() os.chdir(path_tmp) # Generate labels from template vertebral labeling if label_type == 'body': sct.printv('\nGenerate labels from template vertebral labeling', verbose) sct_label_utils.main(args=[ '-i', ftmp_template_label, '-vert-body', '0', '-o', ftmp_template_label ]) # check if provided labels are available in the template sct.printv('\nCheck if provided labels are available in the template', verbose) image_label_template = Image(ftmp_template_label) labels_template = image_label_template.getNonZeroCoordinates( sorting='value') if labels[-1].value > labels_template[-1].value: sct.printv( 'ERROR: Wrong landmarks input. Labels must have correspondence in template space. \nLabel max ' 'provided: ' + str(labels[-1].value) + '\nLabel max from template: ' + str(labels_template[-1].value), verbose, 'error') # if only one label is present, force affine transformation to be Tx,Ty,Tz only (no scaling) if len(labels) == 1: paramreg.steps['0'].dof = 'Tx_Ty_Tz' sct.printv( 'WARNING: Only one label is present. Forcing initial transformation to: ' + paramreg.steps['0'].dof, 1, 'warning') # Project labels onto the spinal cord centerline because later, an affine transformation is estimated between the # template's labels (centered in the cord) and the subject's labels (assumed to be centered in the cord). # If labels are not centered, mis-registration errors are observed (see issue #1826) ftmp_label = project_labels_on_spinalcord(ftmp_label, ftmp_seg) # binarize segmentation (in case it has values below 0 caused by manual editing) sct.printv('\nBinarize segmentation', verbose) sct.run( ['sct_maths', '-i', 'seg.nii.gz', '-bin', '0.5', '-o', 'seg.nii.gz']) # smooth segmentation (jcohenadad, issue #613) # sct.printv('\nSmooth segmentation...', verbose) # sct.run('sct_maths -i '+ftmp_seg+' -smooth 1.5 -o '+add_suffix(ftmp_seg, '_smooth')) # jcohenadad: updated 2016-06-16: DO NOT smooth the seg anymore. Issue # # sct.run('sct_maths -i '+ftmp_seg+' -smooth 0 -o '+add_suffix(ftmp_seg, '_smooth')) # ftmp_seg = add_suffix(ftmp_seg, '_smooth') # Switch between modes: subject->template or template->subject if ref == 'template': # resample data to 1mm isotropic sct.printv('\nResample data to 1mm isotropic...', verbose) sct.run([ 'sct_resample', '-i', ftmp_data, '-mm', '1.0x1.0x1.0', '-x', 'linear', '-o', add_suffix(ftmp_data, '_1mm') ]) ftmp_data = add_suffix(ftmp_data, '_1mm') sct.run([ 'sct_resample', '-i', ftmp_seg, '-mm', '1.0x1.0x1.0', '-x', 'linear', '-o', add_suffix(ftmp_seg, '_1mm') ]) ftmp_seg = add_suffix(ftmp_seg, '_1mm') # N.B. resampling of labels is more complicated, because they are single-point labels, therefore resampling # with nearest neighbour can make them disappear. resample_labels(ftmp_label, ftmp_data, add_suffix(ftmp_label, '_1mm')) ftmp_label = add_suffix(ftmp_label, '_1mm') # Change orientation of input images to RPI sct.printv('\nChange orientation of input images to RPI...', verbose) sct.run([ 'sct_image', '-i', ftmp_data, '-setorient', 'RPI', '-o', add_suffix(ftmp_data, '_rpi') ]) ftmp_data = add_suffix(ftmp_data, '_rpi') sct.run([ 'sct_image', '-i', ftmp_seg, '-setorient', 'RPI', '-o', add_suffix(ftmp_seg, '_rpi') ]) ftmp_seg = add_suffix(ftmp_seg, '_rpi') sct.run([ 'sct_image', '-i', ftmp_label, '-setorient', 'RPI', '-o', add_suffix(ftmp_label, '_rpi') ]) ftmp_label = add_suffix(ftmp_label, '_rpi') if vertebral_alignment: # cropping the segmentation based on the label coverage to ensure good registration with vertebral alignment # See https://github.com/neuropoly/spinalcordtoolbox/pull/1669 for details image_labels = Image(ftmp_label) coordinates_labels = image_labels.getNonZeroCoordinates( sorting='z') nx, ny, nz, nt, px, py, pz, pt = image_labels.dim offset_crop = 10.0 * pz # cropping the image 10 mm above and below the highest and lowest label cropping_slices = [ coordinates_labels[0].z - offset_crop, coordinates_labels[-1].z + offset_crop ] # make sure that the cropping slices do not extend outside of the slice range (issue #1811) if cropping_slices[0] < 0: cropping_slices[0] = 0 if cropping_slices[1] > nz: cropping_slices[1] = nz status_crop, output_crop = sct.run([ 'sct_crop_image', '-i', ftmp_seg, '-o', add_suffix(ftmp_seg, '_crop'), '-dim', '2', '-start', str(cropping_slices[0]), '-end', str(cropping_slices[1]) ], verbose) else: # if we do not align the vertebral levels, we crop the segmentation from top to bottom status_crop, output_crop = sct.run([ 'sct_crop_image', '-i', ftmp_seg, '-o', add_suffix(ftmp_seg, '_crop'), '-dim', '2', '-bzmax' ], verbose) cropping_slices = output_crop.split('Dimension 2: ')[1].split( '\n')[0].split(' ') # output: segmentation_rpi_crop.nii.gz ftmp_seg = add_suffix(ftmp_seg, '_crop') # straighten segmentation sct.printv( '\nStraighten the spinal cord using centerline/segmentation...', verbose) # check if warp_curve2straight and warp_straight2curve already exist (i.e. no need to do it another time) fn_warp_curve2straight = os.path.join(curdir, "warp_curve2straight.nii.gz") fn_warp_straight2curve = os.path.join(curdir, "warp_straight2curve.nii.gz") fn_straight_ref = os.path.join(curdir, "straight_ref.nii.gz") cache_input_files = [ftmp_seg] if vertebral_alignment: cache_input_files += [ ftmp_template_seg, ftmp_label, ftmp_template_label, ] cache_sig = sct.cache_signature(input_files=cache_input_files, ) cachefile = os.path.join(curdir, "straightening.cache") if sct.cache_valid( cachefile, cache_sig ) and os.path.isfile(fn_warp_curve2straight) and os.path.isfile( fn_warp_straight2curve) and os.path.isfile(fn_straight_ref): sct.printv( 'Reusing existing warping field which seems to be valid', verbose, 'warning') sct.copy(fn_warp_curve2straight, 'warp_curve2straight.nii.gz') sct.copy(fn_warp_straight2curve, 'warp_straight2curve.nii.gz') sct.copy(fn_straight_ref, 'straight_ref.nii.gz') # apply straightening sct.run([ 'sct_apply_transfo', '-i', ftmp_seg, '-w', 'warp_curve2straight.nii.gz', '-d', 'straight_ref.nii.gz', '-o', add_suffix(ftmp_seg, '_straight') ]) else: from sct_straighten_spinalcord import SpinalCordStraightener sc_straight = SpinalCordStraightener(ftmp_seg, ftmp_seg) sc_straight.output_filename = add_suffix(ftmp_seg, '_straight') sc_straight.path_output = './' sc_straight.qc = '0' sc_straight.remove_temp_files = remove_temp_files sc_straight.verbose = verbose if vertebral_alignment: sc_straight.centerline_reference_filename = ftmp_template_seg sc_straight.use_straight_reference = True sc_straight.discs_input_filename = ftmp_label sc_straight.discs_ref_filename = ftmp_template_label sc_straight.straighten() sct.cache_save(cachefile, cache_sig) # N.B. DO NOT UPDATE VARIABLE ftmp_seg BECAUSE TEMPORARY USED LATER # re-define warping field using non-cropped space (to avoid issue #367) sct.run([ 'sct_concat_transfo', '-w', 'warp_straight2curve.nii.gz', '-d', ftmp_data, '-o', 'warp_straight2curve.nii.gz' ]) if vertebral_alignment: sct.copy('warp_curve2straight.nii.gz', 'warp_curve2straightAffine.nii.gz') else: # Label preparation: # -------------------------------------------------------------------------------- # Remove unused label on template. Keep only label present in the input label image sct.printv( '\nRemove unused label on template. Keep only label present in the input label image...', verbose) sct.run([ 'sct_label_utils', '-i', ftmp_template_label, '-o', ftmp_template_label, '-remove', ftmp_label ]) # Dilating the input label so they can be straighten without losing them sct.printv('\nDilating input labels using 3vox ball radius') sct.run([ 'sct_maths', '-i', ftmp_label, '-o', add_suffix(ftmp_label, '_dilate'), '-dilate', '3' ]) ftmp_label = add_suffix(ftmp_label, '_dilate') # Apply straightening to labels sct.printv('\nApply straightening to labels...', verbose) sct.run([ 'sct_apply_transfo', '-i', ftmp_label, '-o', add_suffix(ftmp_label, '_straight'), '-d', add_suffix(ftmp_seg, '_straight'), '-w', 'warp_curve2straight.nii.gz', '-x', 'nn' ]) ftmp_label = add_suffix(ftmp_label, '_straight') # Compute rigid transformation straight landmarks --> template landmarks sct.printv('\nEstimate transformation for step #0...', verbose) from msct_register_landmarks import register_landmarks try: register_landmarks(ftmp_label, ftmp_template_label, paramreg.steps['0'].dof, fname_affine='straight2templateAffine.txt', verbose=verbose) except Exception: sct.printv( 'ERROR: input labels do not seem to be at the right place. Please check the position of the labels. See documentation for more details: https://sourceforge.net/p/spinalcordtoolbox/wiki/create_labels/', verbose=verbose, type='error') # Concatenate transformations: curve --> straight --> affine sct.printv( '\nConcatenate transformations: curve --> straight --> affine...', verbose) sct.run([ 'sct_concat_transfo', '-w', 'warp_curve2straight.nii.gz,straight2templateAffine.txt', '-d', 'template.nii', '-o', 'warp_curve2straightAffine.nii.gz' ]) # Apply transformation sct.printv('\nApply transformation...', verbose) sct.run([ 'sct_apply_transfo', '-i', ftmp_data, '-o', add_suffix(ftmp_data, '_straightAffine'), '-d', ftmp_template, '-w', 'warp_curve2straightAffine.nii.gz' ]) ftmp_data = add_suffix(ftmp_data, '_straightAffine') sct.run([ 'sct_apply_transfo', '-i', ftmp_seg, '-o', add_suffix(ftmp_seg, '_straightAffine'), '-d', ftmp_template, '-w', 'warp_curve2straightAffine.nii.gz', '-x', 'linear' ]) ftmp_seg = add_suffix(ftmp_seg, '_straightAffine') """ # Benjamin: Issue from Allan Martin, about the z=0 slice that is screwed up, caused by the affine transform. # Solution found: remove slices below and above landmarks to avoid rotation effects points_straight = [] for coord in landmark_template: points_straight.append(coord.z) min_point, max_point = int(round(np.min(points_straight))), int(round(np.max(points_straight))) sct.run('sct_crop_image -i ' + ftmp_seg + ' -start ' + str(min_point) + ' -end ' + str(max_point) + ' -dim 2 -b 0 -o ' + add_suffix(ftmp_seg, '_black')) ftmp_seg = add_suffix(ftmp_seg, '_black') """ # binarize sct.printv('\nBinarize segmentation...', verbose) sct.run([ 'sct_maths', '-i', ftmp_seg, '-bin', '0.5', '-o', add_suffix(ftmp_seg, '_bin') ]) ftmp_seg = add_suffix(ftmp_seg, '_bin') # find min-max of anat2template (for subsequent cropping) zmin_template, zmax_template = find_zmin_zmax(ftmp_seg) # crop template in z-direction (for faster processing) sct.printv('\nCrop data in template space (for faster processing)...', verbose) sct.run([ 'sct_crop_image', '-i', ftmp_template, '-o', add_suffix(ftmp_template, '_crop'), '-dim', '2', '-start', str(zmin_template), '-end', str(zmax_template) ]) ftmp_template = add_suffix(ftmp_template, '_crop') sct.run([ 'sct_crop_image', '-i', ftmp_template_seg, '-o', add_suffix(ftmp_template_seg, '_crop'), '-dim', '2', '-start', str(zmin_template), '-end', str(zmax_template) ]) ftmp_template_seg = add_suffix(ftmp_template_seg, '_crop') sct.run([ 'sct_crop_image', '-i', ftmp_data, '-o', add_suffix(ftmp_data, '_crop'), '-dim', '2', '-start', str(zmin_template), '-end', str(zmax_template) ]) ftmp_data = add_suffix(ftmp_data, '_crop') sct.run([ 'sct_crop_image', '-i', ftmp_seg, '-o', add_suffix(ftmp_seg, '_crop'), '-dim', '2', '-start', str(zmin_template), '-end', str(zmax_template) ]) ftmp_seg = add_suffix(ftmp_seg, '_crop') # sub-sample in z-direction sct.printv('\nSub-sample in z-direction (for faster processing)...', verbose) sct.run([ 'sct_resample', '-i', ftmp_template, '-o', add_suffix(ftmp_template, '_sub'), '-f', '1x1x' + zsubsample ], verbose) ftmp_template = add_suffix(ftmp_template, '_sub') sct.run([ 'sct_resample', '-i', ftmp_template_seg, '-o', add_suffix(ftmp_template_seg, '_sub'), '-f', '1x1x' + zsubsample ], verbose) ftmp_template_seg = add_suffix(ftmp_template_seg, '_sub') sct.run([ 'sct_resample', '-i', ftmp_data, '-o', add_suffix(ftmp_data, '_sub'), '-f', '1x1x' + zsubsample ], verbose) ftmp_data = add_suffix(ftmp_data, '_sub') sct.run([ 'sct_resample', '-i', ftmp_seg, '-o', add_suffix(ftmp_seg, '_sub'), '-f', '1x1x' + zsubsample ], verbose) ftmp_seg = add_suffix(ftmp_seg, '_sub') # Registration straight spinal cord to template sct.printv('\nRegister straight spinal cord to template...', verbose) # loop across registration steps warp_forward = [] warp_inverse = [] for i_step in range(1, len(paramreg.steps)): sct.printv( '\nEstimate transformation for step #' + str(i_step) + '...', verbose) # identify which is the src and dest if paramreg.steps[str(i_step)].type == 'im': src = ftmp_data dest = ftmp_template interp_step = 'linear' elif paramreg.steps[str(i_step)].type == 'seg': src = ftmp_seg dest = ftmp_template_seg interp_step = 'nn' else: sct.printv('ERROR: Wrong image type.', 1, 'error') # if step>1, apply warp_forward_concat to the src image to be used if i_step > 1: # sct.run('sct_apply_transfo -i '+src+' -d '+dest+' -w '+','.join(warp_forward)+' -o '+sct.add_suffix(src, '_reg')+' -x '+interp_step, verbose) # apply transformation from previous step, to use as new src for registration sct.run([ 'sct_apply_transfo', '-i', src, '-d', dest, '-w', ','.join(warp_forward), '-o', add_suffix(src, '_regStep' + str(i_step - 1)), '-x', interp_step ], verbose) src = add_suffix(src, '_regStep' + str(i_step - 1)) # register src --> dest # TODO: display param for debugging warp_forward_out, warp_inverse_out = register( src, dest, paramreg, param, str(i_step)) warp_forward.append(warp_forward_out) warp_inverse.append(warp_inverse_out) # Concatenate transformations: sct.printv('\nConcatenate transformations: anat --> template...', verbose) sct.run([ 'sct_concat_transfo', '-w', 'warp_curve2straightAffine.nii.gz,' + ','.join(warp_forward), '-d', 'template.nii', '-o', 'warp_anat2template.nii.gz' ], verbose) # sct.run('sct_concat_transfo -w warp_curve2straight.nii.gz,straight2templateAffine.txt,'+','.join(warp_forward)+' -d template.nii -o warp_anat2template.nii.gz', verbose) sct.printv('\nConcatenate transformations: template --> anat...', verbose) warp_inverse.reverse() if vertebral_alignment: sct.run([ 'sct_concat_transfo', '-w', ','.join(warp_inverse) + ',warp_straight2curve.nii.gz', '-d', 'data.nii', '-o', 'warp_template2anat.nii.gz' ], verbose) else: sct.run([ 'sct_concat_transfo', '-w', ','.join(warp_inverse) + ',-straight2templateAffine.txt,warp_straight2curve.nii.gz', '-d', 'data.nii', '-o', 'warp_template2anat.nii.gz' ], verbose) # register template->subject elif ref == 'subject': # Change orientation of input images to RPI sct.printv('\nChange orientation of input images to RPI...', verbose) sct.run([ 'sct_image', '-i', ftmp_data, '-setorient', 'RPI', '-o', add_suffix(ftmp_data, '_rpi') ]) ftmp_data = add_suffix(ftmp_data, '_rpi') sct.run([ 'sct_image', '-i', ftmp_seg, '-setorient', 'RPI', '-o', add_suffix(ftmp_seg, '_rpi') ]) ftmp_seg = add_suffix(ftmp_seg, '_rpi') sct.run([ 'sct_image', '-i', ftmp_label, '-setorient', 'RPI', '-o', add_suffix(ftmp_label, '_rpi') ]) ftmp_label = add_suffix(ftmp_label, '_rpi') # Remove unused label on template. Keep only label present in the input label image sct.printv( '\nRemove unused label on template. Keep only label present in the input label image...', verbose) sct.run([ 'sct_label_utils', '-i', ftmp_template_label, '-o', ftmp_template_label, '-remove', ftmp_label ]) # Add one label because at least 3 orthogonal labels are required to estimate an affine transformation. This # new label is added at the level of the upper most label (lowest value), at 1cm to the right. for i_file in [ftmp_label, ftmp_template_label]: im_label = Image(i_file) coord_label = im_label.getCoordinatesAveragedByValue( ) # N.B. landmarks are sorted by value # Create new label from copy import deepcopy new_label = deepcopy(coord_label[0]) # move it 5mm to the left (orientation is RAS) nx, ny, nz, nt, px, py, pz, pt = im_label.dim new_label.x = round(coord_label[0].x + 5.0 / px) # assign value 99 new_label.value = 99 # Add to existing image im_label.data[int(new_label.x), int(new_label.y), int(new_label.z)] = new_label.value # Overwrite label file # im_label.setFileName('label_rpi_modif.nii.gz') im_label.save() # Bring template to subject space using landmark-based transformation sct.printv('\nEstimate transformation for step #0...', verbose) from msct_register_landmarks import register_landmarks warp_forward = ['template2subjectAffine.txt'] warp_inverse = ['-template2subjectAffine.txt'] try: register_landmarks(ftmp_template_label, ftmp_label, paramreg.steps['0'].dof, fname_affine=warp_forward[0], verbose=verbose, path_qc="./") except Exception: sct.printv( 'ERROR: input labels do not seem to be at the right place. Please check the position of the labels. See documentation for more details: https://sourceforge.net/p/spinalcordtoolbox/wiki/create_labels/', verbose=verbose, type='error') # loop across registration steps for i_step in range(1, len(paramreg.steps)): sct.printv( '\nEstimate transformation for step #' + str(i_step) + '...', verbose) # identify which is the src and dest if paramreg.steps[str(i_step)].type == 'im': src = ftmp_template dest = ftmp_data interp_step = 'linear' elif paramreg.steps[str(i_step)].type == 'seg': src = ftmp_template_seg dest = ftmp_seg interp_step = 'nn' else: sct.printv('ERROR: Wrong image type.', 1, 'error') # apply transformation from previous step, to use as new src for registration sct.run([ 'sct_apply_transfo', '-i', src, '-d', dest, '-w', ','.join(warp_forward), '-o', add_suffix(src, '_regStep' + str(i_step - 1)), '-x', interp_step ], verbose) src = add_suffix(src, '_regStep' + str(i_step - 1)) # register src --> dest # TODO: display param for debugging warp_forward_out, warp_inverse_out = register( src, dest, paramreg, param, str(i_step)) warp_forward.append(warp_forward_out) warp_inverse.insert(0, warp_inverse_out) # Concatenate transformations: sct.printv('\nConcatenate transformations: template --> subject...', verbose) sct.run([ 'sct_concat_transfo', '-w', ','.join(warp_forward), '-d', 'data.nii', '-o', 'warp_template2anat.nii.gz' ], verbose) sct.printv('\nConcatenate transformations: subject --> template...', verbose) sct.run([ 'sct_concat_transfo', '-w', ','.join(warp_inverse), '-d', 'template.nii', '-o', 'warp_anat2template.nii.gz' ], verbose) # Apply warping fields to anat and template sct.run([ 'sct_apply_transfo', '-i', 'template.nii', '-o', 'template2anat.nii.gz', '-d', 'data.nii', '-w', 'warp_template2anat.nii.gz', '-crop', '1' ], verbose) sct.run([ 'sct_apply_transfo', '-i', 'data.nii', '-o', 'anat2template.nii.gz', '-d', 'template.nii', '-w', 'warp_anat2template.nii.gz', '-crop', '1' ], verbose) # come back os.chdir(curdir) # Generate output files sct.printv('\nGenerate output files...', verbose) fname_template2anat = os.path.join(path_output, 'template2anat' + ext_data) fname_anat2template = os.path.join(path_output, 'anat2template' + ext_data) sct.generate_output_file( os.path.join(path_tmp, "warp_template2anat.nii.gz"), os.path.join(path_output, "warp_template2anat.nii.gz"), verbose) sct.generate_output_file( os.path.join(path_tmp, "warp_anat2template.nii.gz"), os.path.join(path_output, "warp_anat2template.nii.gz"), verbose) sct.generate_output_file(os.path.join(path_tmp, "template2anat.nii.gz"), fname_template2anat, verbose) sct.generate_output_file(os.path.join(path_tmp, "anat2template.nii.gz"), fname_anat2template, verbose) if ref == 'template': # copy straightening files in case subsequent SCT functions need them sct.generate_output_file( os.path.join(path_tmp, "warp_curve2straight.nii.gz"), os.path.join(path_output, "warp_curve2straight.nii.gz"), verbose) sct.generate_output_file( os.path.join(path_tmp, "warp_straight2curve.nii.gz"), os.path.join(path_output, "warp_straight2curve.nii.gz"), verbose) sct.generate_output_file( os.path.join(path_tmp, "straight_ref.nii.gz"), os.path.join(path_output, "straight_ref.nii.gz"), verbose) # Delete temporary files if remove_temp_files: sct.printv('\nDelete temporary files...', verbose) sct.rmtree(path_tmp, verbose=verbose) # display elapsed time elapsed_time = time.time() - start_time sct.printv( '\nFinished! Elapsed time: ' + str(int(round(elapsed_time))) + 's', verbose) if param.path_qc is not None: generate_qc(fname_data, fname_template2anat, fname_seg, args, os.path.abspath(param.path_qc)) sct.display_viewer_syntax([fname_data, fname_template2anat], verbose=verbose) sct.display_viewer_syntax([fname_template, fname_anat2template], verbose=verbose)
def checkFile(self, param): # check if the file exist sct.printv("Check file existence...", 0) if self.parser.check_file_exist: sct.check_file_exist(param, 0) return param
def main(): # Initialization fname_warp_final = '' # concatenated transformations # Check input parameters parser = get_parser() arguments = parser.parse(sys.argv[1:]) fname_dest = arguments['-d'] fname_warp_list = arguments['-w'] if '-o' in arguments: fname_warp_final = arguments['-o'] verbose = int(arguments.get('-v')) sct.init_sct(log_level=verbose, update=True) # Update log level # Parse list of warping fields sct.printv('\nParse list of transformations...', verbose) use_inverse = [] fname_warp_list_invert = [] for i in range(len(fname_warp_list)): # Check if inverse matrix is specified with '-' at the beginning of file name if fname_warp_list[i].find('-') == 0: use_inverse.append('-i') fname_warp_list[i] = fname_warp_list[i][1:] # remove '-' fname_warp_list_invert += [[use_inverse[i], fname_warp_list[i]]] else: use_inverse.append('') fname_warp_list_invert += [[fname_warp_list[i]]] sct.printv( ' Transfo #' + str(i) + ': ' + use_inverse[i] + fname_warp_list[i], verbose) # Check file existence sct.printv('\nCheck file existence...', verbose) sct.check_file_exist(fname_dest, verbose) for i in range(len(fname_warp_list)): sct.check_file_exist(fname_warp_list[i], verbose) # Get output folder and file name if fname_warp_final == '': path_out, file_out, ext_out = sct.extract_fname(param.fname_warp_final) else: path_out, file_out, ext_out = sct.extract_fname(fname_warp_final) # Check dimension of destination data (cf. issue #1419, #1429) im_dest = Image(fname_dest) if im_dest.dim[2] == 1: dimensionality = '2' else: dimensionality = '3' # Concatenate warping fields sct.printv('\nConcatenate warping fields...', verbose) # N.B. Here we take the inverse of the warp list fname_warp_list_invert.reverse() fname_warp_list_invert = functools.reduce(lambda x, y: x + y, fname_warp_list_invert) cmd = [ 'isct_ComposeMultiTransform', dimensionality, 'warp_final' + ext_out, '-R', fname_dest ] + fname_warp_list_invert status, output = sct.run(cmd, verbose=verbose, is_sct_binary=True) # check if output was generated if not os.path.isfile('warp_final' + ext_out): sct.printv('ERROR: Warping field was not generated.\n' + output, 1, 'error') # Generate output files sct.printv('\nGenerate output files...', verbose) sct.generate_output_file('warp_final' + ext_out, os.path.join(path_out, file_out + ext_out))
def main(): # Initialization path_script = os.path.dirname(__file__) fsloutput = 'export FSLOUTPUTTYPE=NIFTI; ' # for faster processing, all outputs are in NIFTI # THIS DOES NOT WORK IN MY LAPTOP: path_sct = os.environ['SCT_DIR'] # path to spinal cord toolbox #path_sct = path_script[:-8] # TODO: make it cleaner! status, path_sct = commands.getstatusoutput('echo $SCT_DIR') fname_segmentation = '' name_process = '' processes = ['centerline', 'csa', 'length'] method_CSA = [ 'counting_ortho_plane', 'counting_z_plane', 'ellipse_ortho_plane', 'ellipse_z_plane' ] name_method = param.name_method volume_output = param.volume_output verbose = param.verbose start_time = time.time() remove_temp_files = param.remove_temp_files # spline_smoothing = param.spline_smoothing step = param.step smoothing_param = param.smoothing_param figure_fit = param.figure_fit name_output = param.name_output slices = param.slices vert_lev = param.vertebral_levels path_to_template = param.path_to_template # Parameters for debug mode if param.debug: fname_segmentation = '/Users/julien/data/temp/sct_example_data/t2/t2_seg.nii.gz' #path_sct+'/testing/data/errsm_23/t2/t2_segmentation_PropSeg.nii.gz' name_process = 'csa' verbose = 1 volume_output = 1 remove_temp_files = 0 else: # Check input parameters try: opts, args = getopt.getopt(sys.argv[1:], 'hi:p:m:b:l:r:s:t:f:o:v:z:a:') except getopt.GetoptError: usage() if not opts: usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ("-i"): fname_segmentation = arg elif opt in ("-p"): name_process = arg elif opt in ("-m"): name_method = arg elif opt in ('-b'): volume_output = int(arg) elif opt in ('-l'): vert_lev = arg elif opt in ('-r'): remove_temp_files = int(arg) elif opt in ('-s'): smoothing_param = int(arg) elif opt in ('-f'): figure_fit = int(arg) elif opt in ('-o'): name_output = arg elif opt in ('-t'): path_to_template = arg elif opt in ('-v'): verbose = int(arg) volume_output = 1 elif opt in ('-z'): slices = arg elif opt in ('-a'): param.algo_fitting = str(arg) # display usage if a mandatory argument is not provided if fname_segmentation == '' or name_process == '': usage() # display usage if the requested process is not available if name_process not in processes: usage() # display usage if incorrect method if name_process == 'csa' and (name_method not in method_CSA): usage() # display usage if no method provided if name_process == 'csa' and method_CSA == '': usage() # update fields param.verbose = verbose # check existence of input files sct.check_file_exist(fname_segmentation) # print arguments print '\nCheck parameters:' print '.. segmentation file: ' + fname_segmentation if name_process == 'centerline': fname_output = extract_centerline(fname_segmentation, remove_temp_files, name_output=name_output, verbose=param.verbose, algo_fitting=param.algo_fitting) # to view results sct.printv('\nDone! To view results, type:', param.verbose) sct.printv('fslview ' + fname_output + ' &\n', param.verbose, 'info') if name_process == 'csa': volume_output = 1 compute_csa(fname_segmentation, name_method, volume_output, verbose, remove_temp_files, step, smoothing_param, figure_fit, name_output, slices, vert_lev, path_to_template, algo_fitting=param.algo_fitting, type_window=param.type_window, window_length=param.window_length) sct.printv('\nDone!', param.verbose) if (volume_output): sct.printv('Output CSA volume: ' + name_output, param.verbose, 'info') if slices or vert_lev: sct.printv('Output CSA file (averaged): csa_mean.txt', param.verbose, 'info') sct.printv('Output CSA file (all slices): ' + param.fname_csa + '\n', param.verbose, 'info') if name_process == 'length': result_length = compute_length(fname_segmentation, remove_temp_files, verbose=verbose) sct.printv( '\nLength of the segmentation = ' + str(round(result_length, 2)) + ' mm\n', verbose, 'info')
def main(): print '\n\n\n===================================================' print ' Running: sct_labeling' print '===================================================\n' # Initialization start_time = time.time() param = param_class() # Check input parameters try: opts, args = getopt.getopt(sys.argv[1:],'hi:c:l:m:a:s:r:o:g:v:') except getopt.GetoptError as err: print str(err) usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ('-i'): param.input_anat = arg elif opt in ('-c'): param.contrast = arg elif opt in ('-l'): param.input_centerline = arg elif opt in ('-m'): param.mean_distance_mat = arg elif opt in ('-a'): param.shift_AP = int(arg) elif opt in ('-s'): param.size_AP = int(arg) elif opt in ('-r'): param.size_RL = int(arg) elif opt in ('-o'): param.output_path = arg elif opt in ('-g'): param.plot_graph = int(arg) elif opt in ('-v'): param.verbose = int(arg) # Display usage if a mandatory argument is not provided if param.input_anat == '' or param.contrast=='' or param.input_centerline=='' or param.mean_distance_mat=='': print '\n \n All mandatory arguments are not provided \n \n' usage() # Extract path, file and extension input_path, file_data, ext_data = sct.extract_fname(param.input_anat) if param.output_path=='': param.output_path = os.getcwd() + '/' print 'Input File:',param.input_anat print 'Center_line file:',param.input_centerline print 'Contrast:',param.contrast print 'Mat File:',param.mean_distance_mat # check existence of input files sct.check_file_exist(param.input_anat) sct.check_file_exist(param.input_centerline) sct.check_file_exist(param.mean_distance_mat) #================================================== # Reorientation of the data if needed #================================================== command = 'fslhd ' + param.input_anat result = commands.getoutput(command) orientation = result[result.find('qform_xorient')+15] + result[result.find('qform_yorient')+15] + result[result.find('qform_zorient')+15] if orientation!='ASR': sct.printv('\nReorient input volume to AP SI RL orientation...',param.verbose) sct.run(sct.fsloutput + 'fslswapdim ' + param.input_anat + ' AP SI RL ' + input_path + 'tmp.anat_orient') sct.run(sct.fsloutput + 'fslswapdim ' + param.input_centerline + ' AP SI RL ' + input_path + 'tmp.centerline_orient') param.input_anat = input_path + 'tmp.anat_orient.nii' param.centerline = input_path + 'tmp.centerline_orient.nii' if param.plot_graph: import pylab as pl #loading images sct.printv('\nLoading Images...',verbose) anat_file = nibabel.load(param.input_anat) anat = anat_file.get_data() hdr = anat_file.get_header() dims = hdr['dim'] scales = hdr['pixdim'] centerline_file = nibabel.load(param.input_centerline) centerline = centerline_file.get_data() shift_AP = param.shift_AP*scales[1] size_AP = param.size_AP*scales[1] size_RL = param.size_RL*scales[3] np.uint16(anat) if param.contrast == 'T1': labeling_vertebrae_T1(param,anat,hdr,dims,scales) else: labeling_vertebrae_T2(param,anat,hdr,dims,scales) #generating output file if ext_data == '.nii.gz': os.system('fslchfiletype NIFTI_GZ '+ param.output_path + param.contrast + '_centerline.nii.gz') # display elapsed time elapsed_time = time.time() - start_time print '\nFinished! Elapsed time: '+str(int(round(elapsed_time)))+'s'
def main(): # get path of the toolbox status, path_sct = getstatusoutput('echo $SCT_DIR') #print path_sct #Initialization fname = '' landmark = '' verbose = param.verbose output_name = 'aligned.nii.gz' template_landmark = '' final_warp = param.final_warp compose = param.compose transfo = 'affine' try: opts, args = getopt.getopt(sys.argv[1:],'hi:l:o:R:t:w:c:v:') except getopt.GetoptError: usage() for opt, arg in opts : if opt == '-h': usage() elif opt in ("-i"): fname = arg elif opt in ("-l"): landmark = arg elif opt in ("-o"): output_name = arg elif opt in ("-R"): template_landmark = arg elif opt in ("-t"): transfo = arg elif opt in ("-w"): final_warp = arg elif opt in ("-c"): compose = int(arg) elif opt in ('-v'): verbose = int(arg) # display usage if a mandatory argument is not provided if fname == '' or landmark == '' or template_landmark == '' : usage() if final_warp not in ['','spline','NN']: usage() if transfo not in ['affine','bspline','SyN']: usage() # check existence of input files print'\nCheck if file exists ...' sct.check_file_exist(fname) sct.check_file_exist(landmark) sct.check_file_exist(template_landmark) # Display arguments print'\nCheck input arguments...' print' Input volume ...................... '+fname print' Verbose ........................... '+str(verbose) if transfo == 'affine': print 'Creating cross using input landmarks\n...' sct.run('sct_label_utils -i ' + landmark + ' -o ' + 'cross_native.nii.gz -t cross ' ) print 'Creating cross using template landmarks\n...' sct.run('sct_label_utils -i ' + template_landmark + ' -o ' + 'cross_template.nii.gz -t cross ' ) print 'Computing affine transformation between subject and destination landmarks\n...' os.system('isct_ANTSUseLandmarkImagesToGetAffineTransform cross_template.nii.gz cross_native.nii.gz affine n2t.txt') warping = 'n2t.txt' elif transfo == 'SyN': warping = 'warp_subject2template.nii.gz' tmp_name = 'tmp.'+time.strftime("%y%m%d%H%M%S") sct.run('mkdir '+tmp_name) tmp_abs_path = os.path.abspath(tmp_name) sct.run('cp ' + landmark + ' ' + tmp_abs_path) os.chdir(tmp_name) # sct.run('sct_label_utils -i '+landmark+' -t dist-inter') # sct.run('sct_label_utils -i '+template_landmark+' -t plan -o template_landmarks_plan.nii.gz -c 5') # sct.run('sct_crop_image -i template_landmarks_plan.nii.gz -o template_landmarks_plan_cropped.nii.gz -start 0.35,0.35 -end 0.65,0.65 -dim 0,1') # sct.run('sct_label_utils -i '+landmark+' -t plan -o landmarks_plan.nii.gz -c 5') # sct.run('sct_crop_image -i landmarks_plan.nii.gz -o landmarks_plan_cropped.nii.gz -start 0.35,0.35 -end 0.65,0.65 -dim 0,1') # sct.run('isct_antsRegistration --dimensionality 3 --transform SyN[0.5,3,0] --metric MeanSquares[template_landmarks_plan_cropped.nii.gz,landmarks_plan_cropped.nii.gz,1] --convergence 400x200 --shrink-factors 4x2 --smoothing-sigmas 4x2mm --restrict-deformation 0x0x1 --output [landmarks_reg,landmarks_reg.nii.gz] --interpolation NearestNeighbor --float') # sct.run('isct_c3d -mcs landmarks_reg0Warp.nii.gz -oo warp_vecx.nii.gz warp_vecy.nii.gz warp_vecz.nii.gz') # sct.run('isct_c3d warp_vecz.nii.gz -resample 200% -o warp_vecz_r.nii.gz') # sct.run('isct_c3d warp_vecz_r.nii.gz -smooth 0x0x3mm -o warp_vecz_r_sm.nii.gz') # sct.run('sct_crop_image -i warp_vecz_r_sm.nii.gz -o warp_vecz_r_sm_line.nii.gz -start 0.5,0.5 -end 0.5,0.5 -dim 0,1 -b 0') # sct.run('sct_label_utils -i warp_vecz_r_sm_line.nii.gz -t plan_ref -o warp_vecz_r_sm_line_extended.nii.gz -c 0 -r '+template_landmark) # sct.run('isct_c3d '+template_landmark+' warp_vecx.nii.gz -reslice-identity -o warp_vecx_res.nii.gz') # sct.run('isct_c3d '+template_landmark+' warp_vecy.nii.gz -reslice-identity -o warp_vecy_res.nii.gz') # sct.run('isct_c3d warp_vecx_res.nii.gz warp_vecy_res.nii.gz warp_vecz_r_sm_line_extended.nii.gz -omc 3 '+warping) # no x? #new #put labels of the subject at the center of the image (for plan xOy) import nibabel from copy import copy file_labels_input = nibabel.load(landmark) hdr_labels_input = file_labels_input.get_header() data_labels_input = file_labels_input.get_data() data_labels_middle = copy(data_labels_input) data_labels_middle *= 0 nx, ny, nz, nt, px, py, pz, pt = sct.get_dimension(landmark) X,Y,Z = data_labels_input.nonzero() x_middle = int(round(nx/2.0)) y_middle = int(round(ny/2.0)) for i in range(len(Z)): data_labels_middle[x_middle, y_middle, Z[i]] = data_labels_input[X[i], Y[i], Z[i]] img = nibabel.Nifti1Image(data_labels_middle, None, hdr_labels_input) nibabel.save(img, 'labels_input_middle_xy.nii.gz') #put labels of the template at the center of the image (for plan xOy) #probably not necessary as already done by average labels file_labels_template = nibabel.load(template_landmark) hdr_labels_template = file_labels_template.get_header() data_labels_template = file_labels_template.get_data() data_template_middle = copy(data_labels_template) data_template_middle *= 0 x,y,z = data_labels_template.nonzero() for i in range(len(Z)): data_template_middle[x_middle, y_middle, z[i]] = data_labels_template[x[i], y[i], z[i]] img_template = nibabel.Nifti1Image(data_template_middle, None, hdr_labels_template) nibabel.save(img_template, 'labels_template_middle_xy.nii.gz') #estimate Bspline transform to register to template sct.run('isct_ANTSUseLandmarkImagesToGetBSplineDisplacementField labels_template_middle_xy.nii.gz labels_input_middle_xy.nii.gz '+ warping+' 40*40*1 2 5 1') # select centerline of warping field according to z and extend it sct.run('isct_c3d -mcs '+warping+' -oo warp_vecx.nii.gz warp_vecy.nii.gz warp_vecz.nii.gz') #sct.run('isct_c3d warp_vecz.nii.gz -resample 200% -o warp_vecz_r.nii.gz') #sct.run('isct_c3d warp_vecz.nii.gz -smooth 0x0x3mm -o warp_vecz_r_sm.nii.gz') sct.run('sct_crop_image -i warp_vecz.nii.gz -o warp_vecz_r_sm_line.nii.gz -start 0.5,0.5 -end 0.5,0.5 -dim 0,1 -b 0') sct.run('sct_label_utils -i warp_vecz_r_sm_line.nii.gz -t plan_ref -o warp_vecz_r_sm_line_extended.nii.gz -r '+template_landmark) sct.run('isct_c3d '+template_landmark+' warp_vecx.nii.gz -reslice-identity -o warp_vecx_res.nii.gz') sct.run('isct_c3d '+template_landmark+' warp_vecy.nii.gz -reslice-identity -o warp_vecy_res.nii.gz') sct.run('isct_c3d warp_vecx_res.nii.gz warp_vecy_res.nii.gz warp_vecz_r_sm_line_extended.nii.gz -omc 3 '+warping) # check results #dilate first labels sct.run('fslmaths labels_input_middle_xy.nii.gz -dilF landmark_dilated.nii.gz') #new sct.run('sct_apply_transfo -i landmark_dilated.nii.gz -o label_moved.nii.gz -d labels_template_middle_xy.nii.gz -w '+warping+' -x nn') #undilate sct.run('sct_label_utils -i label_moved.nii.gz -t cubic-to-point -o label_moved_2point.nii.gz') sct.run('sct_label_utils -i labels_template_middle_xy.nii.gz -r label_moved_2point.nii.gz -o template_removed.nii.gz -t remove') #end new # check results #dilate first labels #sct.run('fslmaths '+landmark+' -dilF landmark_dilated.nii.gz') #old #sct.run('sct_apply_transfo -i landmark_dilated.nii.gz -o label_moved.nii.gz -d '+template_landmark+' -w '+warping+' -x nn') #old #undilate #sct.run('sct_label_utils -i label_moved.nii.gz -t cubic-to-point -o label_moved_2point.nii.gz') #old #sct.run('sct_label_utils -i '+template_landmark+' -r label_moved_2point.nii.gz -o template_removed.nii.gz -t remove') #old # # sct.run('sct_apply_transfo -i '+landmark+' -o label_moved.nii.gz -d '+template_landmark+' -w '+warping+' -x nn') # # sct.run('sct_label_utils -i '+template_landmark+' -r label_moved.nii.gz -o template_removed.nii.gz -t remove') # # status, output = sct.run('sct_label_utils -i label_moved.nii.gz -r template_removed.nii.gz -t MSE') status, output = sct.run('sct_label_utils -i label_moved_2point.nii.gz -r template_removed.nii.gz -t MSE') sct.printv(output,1,'info') remove_temp_files = False if os.path.isfile('error_log_label_moved.txt'): remove_temp_files = False with open('log.txt', 'a') as log_file: log_file.write('Error for '+fname+'\n') # Copy warping into parent folder sct.run('cp '+ warping+' ../'+warping) os.chdir('..') if remove_temp_files: sct.run('rm -rf '+tmp_name) # if transfo == 'bspline' : # print 'Computing bspline transformation between subject and destination landmarks\n...' # sct.run('isct_ANTSUseLandmarkImagesToGetBSplineDisplacementField cross_template.nii.gz cross_native.nii.gz warp_ntotemp.nii.gz 5x5x5 3 2 0') # warping = 'warp_ntotemp.nii.gz' # if final_warp == '' : # print 'Apply transfo to input image\n...' # sct.run('isct_antsApplyTransforms 3 ' + fname + ' ' + output_name + ' -r ' + template_landmark + ' -t ' + warping + ' -n Linear') # if final_warp == 'NN': # print 'Apply transfo to input image\n...' # sct.run('isct_antsApplyTransforms 3 ' + fname + ' ' + output_name + ' -r ' + template_landmark + ' -t ' + warping + ' -n NearestNeighbor') if final_warp == 'spline': print 'Apply transfo to input image\n...' sct.run('sct_apply_transfo -i ' + fname + ' -o ' + output_name + ' -d ' + template_landmark + ' -w ' + warping + ' -x spline') # Remove warping os.remove(warping) # if compose : # print 'Computing affine transformation between subject and destination landmarks\n...' # sct.run('isct_ANTSUseLandmarkImagesToGetAffineTransform cross_template.nii.gz cross_native.nii.gz affine n2t.txt') # warping_affine = 'n2t.txt' # print 'Apply transfo to input landmarks\n...' # sct.run('isct_antsApplyTransforms 3 ' + cross_native + ' cross_affine.nii.gz -r ' + template_landmark + ' -t ' + warping_affine + ' -n NearestNeighbor') # print 'Computing transfo between moved landmarks and template landmarks\n...' # sct.run('isct_ANTSUseLandmarkImagesToGetBSplineDisplacementField cross_template.nii.gz cross_affine.nii.gz warp_affine2temp.nii.gz 5x5x5 3 2 0') # warping_bspline = 'warp_affine2temp.nii.gz' # print 'Composing transformations\n...' # sct.run('isct_ComposeMultiTransform 3 warp_full.nii.gz -r ' + template_landmark + ' ' + warping_bspline + ' ' + warping_affine) # warping_concat = 'warp_full.nii.gz' # if final_warp == '' : # print 'Apply concat warp to input image\n...' # sct.run('isct_antsApplyTransforms 3 ' + fname + ' ' + output_name + ' -r ' + template_landmark + ' -t ' + warping_concat + ' -n Linear') # if final_warp == 'NN': # print 'Apply concat warp to input image\n...' # sct.run('isct_antsApplyTransforms 3 ' + fname + ' ' + output_name + ' -r ' + template_landmark + ' -t ' + warping_concat + ' -n NearestNeighbor') # if final_warp == 'spline': # print 'Apply concat warp to input image\n...' # sct.run('isct_antsApplyTransforms 3 ' + fname + ' ' + output_name + ' -r ' + template_landmark + ' -t ' + warping_concat + ' -n BSpline[3]') print '\nFile created : ' + output_name
def get_parser(): param_default = Param() # Read .txt files referencing the labels (for extended usage description) file_label = os.path.join(param_default.path_label, param_default.file_info_label) sct.check_file_exist(file_label, 0) default_info_label = open(file_label, 'r') label_references = default_info_label.read() default_info_label.close() description = (f"This program extracts metrics (e.g., DTI or MTR) within labels. Labels could be a single file or " f"a folder generated with 'sct_warp_template' and containing multiple label files and a label " f"description file (info_label.txt). The labels should be in the same space coordinates as the " f"input image.\n" f"\n" f"To list white matter atlas labels: {os.path.basename(__file__)} -f " f"{os.path.join(path_sct, 'data', 'atlas')}\n" f"\n" f"To compute FA within labels 0, 2 and 3 within vertebral levels C2 to C7 using binary method: " f"{os.path.basename(__file__)} -i dti_FA.nii.gz -f label/atlas -l 0,2,3 -v 2:7 -m bin\n") if label_references != '': description += (f"\nTo compute average MTR in a region defined by a single label file (could be binary or 0-1 " f"weighted mask) between slices 1 and 4: {os.path.basename(__file__)} -i mtr.nii.gz -f " f"my_mask.nii.gz -z 1:4 -m wa\n" f"List of labels in {file_label}:\n" f"--------------------------------------------------------------------------------------\n" f"{label_references}\n" f"--------------------------------------------------------------------------------------\n") parser = argparse.ArgumentParser( description=description, formatter_class=SmartFormatter, add_help=None, prog=os.path.basename(__file__).strip(".py") ) mandatory = parser.add_argument_group("\nMANDATORY ARGUMENTS") mandatory.add_argument( '-i', metavar=Metavar.file, required=True, help="Image file to extract metrics from. Example: FA.nii.gz" ) optional = parser.add_argument_group("\nOPTIONAL ARGUMENTS") optional.add_argument( "-h", "--help", action="help", help="Show this help message and exit." ) optional.add_argument( '-f', metavar=Metavar.folder, default=os.path.join("label", "atlas"), help=(f"Single label file, or folder that contains WM tract labels." f"Example: {os.path.join(path_sct, 'data', 'atlas')}") ) optional.add_argument( '-l', metavar=Metavar.str, default='', help="Label IDs to extract the metric from. Default = all labels. Separate labels with ','. To select a group " "of consecutive labels use ':'. Example: 1:3 is equivalent to 1,2,3. Maximum Likelihood (or MAP) is " "computed using all tracts, but only values of the selected tracts are reported." ) optional.add_argument( '-method', choices=['ml', 'map', 'wa', 'bin', 'max'], default=param_default.method, help="R|Method to extract metrics.\n" " - ml: maximum likelihood (only use with well-defined regions and low noise)\n" " N.B. ONLY USE THIS METHOD WITH THE WHITE MATTER ATLAS! The sum of all tracts should be 1 in " "all voxels (the algorithm doesn't normalize the atlas).\n" " - map: maximum a posteriori. Mean priors are estimated by maximum likelihood within three clusters " "(white matter, gray matter and CSF). Tract and noise variance are set with flag -p.\n" " N.B. ONLY USE THIS METHOD WITH THE WHITE MATTER ATLAS! The sum of all tracts should be 1 in " "all voxels (the algorithm doesn't normalize the atlas).\n" " - wa: weighted average\n" " - bin: binarize mask (threshold=0.5)\n" " - max: for each z-slice of the input data, extract the max value for each slice of the input data." ) optional.add_argument( '-append', type=int, choices=(0, 1), default=0, help="Whether to append results as a new line in the output csv file instead of overwriting it. 0 = no, 1 = yes" ) optional.add_argument( '-combine', type=int, choices=(0, 1), default=0, help="Whether to combine multiple labels into a single estimation. 0 = no, 1 = yes" ) optional.add_argument( '-o', metavar=Metavar.file, default=param_default.fname_output, help="R|File name of the output result file collecting the metric estimation results. Include the '.csv' " "file extension in the file name. Example: extract_metric.csv" ) optional.add_argument( '-output-map', metavar=Metavar.file, default='', help="File name for an image consisting of the atlas labels multiplied by the estimated metric values " "yielding the metric value map, useful to assess the metric estimation and especially partial volume " "effects." ) optional.add_argument( '-z', metavar=Metavar.str, default=param_default.slices_of_interest, help="R|Slice range to estimate the metric from. First slice is 0. Example: 5:23\n" "You can also select specific slices using commas. Example: 0,2,3,5,12'" ) optional.add_argument( '-perslice', type=int, choices=(0, 1), default=param_default.perslice, help="R|Whether to output one metric per slice instead of a single output metric. 0 = no, 1 = yes.\n" "Please note that when methods ml or map are used, outputting a single metric per slice and then " "averaging them all is not the same as outputting a single metric at once across all slices." ) optional.add_argument( '-vert', metavar=Metavar.str, default=param_default.vertebral_levels, help="Vertebral levels to estimate the metric across. Example: 2:9 (for C2 to T2)" ) optional.add_argument( '-vertfile', metavar=Metavar.file, default="./label/template/PAM50_levels.nii.gz", help="Vertebral labeling file. Only use with flag -vert. Example: PAM50_levels.nii.gz" ) optional.add_argument( '-perlevel', type=int, metavar=Metavar.int, default=0, help="R|Whether to output one metric per vertebral level instead of a single output metric. 0 = no, 1 = yes.\n" "Please note that this flag needs to be used with the -vert option." ) optional.add_argument( '-v', choices=("0", "1"), default="1", help="Verbose. 0 = nothing, 1 = expanded" ) advanced = parser.add_argument_group("\nFOR ADVANCED USERS") advanced.add_argument( '-param', metavar=Metavar.str, default='', help="R|Advanced parameters for the 'map' method. Separate with comma. All items must be listed (separated " "with comma).\n" " - #1: standard deviation of metrics across labels\n" " - #2: standard deviation of the noise (assumed Gaussian)" ) advanced.add_argument( '-fix-label', metavar=Metavar.list, type=list_type(',', str), default='', help="When using ML or MAP estimations, if you do not want to estimate the metric in one label and fix its " "value to avoid effects on other labels, specify <label_ID>,<metric_value. Example: -fix-label 36,0 " "(Fix the CSF value)" ) advanced.add_argument( '-norm-file', metavar=Metavar.file, default='', help='Filename of the label by which the user wants to normalize.' ) advanced.add_argument( '-norm-method', choices=['sbs', 'whole'], default='', help="R|Method to use for normalization:\n" " - sbs: normalization slice-by-slice\n" " - whole: normalization by the metric value in the whole label for all slices." ) advanced.add_argument( '-mask-weighted', metavar=Metavar.file, default='', help="Nifti mask to weight each voxel during ML or MAP estimation. Example: PAM50_wm.nii.gz" ) advanced.add_argument( '-discard-neg-val', choices=('0', '1'), default='0', help='Whether to discard voxels with negative value when computing metrics statistics. 0 = no, 1 = yes' ) return parser
def main(): # Initialization fname_data = '' suffix_out = '_crop' remove_temp_files = param.remove_temp_files verbose = param.verbose fsloutput = 'export FSLOUTPUTTYPE=NIFTI; ' # for faster processing, all outputs are in NIFTI remove_temp_files = param.remove_temp_files # Parameters for debug mode if param.debug: print '\n*** WARNING: DEBUG MODE ON ***\n' fname_data = path_sct+'/testing/data/errsm_23/t2/t2.nii.gz' remove_temp_files = 0 else: # Check input parameters try: opts, args = getopt.getopt(sys.argv[1:],'hi:r:v:') except getopt.GetoptError: usage() if not opts: usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ('-i'): fname_data = arg elif opt in ('-r'): remove_temp_files = int(arg) elif opt in ('-v'): verbose = int(arg) # display usage if a mandatory argument is not provided if fname_data == '': usage() # Check file existence sct.printv('\nCheck file existence...', verbose) sct.check_file_exist(fname_data, verbose) # Get dimensions of data sct.printv('\nGet dimensions of data...', verbose) nx, ny, nz, nt, px, py, pz, pt = Image(fname_data).dim sct.printv('.. '+str(nx)+' x '+str(ny)+' x '+str(nz), verbose) # check if 4D data if not nt == 1: sct.printv('\nERROR in '+os.path.basename(__file__)+': Data should be 3D.\n', 1, 'error') sys.exit(2) # print arguments print '\nCheck parameters:' print ' data ................... '+fname_data print # Extract path/file/extension path_data, file_data, ext_data = sct.extract_fname(fname_data) path_out, file_out, ext_out = '', file_data+suffix_out, ext_data # create temporary folder path_tmp = 'tmp.'+time.strftime("%y%m%d%H%M%S")+'/' sct.run('mkdir '+path_tmp) # copy files into tmp folder sct.run('isct_c3d '+fname_data+' -o '+path_tmp+'data.nii') # go to tmp folder os.chdir(path_tmp) # change orientation sct.printv('\nChange orientation to RPI...', verbose) set_orientation('data.nii', 'RPI', 'data_rpi.nii') # get image of medial slab sct.printv('\nGet image of medial slab...', verbose) image_array = nibabel.load('data_rpi.nii').get_data() nx, ny, nz = image_array.shape scipy.misc.imsave('image.jpg', image_array[math.floor(nx/2), :, :]) # Display the image sct.printv('\nDisplay image and get cropping region...', verbose) fig = plt.figure() # fig = plt.gcf() # ax = plt.gca() ax = fig.add_subplot(111) img = mpimg.imread("image.jpg") implot = ax.imshow(img.T) implot.set_cmap('gray') plt.gca().invert_yaxis() # mouse callback ax.set_title('Left click on the top and bottom of your cropping field.\n Right click to remove last point.\n Close window when your done.') line, = ax.plot([], [], 'ro') # empty line cropping_coordinates = LineBuilder(line) plt.show() # disconnect callback # fig.canvas.mpl_disconnect(line) # check if user clicked two times if len(cropping_coordinates.xs) != 2: sct.printv('\nERROR: You have to select two points. Exit program.\n', 1, 'error') sys.exit(2) # convert coordinates to integer zcrop = [int(i) for i in cropping_coordinates.ys] # sort coordinates zcrop.sort() # crop image sct.printv('\nCrop image...', verbose) nii = Image('data_rpi.nii') data_crop = nii.data[:, :, zcrop[0]:zcrop[1]] nii.data = data_crop nii.setFileName('data_rpi_crop.nii') nii.save() # come back to parent folder os.chdir('..') sct.printv('\nGenerate output files...', verbose) sct.generate_output_file(path_tmp+'data_rpi_crop.nii', path_out+file_out+ext_out) # Remove temporary files if remove_temp_files == 1: print('\nRemove temporary files...') sct.run('rm -rf '+path_tmp) # to view results print '\nDone! To view results, type:' print 'fslview '+path_out+file_out+ext_out+' &' print
def main(args=None): # initializations param = Param() # check user arguments if not args: args = sys.argv[1:] # Get parser info parser = get_parser() arguments = parser.parse(args) fname_data = arguments['-i'] fname_seg = arguments['-s'] if '-l' in arguments: fname_landmarks = arguments['-l'] label_type = 'body' elif '-ldisc' in arguments: fname_landmarks = arguments['-ldisc'] label_type = 'disc' else: sct.printv('ERROR: Labels should be provided.', 1, 'error') if '-ofolder' in arguments: path_output = arguments['-ofolder'] else: path_output = '' param.path_qc = arguments.get("-qc", None) path_template = arguments['-t'] contrast_template = arguments['-c'] ref = arguments['-ref'] param.remove_temp_files = int(arguments.get('-r')) verbose = int(arguments.get('-v')) sct.init_sct(log_level=verbose, update=True) # Update log level param.verbose = verbose # TODO: not clean, unify verbose or param.verbose in code, but not both param_centerline = ParamCenterline( algo_fitting=arguments['-centerline-algo'], smooth=arguments['-centerline-smooth']) # registration parameters if '-param' in arguments: # reset parameters but keep step=0 (might be overwritten if user specified step=0) paramreg = ParamregMultiStep([step0]) if ref == 'subject': paramreg.steps['0'].dof = 'Tx_Ty_Tz_Rx_Ry_Rz_Sz' # add user parameters for paramStep in arguments['-param']: paramreg.addStep(paramStep) else: paramreg = ParamregMultiStep([step0, step1, step2]) # if ref=subject, initialize registration using different affine parameters if ref == 'subject': paramreg.steps['0'].dof = 'Tx_Ty_Tz_Rx_Ry_Rz_Sz' # initialize other parameters zsubsample = param.zsubsample # retrieve template file names file_template_vertebral_labeling = get_file_label(os.path.join(path_template, 'template'), 'vertebral labeling') file_template = get_file_label(os.path.join(path_template, 'template'), contrast_template.upper() + '-weighted template') file_template_seg = get_file_label(os.path.join(path_template, 'template'), 'spinal cord') # start timer start_time = time.time() # get fname of the template + template objects fname_template = os.path.join(path_template, 'template', file_template) fname_template_vertebral_labeling = os.path.join(path_template, 'template', file_template_vertebral_labeling) fname_template_seg = os.path.join(path_template, 'template', file_template_seg) fname_template_disc_labeling = os.path.join(path_template, 'template', 'PAM50_label_disc.nii.gz') # check file existence # TODO: no need to do that! sct.printv('\nCheck template files...') sct.check_file_exist(fname_template, verbose) sct.check_file_exist(fname_template_vertebral_labeling, verbose) sct.check_file_exist(fname_template_seg, verbose) path_data, file_data, ext_data = sct.extract_fname(fname_data) # sct.printv(arguments) sct.printv('\nCheck parameters:', verbose) sct.printv(' Data: ' + fname_data, verbose) sct.printv(' Landmarks: ' + fname_landmarks, verbose) sct.printv(' Segmentation: ' + fname_seg, verbose) sct.printv(' Path template: ' + path_template, verbose) sct.printv(' Remove temp files: ' + str(param.remove_temp_files), verbose) # check input labels labels = check_labels(fname_landmarks, label_type=label_type) vertebral_alignment = False if len(labels) > 2 and label_type == 'disc': vertebral_alignment = True path_tmp = sct.tmp_create(basename="register_to_template", verbose=verbose) # set temporary file names ftmp_data = 'data.nii' ftmp_seg = 'seg.nii.gz' ftmp_label = 'label.nii.gz' ftmp_template = 'template.nii' ftmp_template_seg = 'template_seg.nii.gz' ftmp_template_label = 'template_label.nii.gz' # copy files to temporary folder sct.printv('\nCopying input data to tmp folder and convert to nii...', verbose) Image(fname_data).save(os.path.join(path_tmp, ftmp_data)) Image(fname_seg).save(os.path.join(path_tmp, ftmp_seg)) Image(fname_landmarks).save(os.path.join(path_tmp, ftmp_label)) Image(fname_template).save(os.path.join(path_tmp, ftmp_template)) Image(fname_template_seg).save(os.path.join(path_tmp, ftmp_template_seg)) Image(fname_template_vertebral_labeling).save(os.path.join(path_tmp, ftmp_template_label)) if label_type == 'disc': Image(fname_template_disc_labeling).save(os.path.join(path_tmp, ftmp_template_label)) # go to tmp folder curdir = os.getcwd() os.chdir(path_tmp) # Generate labels from template vertebral labeling if label_type == 'body': sct.printv('\nGenerate labels from template vertebral labeling', verbose) ftmp_template_label_, ftmp_template_label = ftmp_template_label, sct.add_suffix(ftmp_template_label, "_body") sct_label_utils.main(args=['-i', ftmp_template_label_, '-vert-body', '0', '-o', ftmp_template_label]) # check if provided labels are available in the template sct.printv('\nCheck if provided labels are available in the template', verbose) image_label_template = Image(ftmp_template_label) labels_template = image_label_template.getNonZeroCoordinates(sorting='value') if labels[-1].value > labels_template[-1].value: sct.printv('ERROR: Wrong landmarks input. Labels must have correspondence in template space. \nLabel max ' 'provided: ' + str(labels[-1].value) + '\nLabel max from template: ' + str(labels_template[-1].value), verbose, 'error') # if only one label is present, force affine transformation to be Tx,Ty,Tz only (no scaling) if len(labels) == 1: paramreg.steps['0'].dof = 'Tx_Ty_Tz' sct.printv('WARNING: Only one label is present. Forcing initial transformation to: ' + paramreg.steps['0'].dof, 1, 'warning') # Project labels onto the spinal cord centerline because later, an affine transformation is estimated between the # template's labels (centered in the cord) and the subject's labels (assumed to be centered in the cord). # If labels are not centered, mis-registration errors are observed (see issue #1826) ftmp_label = project_labels_on_spinalcord(ftmp_label, ftmp_seg, param_centerline) # binarize segmentation (in case it has values below 0 caused by manual editing) sct.printv('\nBinarize segmentation', verbose) ftmp_seg_, ftmp_seg = ftmp_seg, sct.add_suffix(ftmp_seg, "_bin") sct_maths.main(['-i', ftmp_seg_, '-bin', '0.5', '-o', ftmp_seg]) # Switch between modes: subject->template or template->subject if ref == 'template': # resample data to 1mm isotropic sct.printv('\nResample data to 1mm isotropic...', verbose) resample_file(ftmp_data, add_suffix(ftmp_data, '_1mm'), '1.0x1.0x1.0', 'mm', 'linear', verbose) ftmp_data = add_suffix(ftmp_data, '_1mm') resample_file(ftmp_seg, add_suffix(ftmp_seg, '_1mm'), '1.0x1.0x1.0', 'mm', 'linear', verbose) ftmp_seg = add_suffix(ftmp_seg, '_1mm') # N.B. resampling of labels is more complicated, because they are single-point labels, therefore resampling # with nearest neighbour can make them disappear. resample_labels(ftmp_label, ftmp_data, add_suffix(ftmp_label, '_1mm')) ftmp_label = add_suffix(ftmp_label, '_1mm') # Change orientation of input images to RPI sct.printv('\nChange orientation of input images to RPI...', verbose) ftmp_data = Image(ftmp_data).change_orientation("RPI", generate_path=True).save().absolutepath ftmp_seg = Image(ftmp_seg).change_orientation("RPI", generate_path=True).save().absolutepath ftmp_label = Image(ftmp_label).change_orientation("RPI", generate_path=True).save().absolutepath ftmp_seg_, ftmp_seg = ftmp_seg, add_suffix(ftmp_seg, '_crop') if vertebral_alignment: # cropping the segmentation based on the label coverage to ensure good registration with vertebral alignment # See https://github.com/neuropoly/spinalcordtoolbox/pull/1669 for details image_labels = Image(ftmp_label) coordinates_labels = image_labels.getNonZeroCoordinates(sorting='z') nx, ny, nz, nt, px, py, pz, pt = image_labels.dim offset_crop = 10.0 * pz # cropping the image 10 mm above and below the highest and lowest label cropping_slices = [coordinates_labels[0].z - offset_crop, coordinates_labels[-1].z + offset_crop] # make sure that the cropping slices do not extend outside of the slice range (issue #1811) if cropping_slices[0] < 0: cropping_slices[0] = 0 if cropping_slices[1] > nz: cropping_slices[1] = nz msct_image.spatial_crop(Image(ftmp_seg_), dict(((2, np.int32(np.round(cropping_slices))),))).save(ftmp_seg) else: # if we do not align the vertebral levels, we crop the segmentation from top to bottom im_seg_rpi = Image(ftmp_seg_) bottom = 0 for data in msct_image.SlicerOneAxis(im_seg_rpi, "IS"): if (data != 0).any(): break bottom += 1 top = im_seg_rpi.data.shape[2] for data in msct_image.SlicerOneAxis(im_seg_rpi, "SI"): if (data != 0).any(): break top -= 1 msct_image.spatial_crop(im_seg_rpi, dict(((2, (bottom, top)),))).save(ftmp_seg) # straighten segmentation sct.printv('\nStraighten the spinal cord using centerline/segmentation...', verbose) # check if warp_curve2straight and warp_straight2curve already exist (i.e. no need to do it another time) fn_warp_curve2straight = os.path.join(curdir, "warp_curve2straight.nii.gz") fn_warp_straight2curve = os.path.join(curdir, "warp_straight2curve.nii.gz") fn_straight_ref = os.path.join(curdir, "straight_ref.nii.gz") cache_input_files=[ftmp_seg] if vertebral_alignment: cache_input_files += [ ftmp_template_seg, ftmp_label, ftmp_template_label, ] cache_sig = sct.cache_signature( input_files=cache_input_files, ) cachefile = os.path.join(curdir, "straightening.cache") if sct.cache_valid(cachefile, cache_sig) and os.path.isfile(fn_warp_curve2straight) and os.path.isfile(fn_warp_straight2curve) and os.path.isfile(fn_straight_ref): sct.printv('Reusing existing warping field which seems to be valid', verbose, 'warning') sct.copy(fn_warp_curve2straight, 'warp_curve2straight.nii.gz') sct.copy(fn_warp_straight2curve, 'warp_straight2curve.nii.gz') sct.copy(fn_straight_ref, 'straight_ref.nii.gz') # apply straightening sct_apply_transfo.main(args=[ '-i', ftmp_seg, '-w', 'warp_curve2straight.nii.gz', '-d', 'straight_ref.nii.gz', '-o', add_suffix(ftmp_seg, '_straight')]) else: from spinalcordtoolbox.straightening import SpinalCordStraightener sc_straight = SpinalCordStraightener(ftmp_seg, ftmp_seg) sc_straight.param_centerline = param_centerline sc_straight.output_filename = add_suffix(ftmp_seg, '_straight') sc_straight.path_output = './' sc_straight.qc = '0' sc_straight.remove_temp_files = param.remove_temp_files sc_straight.verbose = verbose if vertebral_alignment: sc_straight.centerline_reference_filename = ftmp_template_seg sc_straight.use_straight_reference = True sc_straight.discs_input_filename = ftmp_label sc_straight.discs_ref_filename = ftmp_template_label sc_straight.straighten() sct.cache_save(cachefile, cache_sig) # N.B. DO NOT UPDATE VARIABLE ftmp_seg BECAUSE TEMPORARY USED LATER # re-define warping field using non-cropped space (to avoid issue #367) sct_concat_transfo.main(args=[ '-w', 'warp_straight2curve.nii.gz', '-d', ftmp_data, '-o', 'warp_straight2curve.nii.gz']) if vertebral_alignment: sct.copy('warp_curve2straight.nii.gz', 'warp_curve2straightAffine.nii.gz') else: # Label preparation: # -------------------------------------------------------------------------------- # Remove unused label on template. Keep only label present in the input label image sct.printv('\nRemove unused label on template. Keep only label present in the input label image...', verbose) sct.run(['sct_label_utils', '-i', ftmp_template_label, '-o', ftmp_template_label, '-remove-reference', ftmp_label]) # Dilating the input label so they can be straighten without losing them sct.printv('\nDilating input labels using 3vox ball radius') sct_maths.main(['-i', ftmp_label, '-dilate', '3', '-o', add_suffix(ftmp_label, '_dilate')]) ftmp_label = add_suffix(ftmp_label, '_dilate') # Apply straightening to labels sct.printv('\nApply straightening to labels...', verbose) sct_apply_transfo.main(args=[ '-i', ftmp_label, '-o', add_suffix(ftmp_label, '_straight'), '-d', add_suffix(ftmp_seg, '_straight'), '-w', 'warp_curve2straight.nii.gz', '-x', 'nn']) ftmp_label = add_suffix(ftmp_label, '_straight') # Compute rigid transformation straight landmarks --> template landmarks sct.printv('\nEstimate transformation for step #0...', verbose) try: register_landmarks(ftmp_label, ftmp_template_label, paramreg.steps['0'].dof, fname_affine='straight2templateAffine.txt', verbose=verbose) except RuntimeError: raise('Input labels do not seem to be at the right place. Please check the position of the labels. ' 'See documentation for more details: https://www.slideshare.net/neuropoly/sct-course-20190121/42') # Concatenate transformations: curve --> straight --> affine sct.printv('\nConcatenate transformations: curve --> straight --> affine...', verbose) sct_concat_transfo.main(args=[ '-w', ['warp_curve2straight.nii.gz', 'straight2templateAffine.txt'], '-d', 'template.nii', '-o', 'warp_curve2straightAffine.nii.gz']) # Apply transformation sct.printv('\nApply transformation...', verbose) sct_apply_transfo.main(args=[ '-i', ftmp_data, '-o', add_suffix(ftmp_data, '_straightAffine'), '-d', ftmp_template, '-w', 'warp_curve2straightAffine.nii.gz']) ftmp_data = add_suffix(ftmp_data, '_straightAffine') sct_apply_transfo.main(args=[ '-i', ftmp_seg, '-o', add_suffix(ftmp_seg, '_straightAffine'), '-d', ftmp_template, '-w', 'warp_curve2straightAffine.nii.gz', '-x', 'linear']) ftmp_seg = add_suffix(ftmp_seg, '_straightAffine') """ # Benjamin: Issue from Allan Martin, about the z=0 slice that is screwed up, caused by the affine transform. # Solution found: remove slices below and above landmarks to avoid rotation effects points_straight = [] for coord in landmark_template: points_straight.append(coord.z) min_point, max_point = int(np.round(np.min(points_straight))), int(np.round(np.max(points_straight))) ftmp_seg_, ftmp_seg = ftmp_seg, add_suffix(ftmp_seg, '_black') msct_image.spatial_crop(Image(ftmp_seg_), dict(((2, (min_point,max_point)),))).save(ftmp_seg) """ # open segmentation im = Image(ftmp_seg) im_new = msct_image.empty_like(im) # binarize im_new.data = im.data > 0.5 # find min-max of anat2template (for subsequent cropping) zmin_template, zmax_template = msct_image.find_zmin_zmax(im_new, threshold=0.5) # save binarized segmentation im_new.save(add_suffix(ftmp_seg, '_bin')) # unused? # crop template in z-direction (for faster processing) # TODO: refactor to use python module instead of doing i/o sct.printv('\nCrop data in template space (for faster processing)...', verbose) ftmp_template_, ftmp_template = ftmp_template, add_suffix(ftmp_template, '_crop') msct_image.spatial_crop(Image(ftmp_template_), dict(((2, (zmin_template,zmax_template)),))).save(ftmp_template) ftmp_template_seg_, ftmp_template_seg = ftmp_template_seg, add_suffix(ftmp_template_seg, '_crop') msct_image.spatial_crop(Image(ftmp_template_seg_), dict(((2, (zmin_template,zmax_template)),))).save(ftmp_template_seg) ftmp_data_, ftmp_data = ftmp_data, add_suffix(ftmp_data, '_crop') msct_image.spatial_crop(Image(ftmp_data_), dict(((2, (zmin_template,zmax_template)),))).save(ftmp_data) ftmp_seg_, ftmp_seg = ftmp_seg, add_suffix(ftmp_seg, '_crop') msct_image.spatial_crop(Image(ftmp_seg_), dict(((2, (zmin_template,zmax_template)),))).save(ftmp_seg) # sub-sample in z-direction # TODO: refactor to use python module instead of doing i/o sct.printv('\nSub-sample in z-direction (for faster processing)...', verbose) sct.run(['sct_resample', '-i', ftmp_template, '-o', add_suffix(ftmp_template, '_sub'), '-f', '1x1x' + zsubsample], verbose) ftmp_template = add_suffix(ftmp_template, '_sub') sct.run(['sct_resample', '-i', ftmp_template_seg, '-o', add_suffix(ftmp_template_seg, '_sub'), '-f', '1x1x' + zsubsample], verbose) ftmp_template_seg = add_suffix(ftmp_template_seg, '_sub') sct.run(['sct_resample', '-i', ftmp_data, '-o', add_suffix(ftmp_data, '_sub'), '-f', '1x1x' + zsubsample], verbose) ftmp_data = add_suffix(ftmp_data, '_sub') sct.run(['sct_resample', '-i', ftmp_seg, '-o', add_suffix(ftmp_seg, '_sub'), '-f', '1x1x' + zsubsample], verbose) ftmp_seg = add_suffix(ftmp_seg, '_sub') # Registration straight spinal cord to template sct.printv('\nRegister straight spinal cord to template...', verbose) # loop across registration steps warp_forward = [] warp_inverse = [] for i_step in range(1, len(paramreg.steps)): sct.printv('\nEstimate transformation for step #' + str(i_step) + '...', verbose) # identify which is the src and dest if paramreg.steps[str(i_step)].type == 'im': src = ftmp_data dest = ftmp_template interp_step = 'linear' elif paramreg.steps[str(i_step)].type == 'seg': src = ftmp_seg dest = ftmp_template_seg interp_step = 'nn' else: sct.printv('ERROR: Wrong image type.', 1, 'error') if paramreg.steps[str(i_step)].algo == 'centermassrot' and paramreg.steps[str(i_step)].rot_method == 'hog': src_seg = ftmp_seg dest_seg = ftmp_template_seg # if step>1, apply warp_forward_concat to the src image to be used if i_step > 1: # apply transformation from previous step, to use as new src for registration sct_apply_transfo.main(args=[ '-i', src, '-d', dest, '-w', warp_forward, '-o', add_suffix(src, '_regStep' + str(i_step - 1)), '-x', interp_step]) src = add_suffix(src, '_regStep' + str(i_step - 1)) if paramreg.steps[str(i_step)].algo == 'centermassrot' and paramreg.steps[str(i_step)].rot_method == 'hog': # also apply transformation to the seg sct_apply_transfo.main(args=[ '-i', src_seg, '-d', dest_seg, '-w', warp_forward, '-o', add_suffix(src, '_regStep' + str(i_step - 1)), '-x', interp_step]) src_seg = add_suffix(src_seg, '_regStep' + str(i_step - 1)) # register src --> dest # TODO: display param for debugging if paramreg.steps[str(i_step)].algo == 'centermassrot' and paramreg.steps[str(i_step)].rot_method == 'hog': # im_seg case warp_forward_out, warp_inverse_out = register([src, src_seg], [dest, dest_seg], paramreg, param, str(i_step)) else: warp_forward_out, warp_inverse_out = register(src, dest, paramreg, param, str(i_step)) warp_forward.append(warp_forward_out) warp_inverse.append(warp_inverse_out) # Concatenate transformations: anat --> template sct.printv('\nConcatenate transformations: anat --> template...', verbose) warp_forward.insert(0, 'warp_curve2straightAffine.nii.gz') sct_concat_transfo.main(args=[ '-w', warp_forward, '-d', 'template.nii', '-o', 'warp_anat2template.nii.gz']) # Concatenate transformations: template --> anat sct.printv('\nConcatenate transformations: template --> anat...', verbose) warp_inverse.reverse() if vertebral_alignment: warp_inverse.append('warp_straight2curve.nii.gz') sct_concat_transfo.main(args=[ '-w', warp_inverse, '-d', 'data.nii', '-o', 'warp_template2anat.nii.gz']) else: warp_inverse.append('straight2templateAffine.txt') warp_inverse.append('warp_straight2curve.nii.gz') sct_concat_transfo.main(args=[ '-w', warp_inverse, '-winv', ['straight2templateAffine.txt'], '-d', 'data.nii', '-o', 'warp_template2anat.nii.gz']) # register template->subject elif ref == 'subject': # Change orientation of input images to RPI sct.printv('\nChange orientation of input images to RPI...', verbose) ftmp_data = Image(ftmp_data).change_orientation("RPI", generate_path=True).save().absolutepath ftmp_seg = Image(ftmp_seg).change_orientation("RPI", generate_path=True).save().absolutepath ftmp_label = Image(ftmp_label).change_orientation("RPI", generate_path=True).save().absolutepath # Remove unused label on template. Keep only label present in the input label image sct.printv('\nRemove unused label on template. Keep only label present in the input label image...', verbose) sct.run(['sct_label_utils', '-i', ftmp_template_label, '-o', ftmp_template_label, '-remove-reference', ftmp_label]) # Add one label because at least 3 orthogonal labels are required to estimate an affine transformation. This # new label is added at the level of the upper most label (lowest value), at 1cm to the right. for i_file in [ftmp_label, ftmp_template_label]: im_label = Image(i_file) coord_label = im_label.getCoordinatesAveragedByValue() # N.B. landmarks are sorted by value # Create new label from copy import deepcopy new_label = deepcopy(coord_label[0]) # move it 5mm to the left (orientation is RAS) nx, ny, nz, nt, px, py, pz, pt = im_label.dim new_label.x = np.round(coord_label[0].x + 5.0 / px) # assign value 99 new_label.value = 99 # Add to existing image im_label.data[int(new_label.x), int(new_label.y), int(new_label.z)] = new_label.value # Overwrite label file # im_label.absolutepath = 'label_rpi_modif.nii.gz' im_label.save() # Bring template to subject space using landmark-based transformation sct.printv('\nEstimate transformation for step #0...', verbose) warp_forward = ['template2subjectAffine.txt'] warp_inverse = ['template2subjectAffine.txt'] try: register_landmarks(ftmp_template_label, ftmp_label, paramreg.steps['0'].dof, fname_affine=warp_forward[0], verbose=verbose, path_qc="./") except Exception: sct.printv('ERROR: input labels do not seem to be at the right place. Please check the position of the labels. See documentation for more details: https://www.slideshare.net/neuropoly/sct-course-20190121/42', verbose=verbose, type='error') # loop across registration steps for i_step in range(1, len(paramreg.steps)): sct.printv('\nEstimate transformation for step #' + str(i_step) + '...', verbose) # identify which is the src and dest if paramreg.steps[str(i_step)].type == 'im': src = ftmp_template dest = ftmp_data interp_step = 'linear' elif paramreg.steps[str(i_step)].type == 'seg': src = ftmp_template_seg dest = ftmp_seg interp_step = 'nn' else: sct.printv('ERROR: Wrong image type.', 1, 'error') # apply transformation from previous step, to use as new src for registration sct_apply_transfo.main(args=[ '-i', src, '-d', dest, '-w', warp_forward, '-o', add_suffix(src, '_regStep' + str(i_step - 1)), '-x', interp_step]) src = add_suffix(src, '_regStep' + str(i_step - 1)) # register src --> dest # TODO: display param for debugging warp_forward_out, warp_inverse_out = register(src, dest, paramreg, param, str(i_step)) warp_forward.append(warp_forward_out) warp_inverse.insert(0, warp_inverse_out) # Concatenate transformations: sct.printv('\nConcatenate transformations: template --> subject...', verbose) sct_concat_transfo.main(args=[ '-w', warp_forward, '-d', 'data.nii', '-o', 'warp_template2anat.nii.gz']) sct.printv('\nConcatenate transformations: subject --> template...', verbose) sct_concat_transfo.main(args=[ '-w', warp_inverse, '-winv', ['template2subjectAffine.txt'], '-d', 'template.nii', '-o', 'warp_anat2template.nii.gz']) # Apply warping fields to anat and template sct.run(['sct_apply_transfo', '-i', 'template.nii', '-o', 'template2anat.nii.gz', '-d', 'data.nii', '-w', 'warp_template2anat.nii.gz', '-crop', '1'], verbose) sct.run(['sct_apply_transfo', '-i', 'data.nii', '-o', 'anat2template.nii.gz', '-d', 'template.nii', '-w', 'warp_anat2template.nii.gz', '-crop', '1'], verbose) # come back os.chdir(curdir) # Generate output files sct.printv('\nGenerate output files...', verbose) fname_template2anat = os.path.join(path_output, 'template2anat' + ext_data) fname_anat2template = os.path.join(path_output, 'anat2template' + ext_data) sct.generate_output_file(os.path.join(path_tmp, "warp_template2anat.nii.gz"), os.path.join(path_output, "warp_template2anat.nii.gz"), verbose) sct.generate_output_file(os.path.join(path_tmp, "warp_anat2template.nii.gz"), os.path.join(path_output, "warp_anat2template.nii.gz"), verbose) sct.generate_output_file(os.path.join(path_tmp, "template2anat.nii.gz"), fname_template2anat, verbose) sct.generate_output_file(os.path.join(path_tmp, "anat2template.nii.gz"), fname_anat2template, verbose) if ref == 'template': # copy straightening files in case subsequent SCT functions need them sct.generate_output_file(os.path.join(path_tmp, "warp_curve2straight.nii.gz"), os.path.join(path_output, "warp_curve2straight.nii.gz"), verbose) sct.generate_output_file(os.path.join(path_tmp, "warp_straight2curve.nii.gz"), os.path.join(path_output, "warp_straight2curve.nii.gz"), verbose) sct.generate_output_file(os.path.join(path_tmp, "straight_ref.nii.gz"), os.path.join(path_output, "straight_ref.nii.gz"), verbose) # Delete temporary files if param.remove_temp_files: sct.printv('\nDelete temporary files...', verbose) sct.rmtree(path_tmp, verbose=verbose) # display elapsed time elapsed_time = time.time() - start_time sct.printv('\nFinished! Elapsed time: ' + str(int(np.round(elapsed_time))) + 's', verbose) qc_dataset = arguments.get("-qc-dataset", None) qc_subject = arguments.get("-qc-subject", None) if param.path_qc is not None: generate_qc(fname_data, fname_in2=fname_template2anat, fname_seg=fname_seg, args=args, path_qc=os.path.abspath(param.path_qc), dataset=qc_dataset, subject=qc_subject, process='sct_register_to_template') sct.display_viewer_syntax([fname_data, fname_template2anat], verbose=verbose) sct.display_viewer_syntax([fname_template, fname_anat2template], verbose=verbose)
def crop_with_gui(self): import matplotlib.pyplot as plt import matplotlib.image as mpimg # Initialization fname_data = self.input_filename suffix_out = '_crop' remove_temp_files = self.rm_tmp_files verbose = self.verbose # Check file existence sct.printv('\nCheck file existence...', verbose) sct.check_file_exist(fname_data, verbose) # Get dimensions of data sct.printv('\nGet dimensions of data...', verbose) nx, ny, nz, nt, px, py, pz, pt = Image(fname_data).dim sct.printv('.. ' + str(nx) + ' x ' + str(ny) + ' x ' + str(nz), verbose) # check if 4D data if not nt == 1: sct.printv( '\nERROR in ' + os.path.basename(__file__) + ': Data should be 3D.\n', 1, 'error') sys.exit(2) # sct.printv(arguments) sct.printv('\nCheck parameters:') sct.printv(' data ................... ' + fname_data) # Extract path/file/extension path_data, file_data, ext_data = sct.extract_fname(fname_data) path_out, file_out, ext_out = '', file_data + suffix_out, ext_data path_tmp = sct.tmp_create() + "/" # copy files into tmp folder from sct_convert import convert sct.printv('\nCopying input data to tmp folder and convert to nii...', verbose) convert(fname_data, os.path.join(path_tmp, "data.nii")) # go to tmp folder curdir = os.getcwd() os.chdir(path_tmp) # change orientation sct.printv('\nChange orientation to RPI...', verbose) Image('data.nii').change_orientation("RPI").save('data_rpi.nii') # get image of medial slab sct.printv('\nGet image of medial slab...', verbose) image_array = nibabel.load('data_rpi.nii').get_data() nx, ny, nz = image_array.shape scipy.misc.imsave('image.jpg', image_array[math.floor(nx / 2), :, :]) # Display the image sct.printv('\nDisplay image and get cropping region...', verbose) fig = plt.figure() # fig = plt.gcf() # ax = plt.gca() ax = fig.add_subplot(111) img = mpimg.imread("image.jpg") implot = ax.imshow(img.T) implot.set_cmap('gray') plt.gca().invert_yaxis() # mouse callback ax.set_title( 'Left click on the top and bottom of your cropping field.\n Right click to remove last point.\n Close window when your done.' ) line, = ax.plot([], [], 'ro') # empty line cropping_coordinates = LineBuilder(line) plt.show() # disconnect callback # fig.canvas.mpl_disconnect(line) # check if user clicked two times if len(cropping_coordinates.xs) != 2: sct.printv( '\nERROR: You have to select two points. Exit program.\n', 1, 'error') sys.exit(2) # convert coordinates to integer zcrop = [int(i) for i in cropping_coordinates.ys] # sort coordinates zcrop.sort() # crop image sct.printv('\nCrop image...', verbose) nii = Image('data_rpi.nii') data_crop = nii.data[:, :, zcrop[0]:zcrop[1]] nii.data = data_crop nii.absolutepath = 'data_rpi_crop.nii' nii.save() # come back os.chdir(curdir) sct.printv('\nGenerate output files...', verbose) sct.generate_output_file(os.path.join(path_tmp, "data_rpi_crop.nii"), os.path.join(path_out, file_out + ext_out)) # Remove temporary files if remove_temp_files == 1: sct.printv('\nRemove temporary files...') sct.rmtree(path_tmp) sct.display_viewer_syntax( files=[os.path.join(path_out, file_out + ext_out)])
def get_slices_matching_with_vertebral_levels(metric_data,fname_tracts,vert_levels_list=None): """Return the slices of the input image corresponding to the vertebral levels given as argument.""" # check existence of "vertebral_labeling.nii.gz" file fname_vertebral_labeling = fname_tracts + '/vertebral_labeling.nii.gz' sct.check_file_exist(fname_vertebral_labeling) # Read files vertebral_labeling.nii.gz print '\nRead files vertebral_labeling.nii.gz...' # Load vertebral_labeling.nii.gz file_vert_labeling = load(fname_vertebral_labeling) # Make data in array format data_vert_labeling = file_vert_labeling.get_data() # Extract metric data size X, Y, Z [mx, my, mz] = metric_data.shape # Extract vertebral labeling data size X, Y, Z [vx, vy, vz] = data_vert_labeling.shape # Initialisation of check error flag exit_program = 0 # Check if sizes along X are the same if mx != vx: print '\tERROR: Size of vertebral_labeling.nii.gz along X is not the same as the metric data.' exit_program = 1 # Check if sizes along Y are the same if my != vy: print '\tERROR: Size of vertebral_labeling.nii.gz along Y is not the same as the metric data.' exit_program = 1 # Check if sizes along Z are the same if mz != vz: print '\tERROR: Size of vertebral_labeling.nii.gz along Z is not the same as the metric data.' exit_program = 1 # Compute the minimum and maximum vertebral levels available in the input image min_vert_level, max_vert_level = int(numpy.amin(concatenate(data_vert_labeling,axis=None))), int(numpy.amax(concatenate(data_vert_labeling,axis=None))) if vert_levels_list!=None: # Check if the vertebral levels selected are available in the input image if (vert_levels_list[0] < min_vert_level or vert_levels_list[1] > max_vert_level): print '\tERROR: The vertebral levels you selected are not available in the input image.' print 'Minimum level asked: '+ str(vert_levels_list[0]) print '...minimum level available in the input image: '+str(min_vert_level) print 'Maximum level asked: '+ str(vert_levels_list[1]) print '...maximum level available in the input image: '+str(max_vert_level) exit_program = 1 # Exit program if error is detect in sizes if exit_program == 1 : print '\nExit program.\n' sys.exit(2) # Extract the X, Y, Z positions of voxels belonging to the first vertebral level X_bottom_level, Y_bottom_level, Z_bottom_level = (data_vert_labeling==vert_levels_list[0]).nonzero() # Record the bottom of slice of this level slice_min_bottom = min(Z_bottom_level) # Extract the X, Y, Z positions of voxels belonging to the last vertebral level X_top_level, Y_top_level, Z_top_level = (data_vert_labeling==vert_levels_list[1]).nonzero() # Record the top slice of this level slice_max_top = max(Z_top_level) # Take into account the case where the ordering of the slice is reversed compared to the ordering of the vertebral level if slice_min_bottom > slice_max_top: slice_min = min(Z_top_level) slice_max = max(Z_bottom_level) else: slice_min = min(Z_bottom_level) slice_max = max(Z_top_level) # Return the slice numbers in the right format return str(slice_min)+':'+str(slice_max) else: # Exit program if error is detect in sizes if exit_program == 1 : print '\nExit program.\n' sys.exit(2) # Return the minimum and maximum vertebral levels available in the input image return [min_vert_level, max_vert_level]
def main(): #Initialization fname = '' landmarks_native = '' #landmarks_template = path_sct + '/dev/template_creation/template_landmarks-mm.nii.gz' landmarks_template = path_sct + '/dev/template_creation/landmark_native.nii.gz' reference = path_sct + '/dev/template_creation/template_shape.nii.gz' verbose = param.verbose interpolation_method = 'spline' try: opts, args = getopt.getopt(sys.argv[1:], 'hi:n:t:R:v:a:') except getopt.GetoptError: usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ("-i"): fname = arg elif opt in ("-n"): landmarks_native = arg elif opt in ("-t"): landmarks_template = arg elif opt in ("-R"): reference = arg elif opt in ('-v'): verbose = int(arg) elif opt in ('-a'): interpolation_method = str(arg) # display usage if a mandatory argument is not provided if fname == '': usage() # check existence of input files print '\nCheck if file exists ...' sct.check_file_exist(fname) sct.check_file_exist(landmarks_native) sct.check_file_exist(landmarks_template) sct.check_file_exist(reference) path_input, file_input, ext_input = sct.extract_fname(fname) output_name = path_input + file_input + '_2temp' + ext_input print output_name transfo = 'native2temp.txt' # Display arguments print '\nCheck input arguments...' print ' Input volume ...................... ' + fname print ' Landmarks in native space ...................... ' + landmarks_native print ' Landmarks in template space ...................... ' + landmarks_template print ' Reference ...................... ' + reference print ' Verbose ........................... ' + str(verbose) print '\nEstimate rigid transformation between paired landmarks...' sct.run('isct_ANTSUseLandmarkImagesToGetAffineTransform ' + landmarks_template + ' ' + landmarks_native + ' affine ' + transfo) # Apply rigid transformation print '\nApply affine transformation to native landmarks...' sct.run('sct_apply_transfo -i ' + fname + ' -o ' + output_name + ' -d ' + reference + ' -w ' + transfo + ' -x ' + interpolation_method) # sct.run('WarpImageMultiTransform 3 ' + fname + ' ' + output_name + ' -R ' + reference + ' ' + transfo) print '\nFile created : ' + output_name
def main(): # initialization start_time = time.time() path_out = '.' param_user = '' # reducing the number of CPU used for moco (see issue #201) os.environ["ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS"] = "1" # get path of the toolbox status, param.path_sct = commands.getstatusoutput('echo $SCT_DIR') # Parameters for debug mode if param.debug: # get path of the testing data status, path_sct_data = commands.getstatusoutput('echo $SCT_TESTING_DATA_DIR') param.fname_data = path_sct_data+'/dmri/dmri.nii.gz' param.fname_bvecs = path_sct_data+'/dmri/bvecs.txt' param.fname_mask = path_sct_data+'/dmri/dmri.nii.gz' param.remove_tmp_files = 0 param.verbose = 1 param.run_eddy = 0 param.otsu = 0 param.group_size = 5 param.iterative_averaging = 1 else: # Check input parameters try: opts, args = getopt.getopt(sys.argv[1:], 'hi:a:b:e:f:g:m:o:p:r:t:v:x:') except getopt.GetoptError: usage() if not opts: usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ('-a'): param.fname_bvals = arg elif opt in ('-b'): param.fname_bvecs = arg elif opt in ('-e'): param.run_eddy = int(arg) elif opt in ('-f'): param.spline_fitting = int(arg) elif opt in ('-g'): param.group_size = int(arg) elif opt in ('-i'): param.fname_data = arg elif opt in ('-m'): param.fname_mask = arg elif opt in ('-o'): path_out = arg elif opt in ('-p'): param_user = arg elif opt in ('-r'): param.remove_tmp_files = int(arg) elif opt in ('-t'): param.otsu = int(arg) elif opt in ('-v'): param.verbose = int(arg) elif opt in ('-x'): param.interp = arg # display usage if a mandatory argument is not provided if param.fname_data == '' or param.fname_bvecs == '': sct.printv('ERROR: All mandatory arguments are not provided. See usage.', 1, 'error') usage() # parse argument for param if not param_user == '': param.param = param_user.replace(' ', '').split(',') # remove spaces and parse with comma # TODO: check integrity of input # param.param = [i for i in range(len(param_user))] del param_user sct.printv('\nInput parameters:', param.verbose) sct.printv(' input file ............'+param.fname_data, param.verbose) sct.printv(' bvecs file ............'+param.fname_bvecs, param.verbose) sct.printv(' bvals file ............'+param.fname_bvals, param.verbose) sct.printv(' mask file .............'+param.fname_mask, param.verbose) # check existence of input files sct.printv('\nCheck file existence...', param.verbose) sct.check_file_exist(param.fname_data, param.verbose) sct.check_file_exist(param.fname_bvecs, param.verbose) if not param.fname_bvals == '': sct.check_file_exist(param.fname_bvals, param.verbose) if not param.fname_mask == '': sct.check_file_exist(param.fname_mask, param.verbose) # Get full path param.fname_data = os.path.abspath(param.fname_data) param.fname_bvecs = os.path.abspath(param.fname_bvecs) if param.fname_bvals != '': param.fname_bvals = os.path.abspath(param.fname_bvals) if param.fname_mask != '': param.fname_mask = os.path.abspath(param.fname_mask) # Extract path, file and extension path_data, file_data, ext_data = sct.extract_fname(param.fname_data) path_mask, file_mask, ext_mask = sct.extract_fname(param.fname_mask) # create temporary folder sct.printv('\nCreate temporary folder...', param.verbose) path_tmp = sct.slash_at_the_end('tmp.'+time.strftime("%y%m%d%H%M%S"), 1) sct.run('mkdir '+path_tmp, param.verbose) # Copying input data to tmp folder # NB: cannot use c3d here because c3d cannot convert 4D data. sct.printv('\nCopying input data to tmp folder and convert to nii...', param.verbose) sct.run('cp '+param.fname_data+' '+path_tmp+'dmri'+ext_data, param.verbose) sct.run('cp '+param.fname_bvecs+' '+path_tmp+'bvecs.txt', param.verbose) if param.fname_mask != '': sct.run('cp '+param.fname_mask+' '+path_tmp+'mask'+ext_mask, param.verbose) # go to tmp folder os.chdir(path_tmp) # convert dmri to nii format convert('dmri'+ext_data, 'dmri.nii') # update field in param (because used later). # TODO: make this cleaner... if param.fname_mask != '': param.fname_mask = 'mask'+ext_mask # run moco dmri_moco(param) # come back to parent folder os.chdir('..') # Generate output files path_out = sct.slash_at_the_end(path_out, 1) sct.create_folder(path_out) sct.printv('\nGenerate output files...', param.verbose) sct.generate_output_file(path_tmp+'dmri'+param.suffix+'.nii', path_out+file_data+param.suffix+ext_data, param.verbose) sct.generate_output_file(path_tmp+'b0_mean.nii', path_out+'b0'+param.suffix+'_mean'+ext_data, param.verbose) sct.generate_output_file(path_tmp+'dwi_mean.nii', path_out+'dwi'+param.suffix+'_mean'+ext_data, param.verbose) # Delete temporary files if param.remove_tmp_files == 1: sct.printv('\nDelete temporary files...', param.verbose) sct.run('rm -rf '+path_tmp, param.verbose) # display elapsed time elapsed_time = time.time() - start_time sct.printv('\nFinished! Elapsed time: '+str(int(round(elapsed_time)))+'s', param.verbose) #To view results sct.printv('\nTo view results, type:', param.verbose) sct.printv('fslview -m ortho,ortho '+param.path_out+file_data+param.suffix+' '+file_data+' &\n', param.verbose, 'info')
def main(): # Initialization fname_anat = '' fname_centerline = '' fwhm = param.fwhm width=param.width remove_temp_files = param.remove_temp_files start_time = time.time() verbose = param.verbose # extract path of the script path_script = os.path.dirname(__file__) + '/' # Parameters for debug mode if param.debug == 1: print '\n*** WARNING: DEBUG MODE ON ***\n' fname_anat = '/home/django/ibouchard/errsm_22_t2_cropped_rpi.nii.gz' fname_centerline = '/home/django/ibouchard//errsm_22_t2_cropped_centerline.nii.gz' fwhm=1 width=20 # Check input param try: opts, args = getopt.getopt(sys.argv[1:], 'hi:c:f:w:r:') except getopt.GetoptError as err: print str(err) usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ('-i'): fname_anat = arg elif opt in ('-c'): fname_centerline = arg elif opt in ('-f'): fwhm = int(arg) elif opt in ('w'): width=int(arg) elif opt in ('-r'): remove_temp_files = int(arg) # display usage if a mandatory argument is not provided if fname_anat == '' or fname_centerline == '': usage() # check existence of input files sct.check_file_exist(fname_anat) sct.check_file_exist(fname_centerline) # extract path/file/extension path_anat, file_anat, ext_anat = sct.extract_fname(fname_anat) # extract path/file/extension path_centerline, file_centerline, ext_centerline = sct.extract_fname(fname_centerline) # Display arguments print '\nCheck input arguments...' print '.. Anatomical image: ' + fname_anat print '.. Centerline: ' + fname_centerline print '.. Full width at half maximum: ' + str(fwhm) print '.. Width of the square window: ' + str(width) # create temporary folder sct.printv('\nCreate temporary folder...', verbose) path_tmp = sct.slash_at_the_end('tmp.'+time.strftime("%y%m%d%H%M%S"), 1) sct.run('mkdir '+path_tmp, verbose) # Copying input data to tmp folder and convert to nii sct.printv('\nCopying input data to tmp folder and convert to nii...', verbose) sct.run('cp '+fname_anat+' '+path_tmp+'data'+ext_anat, verbose) sct.run('cp '+fname_centerline+' '+path_tmp+'centerline'+ext_centerline, verbose) # go to tmp folder os.chdir(path_tmp) # convert to nii format convert('data'+ext_anat, 'data.nii') convert('centerline'+ext_centerline, 'centerline.nii') # # Get dimensions of data # sct.printv('\nGet dimensions of data...', param.verbose) # nx, ny, nz, nt, px, py, pz, pt = Image('data.nii').dim # # #Delete existing tmp file in the current folder to avoid problems # #Delete existing tmp file in the current folder to avoid problems # if os.path.isfile('tmp.anat.nii'): # sct.run('rm tmp.anat.nii') # if os.path.isfile('tmp.centerline.nii'): # sct.run('rm tmp.centerline.nii') # # # Convert to nii and delete nii.gz if still existing # print '\nCopy input data...' # sct.run('cp ' + fname_anat + ' tmp.anat'+ext_anat) # convert('data'+ext_data, 'data.nii') # # sct.run('fslchfiletype NIFTI tmp.anat') # if os.path.isfile('tmp.anat.nii.gz'): # sct.run('rm tmp.anat.nii.gz') # print '.. Anatomical image copied' # sct.run('cp ' + fname_centerline + ' tmp.centerline'+ext_centerline) # sct.run('fslchfiletype NIFTI tmp.centerline') # if os.path.isfile('tmp.centerline.nii.gz'): # sct.run('rm tmp.centerline.nii.gz') # print '.. Centerline image copied' # Open anatomical image #========================================================================================== # Reorient input anatomical volume into RL PA IS orientation print '\nReorient input volume to RL PA IS orientation...' sct.run(sct.fsloutput + 'fslswapdim tmp.anat RL PA IS tmp.anat_orient') print '\nGet dimensions of input anatomical image...' nx_a, ny_a, nz_a, nt_a, px_a, py_a, pz_a, pt_a = sct.get_dimension('tmp.anat_orient') #nx_a, ny_a, nz_a, nt_a, px_a, py_a, pz_a, pt_a = sct.get_dimension(fname_anat) print '.. matrix size: ' + str(nx_a) + ' x ' + str(ny_a) + ' x ' + str(nz_a) print '.. voxel size: ' + str(px_a) + 'mm x ' + str(py_a) + 'mm x ' + str(pz_a) + 'mm' print '\nOpen anatomical volume...' file = nibabel.load('tmp.anat_orient.nii') #file = nibabel.load(fname_anat) data_anat = file.get_data() data_anat=np.array(data_anat) data_anat_smoothed=np.copy(data_anat) # Open centerline #========================================================================================== # Reorient binary point into RL PA IS orientation print '\nReorient centerline volume into RL PA IS orientation...' sct.run(sct.fsloutput + 'fslswapdim tmp.centerline RL PA IS tmp.centerline_orient') print '\nGet dimensions of input centerline...' nx, ny, nz, nt, px, py, pz, pt = sct.get_dimension('tmp.centerline_orient') #nx, ny, nz, nt, px, py, pz, pt = sct.get_dimension(fname_centerline) print '.. matrix size: ' + str(nx) + ' x ' + str(ny) + ' x ' + str(nz) print '.. voxel size: ' + str(px) + 'mm x ' + str(py) + 'mm x ' + str(pz) + 'mm' print '\nOpen centerline volume...' file = nibabel.load('tmp.centerline_orient.nii') #file = nibabel.load(fname_centerline) data_centerline = file.get_data() #Loop across z and associate x,y coordinate with the point having maximum intensity x_centerline = [0 for iz in range(0, nz, 1)] y_centerline = [0 for iz in range(0, nz, 1)] z_centerline = [iz for iz in range(0, nz, 1)] for iz in range(0, nz, 1): x_centerline[iz], y_centerline[iz] = np.unravel_index(data_centerline[:, :, iz].argmax(), data_centerline[:, :, iz].shape) del data_centerline # Fit polynomial function through centerline #========================================================================================== #Fit centerline in the Z-X plane using polynomial function print '\nFit centerline in the Z-X plane using polynomial function...' coeffsx = np.polyfit(z_centerline, x_centerline, deg=param.deg_poly) polyx = np.poly1d(coeffsx) x_centerline_fit = np.polyval(polyx, z_centerline) #Fit centerline in the Z-Y plane using polynomial function print '\nFit centerline in the Z-Y plane using polynomial function...' coeffsy = np.polyfit(z_centerline, y_centerline, deg=param.deg_poly) polyy = np.poly1d(coeffsy) y_centerline_fit = np.polyval(polyy, z_centerline) # Find tangent function of centerline along z #========================================================================================== # Find tangent to centerline in zx plane, along z print '\nFind tangent to centerline along z, in the Z-X plane...' poly_tangent_xz = np.polyder(polyx) tangent_xz = np.polyval(poly_tangent_xz, z_centerline) # Find tangent to centerline in zy plane, along z print '\nFind tangent to centerline along z, in the Z-Y plane...' poly_tangent_yz = np.polyder(polyy) tangent_yz = np.polyval(poly_tangent_yz, z_centerline) # Create a Gaussian kernel with users parameters #========================================================================================== print '\nGenerate a Gaussian kernel with users parameters... ' # Convert the fwhm given by users in standard deviation (sigma) and find the size of gaussian kernel knowing # that size_kernel=(6*sigma-1) must be odd sigma = int(np.round((fwhm/pz_a)*(math.sqrt(1/(2*(math.log(2))))))) size_kernel= (np.round(6*sigma)) if size_kernel%2==0: size_kernel=size_kernel-1 #Creates an 1D-array impulsion and apply a gaussian filter. The result is a Gaussian kernel. kernel_temp = np.zeros(size_kernel) kernel_temp[math.ceil(size_kernel/2)] = 1 kernel= ndimage.filters.gaussian_filter1d(kernel_temp, sigma, order=0) sum_kernel=np.sum(kernel) print '.. Full width at half maximum: ' + str(fwhm) print '.. Kernel size : '+str(size_kernel) print '.. Sigma (Standard deviation): ' + str(sigma) del kernel_temp ## Smooth along the spinal cord ##========================================================================================== print '\nSmooth along the spinal cord...' print '\n Voxel position along z axis...' # Initialisations position=np.zeros(3) flag=np.zeros((nx_a,ny_a,nz_a)) data_weight=np.ones((nx_a,ny_a,nz_a)) smoothing_array=np.zeros(size_kernel) x_near=np.zeros(2) y_near=np.zeros(2) z_near=np.zeros(2) floor_position=np.zeros(3) ceil_position=np.zeros(3) position_d=np.zeros(3) #For every voxel along z axis, for iz in range(0,nz_a,1): print '.. '+str(iz+1)+ '/'+str(nz_a) # Determine the square area to smooth around the centerline xmin=x_centerline[iz]-int(width/2) xmax=x_centerline[iz]+int(width/2) ymin=y_centerline[iz]-int(width/2) ymax=y_centerline[iz]+int(width/2) #Find the angle between the tangent and the x axis in xz plane. theta_xz = -(math.atan(tangent_xz[iz])) #Find the angle between the tangent and the y axis in yz plane. theta_yz = -(math.atan(tangent_yz[iz])) #Construct a rotation array around y axis. Rxz=np.zeros((3,3)) Rxz[1,1]=1 Rxz[0,0]=(math.cos(theta_xz)) Rxz[2,0]=(math.sin(theta_xz)) Rxz[0,2]=-(math.sin(theta_xz)) Rxz[2,2]=(math.cos(theta_xz)) #Construct a rotation array around x axis. Ryz=np.zeros((3,3)) Ryz[0,0]=1 Ryz[1,1]=(math.cos(theta_yz)) Ryz[1,2]=(math.sin(theta_yz)) Ryz[2,1]=-(math.sin(theta_yz)) Ryz[2,2]=(math.cos(theta_yz)) #For every voxels in the given plane, included in the square area for ix in range(xmin,xmax,1): for iy in range(ymin,ymax,1): #The area to smooth has the same high as the 1D mask length isize=0 centerline_point=[np.copy(x_centerline[iz]), np.copy(y_centerline[iz]), np.copy(iz)] #For every voxels along the line orthogonal to the considered plane and included in the kernel. #(Here we full a vector called smoothing_array, which has the same length as the kernel, is oriented in the direction of centerline and contains interpolated values of intensity) for isize in range(0,size_kernel, 1): #Find the position in the xy plane, before rotation position = [ix, iy, iz+isize-(np.floor(size_kernel/2))] #Find the position after rotation by multiplying the position centered on centerline point with rotation array around x and y axis. new_position= np.dot((np.dot((np.subtract(np.copy(position),centerline_point)), Rxz)), Ryz) + centerline_point #If the resulting voxel is out of image boundaries, pad the smoothing array with a zero if (new_position[0]<0)or (new_position[1]<0)or(new_position[2]<0)or(new_position[0]>nx_a-1)or (new_position[1]>ny_a-1)or(new_position[2]>nz_a-1): smoothing_array[isize]=0 #Otherwise, fill the smoothing array with the linear interpolation of values around the actual position else: # Trilinear interpolation #========================================================================================================================================== # Determine the coordinates in grid surrounding the position of the central voxel and perform a trilinear interpolation x_near[0]=np.copy(np.floor(new_position[0])) x_near[1]=np.copy(np.ceil(new_position[0])) xd=(new_position[0]-x_near[0]) y_near[0]=np.copy(np.floor(new_position[1])) y_near[1]=np.copy(np.ceil(new_position[1])) yd=(new_position[1]-y_near[0]) z_near[0]=np.copy(np.floor(new_position[2])) z_near[1]=np.copy(np.ceil(new_position[2])) zd=(new_position[2]-z_near[0]) c00=((data_anat[x_near[0],y_near[0],z_near[0]])*(1-xd))+((data_anat[x_near[1],y_near[0],z_near[0]])*(xd)) c10=((data_anat[x_near[0],y_near[1],z_near[0]])*(1-xd))+((data_anat[x_near[1],y_near[1],z_near[0]])*(xd)) c01=((data_anat[x_near[0],y_near[0],z_near[1]])*(1-xd))+((data_anat[x_near[1],y_near[0],z_near[1]])*(xd)) c11=((data_anat[x_near[0],y_near[1],z_near[1]])*(1-xd))+((data_anat[x_near[1],y_near[1],z_near[1]])*(xd)) c0=c00*(1-yd)+c10*yd c1=c01*(1-yd)+c11*yd smoothing_array[isize]=c0*(1-zd)+c1*zd #If actual position is in the z=z_centerline plane, save the coordinates in the variable central_position. (Otherwise, don't save it). if isize==(np.floor(size_kernel/2)): central_position=np.copy(new_position) #If the central_position is out of boundaries, don't consider it anymore. if (central_position[0]<0)or (central_position[1]<0)or(central_position[2]<0)or(central_position[0]>nx_a-1)or (central_position[1]>ny_a-1)or(central_position[2]>nz_a-1): continue else: #Otherwise, perform the convolution of the smoothing_array and the kernel for the central voxel only (equivalent to element-wise multiply). Normalize the result. result=((np.sum(np.copy(smoothing_array)*kernel))/sum_kernel) # Determine the coordinates in grid surrounding the position of the central voxel for i in range(0,3,1): floor_position[i]=math.floor(central_position[i]) ceil_position[i]=math.ceil(central_position[i]) position_d[i]=central_position[i]-floor_position[i] # Reverse trilinear interpolation #========================================================================================================================================== # Split the resuling intensity given by the convolution between the 8 voxels surrounding the point where the convolution is calculated (central_position). # The array data_anat_smoothed is the the volume os the anatomical image smoothed alog the spinal cord. # The array flag is a volume that indicates if a the corresponding voxel in the anatomical image is inside the smoothing area around the spinal cord and if there is already been an operation on this voxel. # The default value of flag is 0. If it is set to 1, it means there is an operation on the corresponding voxel in anatomical image. Then we clear both the data_anat_smoothed and data_weight corresponding voxel to 0. # The array data_weight represent the is represent the sum of weights used to calculate the intensity for every voxel. In a perfect case, this sum would be 1, but because there is an angle between # two adjacent planes, the sum will be lower so we need to normalize the result. The default value for data_weight is 1, but once there is an operation on the corresponding voxel (flag=1), we accumulate the weights used. if (flag[ceil_position[0],ceil_position[1],ceil_position[2]]==0): data_anat_smoothed[ceil_position[0],ceil_position[1],ceil_position[2]]=0 data_weight[ceil_position[0],ceil_position[1],ceil_position[2]]=0 flag[ceil_position[0],ceil_position[1],ceil_position[2]]=1 weight=(position_d[0])*(position_d[1])*(position_d[2]) data_anat_smoothed[ceil_position[0],ceil_position[1],ceil_position[2]]=data_anat_smoothed[ceil_position[0],ceil_position[1],ceil_position[2]]+(weight*result) data_weight[ceil_position[0],ceil_position[1],ceil_position[2]]=data_weight[ceil_position[0],ceil_position[1],ceil_position[2]]+(weight) if (flag[floor_position[0],floor_position[1],floor_position[2]]==0): data_anat_smoothed[floor_position[0],floor_position[1],floor_position[2]]=0 data_weight[floor_position[0],floor_position[1],floor_position[2]]=0 flag[floor_position[0],floor_position[1],floor_position[2]]=1 weight=(1-position_d[0])*(1-position_d[1])*(1-position_d[2]) data_anat_smoothed[floor_position[0],floor_position[1],floor_position[2]]=data_anat_smoothed[floor_position[0],floor_position[1],floor_position[2]]+(weight*result) data_weight[floor_position[0],floor_position[1],floor_position[2]]=data_weight[floor_position[0],floor_position[1],floor_position[2]]+(weight) if (flag[ceil_position[0],floor_position[1],floor_position[2]]==0): data_anat_smoothed[ceil_position[0],floor_position[1],floor_position[2]]=0 data_weight[ceil_position[0],floor_position[1],floor_position[2]]=0 flag[ceil_position[0],floor_position[1],floor_position[2]]=1 weight=(position_d[0])*(1-position_d[1])*(1-position_d[2]) data_anat_smoothed[ceil_position[0],floor_position[1],floor_position[2]]=data_anat_smoothed[ceil_position[0],floor_position[1],floor_position[2]]+(weight*result) data_weight[ceil_position[0],floor_position[1],floor_position[2]]=data_weight[ceil_position[0],floor_position[1],floor_position[2]]+(weight) if (flag[ceil_position[0],ceil_position[1],floor_position[2]]==0): data_anat_smoothed[ceil_position[0],ceil_position[1],floor_position[2]]=0 data_weight[ceil_position[0],ceil_position[1],floor_position[2]]=0 flag[ceil_position[0],ceil_position[1],floor_position[2]]=1 weight=(position_d[0])*(position_d[1])*(1-position_d[2]) data_anat_smoothed[ceil_position[0],ceil_position[1],floor_position[2]]=data_anat_smoothed[ceil_position[0],ceil_position[1],floor_position[2]]+(weight*result) data_weight[ceil_position[0],ceil_position[1],floor_position[2]]=data_weight[ceil_position[0],ceil_position[1],floor_position[2]]+(weight) if (flag[ceil_position[0],floor_position[1],ceil_position[2]]==0): data_anat_smoothed[ceil_position[0],floor_position[1],ceil_position[2]]=0 data_weight[ceil_position[0],floor_position[1],ceil_position[2]]=0 flag[ceil_position[0],floor_position[1],ceil_position[2]]=1 weight=(position_d[0])*(1-position_d[1])*(position_d[2]) data_anat_smoothed[ceil_position[0],floor_position[1],ceil_position[2]]=data_anat_smoothed[ceil_position[0],floor_position[1],ceil_position[2]]+(weight*result) data_weight[ceil_position[0],floor_position[1],ceil_position[2]]=data_weight[ceil_position[0],floor_position[1],ceil_position[2]]+(weight) if (flag[floor_position[0],ceil_position[1],floor_position[2]]==0): data_anat_smoothed[floor_position[0],ceil_position[1],floor_position[2]]=0 data_weight[floor_position[0],ceil_position[1],floor_position[2]]=0 flag[floor_position[0],ceil_position[1],floor_position[2]]=1 weight=(1-position_d[0])*(position_d[1])*(1-position_d[2]) data_anat_smoothed[floor_position[0],ceil_position[1],floor_position[2]]=data_anat_smoothed[floor_position[0],ceil_position[1],floor_position[2]]+(weight*result) data_weight[floor_position[0],ceil_position[1],floor_position[2]]=data_weight[floor_position[0],ceil_position[1],floor_position[2]]+(weight) if (flag[floor_position[0],ceil_position[1],ceil_position[2]]==0): data_anat_smoothed[floor_position[0],ceil_position[1],ceil_position[2]]=0 data_weight[floor_position[0],ceil_position[1],ceil_position[2]]=0 flag[floor_position[0],ceil_position[1],ceil_position[2]]=1 weight=(1-position_d[0])*(position_d[1])*(position_d[2]) data_anat_smoothed[floor_position[0],ceil_position[1], ceil_position[2]]= data_anat_smoothed[floor_position[0],ceil_position[1], ceil_position[2]]+(weight*result) data_weight[floor_position[0],ceil_position[1], ceil_position[2]]= data_weight[floor_position[0],ceil_position[1], ceil_position[2]]+(weight) if (flag[floor_position[0],floor_position[1],ceil_position[2]]==0): data_anat_smoothed[floor_position[0],floor_position[1],ceil_position[2]]=0 flag[floor_position[0],floor_position[1],ceil_position[2]]=1 data_weight[floor_position[0],floor_position[1],ceil_position[2]]=0 weight=(1-position_d[0])*(1-position_d[1])*(position_d[2]) data_anat_smoothed[floor_position[0],floor_position[1],ceil_position[2]]=data_anat_smoothed[floor_position[0],floor_position[1],ceil_position[2]]+(weight*result) data_weight[floor_position[0],floor_position[1],ceil_position[2]]=data_weight[floor_position[0],floor_position[1],ceil_position[2]]+(weight) # Once we covered the whole spinal cord along z, we normalize the resulting image considering the weight used to calculate each voxel intensity data_anat_smoothed=data_anat_smoothed/data_weight #Generate output file #========================================================================================== # Write NIFTI volumes print '\nWrite NIFTI volumes...' if os.path.isfile('tmp.im_smoothed.nii'): sct.run('rm tmp.im_smoothed.nii') img = nibabel.Nifti1Image(data_anat_smoothed, None) nibabel.save(img, 'tmp.im_smoothed.nii') print '.. File created: tmp.im_smoothed.nii' #Copy header geometry from input data print '\nCopy header geometry from input data and reorient the volume...' sct.run(sct.fsloutput+'fslcpgeom tmp.anat_orient.nii tmp.im_smoothed.nii ') #Generate output file print '\nGenerate output file (in current folder)...' sct.generate_output_file('tmp.im_smoothed.nii','./',file_anat+'_smoothed',ext_anat) # Delete temporary files if remove_temp_files == 1: print '\nDelete temporary files...' sct.run('rm tmp.anat.nii') sct.run('rm tmp.centerline.nii') sct.run('rm tmp.anat_orient.nii') sct.run('rm tmp.centerline_orient.nii') #Display elapsed time elapsed_time = time.time() - start_time print '\nFinished!' print '.. '+str(int(round(elapsed_time)))+'s\n'
def main(): # Initialization fname_anat = '' fname_centerline = '' centerline_fitting = 'polynome' remove_temp_files = param.remove_temp_files interp = param.interp degree_poly = param.deg_poly # extract path of the script path_script = os.path.dirname(__file__)+'/' # Parameters for debug mode if param.debug == 1: print '\n*** WARNING: DEBUG MODE ON ***\n' status, path_sct_data = commands.getstatusoutput('echo $SCT_TESTING_DATA_DIR') fname_anat = path_sct_data+'/t2/t2.nii.gz' fname_centerline = path_sct_data+'/t2/t2_seg.nii.gz' else: # Check input param try: opts, args = getopt.getopt(sys.argv[1:],'hi:c:r:d:f:s:') except getopt.GetoptError as err: print str(err) usage() if not opts: usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ('-i'): fname_anat = arg elif opt in ('-c'): fname_centerline = arg elif opt in ('-r'): remove_temp_files = int(arg) elif opt in ('-d'): degree_poly = int(arg) elif opt in ('-f'): centerline_fitting = str(arg) elif opt in ('-s'): interp = str(arg) # display usage if a mandatory argument is not provided if fname_anat == '' or fname_centerline == '': usage() # check existence of input files sct.check_file_exist(fname_anat) sct.check_file_exist(fname_centerline) # extract path/file/extension path_anat, file_anat, ext_anat = sct.extract_fname(fname_anat) # Display arguments print '\nCheck input arguments...' print ' Input volume ...................... '+fname_anat print ' Centerline ........................ '+fname_centerline print '' # Get input image orientation input_image_orientation = get_orientation(fname_anat) # Reorient input data into RL PA IS orientation set_orientation(fname_anat, 'RPI', 'tmp.anat_orient.nii') set_orientation(fname_centerline, 'RPI', 'tmp.centerline_orient.nii') # Open centerline #========================================================================================== print '\nGet dimensions of input centerline...' nx, ny, nz, nt, px, py, pz, pt = sct.get_dimension('tmp.centerline_orient.nii') print '.. matrix size: '+str(nx)+' x '+str(ny)+' x '+str(nz) print '.. voxel size: '+str(px)+'mm x '+str(py)+'mm x '+str(pz)+'mm' print '\nOpen centerline volume...' file = nibabel.load('tmp.centerline_orient.nii') data = file.get_data() X, Y, Z = (data>0).nonzero() min_z_index, max_z_index = min(Z), max(Z) # loop across z and associate x,y coordinate with the point having maximum intensity x_centerline = [0 for iz in range(min_z_index, max_z_index+1, 1)] y_centerline = [0 for iz in range(min_z_index, max_z_index+1, 1)] z_centerline = [iz for iz in range(min_z_index, max_z_index+1, 1)] # Two possible scenario: # 1. the centerline is probabilistic: each slices contains voxels with the probability of containing the centerline [0:...:1] # We only take the maximum value of the image to aproximate the centerline. # 2. The centerline/segmentation image contains many pixels per slice with values {0,1}. # We take all the points and approximate the centerline on all these points. X, Y, Z = ((data<1)*(data>0)).nonzero() # X is empty if binary image if (len(X) > 0): # Scenario 1 for iz in range(min_z_index, max_z_index+1, 1): x_centerline[iz-min_z_index], y_centerline[iz-min_z_index] = numpy.unravel_index(data[:,:,iz].argmax(), data[:,:,iz].shape) else: # Scenario 2 for iz in range(min_z_index, max_z_index+1, 1): x_seg, y_seg = (data[:,:,iz]>0).nonzero() if len(x_seg) > 0: x_centerline[iz-min_z_index] = numpy.mean(x_seg) y_centerline[iz-min_z_index] = numpy.mean(y_seg) # TODO: find a way to do the previous loop with this, which is more neat: # [numpy.unravel_index(data[:,:,iz].argmax(), data[:,:,iz].shape) for iz in range(0,nz,1)] # clear variable del data # Fit the centerline points with the kind of curve given as argument of the script and return the new smoothed coordinates if centerline_fitting == 'splines': try: x_centerline_fit, y_centerline_fit = b_spline_centerline(x_centerline,y_centerline,z_centerline) except ValueError: print "splines fitting doesn't work, trying with polynomial fitting...\n" x_centerline_fit, y_centerline_fit = polynome_centerline(x_centerline,y_centerline,z_centerline) elif centerline_fitting == 'polynome': x_centerline_fit, y_centerline_fit = polynome_centerline(x_centerline,y_centerline,z_centerline) #========================================================================================== # Split input volume print '\nSplit input volume...' sct.run(sct.fsloutput + 'fslsplit tmp.anat_orient.nii tmp.anat_z -z') file_anat_split = ['tmp.anat_z'+str(z).zfill(4) for z in range(0,nz,1)] # initialize variables file_mat_inv_cumul = ['tmp.mat_inv_cumul_z'+str(z).zfill(4) for z in range(0,nz,1)] z_init = min_z_index displacement_max_z_index = x_centerline_fit[z_init-min_z_index]-x_centerline_fit[max_z_index-min_z_index] # write centerline as text file print '\nGenerate fitted transformation matrices...' file_mat_inv_cumul_fit = ['tmp.mat_inv_cumul_fit_z'+str(z).zfill(4) for z in range(0,nz,1)] for iz in range(min_z_index, max_z_index+1, 1): # compute inverse cumulative fitted transformation matrix fid = open(file_mat_inv_cumul_fit[iz], 'w') if (x_centerline[iz-min_z_index] == 0 and y_centerline[iz-min_z_index] == 0): displacement = 0 else: displacement = x_centerline_fit[z_init-min_z_index]-x_centerline_fit[iz-min_z_index] fid.write('%i %i %i %f\n' %(1, 0, 0, displacement) ) fid.write('%i %i %i %f\n' %(0, 1, 0, 0) ) fid.write('%i %i %i %i\n' %(0, 0, 1, 0) ) fid.write('%i %i %i %i\n' %(0, 0, 0, 1) ) fid.close() # we complete the displacement matrix in z direction for iz in range(0, min_z_index, 1): fid = open(file_mat_inv_cumul_fit[iz], 'w') fid.write('%i %i %i %f\n' %(1, 0, 0, 0) ) fid.write('%i %i %i %f\n' %(0, 1, 0, 0) ) fid.write('%i %i %i %i\n' %(0, 0, 1, 0) ) fid.write('%i %i %i %i\n' %(0, 0, 0, 1) ) fid.close() for iz in range(max_z_index+1, nz, 1): fid = open(file_mat_inv_cumul_fit[iz], 'w') fid.write('%i %i %i %f\n' %(1, 0, 0, displacement_max_z_index) ) fid.write('%i %i %i %f\n' %(0, 1, 0, 0) ) fid.write('%i %i %i %i\n' %(0, 0, 1, 0) ) fid.write('%i %i %i %i\n' %(0, 0, 0, 1) ) fid.close() # apply transformations to data print '\nApply fitted transformation matrices...' file_anat_split_fit = ['tmp.anat_orient_fit_z'+str(z).zfill(4) for z in range(0,nz,1)] for iz in range(0, nz, 1): # forward cumulative transformation to data sct.run(fsloutput+'flirt -in '+file_anat_split[iz]+' -ref '+file_anat_split[iz]+' -applyxfm -init '+file_mat_inv_cumul_fit[iz]+' -out '+file_anat_split_fit[iz]+' -interp '+interp) # Merge into 4D volume print '\nMerge into 4D volume...' sct.run(fsloutput+'fslmerge -z tmp.anat_orient_fit tmp.anat_orient_fit_z*') # Reorient data as it was before print '\nReorient data back into native orientation...' set_orientation('tmp.anat_orient_fit.nii', input_image_orientation, 'tmp.anat_orient_fit_reorient.nii') # Generate output file (in current folder) print '\nGenerate output file (in current folder)...' sct.generate_output_file('tmp.anat_orient_fit_reorient.nii', file_anat+'_flatten'+ext_anat) # Delete temporary files if remove_temp_files == 1: print '\nDelete temporary files...' sct.run('rm -rf tmp.*') # to view results print '\nDone! To view results, type:' print 'fslview '+file_anat+ext_anat+' '+file_anat+'_flatten'+ext_anat+' &\n'
def main(): # Initialization fname_anat = '' fname_landmark_anat = '' fname_template = '' fname_landmark_template = '' fname_mask = '' remove_temp_files = param.remove_temp_files number_iterations = param.number_iterations verbose = param.verbose start_time = time.time() # extract path of the script path_script = os.path.dirname(__file__)+'/' # Parameters for debug mode if param.debug == 1: print '\n*** WARNING: DEBUG MODE ON ***\n' fname_anat = path_script+'../testing/sct_register_straight_spinalcord_to_template/data/errsm_22_t2_cropped_rpi_straight.nii.gz' fname_landmark_anat = path_script+'../testing/sct_register_straight_spinalcord_to_template/data/landmarks_C2_T5.nii.gz' fname_seg_anat = path_script+'../testing/sct_register_straight_spinalcord_to_template/data/landmarks_C2_T5.nii.gz' fname_template = path_script+'../data/template/MNI-Poly-AMU_T2.nii.gz' fname_landmark_template = path_script+'../data/template/landmarks_C2_T5.nii.gz' # Check input param try: opts, args = getopt.getopt(sys.argv[1:],'hi:f:l:m:n:o:r:s:t:v:') except getopt.GetoptError as err: print str(err) usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ('-f'): fname_landmark_template = arg elif opt in ('-i'): fname_anat = arg elif opt in ('-l'): fname_landmark_anat = arg elif opt in ('-m'): fname_mask = arg elif opt in ('-n'): number_iterations = arg elif opt in ("-o"): fname_template_seg = arg elif opt in ('-r'): remove_temp_files = int(arg) elif opt in ("-s"): fname_anat_seg = arg elif opt in ('-t'): fname_template = arg elif opt in ('-v'): verbose = int(arg) # display usage if a mandatory argument is not provided if fname_anat == '' or fname_landmark_anat == '' or fname_template == '' or fname_landmark_template == '': usage() # check existence of input files sct.check_file_exist(fname_anat) sct.check_file_exist(fname_landmark_anat) sct.check_file_exist(fname_template) sct.check_file_exist(fname_landmark_template) sct.check_file_exist(fname_seg_template) # Display arguments print '\nCheck input arguments:' print ' straight anatomic: '+fname_anat print ' landmarks anatomic: '+fname_landmark_anat print ' template T2: '+fname_template print ' template landmarks: '+fname_landmark_template print ' template segmentation:'+fname_landmark_template print ' number of iterations: '+str(number_iterations) print ' mask anatomic: '+fname_mask print ' Verbose: '+str(verbose) # Get full path fname_anat = os.path.abspath(fname_anat) fname_landmark_anat = os.path.abspath(fname_landmark_anat) fname_template = os.path.abspath(fname_template) fname_landmark_template = os.path.abspath(fname_landmark_template) # extract path/file/extension path_anat, file_anat, ext_anat = sct.extract_fname(fname_anat) path_template, file_template, ext_template = sct.extract_fname(fname_template) # create temporary folder path_tmp = 'tmp.'+time.strftime("%y%m%d%H%M%S") sct.run('mkdir '+path_tmp) # go to tmp folder os.chdir(path_tmp) # Estimate transfo: straight --> template (affine landmark-based)' print '\nEstimate transfo: straight anat --> template (affine landmark-based)...' sct.run('ANTSUseLandmarkImagesToGetAffineTransform '+fname_landmark_template+' '+fname_landmark_anat+' affine tmp.straight2templateAffine.txt') # Apply transformation: straight --> template print '\nApply transformation straight --> template...' sct.run('WarpImageMultiTransform 3 '+fname_anat+' tmp.straight2templateAffine.nii tmp.straight2templateAffine.txt -R '+fname_template) # Estimate transformation: straight --> template (deformation) print '\nEstimate transformation: straight --> template (diffeomorphic transformation). Takes ~15-45 minutes...' cmd = 'antsRegistration \ --dimensionality 3 \ --transform SyN[0.2,3] \ --metric MI['+fname_template+',tmp.straight2templateAffine.nii,1,32] \ --convergence '+number_iterations+' \ --shrink-factors 4x1 \ --smoothing-sigmas 1x0mm \ --Restrict-Deformation 1x1x0 \ --output [tmp.straight2template,tmp.straight2template.nii.gz] \ --collapse-output-transforms 1 \ --interpolation BSpline[3] \ --winsorize-image-intensities [0.005,0.995]' if fname_mask != '': # TODO: check if mask exist cmd = cmd+' -x '+fname_mask # run command status, output = sct.run(cmd) if verbose: print output # Concatenate affine and non-linear transformations... print '\nConcatenate affine and non-linear transformations: straight --> template...' # NB: cannot use sct.run() because output of ComposeMultiTransform is not 0, even if there is no error (bug in ANTS-- already reported on 2013-12-30) cmd = 'ComposeMultiTransform 3 tmp.warp_straight2template.nii.gz -R '+fname_template+' tmp.straight2template0Warp.nii.gz tmp.straight2templateAffine.txt' print('>> '+cmd) commands.getstatusoutput(cmd) # Concatenate affine and non-linear transformations... print '\nConcatenate affine and non-linear transformations: template --> straight...' # NB: cannot use sct.run() because output of ComposeMultiTransform is not 0, even if there is no error (bug in ANTS-- already reported on 2013-12-30) cmd = 'ComposeMultiTransform 3 tmp.warp_template2straight.nii.gz -R '+fname_anat+' -i tmp.straight2templateAffine.txt tmp.straight2template0InverseWarp.nii.gz' print('>> '+cmd) commands.getstatusoutput(cmd) # Apply transformation: template --> straight print '\nApply transformation: template --> straight...' sct.run('WarpImageMultiTransform 3 '+fname_template+' tmp.template2straight.nii.gz'+' -R '+fname_anat+' tmp.warp_template2straight.nii.gz') # THIS CODE USES 2-STEP METHOD WITH SEGMENTATION # # Estimate transfo: straight --> template (affine landmark-based)' # print '\nEstimate transfo: straight anat --> template (affine landmark-based)...' # sct.run('ANTSUseLandmarkImagesToGetAffineTransform '+fname_landmark_template+' '+fname_landmark_anat+' affine tmp.straight2templateAffine.txt') # # # Apply transformation: straight --> template # print '\nApply transformation straight --> template...' # sct.run('WarpImageMultiTransform 3 '+fname_anat+' tmp.straight2templateAffine.nii tmp.straight2templateAffine.txt -R '+fname_template) # sct.run('WarpImageMultiTransform 3 '+fname_anat_seg+' tmp.straightSeg2templateAffine.nii tmp.straight2templateAffine.txt -R '+fname_template) # # # Estimate transformation using ANTS # print('\nStep #1: Estimate transformation using spinal cord segmentations...') # # cmd = 'antsRegistration \ # --dimensionality 3 \ # --transform SyN[0.2,3,0] \ # --metric MI['+fname_template_seg+',tmp.straightSeg2templateAffine.nii,1,32] \ # --convergence 50x10 \ # --shrink-factors 2x1 \ # --smoothing-sigmas 2x1mm \ # --Restrict-Deformation 1x1x0 \ # --output [tmp.regSeg,tmp.straightSeg2template.nii.gz]' # # # run command # status, output = sct.run(cmd) # if verbose: # print output # # # Apply warping field: seg --> template_seg # print '\nApply transformation anat_seg --> template_seg...' # sct.run('WarpImageMultiTransform 3 '+fname_anat+' tmp.straight2templateStep1.nii tmp.regSeg0Warp.nii.gz -R '+fname_template) # # print('\nStep #2: Improve local deformation using images (start from previous transformation)...') # # # Estimate transformation: straight --> template (deformation) # print '\nEstimate transformation: straight --> template (diffeomorphic transformation). Takes 10-45 minutes...' # cmd = 'antsRegistration \ # --dimensionality 3 \ # --transform SyN[0.1,1,0] \ # --metric CC['+fname_template+',tmp.straight2templateStep1.nii,1,4] \ # --convergence 20 \ # --shrink-factors 1 \ # --smoothing-sigmas 0mm \ # --Restrict-Deformation 1x1x0 \ # --output [tmp.straight2template,tmp.straight2template.nii.gz] \ # --interpolation BSpline[3]' # # # use mask (if provided by user) # if fname_mask != '': # # TODO: check if mask exist # cmd = cmd+' -x '+fname_mask # # # run command # status, output = sct.run(cmd) # if verbose: # print output # # # Concatenate affine and non-linear transformations... # print '\nConcatenate affine and non-linear transformations: straight --> template...' # # NB: cannot use sct.run() because output of ComposeMultiTransform is not 0, even if there is no error (bug in ANTS-- already reported on 2013-12-30) # cmd = 'ComposeMultiTransform 3 tmp.warp_straight2template.nii.gz -R '+fname_template+' tmp.straight2template0Warp.nii.gz tmp.regSeg0Warp.nii.gz tmp.straight2templateAffine.txt' # print('>> '+cmd) # commands.getstatusoutput(cmd) # # # Concatenate affine and non-linear transformations... # print '\nConcatenate affine and non-linear transformations: template --> straight...' # # NB: cannot use sct.run() because output of ComposeMultiTransform is not 0, even if there is no error (bug in ANTS-- already reported on 2013-12-30) # cmd = 'ComposeMultiTransform 3 tmp.warp_template2straight.nii.gz -R '+fname_anat+' -i tmp.straight2templateAffine.txt tmp.straight2template0InverseWarp.nii.gz' # print('>> '+cmd) # commands.getstatusoutput(cmd) # # # Apply transformation: template --> straight # print '\nApply transformation: template --> straight...' # sct.run('WarpImageMultiTransform 3 '+fname_template+' tmp.template2straight.nii.gz'+' -R '+fname_anat+' tmp.warp_template2straight.nii.gz') # # Generate output file (in current folder) print '\nGenerate output file...' sct.generate_output_file('tmp.warp_template2straight.nii.gz','./','warp_template2straight',ext_anat) # warping field template --> straight sct.generate_output_file('tmp.warp_straight2template.nii.gz','./','warp_straight2template',ext_anat) # warping field straight --> template sct.generate_output_file('tmp.straight2template.nii.gz','./',file_anat+'2template',ext_anat) # anat --> template sct.generate_output_file('tmp.template2straight.nii.gz','./',file_template+'2straight',ext_anat) # anat --> template # Delete temporary files if remove_temp_files == 1: print '\nDelete temporary files...' sct.run('rm tmp.*') elapsed_time = time.time() - start_time print '\nFinished! Elapsed time: '+str(int(round(elapsed_time)))+'s\n'
def main(): # get path of the toolbox status, path_sct = getstatusoutput('echo $SCT_DIR') #print path_sct #Initialization fname = '' landmark = '' verbose = param.verbose output_name = 'aligned.nii.gz' template_landmark = '' final_warp = param.final_warp compose = param.compose transfo = 'affine' try: opts, args = getopt.getopt(sys.argv[1:],'hi:l:o:R:t:w:c:v:') except getopt.GetoptError: usage() for opt, arg in opts : if opt == '-h': usage() elif opt in ("-i"): fname = arg elif opt in ("-l"): landmark = arg elif opt in ("-o"): output_name = arg elif opt in ("-R"): template_landmark = arg elif opt in ("-t"): transfo = arg elif opt in ("-w"): final_warp = arg elif opt in ("-c"): compose = int(arg) elif opt in ('-v'): verbose = int(arg) # display usage if a mandatory argument is not provided if fname == '' or landmark == '' or template_landmark == '' : usage() if final_warp not in ['','spline','NN']: usage() if transfo not in ['affine', 'bspline', 'SyN', 'nurbs']: usage() # check existence of input files print'\nCheck if file exists ...' sct.check_file_exist(fname) sct.check_file_exist(landmark) sct.check_file_exist(template_landmark) # Display arguments print'\nCheck input arguments...' print' Input volume ...................... '+fname print' Verbose ........................... '+str(verbose) if transfo == 'affine': print 'Creating cross using input landmarks\n...' sct.run('sct_label_utils -i ' + landmark + ' -o ' + 'cross_native.nii.gz -t cross ' ) print 'Creating cross using template landmarks\n...' sct.run('sct_label_utils -i ' + template_landmark + ' -o ' + 'cross_template.nii.gz -t cross ' ) print 'Computing affine transformation between subject and destination landmarks\n...' os.system('isct_ANTSUseLandmarkImagesToGetAffineTransform cross_template.nii.gz cross_native.nii.gz affine n2t.txt') warping = 'n2t.txt' elif transfo == 'nurbs': warping_subject2template = 'warp_subject2template.nii.gz' warping_template2subject = 'warp_template2subject.nii.gz' tmp_name = 'tmp.' + time.strftime("%y%m%d%H%M%S") sct.run('mkdir ' + tmp_name) tmp_abs_path = os.path.abspath(tmp_name) sct.run('cp ' + landmark + ' ' + tmp_abs_path) os.chdir(tmp_name) from msct_image import Image image_landmark = Image(landmark) image_template = Image(template_landmark) landmarks_input = image_landmark.getNonZeroCoordinates(sorting='value') landmarks_template = image_template.getNonZeroCoordinates(sorting='value') min_value = min([int(landmarks_input[0].value), int(landmarks_template[0].value)]) max_value = max([int(landmarks_input[-1].value), int(landmarks_template[-1].value)]) nx, ny, nz, nt, px, py, pz, pt = image_landmark.dim displacement_subject2template, displacement_template2subject = [], [] for value in range(min_value, max_value+1): is_in_input = False coord_input = None for coord in landmarks_input: if int(value) == int(coord.value): coord_input = coord is_in_input = True break is_in_template = False coord_template = None for coord in landmarks_template: if int(value) == int(coord.value): coord_template = coord is_in_template = True break if is_in_template and is_in_input: displacement_subject2template.append([0.0, coord_input.z, coord_template.z - coord_input.z]) displacement_template2subject.append([0.0, coord_template.z, coord_input.z - coord_template.z]) # create displacement field from numpy import zeros from nibabel import Nifti1Image, save data_warp_subject2template = zeros((nx, ny, nz, 1, 3)) data_warp_template2subject = zeros((nx, ny, nz, 1, 3)) hdr_warp = image_template.hdr.copy() hdr_warp.set_intent('vector', (), '') hdr_warp.set_data_dtype('float32') # approximate displacement with nurbs from msct_smooth import b_spline_nurbs displacement_z = [item[1] for item in displacement_subject2template] displacement_x = [item[2] for item in displacement_subject2template] verbose = 1 displacement_z, displacement_y, displacement_y_deriv, displacement_z_deriv = b_spline_nurbs(displacement_x, displacement_z, None, nbControl=None, verbose=verbose, all_slices=True) arg_min_z, arg_max_z = np.argmin(displacement_y), np.argmax(displacement_y) min_z, max_z = int(displacement_y[arg_min_z]), int(displacement_y[arg_max_z]) displac = [] for index, iz in enumerate(displacement_y): displac.append([iz, displacement_z[index]]) for iz in range(0, min_z): displac.append([iz, displacement_z[arg_min_z]]) for iz in range(max_z, nz): displac.append([iz, displacement_z[arg_max_z]]) for item in displac: if 0 <= item[0] < nz: data_warp_template2subject[:, :, item[0], 0, 2] = item[1] * pz displacement_z = [item[1] for item in displacement_template2subject] displacement_x = [item[2] for item in displacement_template2subject] verbose = 1 displacement_z, displacement_y, displacement_y_deriv, displacement_z_deriv = b_spline_nurbs(displacement_x, displacement_z, None, nbControl=None, verbose=verbose, all_slices=True) arg_min_z, arg_max_z = np.argmin(displacement_y), np.argmax(displacement_y) min_z, max_z = int(displacement_y[arg_min_z]), int(displacement_y[arg_max_z]) displac = [] for index, iz in enumerate(displacement_y): displac.append([iz, displacement_z[index]]) for iz in range(0, min_z): displac.append([iz, displacement_z[arg_min_z]]) for iz in range(max_z, nz): displac.append([iz, displacement_z[arg_max_z]]) for item in displac: data_warp_subject2template[:, :, item[0], 0, 2] = item[1] * pz img = Nifti1Image(data_warp_template2subject, None, hdr_warp) save(img, warping_template2subject) sct.printv('\nDONE ! Warping field generated: ' + warping_template2subject, verbose) img = Nifti1Image(data_warp_subject2template, None, hdr_warp) save(img, warping_subject2template) sct.printv('\nDONE ! Warping field generated: ' + warping_subject2template, verbose) # Copy warping into parent folder sct.run('cp ' + warping_subject2template + ' ../' + warping_subject2template) sct.run('cp ' + warping_template2subject + ' ../' + warping_template2subject) warping = warping_subject2template os.chdir('..') remove_temp_files = True if remove_temp_files: sct.run('rm -rf ' + tmp_name) elif transfo == 'SyN': warping = 'warp_subject2template.nii.gz' tmp_name = 'tmp.'+time.strftime("%y%m%d%H%M%S") sct.run('mkdir '+tmp_name) tmp_abs_path = os.path.abspath(tmp_name) sct.run('cp ' + landmark + ' ' + tmp_abs_path) os.chdir(tmp_name) # sct.run('sct_label_utils -i '+landmark+' -t dist-inter') # sct.run('sct_label_utils -i '+template_landmark+' -t plan -o template_landmarks_plan.nii.gz -c 5') # sct.run('sct_crop_image -i template_landmarks_plan.nii.gz -o template_landmarks_plan_cropped.nii.gz -start 0.35,0.35 -end 0.65,0.65 -dim 0,1') # sct.run('sct_label_utils -i '+landmark+' -t plan -o landmarks_plan.nii.gz -c 5') # sct.run('sct_crop_image -i landmarks_plan.nii.gz -o landmarks_plan_cropped.nii.gz -start 0.35,0.35 -end 0.65,0.65 -dim 0,1') # sct.run('isct_antsRegistration --dimensionality 3 --transform SyN[0.5,3,0] --metric MeanSquares[template_landmarks_plan_cropped.nii.gz,landmarks_plan_cropped.nii.gz,1] --convergence 400x200 --shrink-factors 4x2 --smoothing-sigmas 4x2mm --restrict-deformation 0x0x1 --output [landmarks_reg,landmarks_reg.nii.gz] --interpolation NearestNeighbor --float') # sct.run('isct_c3d -mcs landmarks_reg0Warp.nii.gz -oo warp_vecx.nii.gz warp_vecy.nii.gz warp_vecz.nii.gz') # sct.run('isct_c3d warp_vecz.nii.gz -resample 200% -o warp_vecz_r.nii.gz') # sct.run('isct_c3d warp_vecz_r.nii.gz -smooth 0x0x3mm -o warp_vecz_r_sm.nii.gz') # sct.run('sct_crop_image -i warp_vecz_r_sm.nii.gz -o warp_vecz_r_sm_line.nii.gz -start 0.5,0.5 -end 0.5,0.5 -dim 0,1 -b 0') # sct.run('sct_label_utils -i warp_vecz_r_sm_line.nii.gz -t plan_ref -o warp_vecz_r_sm_line_extended.nii.gz -c 0 -r '+template_landmark) # sct.run('isct_c3d '+template_landmark+' warp_vecx.nii.gz -reslice-identity -o warp_vecx_res.nii.gz') # sct.run('isct_c3d '+template_landmark+' warp_vecy.nii.gz -reslice-identity -o warp_vecy_res.nii.gz') # sct.run('isct_c3d warp_vecx_res.nii.gz warp_vecy_res.nii.gz warp_vecz_r_sm_line_extended.nii.gz -omc 3 '+warping) # no x? #new #put labels of the subject at the center of the image (for plan xOy) import nibabel from copy import copy file_labels_input = nibabel.load(landmark) hdr_labels_input = file_labels_input.get_header() data_labels_input = file_labels_input.get_data() data_labels_middle = copy(data_labels_input) data_labels_middle *= 0 from msct_image import Image nx, ny, nz, nt, px, py, pz, pt = Image(landmark).dim X,Y,Z = data_labels_input.nonzero() x_middle = int(round(nx/2.0)) y_middle = int(round(ny/2.0)) #put labels of the template at the center of the image (for plan xOy) #probably not necessary as already done by average labels file_labels_template = nibabel.load(template_landmark) hdr_labels_template = file_labels_template.get_header() data_labels_template = file_labels_template.get_data() data_template_middle = copy(data_labels_template) data_template_middle *= 0 x, y, z = data_labels_template.nonzero() max_num = min([len(z), len(Z)]) index_sort = np.argsort(Z) index_sort = index_sort[::-1] X = X[index_sort] Y = Y[index_sort] Z = Z[index_sort] index_sort = np.argsort(z) index_sort = index_sort[::-1] x = x[index_sort] y = y[index_sort] z = z[index_sort] for i in range(max_num): data_labels_middle[x_middle, y_middle, Z[i]] = data_labels_input[X[i], Y[i], Z[i]] img = nibabel.Nifti1Image(data_labels_middle, None, hdr_labels_input) nibabel.save(img, 'labels_input_middle_xy.nii.gz') for i in range(max_num): data_template_middle[x_middle, y_middle, z[i]] = data_labels_template[x[i], y[i], z[i]] img_template = nibabel.Nifti1Image(data_template_middle, None, hdr_labels_template) nibabel.save(img_template, 'labels_template_middle_xy.nii.gz') #estimate Bspline transform to register to template sct.run('isct_ANTSUseLandmarkImagesToGetBSplineDisplacementField labels_template_middle_xy.nii.gz labels_input_middle_xy.nii.gz '+ warping+' 40x40x1 5 5 0') # select centerline of warping field according to z and extend it sct.run('isct_c3d -mcs '+warping+' -oo warp_vecx.nii.gz warp_vecy.nii.gz warp_vecz.nii.gz') #sct.run('isct_c3d warp_vecz.nii.gz -resample 200% -o warp_vecz_r.nii.gz') #sct.run('isct_c3d warp_vecz.nii.gz -smooth 0x0x3mm -o warp_vecz_r_sm.nii.gz') sct.run('sct_crop_image -i warp_vecz.nii.gz -o warp_vecz_r_sm_line.nii.gz -start 0.5,0.5 -end 0.5,0.5 -dim 0,1 -b 0') sct.run('sct_label_utils -i warp_vecz_r_sm_line.nii.gz -t plan_ref -o warp_vecz_r_sm_line_extended.nii.gz -r '+template_landmark) sct.run('isct_c3d '+template_landmark+' warp_vecx.nii.gz -reslice-identity -o warp_vecx_res.nii.gz') sct.run('isct_c3d '+template_landmark+' warp_vecy.nii.gz -reslice-identity -o warp_vecy_res.nii.gz') sct.run('isct_c3d warp_vecx_res.nii.gz warp_vecy_res.nii.gz warp_vecz_r_sm_line_extended.nii.gz -omc 3 '+warping) # check results #dilate first labels sct.run('fslmaths labels_input_middle_xy.nii.gz -dilF landmark_dilated.nii.gz') #new sct.run('sct_apply_transfo -i landmark_dilated.nii.gz -o label_moved.nii.gz -d labels_template_middle_xy.nii.gz -w '+warping+' -x nn') #undilate sct.run('sct_label_utils -i label_moved.nii.gz -t cubic-to-point -o label_moved_2point.nii.gz') sct.run('sct_label_utils -i labels_template_middle_xy.nii.gz -r label_moved_2point.nii.gz -o template_removed.nii.gz -t remove') #end new # check results #dilate first labels #sct.run('fslmaths '+landmark+' -dilF landmark_dilated.nii.gz') #old #sct.run('sct_apply_transfo -i landmark_dilated.nii.gz -o label_moved.nii.gz -d '+template_landmark+' -w '+warping+' -x nn') #old #undilate #sct.run('sct_label_utils -i label_moved.nii.gz -t cubic-to-point -o label_moved_2point.nii.gz') #old #sct.run('sct_label_utils -i '+template_landmark+' -r label_moved_2point.nii.gz -o template_removed.nii.gz -t remove') #old # # sct.run('sct_apply_transfo -i '+landmark+' -o label_moved.nii.gz -d '+template_landmark+' -w '+warping+' -x nn') # # sct.run('sct_label_utils -i '+template_landmark+' -r label_moved.nii.gz -o template_removed.nii.gz -t remove') # # status, output = sct.run('sct_label_utils -i label_moved.nii.gz -r template_removed.nii.gz -t MSE') status, output = sct.run('sct_label_utils -i label_moved_2point.nii.gz -r template_removed.nii.gz -t MSE') sct.printv(output,1,'info') remove_temp_files = False if os.path.isfile('error_log_label_moved.txt'): remove_temp_files = False with open('log.txt', 'a') as log_file: log_file.write('Error for '+fname+'\n') # Copy warping into parent folder sct.run('cp '+ warping+' ../'+warping) os.chdir('..') if remove_temp_files: sct.run('rm -rf '+tmp_name) # if transfo == 'bspline' : # print 'Computing bspline transformation between subject and destination landmarks\n...' # sct.run('isct_ANTSUseLandmarkImagesToGetBSplineDisplacementField cross_template.nii.gz cross_native.nii.gz warp_ntotemp.nii.gz 5x5x5 3 2 0') # warping = 'warp_ntotemp.nii.gz' # if final_warp == '' : # print 'Apply transfo to input image\n...' # sct.run('isct_antsApplyTransforms 3 ' + fname + ' ' + output_name + ' -r ' + template_landmark + ' -t ' + warping + ' -n Linear') # if final_warp == 'NN': # print 'Apply transfo to input image\n...' # sct.run('isct_antsApplyTransforms 3 ' + fname + ' ' + output_name + ' -r ' + template_landmark + ' -t ' + warping + ' -n NearestNeighbor') if final_warp == 'spline': print 'Apply transfo to input image\n...' sct.run('sct_apply_transfo -i ' + fname + ' -o ' + output_name + ' -d ' + template_landmark + ' -w ' + warping + ' -x spline') # Remove warping #os.remove(warping) # if compose : # print 'Computing affine transformation between subject and destination landmarks\n...' # sct.run('isct_ANTSUseLandmarkImagesToGetAffineTransform cross_template.nii.gz cross_native.nii.gz affine n2t.txt') # warping_affine = 'n2t.txt' # print 'Apply transfo to input landmarks\n...' # sct.run('isct_antsApplyTransforms 3 ' + cross_native + ' cross_affine.nii.gz -r ' + template_landmark + ' -t ' + warping_affine + ' -n NearestNeighbor') # print 'Computing transfo between moved landmarks and template landmarks\n...' # sct.run('isct_ANTSUseLandmarkImagesToGetBSplineDisplacementField cross_template.nii.gz cross_affine.nii.gz warp_affine2temp.nii.gz 5x5x5 3 2 0') # warping_bspline = 'warp_affine2temp.nii.gz' # print 'Composing transformations\n...' # sct.run('isct_ComposeMultiTransform 3 warp_full.nii.gz -r ' + template_landmark + ' ' + warping_bspline + ' ' + warping_affine) # warping_concat = 'warp_full.nii.gz' # if final_warp == '' : # print 'Apply concat warp to input image\n...' # sct.run('isct_antsApplyTransforms 3 ' + fname + ' ' + output_name + ' -r ' + template_landmark + ' -t ' + warping_concat + ' -n Linear') # if final_warp == 'NN': # print 'Apply concat warp to input image\n...' # sct.run('isct_antsApplyTransforms 3 ' + fname + ' ' + output_name + ' -r ' + template_landmark + ' -t ' + warping_concat + ' -n NearestNeighbor') # if final_warp == 'spline': # print 'Apply concat warp to input image\n...' # sct.run('isct_antsApplyTransforms 3 ' + fname + ' ' + output_name + ' -r ' + template_landmark + ' -t ' + warping_concat + ' -n BSpline[3]') print '\nFile created : ' + output_name
def main(): # get path of the toolbox status, path_sct = getstatusoutput('echo $SCT_DIR') #print path_sct #Initialization fname = '' landmark = '' verbose = param.verbose output_name = 'aligned.nii.gz' template_landmark = '' final_warp = param.final_warp compose = param.compose transfo = 'affine' try: opts, args = getopt.getopt(sys.argv[1:],'hi:l:o:R:t:w:c:v:') except getopt.GetoptError: usage() for opt, arg in opts : if opt == '-h': usage() elif opt in ("-i"): fname = arg elif opt in ("-l"): landmark = arg elif opt in ("-o"): output_name = arg elif opt in ("-R"): template_landmark = arg elif opt in ("-t"): transfo = arg elif opt in ("-w"): final_warp = arg elif opt in ("-c"): compose = int(arg) elif opt in ('-v'): verbose = int(arg) # display usage if a mandatory argument is not provided if fname == '' or landmark == '' or template_landmark == '' : usage() if final_warp not in ['','spline','NN']: usage() if transfo not in ['affine','bspline','SyN']: usage() # check existence of input files print'\nCheck if file exists ...' sct.check_file_exist(fname) sct.check_file_exist(landmark) sct.check_file_exist(template_landmark) # Display arguments print'\nCheck input arguments...' print' Input volume ...................... '+fname print' Verbose ........................... '+str(verbose) if transfo == 'affine': print 'Creating cross using input landmarks\n...' sct.run('sct_label_utils -i ' + landmark + ' -o ' + 'cross_native.nii.gz -t cross ' ) print 'Creating cross using template landmarks\n...' sct.run('sct_label_utils -i ' + template_landmark + ' -o ' + 'cross_template.nii.gz -t cross ' ) print 'Computing affine transformation between subject and destination landmarks\n...' os.system('isct_ANTSUseLandmarkImagesToGetAffineTransform cross_template.nii.gz cross_native.nii.gz affine n2t.txt') warping = 'n2t.txt' elif transfo == 'SyN': warping = 'warp_subject2template.nii.gz' tmp_name = 'tmp.'+time.strftime("%y%m%d%H%M%S") sct.run('mkdir '+tmp_name) tmp_abs_path = os.path.abspath(tmp_name) sct.run('cp ' + landmark + ' ' + tmp_abs_path) os.chdir(tmp_name) # sct.run('sct_label_utils -i '+landmark+' -t dist-inter') # sct.run('sct_label_utils -i '+template_landmark+' -t plan -o template_landmarks_plan.nii.gz -c 5') # sct.run('sct_crop_image -i template_landmarks_plan.nii.gz -o template_landmarks_plan_cropped.nii.gz -start 0.35,0.35 -end 0.65,0.65 -dim 0,1') # sct.run('sct_label_utils -i '+landmark+' -t plan -o landmarks_plan.nii.gz -c 5') # sct.run('sct_crop_image -i landmarks_plan.nii.gz -o landmarks_plan_cropped.nii.gz -start 0.35,0.35 -end 0.65,0.65 -dim 0,1') # sct.run('isct_antsRegistration --dimensionality 3 --transform SyN[0.5,3,0] --metric MeanSquares[template_landmarks_plan_cropped.nii.gz,landmarks_plan_cropped.nii.gz,1] --convergence 400x200 --shrink-factors 4x2 --smoothing-sigmas 4x2mm --restrict-deformation 0x0x1 --output [landmarks_reg,landmarks_reg.nii.gz] --interpolation NearestNeighbor --float') # sct.run('isct_c3d -mcs landmarks_reg0Warp.nii.gz -oo warp_vecx.nii.gz warp_vecy.nii.gz warp_vecz.nii.gz') # sct.run('isct_c3d warp_vecz.nii.gz -resample 200% -o warp_vecz_r.nii.gz') # sct.run('isct_c3d warp_vecz_r.nii.gz -smooth 0x0x3mm -o warp_vecz_r_sm.nii.gz') # sct.run('sct_crop_image -i warp_vecz_r_sm.nii.gz -o warp_vecz_r_sm_line.nii.gz -start 0.5,0.5 -end 0.5,0.5 -dim 0,1 -b 0') # sct.run('sct_label_utils -i warp_vecz_r_sm_line.nii.gz -t plan_ref -o warp_vecz_r_sm_line_extended.nii.gz -c 0 -r '+template_landmark) # sct.run('isct_c3d '+template_landmark+' warp_vecx.nii.gz -reslice-identity -o warp_vecx_res.nii.gz') # sct.run('isct_c3d '+template_landmark+' warp_vecy.nii.gz -reslice-identity -o warp_vecy_res.nii.gz') # sct.run('isct_c3d warp_vecx_res.nii.gz warp_vecy_res.nii.gz warp_vecz_r_sm_line_extended.nii.gz -omc 3 '+warping) # no x? #new #put labels of the subject at the center of the image (for plan xOy) import nibabel from copy import copy file_labels_input = nibabel.load(landmark) hdr_labels_input = file_labels_input.get_header() data_labels_input = file_labels_input.get_data() data_labels_middle = copy(data_labels_input) data_labels_middle *= 0 from msct_image import Image nx, ny, nz, nt, px, py, pz, pt = Image(landmark).dim X,Y,Z = data_labels_input.nonzero() x_middle = int(round(nx/2.0)) y_middle = int(round(ny/2.0)) for i in range(len(Z)): data_labels_middle[x_middle, y_middle, Z[i]] = data_labels_input[X[i], Y[i], Z[i]] img = nibabel.Nifti1Image(data_labels_middle, None, hdr_labels_input) nibabel.save(img, 'labels_input_middle_xy.nii.gz') #put labels of the template at the center of the image (for plan xOy) #probably not necessary as already done by average labels file_labels_template = nibabel.load(template_landmark) hdr_labels_template = file_labels_template.get_header() data_labels_template = file_labels_template.get_data() data_template_middle = copy(data_labels_template) data_template_middle *= 0 x,y,z = data_labels_template.nonzero() for i in range(len(Z)): data_template_middle[x_middle, y_middle, z[i]] = data_labels_template[x[i], y[i], z[i]] img_template = nibabel.Nifti1Image(data_template_middle, None, hdr_labels_template) nibabel.save(img_template, 'labels_template_middle_xy.nii.gz') #estimate Bspline transform to register to template sct.run('isct_ANTSUseLandmarkImagesToGetBSplineDisplacementField labels_template_middle_xy.nii.gz labels_input_middle_xy.nii.gz '+ warping+' 40x40x1 5 5 0') # select centerline of warping field according to z and extend it sct.run('isct_c3d -mcs '+warping+' -oo warp_vecx.nii.gz warp_vecy.nii.gz warp_vecz.nii.gz') #sct.run('isct_c3d warp_vecz.nii.gz -resample 200% -o warp_vecz_r.nii.gz') #sct.run('isct_c3d warp_vecz.nii.gz -smooth 0x0x3mm -o warp_vecz_r_sm.nii.gz') sct.run('sct_crop_image -i warp_vecz.nii.gz -o warp_vecz_r_sm_line.nii.gz -start 0.5,0.5 -end 0.5,0.5 -dim 0,1 -b 0') sct.run('sct_label_utils -i warp_vecz_r_sm_line.nii.gz -t plan_ref -o warp_vecz_r_sm_line_extended.nii.gz -r '+template_landmark) sct.run('isct_c3d '+template_landmark+' warp_vecx.nii.gz -reslice-identity -o warp_vecx_res.nii.gz') sct.run('isct_c3d '+template_landmark+' warp_vecy.nii.gz -reslice-identity -o warp_vecy_res.nii.gz') sct.run('isct_c3d warp_vecx_res.nii.gz warp_vecy_res.nii.gz warp_vecz_r_sm_line_extended.nii.gz -omc 3 '+warping) # check results #dilate first labels sct.run('fslmaths labels_input_middle_xy.nii.gz -dilF landmark_dilated.nii.gz') #new sct.run('sct_apply_transfo -i landmark_dilated.nii.gz -o label_moved.nii.gz -d labels_template_middle_xy.nii.gz -w '+warping+' -x nn') #undilate sct.run('sct_label_utils -i label_moved.nii.gz -t cubic-to-point -o label_moved_2point.nii.gz') sct.run('sct_label_utils -i labels_template_middle_xy.nii.gz -r label_moved_2point.nii.gz -o template_removed.nii.gz -t remove') #end new # check results #dilate first labels #sct.run('fslmaths '+landmark+' -dilF landmark_dilated.nii.gz') #old #sct.run('sct_apply_transfo -i landmark_dilated.nii.gz -o label_moved.nii.gz -d '+template_landmark+' -w '+warping+' -x nn') #old #undilate #sct.run('sct_label_utils -i label_moved.nii.gz -t cubic-to-point -o label_moved_2point.nii.gz') #old #sct.run('sct_label_utils -i '+template_landmark+' -r label_moved_2point.nii.gz -o template_removed.nii.gz -t remove') #old # # sct.run('sct_apply_transfo -i '+landmark+' -o label_moved.nii.gz -d '+template_landmark+' -w '+warping+' -x nn') # # sct.run('sct_label_utils -i '+template_landmark+' -r label_moved.nii.gz -o template_removed.nii.gz -t remove') # # status, output = sct.run('sct_label_utils -i label_moved.nii.gz -r template_removed.nii.gz -t MSE') status, output = sct.run('sct_label_utils -i label_moved_2point.nii.gz -r template_removed.nii.gz -t MSE') sct.printv(output,1,'info') remove_temp_files = False if os.path.isfile('error_log_label_moved.txt'): remove_temp_files = False with open('log.txt', 'a') as log_file: log_file.write('Error for '+fname+'\n') # Copy warping into parent folder sct.run('cp '+ warping+' ../'+warping) os.chdir('..') if remove_temp_files: sct.run('rm -rf '+tmp_name) # if transfo == 'bspline' : # print 'Computing bspline transformation between subject and destination landmarks\n...' # sct.run('isct_ANTSUseLandmarkImagesToGetBSplineDisplacementField cross_template.nii.gz cross_native.nii.gz warp_ntotemp.nii.gz 5x5x5 3 2 0') # warping = 'warp_ntotemp.nii.gz' # if final_warp == '' : # print 'Apply transfo to input image\n...' # sct.run('isct_antsApplyTransforms 3 ' + fname + ' ' + output_name + ' -r ' + template_landmark + ' -t ' + warping + ' -n Linear') # if final_warp == 'NN': # print 'Apply transfo to input image\n...' # sct.run('isct_antsApplyTransforms 3 ' + fname + ' ' + output_name + ' -r ' + template_landmark + ' -t ' + warping + ' -n NearestNeighbor') if final_warp == 'spline': print 'Apply transfo to input image\n...' sct.run('sct_apply_transfo -i ' + fname + ' -o ' + output_name + ' -d ' + template_landmark + ' -w ' + warping + ' -x spline') # Remove warping os.remove(warping) # if compose : # print 'Computing affine transformation between subject and destination landmarks\n...' # sct.run('isct_ANTSUseLandmarkImagesToGetAffineTransform cross_template.nii.gz cross_native.nii.gz affine n2t.txt') # warping_affine = 'n2t.txt' # print 'Apply transfo to input landmarks\n...' # sct.run('isct_antsApplyTransforms 3 ' + cross_native + ' cross_affine.nii.gz -r ' + template_landmark + ' -t ' + warping_affine + ' -n NearestNeighbor') # print 'Computing transfo between moved landmarks and template landmarks\n...' # sct.run('isct_ANTSUseLandmarkImagesToGetBSplineDisplacementField cross_template.nii.gz cross_affine.nii.gz warp_affine2temp.nii.gz 5x5x5 3 2 0') # warping_bspline = 'warp_affine2temp.nii.gz' # print 'Composing transformations\n...' # sct.run('isct_ComposeMultiTransform 3 warp_full.nii.gz -r ' + template_landmark + ' ' + warping_bspline + ' ' + warping_affine) # warping_concat = 'warp_full.nii.gz' # if final_warp == '' : # print 'Apply concat warp to input image\n...' # sct.run('isct_antsApplyTransforms 3 ' + fname + ' ' + output_name + ' -r ' + template_landmark + ' -t ' + warping_concat + ' -n Linear') # if final_warp == 'NN': # print 'Apply concat warp to input image\n...' # sct.run('isct_antsApplyTransforms 3 ' + fname + ' ' + output_name + ' -r ' + template_landmark + ' -t ' + warping_concat + ' -n NearestNeighbor') # if final_warp == 'spline': # print 'Apply concat warp to input image\n...' # sct.run('isct_antsApplyTransforms 3 ' + fname + ' ' + output_name + ' -r ' + template_landmark + ' -t ' + warping_concat + ' -n BSpline[3]') print '\nFile created : ' + output_name
def main(): # Initialization fname_data = '' interp_factor = param.interp_factor remove_temp_files = param.remove_temp_files verbose = param.verbose suffix = param.suffix smoothing_sigma = param.smoothing_sigma # start timer start_time = time.time() # get path of the toolbox path_sct = os.environ.get("SCT_DIR", os.path.dirname(os.path.dirname(__file__))) # Parameters for debug mode if param.debug: fname_data = os.path.join(path_sct, 'testing', 'data', 'errsm_23', 't2', 't2_manual_segmentation.nii.gz') remove_temp_files = 0 param.mask_size = 10 else: # Check input parameters try: opts, args = getopt.getopt(sys.argv[1:], 'hi:v:r:s:') except getopt.GetoptError: usage() if not opts: usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ('-i'): fname_data = arg elif opt in ('-r'): remove_temp_files = int(arg) elif opt in ('-s'): smoothing_sigma = arg elif opt in ('-v'): verbose = int(arg) # display usage if a mandatory argument is not provided if fname_data == '': usage() # sct.printv(arguments) sct.printv('\nCheck parameters:') sct.printv(' segmentation ........... ' + fname_data) sct.printv(' interp factor .......... ' + str(interp_factor)) sct.printv(' smoothing sigma ........ ' + str(smoothing_sigma)) # check existence of input files sct.printv('\nCheck existence of input files...') sct.check_file_exist(fname_data, verbose) # Extract path, file and extension path_data, file_data, ext_data = sct.extract_fname(fname_data) path_tmp = sct.tmp_create(basename="binary_to_trilinear", verbose=verbose) from sct_convert import convert sct.printv('\nCopying input data to tmp folder and convert to nii...', param.verbose) convert(fname_data, os.path.join(path_tmp, "data.nii")) # go to tmp folder curdir = os.getcwd() os.chdir(path_tmp) # Get dimensions of data sct.printv('\nGet dimensions of data...', verbose) nx, ny, nz, nt, px, py, pz, pt = Image('data.nii').dim sct.printv('.. ' + str(nx) + ' x ' + str(ny) + ' x ' + str(nz), verbose) # upsample data sct.printv('\nUpsample data...', verbose) sct.run([ "sct_resample", "-i", "data.nii", "-x", "linear", "-vox", str(nx * interp_factor) + 'x' + str(ny * interp_factor) + 'x' + str(nz * interp_factor), "-o", "data_up.nii" ], verbose) # Smooth along centerline sct.printv('\nSmooth along centerline...', verbose) sct.run([ "sct_smooth_spinalcord", "-i", "data_up.nii", "-s", "data_up.nii", "-smooth", str(smoothing_sigma), "-r", str(remove_temp_files), "-v", str(verbose) ], verbose) # downsample data sct.printv('\nDownsample data...', verbose) sct.run([ "sct_resample", "-i", "data_up_smooth.nii", "-x", "linear", "-vox", str(nx) + 'x' + str(ny) + 'x' + str(nz), "-o", "data_up_smooth_down.nii" ], verbose) # come back os.chdir(curdir) # Generate output files sct.printv('\nGenerate output files...') fname_out = sct.generate_output_file( os.path.join(path_tmp, "data_up_smooth_down.nii"), '' + file_data + suffix + ext_data) # Delete temporary files if remove_temp_files == 1: sct.printv('\nRemove temporary files...') sct.rmtree(path_tmp) # display elapsed time elapsed_time = time.time() - start_time sct.printv('\nFinished! Elapsed time: ' + str(int(round(elapsed_time))) + 's') # to view results sct.printv('\nTo view results, type:') sct.printv('fslview ' + file_data + ' ' + file_data + suffix + ' &\n')
def main(): # get default parameters step1 = Paramreg(step='1', type='seg', algo='slicereg', metric='MeanSquares', iter='10') step2 = Paramreg(step='2', type='im', algo='syn', metric='MI', iter='3') # step1 = Paramreg() paramreg = ParamregMultiStep([step1, step2]) # step1 = Paramreg_step(step='1', type='seg', algo='bsplinesyn', metric='MeanSquares', iter='10', shrink='1', smooth='0', gradStep='0.5') # step2 = Paramreg_step(step='2', type='im', algo='syn', metric='MI', iter='10', shrink='1', smooth='0', gradStep='0.5') # paramreg = ParamregMultiStep([step1, step2]) # Initialize the parser parser = Parser(__file__) parser.usage.set_description('Register anatomical image to the template.') parser.add_option(name="-i", type_value="file", description="Anatomical image.", mandatory=True, example="anat.nii.gz") parser.add_option(name="-s", type_value="file", description="Spinal cord segmentation.", mandatory=True, example="anat_seg.nii.gz") parser.add_option(name="-l", type_value="file", description="Labels. See: http://sourceforge.net/p/spinalcordtoolbox/wiki/create_labels/", mandatory=True, default_value='', example="anat_labels.nii.gz") parser.add_option(name="-t", type_value="folder", description="Path to MNI-Poly-AMU template.", mandatory=False, default_value=param.path_template) parser.add_option(name="-p", type_value=[[':'], 'str'], description="""Parameters for registration (see sct_register_multimodal). Default:\n--\nstep=1\ntype="""+paramreg.steps['1'].type+"""\nalgo="""+paramreg.steps['1'].algo+"""\nmetric="""+paramreg.steps['1'].metric+"""\npoly="""+paramreg.steps['1'].poly+"""\n--\nstep=2\ntype="""+paramreg.steps['2'].type+"""\nalgo="""+paramreg.steps['2'].algo+"""\nmetric="""+paramreg.steps['2'].metric+"""\niter="""+paramreg.steps['2'].iter+"""\nshrink="""+paramreg.steps['2'].shrink+"""\nsmooth="""+paramreg.steps['2'].smooth+"""\ngradStep="""+paramreg.steps['2'].gradStep+"""\n--""", mandatory=False, example="step=2,type=seg,algo=bsplinesyn,metric=MeanSquares,iter=5,shrink=2:step=3,type=im,algo=syn,metric=MI,iter=5,shrink=1,gradStep=0.3") parser.add_option(name="-r", type_value="multiple_choice", description="""Remove temporary files.""", mandatory=False, default_value='1', example=['0', '1']) parser.add_option(name="-v", type_value="multiple_choice", description="""Verbose. 0: nothing. 1: basic. 2: extended.""", mandatory=False, default_value=param.verbose, example=['0', '1', '2']) if param.debug: print '\n*** WARNING: DEBUG MODE ON ***\n' fname_data = '/Users/julien/data/temp/sct_example_data/t2/t2.nii.gz' fname_landmarks = '/Users/julien/data/temp/sct_example_data/t2/labels.nii.gz' fname_seg = '/Users/julien/data/temp/sct_example_data/t2/t2_seg.nii.gz' path_template = param.path_template remove_temp_files = 0 verbose = 2 # speed = 'superfast' #param_reg = '2,BSplineSyN,0.6,MeanSquares' else: arguments = parser.parse(sys.argv[1:]) # get arguments fname_data = arguments['-i'] fname_seg = arguments['-s'] fname_landmarks = arguments['-l'] path_template = arguments['-t'] remove_temp_files = int(arguments['-r']) verbose = int(arguments['-v']) if '-p' in arguments: paramreg_user = arguments['-p'] # update registration parameters for paramStep in paramreg_user: paramreg.addStep(paramStep) # initialize other parameters file_template = param.file_template file_template_label = param.file_template_label file_template_seg = param.file_template_seg output_type = param.output_type zsubsample = param.zsubsample # smoothing_sigma = param.smoothing_sigma # start timer start_time = time.time() # get absolute path - TO DO: remove! NEVER USE ABSOLUTE PATH... path_template = os.path.abspath(path_template) # get fname of the template + template objects fname_template = sct.slash_at_the_end(path_template, 1)+file_template fname_template_label = sct.slash_at_the_end(path_template, 1)+file_template_label fname_template_seg = sct.slash_at_the_end(path_template, 1)+file_template_seg # check file existence sct.printv('\nCheck template files...') sct.check_file_exist(fname_template, verbose) sct.check_file_exist(fname_template_label, verbose) sct.check_file_exist(fname_template_seg, verbose) # print arguments sct.printv('\nCheck parameters:', verbose) sct.printv('.. Data: '+fname_data, verbose) sct.printv('.. Landmarks: '+fname_landmarks, verbose) sct.printv('.. Segmentation: '+fname_seg, verbose) sct.printv('.. Path template: '+path_template, verbose) sct.printv('.. Output type: '+str(output_type), verbose) sct.printv('.. Remove temp files: '+str(remove_temp_files), verbose) sct.printv('\nParameters for registration:') for pStep in range(1, len(paramreg.steps)+1): sct.printv('Step #'+paramreg.steps[str(pStep)].step, verbose) sct.printv('.. Type #'+paramreg.steps[str(pStep)].type, verbose) sct.printv('.. Algorithm................ '+paramreg.steps[str(pStep)].algo, verbose) sct.printv('.. Metric................... '+paramreg.steps[str(pStep)].metric, verbose) sct.printv('.. Number of iterations..... '+paramreg.steps[str(pStep)].iter, verbose) sct.printv('.. Shrink factor............ '+paramreg.steps[str(pStep)].shrink, verbose) sct.printv('.. Smoothing factor......... '+paramreg.steps[str(pStep)].smooth, verbose) sct.printv('.. Gradient step............ '+paramreg.steps[str(pStep)].gradStep, verbose) sct.printv('.. Degree of polynomial..... '+paramreg.steps[str(pStep)].poly, verbose) path_data, file_data, ext_data = sct.extract_fname(fname_data) sct.printv('\nCheck input labels...') # check if label image contains coherent labels image_label = Image(fname_landmarks) # -> all labels must be different labels = image_label.getNonZeroCoordinates(sorting='value') hasDifferentLabels = True for lab in labels: for otherlabel in labels: if lab != otherlabel and lab.hasEqualValue(otherlabel): hasDifferentLabels = False break if not hasDifferentLabels: sct.printv('ERROR: Wrong landmarks input. All labels must be different.', verbose, 'error') # all labels must be available in tempalte image_label_template = Image(fname_template_label) labels_template = image_label_template.getNonZeroCoordinates(sorting='value') if labels[-1].value > labels_template[-1].value: sct.printv('ERROR: Wrong landmarks input. Labels must have correspondance in tempalte space. \nLabel max ' 'provided: ' + str(labels[-1].value) + '\nLabel max from template: ' + str(labels_template[-1].value), verbose, 'error') # create temporary folder sct.printv('\nCreate temporary folder...', verbose) path_tmp = 'tmp.'+time.strftime("%y%m%d%H%M%S") status, output = sct.run('mkdir '+path_tmp) # copy files to temporary folder sct.printv('\nCopy files...', verbose) sct.run('isct_c3d '+fname_data+' -o '+path_tmp+'/data.nii') sct.run('isct_c3d '+fname_landmarks+' -o '+path_tmp+'/landmarks.nii.gz') sct.run('isct_c3d '+fname_seg+' -o '+path_tmp+'/segmentation.nii.gz') sct.run('isct_c3d '+fname_template+' -o '+path_tmp+'/template.nii') sct.run('isct_c3d '+fname_template_label+' -o '+path_tmp+'/template_labels.nii.gz') sct.run('isct_c3d '+fname_template_seg+' -o '+path_tmp+'/template_seg.nii.gz') # go to tmp folder os.chdir(path_tmp) # resample data to 1mm isotropic sct.printv('\nResample data to 1mm isotropic...', verbose) sct.run('isct_c3d data.nii -resample-mm 1.0x1.0x1.0mm -interpolation Linear -o datar.nii') sct.run('isct_c3d segmentation.nii.gz -resample-mm 1.0x1.0x1.0mm -interpolation NearestNeighbor -o segmentationr.nii.gz') # N.B. resampling of labels is more complicated, because they are single-point labels, therefore resampling with neighrest neighbour can make them disappear. Therefore a more clever approach is required. resample_labels('landmarks.nii.gz', 'datar.nii', 'landmarksr.nii.gz') # # TODO # sct.run('sct_label_utils -i datar.nii -t create -x 124,186,19,2:129,98,23,8 -o landmarksr.nii.gz') # Change orientation of input images to RPI sct.printv('\nChange orientation of input images to RPI...', verbose) set_orientation('datar.nii', 'RPI', 'data_rpi.nii') set_orientation('landmarksr.nii.gz', 'RPI', 'landmarks_rpi.nii.gz') set_orientation('segmentationr.nii.gz', 'RPI', 'segmentation_rpi.nii.gz') # # Change orientation of input images to RPI # sct.printv('\nChange orientation of input images to RPI...', verbose) # set_orientation('data.nii', 'RPI', 'data_rpi.nii') # set_orientation('landmarks.nii.gz', 'RPI', 'landmarks_rpi.nii.gz') # set_orientation('segmentation.nii.gz', 'RPI', 'segmentation_rpi.nii.gz') # get landmarks in native space # crop segmentation # output: segmentation_rpi_crop.nii.gz sct.run('sct_crop_image -i segmentation_rpi.nii.gz -o segmentation_rpi_crop.nii.gz -dim 2 -bzmax') # straighten segmentation sct.printv('\nStraighten the spinal cord using centerline/segmentation...', verbose) sct.run('sct_straighten_spinalcord -i segmentation_rpi_crop.nii.gz -c segmentation_rpi_crop.nii.gz -r 0 -v '+str(verbose), verbose) # re-define warping field using non-cropped space (to avoid issue #367) sct.run('sct_concat_transfo -w warp_straight2curve.nii.gz -d data_rpi.nii -o warp_straight2curve.nii.gz') # Label preparation: # -------------------------------------------------------------------------------- # Remove unused label on template. Keep only label present in the input label image sct.printv('\nRemove unused label on template. Keep only label present in the input label image...', verbose) sct.run('sct_label_utils -t remove -i template_labels.nii.gz -o template_label.nii.gz -r landmarks_rpi.nii.gz') # Make sure landmarks are INT sct.printv('\nConvert landmarks to INT...', verbose) sct.run('isct_c3d template_label.nii.gz -type int -o template_label.nii.gz', verbose) # Create a cross for the template labels - 5 mm sct.printv('\nCreate a 5 mm cross for the template labels...', verbose) sct.run('sct_label_utils -t cross -i template_label.nii.gz -o template_label_cross.nii.gz -c 5') # Create a cross for the input labels and dilate for straightening preparation - 5 mm sct.printv('\nCreate a 5mm cross for the input labels and dilate for straightening preparation...', verbose) sct.run('sct_label_utils -t cross -i landmarks_rpi.nii.gz -o landmarks_rpi_cross3x3.nii.gz -c 5 -d') # Apply straightening to labels sct.printv('\nApply straightening to labels...', verbose) sct.run('sct_apply_transfo -i landmarks_rpi_cross3x3.nii.gz -o landmarks_rpi_cross3x3_straight.nii.gz -d segmentation_rpi_crop_straight.nii.gz -w warp_curve2straight.nii.gz -x nn') # Convert landmarks from FLOAT32 to INT sct.printv('\nConvert landmarks from FLOAT32 to INT...', verbose) sct.run('isct_c3d landmarks_rpi_cross3x3_straight.nii.gz -type int -o landmarks_rpi_cross3x3_straight.nii.gz') # Remove labels that do not correspond with each others. sct.printv('\nRemove labels that do not correspond with each others.', verbose) sct.run('sct_label_utils -t remove-symm -i landmarks_rpi_cross3x3_straight.nii.gz -o landmarks_rpi_cross3x3_straight.nii.gz,template_label_cross.nii.gz -r template_label_cross.nii.gz') # Estimate affine transfo: straight --> template (landmark-based)' sct.printv('\nEstimate affine transfo: straight anat --> template (landmark-based)...', verbose) # converting landmarks straight and curved to physical coordinates image_straight = Image('landmarks_rpi_cross3x3_straight.nii.gz') landmark_straight = image_straight.getNonZeroCoordinates(sorting='value') image_template = Image('template_label_cross.nii.gz') landmark_template = image_template.getNonZeroCoordinates(sorting='value') # Reorganize landmarks points_fixed, points_moving = [], [] landmark_straight_mean = [] for coord in landmark_straight: if coord.value not in [c.value for c in landmark_straight_mean]: temp_landmark = coord temp_number = 1 for other_coord in landmark_straight: if coord.hasEqualValue(other_coord) and coord != other_coord: temp_landmark += other_coord temp_number += 1 landmark_straight_mean.append(temp_landmark / temp_number) for coord in landmark_straight_mean: point_straight = image_straight.transfo_pix2phys([[coord.x, coord.y, coord.z]]) points_moving.append([point_straight[0][0], point_straight[0][1], point_straight[0][2]]) for coord in landmark_template: point_template = image_template.transfo_pix2phys([[coord.x, coord.y, coord.z]]) points_fixed.append([point_template[0][0], point_template[0][1], point_template[0][2]]) # Register curved landmarks on straight landmarks based on python implementation sct.printv('\nComputing rigid transformation (algo=translation-scaling-z) ...', verbose) import msct_register_landmarks (rotation_matrix, translation_array, points_moving_reg, points_moving_barycenter) = \ msct_register_landmarks.getRigidTransformFromLandmarks( points_fixed, points_moving, constraints='translation-scaling-z', show=False) # writing rigid transformation file text_file = open("straight2templateAffine.txt", "w") text_file.write("#Insight Transform File V1.0\n") text_file.write("#Transform 0\n") text_file.write("Transform: FixedCenterOfRotationAffineTransform_double_3_3\n") text_file.write("Parameters: %.9f %.9f %.9f %.9f %.9f %.9f %.9f %.9f %.9f %.9f %.9f %.9f\n" % ( 1.0/rotation_matrix[0, 0], rotation_matrix[0, 1], rotation_matrix[0, 2], rotation_matrix[1, 0], 1.0/rotation_matrix[1, 1], rotation_matrix[1, 2], rotation_matrix[2, 0], rotation_matrix[2, 1], 1.0/rotation_matrix[2, 2], translation_array[0, 0], translation_array[0, 1], -translation_array[0, 2])) text_file.write("FixedParameters: %.9f %.9f %.9f\n" % (points_moving_barycenter[0], points_moving_barycenter[1], points_moving_barycenter[2])) text_file.close() # Apply affine transformation: straight --> template sct.printv('\nApply affine transformation: straight --> template...', verbose) sct.run('sct_concat_transfo -w warp_curve2straight.nii.gz,straight2templateAffine.txt -d template.nii -o warp_curve2straightAffine.nii.gz') sct.run('sct_apply_transfo -i data_rpi.nii -o data_rpi_straight2templateAffine.nii -d template.nii -w warp_curve2straightAffine.nii.gz') sct.run('sct_apply_transfo -i segmentation_rpi.nii.gz -o segmentation_rpi_straight2templateAffine.nii.gz -d template.nii -w warp_curve2straightAffine.nii.gz -x linear') # threshold to 0.5 nii = Image('segmentation_rpi_straight2templateAffine.nii.gz') data = nii.data data[data < 0.5] = 0 nii.data = data nii.setFileName('segmentation_rpi_straight2templateAffine_th.nii.gz') nii.save() # find min-max of anat2template (for subsequent cropping) zmin_template, zmax_template = find_zmin_zmax('segmentation_rpi_straight2templateAffine_th.nii.gz') # crop template in z-direction (for faster processing) sct.printv('\nCrop data in template space (for faster processing)...', verbose) sct.run('sct_crop_image -i template.nii -o template_crop.nii -dim 2 -start '+str(zmin_template)+' -end '+str(zmax_template)) sct.run('sct_crop_image -i template_seg.nii.gz -o template_seg_crop.nii.gz -dim 2 -start '+str(zmin_template)+' -end '+str(zmax_template)) sct.run('sct_crop_image -i data_rpi_straight2templateAffine.nii -o data_rpi_straight2templateAffine_crop.nii -dim 2 -start '+str(zmin_template)+' -end '+str(zmax_template)) sct.run('sct_crop_image -i segmentation_rpi_straight2templateAffine.nii.gz -o segmentation_rpi_straight2templateAffine_crop.nii.gz -dim 2 -start '+str(zmin_template)+' -end '+str(zmax_template)) # sub-sample in z-direction sct.printv('\nSub-sample in z-direction (for faster processing)...', verbose) sct.run('sct_resample -i template_crop.nii -o template_crop_r.nii -f 1x1x'+zsubsample, verbose) sct.run('sct_resample -i template_seg_crop.nii.gz -o template_seg_crop_r.nii.gz -f 1x1x'+zsubsample, verbose) sct.run('sct_resample -i data_rpi_straight2templateAffine_crop.nii -o data_rpi_straight2templateAffine_crop_r.nii -f 1x1x'+zsubsample, verbose) sct.run('sct_resample -i segmentation_rpi_straight2templateAffine_crop.nii.gz -o segmentation_rpi_straight2templateAffine_crop_r.nii.gz -f 1x1x'+zsubsample, verbose) # Registration straight spinal cord to template sct.printv('\nRegister straight spinal cord to template...', verbose) # loop across registration steps warp_forward = [] warp_inverse = [] for i_step in range(1, len(paramreg.steps)+1): sct.printv('\nEstimate transformation for step #'+str(i_step)+'...', verbose) # identify which is the src and dest if paramreg.steps[str(i_step)].type == 'im': src = 'data_rpi_straight2templateAffine_crop_r.nii' dest = 'template_crop_r.nii' interp_step = 'linear' elif paramreg.steps[str(i_step)].type == 'seg': src = 'segmentation_rpi_straight2templateAffine_crop_r.nii.gz' dest = 'template_seg_crop_r.nii.gz' interp_step = 'nn' else: sct.printv('ERROR: Wrong image type.', 1, 'error') # if step>1, apply warp_forward_concat to the src image to be used if i_step > 1: # sct.run('sct_apply_transfo -i '+src+' -d '+dest+' -w '+','.join(warp_forward)+' -o '+sct.add_suffix(src, '_reg')+' -x '+interp_step, verbose) sct.run('sct_apply_transfo -i '+src+' -d '+dest+' -w '+','.join(warp_forward)+' -o '+sct.add_suffix(src, '_reg')+' -x '+interp_step, verbose) src = sct.add_suffix(src, '_reg') # register src --> dest warp_forward_out, warp_inverse_out = register(src, dest, paramreg, param, str(i_step)) warp_forward.append(warp_forward_out) warp_inverse.append(warp_inverse_out) # Concatenate transformations: sct.printv('\nConcatenate transformations: anat --> template...', verbose) sct.run('sct_concat_transfo -w warp_curve2straightAffine.nii.gz,'+','.join(warp_forward)+' -d template.nii -o warp_anat2template.nii.gz', verbose) # sct.run('sct_concat_transfo -w warp_curve2straight.nii.gz,straight2templateAffine.txt,'+','.join(warp_forward)+' -d template.nii -o warp_anat2template.nii.gz', verbose) warp_inverse.reverse() sct.run('sct_concat_transfo -w '+','.join(warp_inverse)+',-straight2templateAffine.txt,warp_straight2curve.nii.gz -d data.nii -o warp_template2anat.nii.gz', verbose) # Apply warping fields to anat and template if output_type == 1: sct.run('sct_apply_transfo -i template.nii -o template2anat.nii.gz -d data.nii -w warp_template2anat.nii.gz -c 1', verbose) sct.run('sct_apply_transfo -i data.nii -o anat2template.nii.gz -d template.nii -w warp_anat2template.nii.gz -c 1', verbose) # come back to parent folder os.chdir('..') # Generate output files sct.printv('\nGenerate output files...', verbose) sct.generate_output_file(path_tmp+'/warp_template2anat.nii.gz', 'warp_template2anat.nii.gz', verbose) sct.generate_output_file(path_tmp+'/warp_anat2template.nii.gz', 'warp_anat2template.nii.gz', verbose) if output_type == 1: sct.generate_output_file(path_tmp+'/template2anat.nii.gz', 'template2anat'+ext_data, verbose) sct.generate_output_file(path_tmp+'/anat2template.nii.gz', 'anat2template'+ext_data, verbose) # Delete temporary files if remove_temp_files: sct.printv('\nDelete temporary files...', verbose) sct.run('rm -rf '+path_tmp) # display elapsed time elapsed_time = time.time() - start_time sct.printv('\nFinished! Elapsed time: '+str(int(round(elapsed_time)))+'s', verbose) # to view results sct.printv('\nTo view results, type:', verbose) sct.printv('fslview '+fname_data+' template2anat -b 0,4000 &', verbose, 'info') sct.printv('fslview '+fname_template+' -b 0,5000 anat2template &\n', verbose, 'info')
def main(): # Initialization fname_src = '' fname_transfo = '' path_out = 'atlas' verbose = param.verbose start_time = time.time() # get path of the toolbox status, path_sct = commands.getstatusoutput('echo $SCT_DIR') print path_sct # Parameters for debug mode if param.debug: fname_src = os.path.expanduser("~")+'/code/spinalcordtoolbox_dev/testing/data/errsm_23/mt/mtr.nii.gz' fname_transfo = os.path.expanduser("~")+'/code/spinalcordtoolbox_dev/testing/data/errsm_23/template/warp_template2mt.nii.gz' verbose = 1 # Check input parameters try: opts, args = getopt.getopt(sys.argv[1:],'hd:w:o:v:') except getopt.GetoptError: usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ("-d"): fname_src = arg elif opt in ("-o"): path_out = arg elif opt in ("-w"): fname_transfo = arg elif opt in ('-v'): verbose = int(arg) # display usage if a mandatory argument is not provided if fname_src == '' or fname_transfo == '': usage() # check existence of input files sct.check_file_exist(fname_src) sct.check_file_exist(fname_transfo) # print arguments print '\nCheck parameters:' print '.. Metric image: '+fname_src print '.. Transformation: '+fname_transfo print '.. Output folder: '+path_out # Extract path, file and extension path_src, file_src, ext_src = sct.extract_fname(fname_src) # create output folder if os.path.exists(path_out): sct.run('rm -rf '+path_out) sct.run('mkdir '+path_out) # get atlas files status, output = sct.run('ls '+path_sct+'/data/atlas/vol*.nii.gz') file_atlas_list = output.split() # Warp atlas for i in xrange (0,len(file_atlas_list)): path_atlas, file_atlas, ext_atlas = sct.extract_fname(file_atlas_list[i]) sct.run('WarpImageMultiTransform 3 '+file_atlas_list[i]+' '+path_out+'/'+file_atlas+ext_atlas+' -R '+fname_src+' '+fname_transfo) # Copy list.txt sct.run('cp '+path_sct+'/data/atlas/list.txt '+path_out+'/') # Warp other template objects sct.run('WarpImageMultiTransform 3 '+path_sct+'/data/template/MNI-Poly-AMU_GM.nii.gz '+path_out+'/../gray_matter.nii.gz -R '+fname_src+' '+fname_transfo) sct.run('WarpImageMultiTransform 3 '+path_sct+'/data/template/MNI-Poly-AMU_WM.nii.gz '+path_out+'/../white_matter.nii.gz -R '+fname_src+' '+fname_transfo) sct.run('WarpImageMultiTransform 3 '+path_sct+'/data/template/MNI-Poly-AMU_level.nii.gz '+path_out+'/../vertebral_labeling.nii.gz -R '+fname_src+' --use-NN '+fname_transfo) sct.run('WarpImageMultiTransform 3 '+path_sct+'/data/template/MNI-Poly-AMU_CSF.nii.gz '+path_out+'/../csf.nii.gz -R '+fname_src+' --use-NN '+fname_transfo)
def create_mask(param): # parse argument for method method_type = param.process[0] # check method val if not method_type == 'center': method_val = param.process[1] # check existence of input files if method_type == 'centerline': sct.check_file_exist(method_val, param.verbose) # Extract path/file/extension path_data, file_data, ext_data = sct.extract_fname(param.fname_data) # Get output folder and file name if param.fname_out == '': param.fname_out = os.path.abspath(param.file_prefix + file_data + ext_data) path_tmp = sct.tmp_create(basename="create_mask", verbose=param.verbose) sct.printv('\nOrientation:', param.verbose) orientation_input = Image(param.fname_data).orientation sct.printv(' ' + orientation_input, param.verbose) # copy input data to tmp folder and re-orient to RPI Image(param.fname_data).change_orientation("RPI").save( os.path.join(path_tmp, "data_RPI.nii")) if method_type == 'centerline': Image(method_val).change_orientation("RPI").save( os.path.join(path_tmp, "centerline_RPI.nii")) if method_type == 'point': Image(method_val).change_orientation("RPI").save( os.path.join(path_tmp, "point_RPI.nii")) # go to tmp folder curdir = os.getcwd() os.chdir(path_tmp) # Get dimensions of data im_data = Image('data_RPI.nii') nx, ny, nz, nt, px, py, pz, pt = im_data.dim sct.printv('\nDimensions:', param.verbose) sct.printv(im_data.dim, param.verbose) # in case user input 4d data if nt != 1: sct.printv( 'WARNING in ' + os.path.basename(__file__) + ': Input image is 4d but output mask will be 3D from first time slice.', param.verbose, 'warning') # extract first volume to have 3d reference nii = msct_image.empty_like(Image('data_RPI.nii')) data3d = nii.data[:, :, :, 0] nii.data = data3d nii.save('data_RPI.nii') if method_type == 'coord': # parse to get coordinate coord = [x for x in map(int, method_val.split('x'))] if method_type == 'point': # get file name # extract coordinate of point sct.printv('\nExtract coordinate of point...', param.verbose) # TODO: change this way to remove dependence to sct.run. ProcessLabels.display_voxel returns list of coordinates status, output = sct.run( ['sct_label_utils', '-i', 'point_RPI.nii', '-display'], verbose=param.verbose) # parse to get coordinate # TODO fixup... this is quite magic coord = output[output.find('Position=') + 10:-17].split(',') if method_type == 'center': # set coordinate at center of FOV coord = np.round(float(nx) / 2), np.round(float(ny) / 2) if method_type == 'centerline': # get name of centerline from user argument fname_centerline = 'centerline_RPI.nii' else: # generate volume with line along Z at coordinates 'coord' sct.printv('\nCreate line...', param.verbose) fname_centerline = create_line(param, 'data_RPI.nii', coord, nz) # create mask sct.printv('\nCreate mask...', param.verbose) centerline = nibabel.load(fname_centerline) # open centerline hdr = centerline.get_header() # get header hdr.set_data_dtype('uint8') # set imagetype to uint8 spacing = hdr.structarr['pixdim'] data_centerline = centerline.get_data() # get centerline # if data is 2D, reshape with empty third dimension if len(data_centerline.shape) == 2: data_centerline_shape = list(data_centerline.shape) data_centerline_shape.append(1) data_centerline = data_centerline.reshape(data_centerline_shape) z_centerline_not_null = [ iz for iz in range(0, nz, 1) if data_centerline[:, :, iz].any() ] # get center of mass of the centerline cx = [0] * nz cy = [0] * nz for iz in range(0, nz, 1): if iz in z_centerline_not_null: cx[iz], cy[iz] = ndimage.measurements.center_of_mass( np.array(data_centerline[:, :, iz])) # create 2d masks file_mask = 'data_mask' for iz in range(nz): if iz not in z_centerline_not_null: # write an empty nifty volume img = nibabel.Nifti1Image(data_centerline[:, :, iz], None, hdr) nibabel.save(img, (file_mask + str(iz) + '.nii')) else: center = np.array([cx[iz], cy[iz]]) mask2d = create_mask2d(param, center, param.shape, param.size, im_data=im_data) # Write NIFTI volumes img = nibabel.Nifti1Image(mask2d, None, hdr) nibabel.save(img, (file_mask + str(iz) + '.nii')) fname_list = [file_mask + str(iz) + '.nii' for iz in range(nz)] im_out = concat_data(fname_list, dim=2).save('mask_RPI.nii.gz') im_out.change_orientation(orientation_input) im_out.header = Image(param.fname_data).header im_out.save(param.fname_out) # come back os.chdir(curdir) # Remove temporary files if param.remove_temp_files == 1: sct.printv('\nRemove temporary files...', param.verbose) sct.rmtree(path_tmp) sct.display_viewer_syntax([param.fname_data, param.fname_out], colormaps=['gray', 'red'], opacities=['', '0.5'])
def main(): #Initialization fname = '' fname_centerline = '' mean_intensity = param.mean_intensity verbose = param.verbose padding = param.padding window_length = param.window_length try: opts, args = getopt.getopt(sys.argv[1:],'hi:c:v:p:') except getopt.GetoptError: usage() for opt, arg in opts : if opt == '-h': usage() elif opt in ("-i"): fname = arg elif opt in ("-c"): fname_centerline = arg elif opt in ("-p"): window_length = int(arg) elif opt in ('-v'): verbose = int(arg) # display usage if a mandatory argument is not provided #if fname == '' or fname_centerline == '': if fname == '': usage() # check existence of input files print'\nCheck if file exists ...' sct.check_file_exist(fname) #sct.check_file_exist(fname_centerline) # Display arguments print'\nCheck input arguments...' print' Input volume ...................... '+fname print' Centerline ...................... '+fname_centerline print' Verbose ........................... '+str(verbose) # Extract path, file and extension path_input, file_input, ext_input = sct.extract_fname(fname) sct.printv('\nOpen volume...',verbose) file = nibabel.load(fname) data = file.get_data() hdr = file.get_header() if fname_centerline != '': ## [Process 1] Command for extracting center of mass for each slice of the centerline file if provided sct.printv('\nOpen centerline...',verbose) print '\nGet dimensions of input centerline...' nx, ny, nz, nt, px, py, pz, pt = Image(fname_centerline).dim print '.. matrix size: '+str(nx)+' x '+str(ny)+' x '+str(nz) print '.. voxel size: '+str(px)+'mm x '+str(py)+'mm x '+str(pz)+'mm' file_c = nibabel.load(fname_centerline) data_c = file_c.get_data() #X,Y,Z = (data_c>0).nonzero() #min_z_index, max_z_index = min(Z), max(Z) z_centerline = [iz for iz in range(0, nz, 1) if data_c[:,:,iz].any() ] nz_nonz = len(z_centerline) if nz_nonz==0 : print '\nERROR: Centerline is empty' sys.exit() x_centerline = [0 for iz in range(0, nz_nonz, 1)] y_centerline = [0 for iz in range(0, nz_nonz, 1)] #print("z_centerline", z_centerline,nz_nonz,len(x_centerline)) print '\nGet center of mass of the centerline ...' for iz in xrange(len(z_centerline)): x_centerline[iz], y_centerline[iz] = ndimage.measurements.center_of_mass(np.array(data_c[:,:,z_centerline[iz]])) # end of Process 1 ## [Process 2] Process for defining the middle vertical line as reference for normalizing the intensity of the image if fname_centerline == '': print '\nGet dimensions of input image...' nx, ny, nz, nt, px, py, pz, pt = Image(fname).dim print '.. matrix size: '+str(nx)+' x '+str(ny)+' x '+str(nz) print '.. voxel size: '+str(px)+'mm x '+str(py)+'mm x '+str(pz)+'mm' z_centerline = [iz for iz in range(0, nz, 1)] nz_nonz = len(z_centerline) x_middle = int(round(nx/2)) y_middle = int(round(ny/2)) x_centerline = [x_middle for iz in range(0, nz, 1)] y_centerline = [y_middle for iz in range(0, nz, 1)] # end of Process 2 means = [0 for iz in range(0, nz_nonz, 1)] print '\nGet mean intensity along the centerline ...' for iz in xrange(len(z_centerline)): means[iz] = np.mean(data[(int(round(x_centerline[iz]))-padding):(int(round(x_centerline[iz]))+padding),(int(round(y_centerline[iz]))-padding):(int(round(y_centerline[iz]))+padding),z_centerline[iz]]) # print('\nSmoothing results with spline...') # # Smoothing with scipy library (Julien Touati's code) # m =np.mean(means) # sigma = np.std(means) # smoothing_param = (((m + np.sqrt(2*m))*(sigma**2))+((m - np.sqrt(2*m))*(sigma**2)))/2 # #Equivalent to : m*sigma**2 # tck = splrep(z_centerline, means, s=smoothing_param) # means_smooth = splev(z_centerline, tck) # Smoothing with low-pass filter print '\nSmoothing with lowpass filter: butterworth order 5...' from msct_smooth import lowpass means_smooth = lowpass(means) # #Smoothing with nurbs #points = [[means[n],0, z_centerline[n]] for n in range(len(z_centerline))] #nurbs = NURBS(3,1000,points) #P = nurbs.getCourbe3D() #means_smooth=P[0] #size of means_smooth? should be bigger than len(z_centerline) # #Smoothing with hanning # print('\nSmoothing results with hanning windowing...') # means = np.asarray(means) # means_smooth = smoothing_window(means, window_len=window_length) # print means.shape[0], means_smooth.shape[0] if verbose : plt.figure() #plt.subplot(2,1,1) plt.plot(z_centerline,means, "ro") #plt.subplot(2,1,2) plt.plot(means_smooth) plt.title("Mean intensity: Type of window: hanning Window_length= %d mm" % window_length) plt.show() print('\nNormalizing intensity along centerline...') #Define extended meaned intensity for all the spinal cord means_smooth_extended = [0 for i in range(0, data.shape[2], 1)] for iz in range(len(z_centerline)): means_smooth_extended[z_centerline[iz]] = means_smooth[iz] X_means_smooth_extended = np.nonzero(means_smooth_extended) X_means_smooth_extended = np.transpose(X_means_smooth_extended) if len(X_means_smooth_extended) != 0: means_smooth_extended[0] = means_smooth_extended[X_means_smooth_extended[0]] means_smooth_extended[-1] = means_smooth_extended[X_means_smooth_extended[-1]] #Add two rows to the vector X_mask_completed: # one before as mask_completed[0] is now diff from 0 # one after as mask_completed[-1] is now diff from 0 X_means_smooth_extended = np.append(X_means_smooth_extended, len(means_smooth_extended)-1) X_means_smooth_extended = np.insert(X_means_smooth_extended, 0, 0) #linear interpolation count_zeros=0 for i in range(1,len(means_smooth_extended)-1): if means_smooth_extended[i]==0: means_smooth_extended[i] = 0.5 * (means_smooth_extended[X_means_smooth_extended[i-1-count_zeros]] + means_smooth_extended[X_means_smooth_extended[i-count_zeros]]) # linear interpolation with closest non zero points #redefine X_mask_completed X_means_smooth_extended = np.nonzero(means_smooth_extended) X_means_smooth_extended = np.transpose(X_means_smooth_extended) #recurrence # count_zeros=0 # for i in range(1,len(means_smooth_extended)-1): # if means_smooth_extended[i]==0: # means_smooth_extended[i] = 0.5*(means_smooth_extended[X_means_smooth_extended[i-1-count_zeros]] + means_smooth_extended[X_means_smooth_extended[i-count_zeros]]) # # redefine X_mask_extended # X_mask_completed = np.nonzero(means_smooth_extended) # X_mask_completed = np.transpose(X_mask_completed) # #count_zeros += 1 if verbose : plt.figure() plt.subplot(2,1,1) plt.plot(z_centerline,means) plt.plot(z_centerline,means_smooth) plt.title("Mean intensity") plt.subplot(2,1,2) plt.plot(z_centerline,means) plt.plot(means_smooth_extended) plt.title("Extended mean intensity") plt.show() for i in range(data.shape[2]): data[:,:,i] = data[:,:,i] * (mean_intensity/means_smooth_extended[i]) hdr.set_data_dtype('uint16') # set imagetype to uint16 # save volume sct.printv('\nWrite NIFTI volumes...',verbose) data = data.astype(np.float32, copy =False) img = nibabel.Nifti1Image(data, None, hdr) output_name = file_input+'_normalized'+ext_input nibabel.save(img,output_name) sct.printv('\n.. File created:' + output_name,verbose) print('\nNormalizing overall intensity...') # sct.run('fslmaths ' + output_name + ' -inm ' + str(mean_intensity) + ' ' + output_name) # to view results print '\nDone !' print '\nTo view results, type:' print 'fslview '+output_name+' &\n'
def main(): parser = get_parser() param = Param() args = sys.argv[1:] arguments = parser.parse(args) # get arguments fname_data = arguments['-i'] fname_seg = arguments['-s'] fname_landmarks = arguments['-l'] if '-ofolder' in arguments: path_output = arguments['-ofolder'] else: path_output = '' path_template = sct.slash_at_the_end(arguments['-t'], 1) contrast_template = arguments['-c'] ref = arguments['-ref'] remove_temp_files = int(arguments['-r']) verbose = int(arguments['-v']) param.verbose = verbose # TODO: not clean, unify verbose or param.verbose in code, but not both if '-param-straighten' in arguments: param.param_straighten = arguments['-param-straighten'] # if '-cpu-nb' in arguments: # arg_cpu = ' -cpu-nb '+str(arguments['-cpu-nb']) # else: # arg_cpu = '' # registration parameters if '-param' in arguments: # reset parameters but keep step=0 (might be overwritten if user specified step=0) paramreg = ParamregMultiStep([step0]) if ref == 'subject': paramreg.steps['0'].dof = 'Tx_Ty_Tz_Rx_Ry_Rz_Sz' # add user parameters for paramStep in arguments['-param']: paramreg.addStep(paramStep) else: paramreg = ParamregMultiStep([step0, step1, step2]) # if ref=subject, initialize registration using different affine parameters if ref == 'subject': paramreg.steps['0'].dof = 'Tx_Ty_Tz_Rx_Ry_Rz_Sz' # initialize other parameters # file_template_label = param.file_template_label zsubsample = param.zsubsample # smoothing_sigma = param.smoothing_sigma # retrieve template file names from sct_warp_template import get_file_label file_template_vertebral_labeling = get_file_label(path_template + 'template/', 'vertebral') file_template = get_file_label(path_template + 'template/', contrast_template.upper() + '-weighted') file_template_seg = get_file_label(path_template + 'template/', 'spinal cord') # start timer start_time = time.time() # get fname of the template + template objects fname_template = path_template + 'template/' + file_template fname_template_vertebral_labeling = path_template + 'template/' + file_template_vertebral_labeling fname_template_seg = path_template + 'template/' + file_template_seg # check file existence # TODO: no need to do that! sct.printv('\nCheck template files...') sct.check_file_exist(fname_template, verbose) sct.check_file_exist(fname_template_vertebral_labeling, verbose) sct.check_file_exist(fname_template_seg, verbose) path_data, file_data, ext_data = sct.extract_fname(fname_data) # print arguments sct.printv('\nCheck parameters:', verbose) sct.printv(' Data: ' + fname_data, verbose) sct.printv(' Landmarks: ' + fname_landmarks, verbose) sct.printv(' Segmentation: ' + fname_seg, verbose) sct.printv(' Path template: ' + path_template, verbose) sct.printv(' Remove temp files: ' + str(remove_temp_files), verbose) # create QC folder sct.create_folder(param.path_qc) # check if data, segmentation and landmarks are in the same space # JULIEN 2017-04-25: removed because of issue #1168 # sct.printv('\nCheck if data, segmentation and landmarks are in the same space...') # if not sct.check_if_same_space(fname_data, fname_seg): # sct.printv('ERROR: Data image and segmentation are not in the same space. Please check space and orientation of your files', verbose, 'error') # if not sct.check_if_same_space(fname_data, fname_landmarks): # sct.printv('ERROR: Data image and landmarks are not in the same space. Please check space and orientation of your files', verbose, 'error') # check input labels labels = check_labels(fname_landmarks) # create temporary folder path_tmp = sct.tmp_create(verbose=verbose) # set temporary file names ftmp_data = 'data.nii' ftmp_seg = 'seg.nii.gz' ftmp_label = 'label.nii.gz' ftmp_template = 'template.nii' ftmp_template_seg = 'template_seg.nii.gz' ftmp_template_label = 'template_label.nii.gz' # copy files to temporary folder sct.printv('\nCopying input data to tmp folder and convert to nii...', verbose) sct.run('sct_convert -i ' + fname_data + ' -o ' + path_tmp + ftmp_data) sct.run('sct_convert -i ' + fname_seg + ' -o ' + path_tmp + ftmp_seg) sct.run('sct_convert -i ' + fname_landmarks + ' -o ' + path_tmp + ftmp_label) sct.run('sct_convert -i ' + fname_template + ' -o ' + path_tmp + ftmp_template) sct.run('sct_convert -i ' + fname_template_seg + ' -o ' + path_tmp + ftmp_template_seg) # sct.run('sct_convert -i '+fname_template_label+' -o '+path_tmp+ftmp_template_label) # go to tmp folder os.chdir(path_tmp) # copy header of anat to segmentation (issue #1168) # from sct_image import copy_header # im_data = Image(ftmp_data) # im_seg = Image(ftmp_seg) # copy_header(im_data, im_seg) # im_seg.save() # im_label = Image(ftmp_label) # copy_header(im_data, im_label) # im_label.save() # Generate labels from template vertebral labeling sct.printv('\nGenerate labels from template vertebral labeling', verbose) sct.run('sct_label_utils -i ' + fname_template_vertebral_labeling + ' -vert-body 0 -o ' + ftmp_template_label) # check if provided labels are available in the template sct.printv('\nCheck if provided labels are available in the template', verbose) image_label_template = Image(ftmp_template_label) labels_template = image_label_template.getNonZeroCoordinates(sorting='value') if labels[-1].value > labels_template[-1].value: sct.printv('ERROR: Wrong landmarks input. Labels must have correspondence in template space. \nLabel max ' 'provided: ' + str(labels[-1].value) + '\nLabel max from template: ' + str(labels_template[-1].value), verbose, 'error') # binarize segmentation (in case it has values below 0 caused by manual editing) sct.printv('\nBinarize segmentation', verbose) sct.run('sct_maths -i seg.nii.gz -bin 0.5 -o seg.nii.gz') # smooth segmentation (jcohenadad, issue #613) # sct.printv('\nSmooth segmentation...', verbose) # sct.run('sct_maths -i '+ftmp_seg+' -smooth 1.5 -o '+add_suffix(ftmp_seg, '_smooth')) # jcohenadad: updated 2016-06-16: DO NOT smooth the seg anymore. Issue # # sct.run('sct_maths -i '+ftmp_seg+' -smooth 0 -o '+add_suffix(ftmp_seg, '_smooth')) # ftmp_seg = add_suffix(ftmp_seg, '_smooth') # Switch between modes: subject->template or template->subject if ref == 'template': # resample data to 1mm isotropic sct.printv('\nResample data to 1mm isotropic...', verbose) sct.run('sct_resample -i ' + ftmp_data + ' -mm 1.0x1.0x1.0 -x linear -o ' + add_suffix(ftmp_data, '_1mm')) ftmp_data = add_suffix(ftmp_data, '_1mm') sct.run('sct_resample -i ' + ftmp_seg + ' -mm 1.0x1.0x1.0 -x linear -o ' + add_suffix(ftmp_seg, '_1mm')) ftmp_seg = add_suffix(ftmp_seg, '_1mm') # N.B. resampling of labels is more complicated, because they are single-point labels, therefore resampling with neighrest neighbour can make them disappear. Therefore a more clever approach is required. resample_labels(ftmp_label, ftmp_data, add_suffix(ftmp_label, '_1mm')) ftmp_label = add_suffix(ftmp_label, '_1mm') # Change orientation of input images to RPI sct.printv('\nChange orientation of input images to RPI...', verbose) sct.run('sct_image -i ' + ftmp_data + ' -setorient RPI -o ' + add_suffix(ftmp_data, '_rpi')) ftmp_data = add_suffix(ftmp_data, '_rpi') sct.run('sct_image -i ' + ftmp_seg + ' -setorient RPI -o ' + add_suffix(ftmp_seg, '_rpi')) ftmp_seg = add_suffix(ftmp_seg, '_rpi') sct.run('sct_image -i ' + ftmp_label + ' -setorient RPI -o ' + add_suffix(ftmp_label, '_rpi')) ftmp_label = add_suffix(ftmp_label, '_rpi') # get landmarks in native space # crop segmentation # output: segmentation_rpi_crop.nii.gz status_crop, output_crop = sct.run('sct_crop_image -i ' + ftmp_seg + ' -o ' + add_suffix(ftmp_seg, '_crop') + ' -dim 2 -bzmax', verbose) ftmp_seg = add_suffix(ftmp_seg, '_crop') cropping_slices = output_crop.split('Dimension 2: ')[1].split('\n')[0].split(' ') # straighten segmentation sct.printv('\nStraighten the spinal cord using centerline/segmentation...', verbose) # check if warp_curve2straight and warp_straight2curve already exist (i.e. no need to do it another time) if os.path.isfile('../warp_curve2straight.nii.gz') and os.path.isfile('../warp_straight2curve.nii.gz') and os.path.isfile('../straight_ref.nii.gz'): # if they exist, copy them into current folder sct.printv('WARNING: Straightening was already run previously. Copying warping fields...', verbose, 'warning') shutil.copy('../warp_curve2straight.nii.gz', 'warp_curve2straight.nii.gz') shutil.copy('../warp_straight2curve.nii.gz', 'warp_straight2curve.nii.gz') shutil.copy('../straight_ref.nii.gz', 'straight_ref.nii.gz') # apply straightening sct.run('sct_apply_transfo -i ' + ftmp_seg + ' -w warp_curve2straight.nii.gz -d straight_ref.nii.gz -o ' + add_suffix(ftmp_seg, '_straight')) else: sct.run('sct_straighten_spinalcord -i ' + ftmp_seg + ' -s ' + ftmp_seg + ' -o ' + add_suffix(ftmp_seg, '_straight') + ' -qc 0 -r 0 -v ' + str(verbose), verbose) # N.B. DO NOT UPDATE VARIABLE ftmp_seg BECAUSE TEMPORARY USED LATER # re-define warping field using non-cropped space (to avoid issue #367) sct.run('sct_concat_transfo -w warp_straight2curve.nii.gz -d ' + ftmp_data + ' -o warp_straight2curve.nii.gz') # Label preparation: # -------------------------------------------------------------------------------- # Remove unused label on template. Keep only label present in the input label image sct.printv('\nRemove unused label on template. Keep only label present in the input label image...', verbose) sct.run('sct_label_utils -i ' + ftmp_template_label + ' -o ' + ftmp_template_label + ' -remove ' + ftmp_label) # Dilating the input label so they can be straighten without losing them sct.printv('\nDilating input labels using 3vox ball radius') sct.run('sct_maths -i ' + ftmp_label + ' -o ' + add_suffix(ftmp_label, '_dilate') + ' -dilate 3') ftmp_label = add_suffix(ftmp_label, '_dilate') # Apply straightening to labels sct.printv('\nApply straightening to labels...', verbose) sct.run('sct_apply_transfo -i ' + ftmp_label + ' -o ' + add_suffix(ftmp_label, '_straight') + ' -d ' + add_suffix(ftmp_seg, '_straight') + ' -w warp_curve2straight.nii.gz -x nn') ftmp_label = add_suffix(ftmp_label, '_straight') # Compute rigid transformation straight landmarks --> template landmarks sct.printv('\nEstimate transformation for step #0...', verbose) from msct_register_landmarks import register_landmarks try: register_landmarks(ftmp_label, ftmp_template_label, paramreg.steps['0'].dof, fname_affine='straight2templateAffine.txt', verbose=verbose) except Exception: sct.printv('ERROR: input labels do not seem to be at the right place. Please check the position of the labels. See documentation for more details: https://sourceforge.net/p/spinalcordtoolbox/wiki/create_labels/', verbose=verbose, type='error') # Concatenate transformations: curve --> straight --> affine sct.printv('\nConcatenate transformations: curve --> straight --> affine...', verbose) sct.run('sct_concat_transfo -w warp_curve2straight.nii.gz,straight2templateAffine.txt -d template.nii -o warp_curve2straightAffine.nii.gz') # Apply transformation sct.printv('\nApply transformation...', verbose) sct.run('sct_apply_transfo -i ' + ftmp_data + ' -o ' + add_suffix(ftmp_data, '_straightAffine') + ' -d ' + ftmp_template + ' -w warp_curve2straightAffine.nii.gz') ftmp_data = add_suffix(ftmp_data, '_straightAffine') sct.run('sct_apply_transfo -i ' + ftmp_seg + ' -o ' + add_suffix(ftmp_seg, '_straightAffine') + ' -d ' + ftmp_template + ' -w warp_curve2straightAffine.nii.gz -x linear') ftmp_seg = add_suffix(ftmp_seg, '_straightAffine') """ # Benjamin: Issue from Allan Martin, about the z=0 slice that is screwed up, caused by the affine transform. # Solution found: remove slices below and above landmarks to avoid rotation effects points_straight = [] for coord in landmark_template: points_straight.append(coord.z) min_point, max_point = int(round(np.min(points_straight))), int(round(np.max(points_straight))) sct.run('sct_crop_image -i ' + ftmp_seg + ' -start ' + str(min_point) + ' -end ' + str(max_point) + ' -dim 2 -b 0 -o ' + add_suffix(ftmp_seg, '_black')) ftmp_seg = add_suffix(ftmp_seg, '_black') """ # binarize sct.printv('\nBinarize segmentation...', verbose) sct.run('sct_maths -i ' + ftmp_seg + ' -bin 0.5 -o ' + add_suffix(ftmp_seg, '_bin')) ftmp_seg = add_suffix(ftmp_seg, '_bin') # find min-max of anat2template (for subsequent cropping) zmin_template, zmax_template = find_zmin_zmax(ftmp_seg) # crop template in z-direction (for faster processing) sct.printv('\nCrop data in template space (for faster processing)...', verbose) sct.run('sct_crop_image -i ' + ftmp_template + ' -o ' + add_suffix(ftmp_template, '_crop') + ' -dim 2 -start ' + str(zmin_template) + ' -end ' + str(zmax_template)) ftmp_template = add_suffix(ftmp_template, '_crop') sct.run('sct_crop_image -i ' + ftmp_template_seg + ' -o ' + add_suffix(ftmp_template_seg, '_crop') + ' -dim 2 -start ' + str(zmin_template) + ' -end ' + str(zmax_template)) ftmp_template_seg = add_suffix(ftmp_template_seg, '_crop') sct.run('sct_crop_image -i ' + ftmp_data + ' -o ' + add_suffix(ftmp_data, '_crop') + ' -dim 2 -start ' + str(zmin_template) + ' -end ' + str(zmax_template)) ftmp_data = add_suffix(ftmp_data, '_crop') sct.run('sct_crop_image -i ' + ftmp_seg + ' -o ' + add_suffix(ftmp_seg, '_crop') + ' -dim 2 -start ' + str(zmin_template) + ' -end ' + str(zmax_template)) ftmp_seg = add_suffix(ftmp_seg, '_crop') # sub-sample in z-direction sct.printv('\nSub-sample in z-direction (for faster processing)...', verbose) sct.run('sct_resample -i ' + ftmp_template + ' -o ' + add_suffix(ftmp_template, '_sub') + ' -f 1x1x' + zsubsample, verbose) ftmp_template = add_suffix(ftmp_template, '_sub') sct.run('sct_resample -i ' + ftmp_template_seg + ' -o ' + add_suffix(ftmp_template_seg, '_sub') + ' -f 1x1x' + zsubsample, verbose) ftmp_template_seg = add_suffix(ftmp_template_seg, '_sub') sct.run('sct_resample -i ' + ftmp_data + ' -o ' + add_suffix(ftmp_data, '_sub') + ' -f 1x1x' + zsubsample, verbose) ftmp_data = add_suffix(ftmp_data, '_sub') sct.run('sct_resample -i ' + ftmp_seg + ' -o ' + add_suffix(ftmp_seg, '_sub') + ' -f 1x1x' + zsubsample, verbose) ftmp_seg = add_suffix(ftmp_seg, '_sub') # Registration straight spinal cord to template sct.printv('\nRegister straight spinal cord to template...', verbose) # loop across registration steps warp_forward = [] warp_inverse = [] for i_step in range(1, len(paramreg.steps)): sct.printv('\nEstimate transformation for step #' + str(i_step) + '...', verbose) # identify which is the src and dest if paramreg.steps[str(i_step)].type == 'im': src = ftmp_data dest = ftmp_template interp_step = 'linear' elif paramreg.steps[str(i_step)].type == 'seg': src = ftmp_seg dest = ftmp_template_seg interp_step = 'nn' else: sct.printv('ERROR: Wrong image type.', 1, 'error') # if step>1, apply warp_forward_concat to the src image to be used if i_step > 1: # sct.run('sct_apply_transfo -i '+src+' -d '+dest+' -w '+','.join(warp_forward)+' -o '+sct.add_suffix(src, '_reg')+' -x '+interp_step, verbose) # apply transformation from previous step, to use as new src for registration sct.run('sct_apply_transfo -i ' + src + ' -d ' + dest + ' -w ' + ','.join(warp_forward) + ' -o ' + add_suffix(src, '_regStep' + str(i_step - 1)) + ' -x ' + interp_step, verbose) src = add_suffix(src, '_regStep' + str(i_step - 1)) # register src --> dest # TODO: display param for debugging warp_forward_out, warp_inverse_out = register(src, dest, paramreg, param, str(i_step)) warp_forward.append(warp_forward_out) warp_inverse.append(warp_inverse_out) # Concatenate transformations: sct.printv('\nConcatenate transformations: anat --> template...', verbose) sct.run('sct_concat_transfo -w warp_curve2straightAffine.nii.gz,' + ','.join(warp_forward) + ' -d template.nii -o warp_anat2template.nii.gz', verbose) # sct.run('sct_concat_transfo -w warp_curve2straight.nii.gz,straight2templateAffine.txt,'+','.join(warp_forward)+' -d template.nii -o warp_anat2template.nii.gz', verbose) sct.printv('\nConcatenate transformations: template --> anat...', verbose) warp_inverse.reverse() sct.run('sct_concat_transfo -w ' + ','.join(warp_inverse) + ',-straight2templateAffine.txt,warp_straight2curve.nii.gz -d data.nii -o warp_template2anat.nii.gz', verbose) # register template->subject elif ref == 'subject': # Change orientation of input images to RPI sct.printv('\nChange orientation of input images to RPI...', verbose) sct.run('sct_image -i ' + ftmp_data + ' -setorient RPI -o ' + add_suffix(ftmp_data, '_rpi')) ftmp_data = add_suffix(ftmp_data, '_rpi') sct.run('sct_image -i ' + ftmp_seg + ' -setorient RPI -o ' + add_suffix(ftmp_seg, '_rpi')) ftmp_seg = add_suffix(ftmp_seg, '_rpi') sct.run('sct_image -i ' + ftmp_label + ' -setorient RPI -o ' + add_suffix(ftmp_label, '_rpi')) ftmp_label = add_suffix(ftmp_label, '_rpi') # Remove unused label on template. Keep only label present in the input label image sct.printv('\nRemove unused label on template. Keep only label present in the input label image...', verbose) sct.run('sct_label_utils -i ' + ftmp_template_label + ' -o ' + ftmp_template_label + ' -remove ' + ftmp_label) # Add one label because at least 3 orthogonal labels are required to estimate an affine transformation. This new label is added at the level of the upper most label (lowest value), at 1cm to the right. for i_file in [ftmp_label, ftmp_template_label]: im_label = Image(i_file) coord_label = im_label.getCoordinatesAveragedByValue() # N.B. landmarks are sorted by value # Create new label from copy import deepcopy new_label = deepcopy(coord_label[0]) # move it 5mm to the left (orientation is RAS) nx, ny, nz, nt, px, py, pz, pt = im_label.dim new_label.x = round(coord_label[0].x + 5.0 / px) # assign value 99 new_label.value = 99 # Add to existing image im_label.data[int(new_label.x), int(new_label.y), int(new_label.z)] = new_label.value # Overwrite label file # im_label.setFileName('label_rpi_modif.nii.gz') im_label.save() # Bring template to subject space using landmark-based transformation sct.printv('\nEstimate transformation for step #0...', verbose) from msct_register_landmarks import register_landmarks warp_forward = ['template2subjectAffine.txt'] warp_inverse = ['-template2subjectAffine.txt'] try: register_landmarks(ftmp_template_label, ftmp_label, paramreg.steps['0'].dof, fname_affine=warp_forward[0], verbose=verbose, path_qc=param.path_qc) except Exception: sct.printv('ERROR: input labels do not seem to be at the right place. Please check the position of the labels. See documentation for more details: https://sourceforge.net/p/spinalcordtoolbox/wiki/create_labels/', verbose=verbose, type='error') # loop across registration steps for i_step in range(1, len(paramreg.steps)): sct.printv('\nEstimate transformation for step #' + str(i_step) + '...', verbose) # identify which is the src and dest if paramreg.steps[str(i_step)].type == 'im': src = ftmp_template dest = ftmp_data interp_step = 'linear' elif paramreg.steps[str(i_step)].type == 'seg': src = ftmp_template_seg dest = ftmp_seg interp_step = 'nn' else: sct.printv('ERROR: Wrong image type.', 1, 'error') # apply transformation from previous step, to use as new src for registration sct.run('sct_apply_transfo -i ' + src + ' -d ' + dest + ' -w ' + ','.join(warp_forward) + ' -o ' + add_suffix(src, '_regStep' + str(i_step - 1)) + ' -x ' + interp_step, verbose) src = add_suffix(src, '_regStep' + str(i_step - 1)) # register src --> dest # TODO: display param for debugging warp_forward_out, warp_inverse_out = register(src, dest, paramreg, param, str(i_step)) warp_forward.append(warp_forward_out) warp_inverse.insert(0, warp_inverse_out) # Concatenate transformations: sct.printv('\nConcatenate transformations: template --> subject...', verbose) sct.run('sct_concat_transfo -w ' + ','.join(warp_forward) + ' -d data.nii -o warp_template2anat.nii.gz', verbose) sct.printv('\nConcatenate transformations: subject --> template...', verbose) sct.run('sct_concat_transfo -w ' + ','.join(warp_inverse) + ' -d template.nii -o warp_anat2template.nii.gz', verbose) # Apply warping fields to anat and template sct.run('sct_apply_transfo -i template.nii -o template2anat.nii.gz -d data.nii -w warp_template2anat.nii.gz -crop 1', verbose) sct.run('sct_apply_transfo -i data.nii -o anat2template.nii.gz -d template.nii -w warp_anat2template.nii.gz -crop 1', verbose) # come back to parent folder os.chdir('..') # Generate output files sct.printv('\nGenerate output files...', verbose) sct.generate_output_file(path_tmp + 'warp_template2anat.nii.gz', path_output + 'warp_template2anat.nii.gz', verbose) sct.generate_output_file(path_tmp + 'warp_anat2template.nii.gz', path_output + 'warp_anat2template.nii.gz', verbose) sct.generate_output_file(path_tmp + 'template2anat.nii.gz', path_output + 'template2anat' + ext_data, verbose) sct.generate_output_file(path_tmp + 'anat2template.nii.gz', path_output + 'anat2template' + ext_data, verbose) if ref == 'template': # copy straightening files in case subsequent SCT functions need them sct.generate_output_file(path_tmp + 'warp_curve2straight.nii.gz', path_output + 'warp_curve2straight.nii.gz', verbose) sct.generate_output_file(path_tmp + 'warp_straight2curve.nii.gz', path_output + 'warp_straight2curve.nii.gz', verbose) sct.generate_output_file(path_tmp + 'straight_ref.nii.gz', path_output + 'straight_ref.nii.gz', verbose) # Delete temporary files if remove_temp_files: sct.printv('\nDelete temporary files...', verbose) sct.run('rm -rf ' + path_tmp) # display elapsed time elapsed_time = time.time() - start_time sct.printv('\nFinished! Elapsed time: ' + str(int(round(elapsed_time))) + 's', verbose) if '-qc' in arguments and not arguments.get('-noqc', False): qc_path = arguments['-qc'] import spinalcordtoolbox.reports.qc as qc import spinalcordtoolbox.reports.slice as qcslice qc_param = qc.Params(fname_data, 'sct_register_to_template', args, 'Sagittal', qc_path) report = qc.QcReport(qc_param, '') @qc.QcImage(report, 'none', [qc.QcImage.no_seg_seg]) def test(qslice): return qslice.single() fname_template2anat = path_output + 'template2anat' + ext_data test(qcslice.SagittalTemplate2Anat(Image(fname_data), Image(fname_template2anat), Image(fname_seg))) sct.printv('Sucessfully generate the QC results in %s' % qc_param.qc_results) sct.printv('Use the following command to see the results in a browser') sct.printv('sct_qc -folder %s' % qc_path, type='info') # to view results sct.printv('\nTo view results, type:', verbose) sct.printv('fslview ' + fname_data + ' ' + path_output + 'template2anat -b 0,4000 &', verbose, 'info') sct.printv('fslview ' + fname_template + ' -b 0,5000 ' + path_output + 'anat2template &\n', verbose, 'info')
def main(): # Initialization fname_anat = '' fname_centerline = '' gapxy = param.gapxy gapz = param.gapz padding = param.padding centerline_fitting = param.fitting_method remove_temp_files = param.remove_temp_files verbose = param.verbose interpolation_warp = param.interpolation_warp # get path of the toolbox status, path_sct = commands.getstatusoutput('echo $SCT_DIR') print path_sct # extract path of the script path_script = os.path.dirname(__file__) + '/' # Parameters for debug mode if param.debug == 1: print '\n*** WARNING: DEBUG MODE ON ***\n' # fname_anat = path_sct+'/testing/data/errsm_23/t2/t2.nii.gz' # fname_centerline = path_sct+'/testing/data/errsm_23/t2/t2_segmentation_PropSeg.nii.gz' fname_anat = '/home/django/jtouati/data/cover_z_slices/errsm13_t2.nii.gz' fname_centerline = '/home/django/jtouati/data/cover_z_slices/segmentation_centerline_binary.nii.gz' remove_temp_files = 0 centerline_fitting = 'splines' import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D verbose = 2 # Check input param try: opts, args = getopt.getopt(sys.argv[1:], 'hi:c:r:w:f:v:') except getopt.GetoptError as err: print str(err) usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ('-i'): fname_anat = arg elif opt in ('-c'): fname_centerline = arg elif opt in ('-r'): remove_temp_files = int(arg) elif opt in ('-w'): interpolation_warp = str(arg) elif opt in ('-f'): centerline_fitting = str(arg) elif opt in ('-v'): verbose = int(arg) # display usage if a mandatory argument is not provided if fname_anat == '' or fname_centerline == '': usage() # Display usage if optional arguments are not correctly provided if centerline_fitting == '': centerline_fitting = 'splines' elif not centerline_fitting == '' and not centerline_fitting == 'splines' and not centerline_fitting == 'polynomial': print '\n \n -f argument is not valid \n \n' usage() # check existence of input files sct.check_file_exist(fname_anat) sct.check_file_exist(fname_centerline) # check interp method if interpolation_warp == 'spline': interpolation_warp_ants = '--use-BSpline' elif interpolation_warp == 'trilinear': interpolation_warp_ants = '' elif interpolation_warp == 'nearestneighbor': interpolation_warp_ants = '--use-NN' else: print '\WARNING: Interpolation method not recognized. Using: ' + param.interpolation_warp interpolation_warp_ants = '--use-BSpline' # Display arguments print '\nCheck input arguments...' print ' Input volume ...................... ' + fname_anat print ' Centerline ........................ ' + fname_centerline print ' Centerline fitting option ......... ' + centerline_fitting print ' Final interpolation ............... ' + interpolation_warp print ' Verbose ........................... ' + str(verbose) print '' # if verbose 2, import matplotlib if verbose == 2: import matplotlib.pyplot as plt # Extract path/file/extension path_anat, file_anat, ext_anat = sct.extract_fname(fname_anat) path_centerline, file_centerline, ext_centerline = sct.extract_fname( fname_centerline) # create temporary folder path_tmp = 'tmp.' + time.strftime("%y%m%d%H%M%S") sct.run('mkdir ' + path_tmp) # copy files into tmp folder sct.run('cp ' + fname_anat + ' ' + path_tmp) sct.run('cp ' + fname_centerline + ' ' + path_tmp) # go to tmp folder os.chdir(path_tmp) # Open centerline #========================================================================================== # Change orientation of the input centerline into RPI print '\nOrient centerline to RPI orientation...' fname_centerline_orient = 'tmp.centerline_rpi' + ext_centerline sct.run('sct_orientation -i ' + file_centerline + ext_centerline + ' -o ' + fname_centerline_orient + ' -orientation RPI') print '\nGet dimensions of input centerline...' nx, ny, nz, nt, px, py, pz, pt = sct.get_dimension(fname_centerline_orient) print '.. matrix size: ' + str(nx) + ' x ' + str(ny) + ' x ' + str(nz) print '.. voxel size: ' + str(px) + 'mm x ' + str(py) + 'mm x ' + str( pz) + 'mm' print '\nOpen centerline volume...' file = nibabel.load(fname_centerline_orient) data = file.get_data() # loop across z and associate x,y coordinate with the point having maximum intensity x_centerline = [0 for iz in range(0, nz, 1)] y_centerline = [0 for iz in range(0, nz, 1)] z_centerline = [iz for iz in range(0, nz, 1)] x_centerline_deriv = [0 for iz in range(0, nz, 1)] y_centerline_deriv = [0 for iz in range(0, nz, 1)] z_centerline_deriv = [0 for iz in range(0, nz, 1)] # Two possible scenario: # 1. the centerline is probabilistic: each slice contains voxels with the probability of containing the centerline [0:...:1] # We only take the maximum value of the image to aproximate the centerline. # 2. The centerline/segmentation image contains many pixels per slice with values {0,1}. # We take all the points and approximate the centerline on all these points. # # x_seg_start, y_seg_start = (data[:,:,0]>0).nonzero() # x_seg_end, y_seg_end = (data[:,:,-1]>0).nonzero() # REMOVED: 2014-07-18 # check if centerline covers all the image # if len(x_seg_start)==0 or len(x_seg_end)==0: # print '\nERROR: centerline/segmentation must cover all "z" slices of the input image.\n' \ # 'To solve the problem, you need to crop the input image (you can use \'sct_crop_image\') and generate one' \ # 'more time the spinal cord centerline/segmentation from this cropped image.\n' # usage() # # X, Y, Z = ((data<1)*(data>0)).nonzero() # X is empty if binary image # if (len(X) > 0): # Scenario 1 # for iz in range(0, nz, 1): # x_centerline[iz], y_centerline[iz] = numpy.unravel_index(data[:,:,iz].argmax(), data[:,:,iz].shape) # else: # Scenario 2 # for iz in range(0, nz, 1): # print (data[:,:,iz]>0).nonzero() # x_seg, y_seg = (data[:,:,iz]>0).nonzero() # x_centerline[iz] = numpy.mean(x_seg) # y_centerline[iz] = numpy.mean(y_seg) # # TODO: find a way to do the previous loop with this, which is more neat: # # [numpy.unravel_index(data[:,:,iz].argmax(), data[:,:,iz].shape) for iz in range(0,nz,1)] # get center of mass of the centerline/segmentation print '\nGet center of mass of the centerline/segmentation...' for iz in range(0, nz, 1): x_centerline[iz], y_centerline[ iz] = ndimage.measurements.center_of_mass( numpy.array(data[:, :, iz])) #print len(x_centerline),len(y_centerline) #print len((numpy.array(x_centerline)>=0).nonzero()[0]),len((numpy.array(y_centerline)>=0).nonzero()[0]) x_seg_start, y_seg_start = (data[:, :, 0] > 0).nonzero() x_seg_end, y_seg_end = (data[:, :, -1] > 0).nonzero() #check if centerline covers all the image if len(x_seg_start) == 0 or len(x_seg_end) == 0: sct.printv( '\nWARNING : the centerline/segmentation you gave does not cover all "z" slices of the input image. Results should be improved if you crop the input image (you can use \'sct_crop_image\') and generate a new spinalcord centerline/segmentation from this cropped image.\n', 1, 'warning') # print '\nWARNING : the centerline/segmentation you gave does not cover all "z" slices of the input image.\n' \ # 'Results should be improved if you crop the input image (you can use \'sct_crop_image\') and generate\n'\ # 'a new spinalcord centerline/segmentation from this cropped image.\n' #print len((numpy.array(x_centerline)>=0).nonzero()[0]),len((numpy.array(y_centerline)>=0).nonzero()[0]) min_centerline = min((numpy.array(x_centerline) >= 0).nonzero()[0]) max_centerline = max((numpy.array(x_centerline) >= 0).nonzero()[0]) z_centerline = z_centerline[(min_centerline):(max_centerline + 1)] #print len(z_centerline) nz = len(z_centerline) x_centerline = [x for x in x_centerline if not isnan(x)] y_centerline = [y for y in y_centerline if not isnan(y)] #print len(x_centerline),len(y_centerline) # clear variable del data # Fit the centerline points with the kind of curve given as argument of the script and return the new fitted coordinates if centerline_fitting == 'splines': x_centerline_fit, y_centerline_fit, x_centerline_deriv, y_centerline_deriv, z_centerline_deriv = msct_smooth.b_spline_nurbs( x_centerline, y_centerline, z_centerline) #x_centerline_fit, y_centerline_fit, x_centerline_deriv, y_centerline_deriv, z_centerline_deriv = b_spline_centerline(x_centerline,y_centerline,z_centerline) elif centerline_fitting == 'polynomial': x_centerline_fit, y_centerline_fit, polyx, polyy = polynome_centerline( x_centerline, y_centerline, z_centerline) #numpy.interp([i for i in xrange(0,min_centerline+1)], #y_centerline_fit #print z_centerline if verbose == 2: # plot centerline ax = plt.subplot(1, 2, 1) plt.plot(x_centerline, z_centerline, 'b:', label='centerline') plt.plot(x_centerline_fit, z_centerline, 'r-', label='fit') plt.xlabel('x') plt.ylabel('z') ax = plt.subplot(1, 2, 2) plt.plot(y_centerline, z_centerline, 'b:', label='centerline') plt.plot(y_centerline_fit, z_centerline, 'r-', label='fit') plt.xlabel('y') plt.ylabel('z') handles, labels = ax.get_legend_handles_labels() ax.legend(handles, labels) plt.show() # Get coordinates of landmarks along curved centerline #========================================================================================== print '\nGet coordinates of landmarks along curved centerline...' # landmarks are created along the curved centerline every z=gapz. They consist of a "cross" of size gapx and gapy. # find derivative of polynomial step_z = round(nz / gapz) #iz_curved = [i for i in range (0, nz, gapz)] iz_curved = [(min(z_centerline) + i * step_z) for i in range(0, gapz)] iz_curved.append(max(z_centerline)) #print iz_curved, len(iz_curved) n_iz_curved = len(iz_curved) #print n_iz_curved landmark_curved = [[[0 for i in range(0, 3)] for i in range(0, 5)] for i in iz_curved] # print x_centerline_deriv,len(x_centerline_deriv) # landmark[a][b][c] # a: index along z. E.g., the first cross with have index=0, the next index=1, and so on... # b: index of element on the cross. I.e., 0: center of the cross, 1: +x, 2 -x, 3: +y, 4: -y # c: dimension, i.e., 0: x, 1: y, 2: z # loop across index, which corresponds to iz (points along the centerline) if centerline_fitting == 'polynomial': for index in range(0, n_iz_curved, 1): # set coordinates for landmark at the center of the cross landmark_curved[index][0][0], landmark_curved[index][0][ 1], landmark_curved[index][0][2] = x_centerline_fit[ iz_curved[index]], y_centerline_fit[ iz_curved[index]], iz_curved[index] # set x and z coordinates for landmarks +x and -x landmark_curved[index][1][2], landmark_curved[index][1][ 0], landmark_curved[index][2][2], landmark_curved[index][2][ 0] = get_points_perpendicular_to_curve( polyx, polyx.deriv(), iz_curved[index], gapxy) # set y coordinate to y_centerline_fit[iz] for elements 1 and 2 of the cross for i in range(1, 3): landmark_curved[index][i][1] = y_centerline_fit[ iz_curved[index]] # set coordinates for landmarks +y and -y. Here, x coordinate is 0 (already initialized). landmark_curved[index][3][2], landmark_curved[index][3][ 1], landmark_curved[index][4][2], landmark_curved[index][4][ 1] = get_points_perpendicular_to_curve( polyy, polyy.deriv(), iz_curved[index], gapxy) # set x coordinate to x_centerline_fit[iz] for elements 3 and 4 of the cross for i in range(3, 5): landmark_curved[index][i][0] = x_centerline_fit[ iz_curved[index]] elif centerline_fitting == 'splines': for index in range(0, n_iz_curved, 1): # calculate d (ax+by+cz+d=0) # print iz_curved[index] a = x_centerline_deriv[iz_curved[index] - min(z_centerline)] b = y_centerline_deriv[iz_curved[index] - min(z_centerline)] c = z_centerline_deriv[iz_curved[index] - min(z_centerline)] x = x_centerline_fit[iz_curved[index] - min(z_centerline)] y = y_centerline_fit[iz_curved[index] - min(z_centerline)] z = iz_curved[index] d = -(a * x + b * y + c * z) #print a,b,c,d,x,y,z # set coordinates for landmark at the center of the cross landmark_curved[index][0][0], landmark_curved[index][0][ 1], landmark_curved[index][0][2] = x_centerline_fit[ iz_curved[index] - min(z_centerline)], y_centerline_fit[ iz_curved[index] - min(z_centerline)], iz_curved[index] # set y coordinate to y_centerline_fit[iz] for elements 1 and 2 of the cross for i in range(1, 3): landmark_curved[index][i][1] = y_centerline_fit[ iz_curved[index] - min(z_centerline)] # set x and z coordinates for landmarks +x and -x, forcing de landmark to be in the orthogonal plan and the distance landmark/curve to be gapxy x_n = Symbol('x_n') landmark_curved[index][2][0], landmark_curved[index][1][0] = solve( (x_n - x)**2 + ((-1 / c) * (a * x_n + b * y + d) - z)**2 - gapxy**2, x_n) #x for -x and +x landmark_curved[index][1][2] = (-1 / c) * ( a * landmark_curved[index][1][0] + b * y + d) #z for +x landmark_curved[index][2][2] = (-1 / c) * ( a * landmark_curved[index][2][0] + b * y + d) #z for -x # set x coordinate to x_centerline_fit[iz] for elements 3 and 4 of the cross for i in range(3, 5): landmark_curved[index][i][0] = x_centerline_fit[ iz_curved[index] - min(z_centerline)] # set coordinates for landmarks +y and -y. Here, x coordinate is 0 (already initialized). y_n = Symbol('y_n') landmark_curved[index][4][1], landmark_curved[index][3][1] = solve( (y_n - y)**2 + ((-1 / c) * (a * x + b * y_n + d) - z)**2 - gapxy**2, y_n) #y for -y and +y landmark_curved[index][3][2] = (-1 / c) * ( a * x + b * landmark_curved[index][3][1] + d) #z for +y landmark_curved[index][4][2] = (-1 / c) * ( a * x + b * landmark_curved[index][4][1] + d) #z for -y # #display # fig = plt.figure() # ax = fig.add_subplot(111, projection='3d') # ax.plot(x_centerline_fit, y_centerline_fit,z_centerline, 'g') # ax.plot(x_centerline, y_centerline,z_centerline, 'r') # ax.plot([landmark_curved[i][j][0] for i in range(0, n_iz_curved) for j in range(0, 5)], \ # [landmark_curved[i][j][1] for i in range(0, n_iz_curved) for j in range(0, 5)], \ # [landmark_curved[i][j][2] for i in range(0, n_iz_curved) for j in range(0, 5)], '.') # ax.set_xlabel('x') # ax.set_ylabel('y') # ax.set_zlabel('z') # plt.show() # Get coordinates of landmarks along straight centerline #========================================================================================== print '\nGet coordinates of landmarks along straight centerline...' landmark_straight = [[[0 for i in range(0, 3)] for i in range(0, 5)] for i in iz_curved ] # same structure as landmark_curved # calculate the z indices corresponding to the Euclidean distance between two consecutive points on the curved centerline (approximation curve --> line) iz_straight = [(min(z_centerline) + 0) for i in range(0, gapz + 1)] #print iz_straight,len(iz_straight) for index in range(1, n_iz_curved, 1): # compute vector between two consecutive points on the curved centerline vector_centerline = [x_centerline_fit[iz_curved[index]-min(z_centerline)] - x_centerline_fit[iz_curved[index-1]-min(z_centerline)], \ y_centerline_fit[iz_curved[index]-min(z_centerline)] - y_centerline_fit[iz_curved[index-1]-min(z_centerline)], \ iz_curved[index] - iz_curved[index-1]] # compute norm of this vector norm_vector_centerline = numpy.linalg.norm(vector_centerline, ord=2) # round to closest integer value norm_vector_centerline_rounded = int(round(norm_vector_centerline, 0)) # assign this value to the current z-coordinate on the straight centerline iz_straight[index] = iz_straight[index - 1] + norm_vector_centerline_rounded # initialize x0 and y0 to be at the center of the FOV x0 = int(round(nx / 2)) y0 = int(round(ny / 2)) for index in range(0, n_iz_curved, 1): # set coordinates for landmark at the center of the cross landmark_straight[index][0][0], landmark_straight[index][0][ 1], landmark_straight[index][0][2] = x0, y0, iz_straight[index] # set x, y and z coordinates for landmarks +x landmark_straight[index][1][0], landmark_straight[index][1][ 1], landmark_straight[index][1][2] = x0 + gapxy, y0, iz_straight[ index] # set x, y and z coordinates for landmarks -x landmark_straight[index][2][0], landmark_straight[index][2][ 1], landmark_straight[index][2][2] = x0 - gapxy, y0, iz_straight[ index] # set x, y and z coordinates for landmarks +y landmark_straight[index][3][0], landmark_straight[index][3][ 1], landmark_straight[index][3][2] = x0, y0 + gapxy, iz_straight[ index] # set x, y and z coordinates for landmarks -y landmark_straight[index][4][0], landmark_straight[index][4][ 1], landmark_straight[index][4][2] = x0, y0 - gapxy, iz_straight[ index] # # display # fig = plt.figure() # ax = fig.add_subplot(111, projection='3d') # #ax.plot(x_centerline_fit, y_centerline_fit,z_centerline, 'r') # ax.plot([landmark_straight[i][j][0] for i in range(0, n_iz_curved) for j in range(0, 5)], \ # [landmark_straight[i][j][1] for i in range(0, n_iz_curved) for j in range(0, 5)], \ # [landmark_straight[i][j][2] for i in range(0, n_iz_curved) for j in range(0, 5)], '.') # ax.set_xlabel('x') # ax.set_ylabel('y') # ax.set_zlabel('z') # plt.show() # # Create NIFTI volumes with landmarks #========================================================================================== # Pad input volume to deal with the fact that some landmarks on the curved centerline might be outside the FOV # N.B. IT IS VERY IMPORTANT TO PAD ALSO ALONG X and Y, OTHERWISE SOME LANDMARKS MIGHT GET OUT OF THE FOV!!! print '\nPad input volume to deal with the fact that some landmarks on the curved centerline might be outside the FOV...' sct.run('isct_c3d ' + fname_centerline_orient + ' -pad ' + str(padding) + 'x' + str(padding) + 'x' + str(padding) + 'vox ' + str(padding) + 'x' + str(padding) + 'x' + str(padding) + 'vox 0 -o tmp.centerline_pad.nii.gz') # TODO: don't pad input volume: no need for that! instead, try to increase size of hdr when saving landmarks. # Open padded centerline for reading print '\nOpen padded centerline for reading...' file = nibabel.load('tmp.centerline_pad.nii.gz') data = file.get_data() hdr = file.get_header() # Create volumes containing curved and straight landmarks data_curved_landmarks = data * 0 data_straight_landmarks = data * 0 # initialize landmark value landmark_value = 1 # Loop across cross index for index in range(0, n_iz_curved, 1): # loop across cross element index for i_element in range(0, 5, 1): # get x, y and z coordinates of curved landmark (rounded to closest integer) x, y, z = int(round(landmark_curved[index][i_element][0])), int( round(landmark_curved[index][i_element][1])), int( round(landmark_curved[index][i_element][2])) # attribute landmark_value to the voxel and its neighbours data_curved_landmarks[x + padding - 1:x + padding + 2, y + padding - 1:y + padding + 2, z + padding - 1:z + padding + 2] = landmark_value # get x, y and z coordinates of straight landmark (rounded to closest integer) x, y, z = int(round(landmark_straight[index][i_element][0])), int( round(landmark_straight[index][i_element][1])), int( round(landmark_straight[index][i_element][2])) # attribute landmark_value to the voxel and its neighbours data_straight_landmarks[x + padding - 1:x + padding + 2, y + padding - 1:y + padding + 2, z + padding - 1:z + padding + 2] = landmark_value # increment landmark value landmark_value = landmark_value + 1 # Write NIFTI volumes hdr.set_data_dtype( 'uint32') # set imagetype to uint8 #TODO: maybe use int32 print '\nWrite NIFTI volumes...' img = nibabel.Nifti1Image(data_curved_landmarks, None, hdr) nibabel.save(img, 'tmp.landmarks_curved.nii.gz') print '.. File created: tmp.landmarks_curved.nii.gz' img = nibabel.Nifti1Image(data_straight_landmarks, None, hdr) nibabel.save(img, 'tmp.landmarks_straight.nii.gz') print '.. File created: tmp.landmarks_straight.nii.gz' # Estimate deformation field by pairing landmarks #========================================================================================== # Dilate landmarks (because nearest neighbour interpolation will be later used, therefore some landmarks may "disapear" if they are single points) #print '\nDilate landmarks...' #sct.run(fsloutput+'fslmaths tmp.landmarks_curved.nii -kernel box 3x3x3 -dilD tmp.landmarks_curved_dilated -odt short') #sct.run(fsloutput+'fslmaths tmp.landmarks_straight.nii -kernel box 3x3x3 -dilD tmp.landmarks_straight_dilated -odt short') # Estimate rigid transformation print '\nEstimate rigid transformation between paired landmarks...' sct.run( 'isct_ANTSUseLandmarkImagesToGetAffineTransform tmp.landmarks_straight.nii.gz tmp.landmarks_curved.nii.gz rigid tmp.curve2straight_rigid.txt' ) # Apply rigid transformation print '\nApply rigid transformation to curved landmarks...' sct.run( 'sct_WarpImageMultiTransform 3 tmp.landmarks_curved.nii.gz tmp.landmarks_curved_rigid.nii.gz -R tmp.landmarks_straight.nii.gz tmp.curve2straight_rigid.txt --use-NN' ) # Estimate b-spline transformation curve --> straight print '\nEstimate b-spline transformation: curve --> straight...' sct.run( 'isct_ANTSUseLandmarkImagesToGetBSplineDisplacementField tmp.landmarks_straight.nii.gz tmp.landmarks_curved_rigid.nii.gz tmp.warp_curve2straight.nii.gz 5x5x5 3 2 0' ) # Concatenate rigid and non-linear transformations... print '\nConcatenate rigid and non-linear transformations...' #sct.run('isct_ComposeMultiTransform 3 tmp.warp_rigid.nii -R tmp.landmarks_straight.nii tmp.warp.nii tmp.curve2straight_rigid.txt') # TODO: use sct.run() when output from the following command will be different from 0 (currently there seem to be a bug) cmd = 'isct_ComposeMultiTransform 3 tmp.curve2straight.nii.gz -R tmp.landmarks_straight.nii.gz tmp.warp_curve2straight.nii.gz tmp.curve2straight_rigid.txt' print('>> ' + cmd) commands.getstatusoutput(cmd) # Estimate b-spline transformation straight --> curve # TODO: invert warping field instead of estimating a new one print '\nEstimate b-spline transformation: straight --> curve...' sct.run( 'isct_ANTSUseLandmarkImagesToGetBSplineDisplacementField tmp.landmarks_curved_rigid.nii.gz tmp.landmarks_straight.nii.gz tmp.warp_straight2curve.nii.gz 5x5x5 3 2 0' ) # Concatenate rigid and non-linear transformations... print '\nConcatenate rigid and non-linear transformations...' #sct.run('isct_ComposeMultiTransform 3 tmp.warp_rigid.nii -R tmp.landmarks_straight.nii tmp.warp.nii tmp.curve2straight_rigid.txt') # TODO: use sct.run() when output from the following command will be different from 0 (currently there seem to be a bug) cmd = 'isct_ComposeMultiTransform 3 tmp.straight2curve.nii.gz -R tmp.landmarks_straight.nii.gz -i tmp.curve2straight_rigid.txt tmp.warp_straight2curve.nii.gz' print('>> ' + cmd) commands.getstatusoutput(cmd) #print '\nPad input image...' #sct.run('isct_c3d '+fname_anat+' -pad '+str(padz)+'x'+str(padz)+'x'+str(padz)+'vox '+str(padz)+'x'+str(padz)+'x'+str(padz)+'vox 0 -o tmp.anat_pad.nii') # Unpad landmarks... # THIS WAS REMOVED ON 2014-06-03 because the output data was cropped at the edge, which caused landmarks to sometimes disappear # print '\nUnpad landmarks...' # sct.run('fslroi tmp.landmarks_straight.nii.gz tmp.landmarks_straight_crop.nii.gz '+str(padding)+' '+str(nx)+' '+str(padding)+' '+str(ny)+' '+str(padding)+' '+str(nz)) # Apply deformation to input image print '\nApply transformation to input image...' sct.run('sct_WarpImageMultiTransform 3 ' + file_anat + ext_anat + ' tmp.anat_rigid_warp.nii.gz -R tmp.landmarks_straight.nii.gz ' + interpolation_warp + ' tmp.curve2straight.nii.gz') # sct.run('sct_WarpImageMultiTransform 3 '+fname_anat+' tmp.anat_rigid_warp.nii.gz -R tmp.landmarks_straight_crop.nii.gz '+interpolation_warp+ ' tmp.curve2straight.nii.gz') # come back to parent folder os.chdir('..') # Generate output file (in current folder) # TODO: do not uncompress the warping field, it is too time consuming! print '\nGenerate output file (in current folder)...' sct.generate_output_file(path_tmp + '/tmp.curve2straight.nii.gz', '', 'warp_curve2straight', '.nii.gz') # warping field sct.generate_output_file(path_tmp + '/tmp.straight2curve.nii.gz', '', 'warp_straight2curve', '.nii.gz') # warping field sct.generate_output_file(path_tmp + '/tmp.anat_rigid_warp.nii.gz', '', file_anat + '_straight', ext_anat) # straightened anatomic # Remove temporary files if remove_temp_files == 1: print('\nRemove temporary files...') sct.run('rm -rf ' + path_tmp) print '\nDone!\n'
def main(): # Initialization fname_anat = '' fname_centerline = '' sigma = 3 # default value of the standard deviation for the Gaussian smoothing (in terms of number of voxels) remove_temp_files = param.remove_temp_files verbose = param.verbose start_time = time.time() # Check input param try: opts, args = getopt.getopt(sys.argv[1:], 'hi:c:r:s:v:') except getopt.GetoptError as err: print str(err) usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ('-c'): fname_centerline = arg elif opt in ('-i'): fname_anat = arg elif opt in ('-r'): remove_temp_files = arg elif opt in ('-s'): sigma = arg elif opt in ('-v'): verbose = int(arg) # Display usage if a mandatory argument is not provided if fname_anat == '' or fname_centerline == '': usage() # Display arguments print '\nCheck input arguments...' print ' Volume to smooth .................. ' + fname_anat print ' Centerline ........................ ' + fname_centerline print ' FWHM .............................. '+str(sigma) print ' Verbose ........................... '+str(verbose) # Check existence of input files print('\nCheck existence of input files...') sct.check_file_exist(fname_anat) sct.check_file_exist(fname_centerline) # Extract path/file/extension path_anat, file_anat, ext_anat = sct.extract_fname(fname_anat) path_centerline, file_centerline, ext_centerline = sct.extract_fname(fname_centerline) # create temporary folder print('\nCreate temporary folder...') path_tmp = 'tmp.'+time.strftime("%y%m%d%H%M%S") sct.run('mkdir '+path_tmp) # copy files to temporary folder print('\nCopy files...') sct.run('c3d '+fname_anat+' -o '+path_tmp+'/anat.nii') sct.run('c3d '+fname_centerline+' -o '+path_tmp+'/centerline.nii') # go to tmp folder os.chdir(path_tmp) # Change orientation of the input image into RPI print '\nOrient input volume to RPI orientation...' sct.run('sct_orientation -i anat.nii -o anat_rpi.nii -orientation RPI') # Change orientation of the input image into RPI print '\nOrient centerline to RPI orientation...' sct.run('sct_orientation -i centerline.nii -o centerline_rpi.nii -orientation RPI') # Straighten the spinal cord print '\nStraighten the spinal cord...' sct.run('sct_straighten_spinalcord.py -i anat_rpi.nii -c centerline_rpi.nii -w spline -v '+str(verbose)) # Smooth the straightened image along z print '\nSmooth the straightened image along z...' sct.run('c3d anat_rpi_straight.nii -smooth 0x0x'+str(sigma)+'vox -o anat_rpi_straight_smooth.nii') # Apply the reversed warping field to get back the curved spinal cord print '\nApply the reversed warping field to get back the curved spinal cord (assuming a 3D image)...' sct.run('WarpImageMultiTransform 3 anat_rpi_straight_smooth.nii anat_rpi_straight_smooth_curved.nii -R anat.nii --use-BSpline warp_straight2curve.nii.gz') # come back to parent folder os.chdir('..') # Generate output file print '\nGenerate output file...' sct.generate_output_file(path_tmp+'/anat_rpi_straight_smooth_curved.nii','',file_anat+'_smooth',ext_anat) # Remove temporary files if remove_temp_files == 1: print('\nRemove temporary files...') sct.run('rm -rf '+path_tmp) #Display elapsed time elapsed_time = time.time() - start_time print '\nFinished! Elapsed time: '+str(int(round(elapsed_time)))+'s\n' # to view results print 'To view results, type:' print 'fslview '+file_anat+' '+file_anat+'_smooth &\n'
def main(): parser = get_parser() param = Param() arguments = parser.parse(sys.argv[1:]) # get arguments fname_data = arguments['-i'] fname_seg = arguments['-s'] fname_landmarks = arguments['-l'] if '-ofolder' in arguments: path_output = arguments['-ofolder'] else: path_output = '' path_template = sct.slash_at_the_end(arguments['-t'], 1) contrast_template = arguments['-c'] remove_temp_files = int(arguments['-r']) verbose = int(arguments['-v']) if '-param-straighten' in arguments: param.param_straighten = arguments['-param-straighten'] if 'cpu-nb' in arguments: arg_cpu = ' -cpu-nb '+arguments['-cpu-nb'] else: arg_cpu = '' if '-param' in arguments: paramreg_user = arguments['-param'] # update registration parameters for paramStep in paramreg_user: paramreg.addStep(paramStep) # initialize other parameters file_template_label = param.file_template_label output_type = param.output_type zsubsample = param.zsubsample # smoothing_sigma = param.smoothing_sigma # capitalize letters for contrast if contrast_template == 't1': contrast_template = 'T1' elif contrast_template == 't2': contrast_template = 'T2' # retrieve file_template based on contrast fname_template_list = glob(path_template+param.folder_template+'*'+contrast_template+'.nii.gz') # TODO: make sure there is only one file -- check if file is there otherwise it crashes fname_template = fname_template_list[0] # retrieve file_template_seg fname_template_seg_list = glob(path_template+param.folder_template+'*cord.nii.gz') # TODO: make sure there is only one file fname_template_seg = fname_template_seg_list[0] # start timer start_time = time.time() # get absolute path - TO DO: remove! NEVER USE ABSOLUTE PATH... path_template = os.path.abspath(path_template+param.folder_template) # get fname of the template + template objects # fname_template = sct.slash_at_the_end(path_template, 1)+file_template fname_template_label = sct.slash_at_the_end(path_template, 1)+file_template_label # fname_template_seg = sct.slash_at_the_end(path_template, 1)+file_template_seg # check file existence sct.printv('\nCheck template files...') sct.check_file_exist(fname_template, verbose) sct.check_file_exist(fname_template_label, verbose) sct.check_file_exist(fname_template_seg, verbose) # print arguments sct.printv('\nCheck parameters:', verbose) sct.printv('.. Data: '+fname_data, verbose) sct.printv('.. Landmarks: '+fname_landmarks, verbose) sct.printv('.. Segmentation: '+fname_seg, verbose) sct.printv('.. Path template: '+path_template, verbose) sct.printv('.. Path output: '+path_output, verbose) sct.printv('.. Output type: '+str(output_type), verbose) sct.printv('.. Remove temp files: '+str(remove_temp_files), verbose) sct.printv('\nParameters for registration:') for pStep in range(1, len(paramreg.steps)+1): sct.printv('Step #'+paramreg.steps[str(pStep)].step, verbose) sct.printv('.. Type #'+paramreg.steps[str(pStep)].type, verbose) sct.printv('.. Algorithm................ '+paramreg.steps[str(pStep)].algo, verbose) sct.printv('.. Metric................... '+paramreg.steps[str(pStep)].metric, verbose) sct.printv('.. Number of iterations..... '+paramreg.steps[str(pStep)].iter, verbose) sct.printv('.. Shrink factor............ '+paramreg.steps[str(pStep)].shrink, verbose) sct.printv('.. Smoothing factor......... '+paramreg.steps[str(pStep)].smooth, verbose) sct.printv('.. Gradient step............ '+paramreg.steps[str(pStep)].gradStep, verbose) sct.printv('.. Degree of polynomial..... '+paramreg.steps[str(pStep)].poly, verbose) path_data, file_data, ext_data = sct.extract_fname(fname_data) sct.printv('\nCheck input labels...') # check if label image contains coherent labels image_label = Image(fname_landmarks) # -> all labels must be different labels = image_label.getNonZeroCoordinates(sorting='value') hasDifferentLabels = True for lab in labels: for otherlabel in labels: if lab != otherlabel and lab.hasEqualValue(otherlabel): hasDifferentLabels = False break if not hasDifferentLabels: sct.printv('ERROR: Wrong landmarks input. All labels must be different.', verbose, 'error') # all labels must be available in tempalte image_label_template = Image(fname_template_label) labels_template = image_label_template.getNonZeroCoordinates(sorting='value') if labels[-1].value > labels_template[-1].value: sct.printv('ERROR: Wrong landmarks input. Labels must have correspondence in template space. \nLabel max ' 'provided: ' + str(labels[-1].value) + '\nLabel max from template: ' + str(labels_template[-1].value), verbose, 'error') # create temporary folder path_tmp = sct.tmp_create(verbose=verbose) # set temporary file names ftmp_data = 'data.nii' ftmp_seg = 'seg.nii.gz' ftmp_label = 'label.nii.gz' ftmp_template = 'template.nii' ftmp_template_seg = 'template_seg.nii.gz' ftmp_template_label = 'template_label.nii.gz' # copy files to temporary folder sct.printv('\nCopying input data to tmp folder and convert to nii...', verbose) sct.run('sct_convert -i '+fname_data+' -o '+path_tmp+ftmp_data) sct.run('sct_convert -i '+fname_seg+' -o '+path_tmp+ftmp_seg) sct.run('sct_convert -i '+fname_landmarks+' -o '+path_tmp+ftmp_label) sct.run('sct_convert -i '+fname_template+' -o '+path_tmp+ftmp_template) sct.run('sct_convert -i '+fname_template_seg+' -o '+path_tmp+ftmp_template_seg) sct.run('sct_convert -i '+fname_template_label+' -o '+path_tmp+ftmp_template_label) # go to tmp folder os.chdir(path_tmp) # smooth segmentation (jcohenadad, issue #613) sct.printv('\nSmooth segmentation...', verbose) sct.run('sct_maths -i '+ftmp_seg+' -smooth 1.5 -o '+add_suffix(ftmp_seg, '_smooth')) ftmp_seg = add_suffix(ftmp_seg, '_smooth') # resample data to 1mm isotropic sct.printv('\nResample data to 1mm isotropic...', verbose) sct.run('sct_resample -i '+ftmp_data+' -mm 1.0x1.0x1.0 -x linear -o '+add_suffix(ftmp_data, '_1mm')) ftmp_data = add_suffix(ftmp_data, '_1mm') sct.run('sct_resample -i '+ftmp_seg+' -mm 1.0x1.0x1.0 -x linear -o '+add_suffix(ftmp_seg, '_1mm')) ftmp_seg = add_suffix(ftmp_seg, '_1mm') # N.B. resampling of labels is more complicated, because they are single-point labels, therefore resampling with neighrest neighbour can make them disappear. Therefore a more clever approach is required. resample_labels(ftmp_label, ftmp_data, add_suffix(ftmp_label, '_1mm')) ftmp_label = add_suffix(ftmp_label, '_1mm') # Change orientation of input images to RPI sct.printv('\nChange orientation of input images to RPI...', verbose) sct.run('sct_image -i '+ftmp_data+' -setorient RPI -o '+add_suffix(ftmp_data, '_rpi')) ftmp_data = add_suffix(ftmp_data, '_rpi') sct.run('sct_image -i '+ftmp_seg+' -setorient RPI -o '+add_suffix(ftmp_seg, '_rpi')) ftmp_seg = add_suffix(ftmp_seg, '_rpi') sct.run('sct_image -i '+ftmp_label+' -setorient RPI -o '+add_suffix(ftmp_label, '_rpi')) ftmp_label = add_suffix(ftmp_label, '_rpi') # get landmarks in native space # crop segmentation # output: segmentation_rpi_crop.nii.gz status_crop, output_crop = sct.run('sct_crop_image -i '+ftmp_seg+' -o '+add_suffix(ftmp_seg, '_crop')+' -dim 2 -bzmax', verbose) ftmp_seg = add_suffix(ftmp_seg, '_crop') cropping_slices = output_crop.split('Dimension 2: ')[1].split('\n')[0].split(' ') # straighten segmentation sct.printv('\nStraighten the spinal cord using centerline/segmentation...', verbose) sct.run('sct_straighten_spinalcord -i '+ftmp_seg+' -s '+ftmp_seg+' -o '+add_suffix(ftmp_seg, '_straight')+' -qc 0 -r 0 -v '+str(verbose)+' '+param.param_straighten+arg_cpu, verbose) # N.B. DO NOT UPDATE VARIABLE ftmp_seg BECAUSE TEMPORARY USED LATER # re-define warping field using non-cropped space (to avoid issue #367) sct.run('sct_concat_transfo -w warp_straight2curve.nii.gz -d '+ftmp_data+' -o warp_straight2curve.nii.gz') # Label preparation: # -------------------------------------------------------------------------------- # Remove unused label on template. Keep only label present in the input label image sct.printv('\nRemove unused label on template. Keep only label present in the input label image...', verbose) sct.run('sct_label_utils -p remove -i '+ftmp_template_label+' -o '+ftmp_template_label+' -r '+ftmp_label) # Dilating the input label so they can be straighten without losing them sct.printv('\nDilating input labels using 3vox ball radius') sct.run('sct_maths -i '+ftmp_label+' -o '+add_suffix(ftmp_label, '_dilate')+' -dilate 3') ftmp_label = add_suffix(ftmp_label, '_dilate') # Apply straightening to labels sct.printv('\nApply straightening to labels...', verbose) sct.run('sct_apply_transfo -i '+ftmp_label+' -o '+add_suffix(ftmp_label, '_straight')+' -d '+add_suffix(ftmp_seg, '_straight')+' -w warp_curve2straight.nii.gz -x nn') ftmp_label = add_suffix(ftmp_label, '_straight') # Create crosses for the template labels and get coordinates sct.printv('\nCreate a 15 mm cross for the template labels...', verbose) template_image = Image(ftmp_template_label) coordinates_input = template_image.getNonZeroCoordinates(sorting='value') # jcohenadad, issue #628 <<<<< # landmark_template = ProcessLabels.get_crosses_coordinates(coordinates_input, gapxy=15) landmark_template = coordinates_input # >>>>> if verbose == 2: # TODO: assign cross to image before saving template_image.setFileName(add_suffix(ftmp_template_label, '_cross')) template_image.save(type='minimize_int') # Create crosses for the input labels into straight space and get coordinates sct.printv('\nCreate a 15 mm cross for the input labels...', verbose) label_straight_image = Image(ftmp_label) coordinates_input = label_straight_image.getCoordinatesAveragedByValue() # landmarks are sorted by value # jcohenadad, issue #628 <<<<< # landmark_straight = ProcessLabels.get_crosses_coordinates(coordinates_input, gapxy=15) landmark_straight = coordinates_input # >>>>> if verbose == 2: # TODO: assign cross to image before saving label_straight_image.setFileName(add_suffix(ftmp_label, '_cross')) label_straight_image.save(type='minimize_int') # Reorganize landmarks points_fixed, points_moving = [], [] for coord in landmark_straight: point_straight = label_straight_image.transfo_pix2phys([[coord.x, coord.y, coord.z]]) points_moving.append([point_straight[0][0], point_straight[0][1], point_straight[0][2]]) for coord in landmark_template: point_template = template_image.transfo_pix2phys([[coord.x, coord.y, coord.z]]) points_fixed.append([point_template[0][0], point_template[0][1], point_template[0][2]]) # Register curved landmarks on straight landmarks based on python implementation sct.printv('\nComputing rigid transformation (algo=translation-scaling-z) ...', verbose) import msct_register_landmarks # for some reason, the moving and fixed points are inverted between ITK transform and our python-based transform. # and for another unknown reason, x and y dimensions have a negative sign (at least for translation and center of rotation). if verbose == 2: show_transfo = True else: show_transfo = False (rotation_matrix, translation_array, points_moving_reg, points_moving_barycenter) = msct_register_landmarks.getRigidTransformFromLandmarks(points_moving, points_fixed, constraints='translation-scaling-z', show=show_transfo) # writing rigid transformation file text_file = open("straight2templateAffine.txt", "w") text_file.write("#Insight Transform File V1.0\n") text_file.write("#Transform 0\n") text_file.write("Transform: AffineTransform_double_3_3\n") text_file.write("Parameters: %.9f %.9f %.9f %.9f %.9f %.9f %.9f %.9f %.9f %.9f %.9f %.9f\n" % ( rotation_matrix[0, 0], rotation_matrix[0, 1], rotation_matrix[0, 2], rotation_matrix[1, 0], rotation_matrix[1, 1], rotation_matrix[1, 2], rotation_matrix[2, 0], rotation_matrix[2, 1], rotation_matrix[2, 2], -translation_array[0, 0], -translation_array[0, 1], translation_array[0, 2])) text_file.write("FixedParameters: %.9f %.9f %.9f\n" % (-points_moving_barycenter[0], -points_moving_barycenter[1], points_moving_barycenter[2])) text_file.close() # Concatenate transformations: curve --> straight --> affine sct.printv('\nConcatenate transformations: curve --> straight --> affine...', verbose) sct.run('sct_concat_transfo -w warp_curve2straight.nii.gz,straight2templateAffine.txt -d template.nii -o warp_curve2straightAffine.nii.gz') # Apply transformation sct.printv('\nApply transformation...', verbose) sct.run('sct_apply_transfo -i '+ftmp_data+' -o '+add_suffix(ftmp_data, '_straightAffine')+' -d '+ftmp_template+' -w warp_curve2straightAffine.nii.gz') ftmp_data = add_suffix(ftmp_data, '_straightAffine') sct.run('sct_apply_transfo -i '+ftmp_seg+' -o '+add_suffix(ftmp_seg, '_straightAffine')+' -d '+ftmp_template+' -w warp_curve2straightAffine.nii.gz -x linear') ftmp_seg = add_suffix(ftmp_seg, '_straightAffine') # threshold and binarize sct.printv('\nBinarize segmentation...', verbose) sct.run('sct_maths -i '+ftmp_seg+' -thr 0.4 -o '+add_suffix(ftmp_seg, '_thr')) sct.run('sct_maths -i '+add_suffix(ftmp_seg, '_thr')+' -bin -o '+add_suffix(ftmp_seg, '_thr_bin')) ftmp_seg = add_suffix(ftmp_seg, '_thr_bin') # find min-max of anat2template (for subsequent cropping) zmin_template, zmax_template = find_zmin_zmax(ftmp_seg) # crop template in z-direction (for faster processing) sct.printv('\nCrop data in template space (for faster processing)...', verbose) sct.run('sct_crop_image -i '+ftmp_template+' -o '+add_suffix(ftmp_template, '_crop')+' -dim 2 -start '+str(zmin_template)+' -end '+str(zmax_template)) ftmp_template = add_suffix(ftmp_template, '_crop') sct.run('sct_crop_image -i '+ftmp_template_seg+' -o '+add_suffix(ftmp_template_seg, '_crop')+' -dim 2 -start '+str(zmin_template)+' -end '+str(zmax_template)) ftmp_template_seg = add_suffix(ftmp_template_seg, '_crop') sct.run('sct_crop_image -i '+ftmp_data+' -o '+add_suffix(ftmp_data, '_crop')+' -dim 2 -start '+str(zmin_template)+' -end '+str(zmax_template)) ftmp_data = add_suffix(ftmp_data, '_crop') sct.run('sct_crop_image -i '+ftmp_seg+' -o '+add_suffix(ftmp_seg, '_crop')+' -dim 2 -start '+str(zmin_template)+' -end '+str(zmax_template)) ftmp_seg = add_suffix(ftmp_seg, '_crop') # sub-sample in z-direction sct.printv('\nSub-sample in z-direction (for faster processing)...', verbose) sct.run('sct_resample -i '+ftmp_template+' -o '+add_suffix(ftmp_template, '_sub')+' -f 1x1x'+zsubsample, verbose) ftmp_template = add_suffix(ftmp_template, '_sub') sct.run('sct_resample -i '+ftmp_template_seg+' -o '+add_suffix(ftmp_template_seg, '_sub')+' -f 1x1x'+zsubsample, verbose) ftmp_template_seg = add_suffix(ftmp_template_seg, '_sub') sct.run('sct_resample -i '+ftmp_data+' -o '+add_suffix(ftmp_data, '_sub')+' -f 1x1x'+zsubsample, verbose) ftmp_data = add_suffix(ftmp_data, '_sub') sct.run('sct_resample -i '+ftmp_seg+' -o '+add_suffix(ftmp_seg, '_sub')+' -f 1x1x'+zsubsample, verbose) ftmp_seg = add_suffix(ftmp_seg, '_sub') # Registration straight spinal cord to template sct.printv('\nRegister straight spinal cord to template...', verbose) # loop across registration steps warp_forward = [] warp_inverse = [] for i_step in range(1, len(paramreg.steps)+1): sct.printv('\nEstimate transformation for step #'+str(i_step)+'...', verbose) # identify which is the src and dest if paramreg.steps[str(i_step)].type == 'im': src = ftmp_data dest = ftmp_template interp_step = 'linear' elif paramreg.steps[str(i_step)].type == 'seg': src = ftmp_seg dest = ftmp_template_seg interp_step = 'nn' else: sct.printv('ERROR: Wrong image type.', 1, 'error') # if step>1, apply warp_forward_concat to the src image to be used if i_step > 1: # sct.run('sct_apply_transfo -i '+src+' -d '+dest+' -w '+','.join(warp_forward)+' -o '+sct.add_suffix(src, '_reg')+' -x '+interp_step, verbose) sct.run('sct_apply_transfo -i '+src+' -d '+dest+' -w '+','.join(warp_forward)+' -o '+add_suffix(src, '_reg')+' -x '+interp_step, verbose) src = add_suffix(src, '_reg') # register src --> dest warp_forward_out, warp_inverse_out = register(src, dest, paramreg, param, str(i_step)) warp_forward.append(warp_forward_out) warp_inverse.append(warp_inverse_out) # Concatenate transformations: sct.printv('\nConcatenate transformations: anat --> template...', verbose) sct.run('sct_concat_transfo -w warp_curve2straightAffine.nii.gz,'+','.join(warp_forward)+' -d template.nii -o warp_anat2template.nii.gz', verbose) # sct.run('sct_concat_transfo -w warp_curve2straight.nii.gz,straight2templateAffine.txt,'+','.join(warp_forward)+' -d template.nii -o warp_anat2template.nii.gz', verbose) sct.printv('\nConcatenate transformations: template --> anat...', verbose) warp_inverse.reverse() sct.run('sct_concat_transfo -w '+','.join(warp_inverse)+',-straight2templateAffine.txt,warp_straight2curve.nii.gz -d data.nii -o warp_template2anat.nii.gz', verbose) # Apply warping fields to anat and template if output_type == 1: sct.run('sct_apply_transfo -i template.nii -o template2anat.nii.gz -d data.nii -w warp_template2anat.nii.gz -crop 1', verbose) sct.run('sct_apply_transfo -i data.nii -o anat2template.nii.gz -d template.nii -w warp_anat2template.nii.gz -crop 1', verbose) # come back to parent folder os.chdir('..') # Generate output files sct.printv('\nGenerate output files...', verbose) sct.generate_output_file(path_tmp+'warp_template2anat.nii.gz', path_output+'warp_template2anat.nii.gz', verbose) sct.generate_output_file(path_tmp+'warp_anat2template.nii.gz', path_output+'warp_anat2template.nii.gz', verbose) if output_type == 1: sct.generate_output_file(path_tmp+'template2anat.nii.gz', path_output+'template2anat'+ext_data, verbose) sct.generate_output_file(path_tmp+'anat2template.nii.gz', path_output+'anat2template'+ext_data, verbose) # Delete temporary files if remove_temp_files: sct.printv('\nDelete temporary files...', verbose) sct.run('rm -rf '+path_tmp) # display elapsed time elapsed_time = time.time() - start_time sct.printv('\nFinished! Elapsed time: '+str(int(round(elapsed_time)))+'s', verbose) # to view results sct.printv('\nTo view results, type:', verbose) sct.printv('fslview '+fname_data+' '+path_output+'template2anat -b 0,4000 &', verbose, 'info') sct.printv('fslview '+fname_template+' -b 0,5000 '+path_output+'anat2template &\n', verbose, 'info')
def create_mask(): fsloutput = 'export FSLOUTPUTTYPE=NIFTI; ' # for faster processing, all outputs are in NIFTI # parse argument for method method_type = param.process[0] # check method val if not method_type == 'center': method_val = param.process[1] # check existence of input files if method_type == 'centerline': sct.check_file_exist(method_val, param.verbose) # Extract path/file/extension path_data, file_data, ext_data = sct.extract_fname(param.fname_data) # Get output folder and file name if param.fname_out == '': param.fname_out = param.file_prefix+file_data+ext_data # create temporary folder sct.printv('\nCreate temporary folder...', param.verbose) path_tmp = sct.tmp_create(param.verbose) # )sct.slash_at_the_end('tmp.'+time.strftime("%y%m%d%H%M%S"), 1) # sct.run('mkdir '+path_tmp, param.verbose) sct.printv('\nCheck orientation...', param.verbose) orientation_input = get_orientation(Image(param.fname_data)) sct.printv('.. '+orientation_input, param.verbose) reorient_coordinates = False # copy input data to tmp folder convert(param.fname_data, path_tmp+'data.nii') if method_type == 'centerline': convert(method_val, path_tmp+'centerline.nii.gz') if method_type == 'point': convert(method_val, path_tmp+'point.nii.gz') # go to tmp folder os.chdir(path_tmp) # reorient to RPI sct.printv('\nReorient to RPI...', param.verbose) # if not orientation_input == 'RPI': sct.run('sct_image -i data.nii -o data_RPI.nii -setorient RPI -v 0', verbose=False) if method_type == 'centerline': sct.run('sct_image -i centerline.nii.gz -o centerline_RPI.nii.gz -setorient RPI -v 0', verbose=False) if method_type == 'point': sct.run('sct_image -i point.nii.gz -o point_RPI.nii.gz -setorient RPI -v 0', verbose=False) # # if method_type == 'centerline': # orientation_centerline = get_orientation_3d(method_val, filename=True) # if not orientation_centerline == 'RPI': # sct.run('sct_image -i ' + method_val + ' -o ' + path_tmp + 'centerline.nii.gz' + ' -setorient RPI -v 0', verbose=False) # else: # convert(method_val, path_tmp+'centerline.nii.gz') # Get dimensions of data sct.printv('\nGet dimensions of data...', param.verbose) nx, ny, nz, nt, px, py, pz, pt = Image('data_RPI.nii').dim sct.printv(' ' + str(nx) + ' x ' + str(ny) + ' x ' + str(nz)+ ' x ' + str(nt), param.verbose) # in case user input 4d data if nt != 1: sct.printv('WARNING in '+os.path.basename(__file__)+': Input image is 4d but output mask will 3D.', param.verbose, 'warning') # extract first volume to have 3d reference nii = Image('data_RPI.nii') data3d = nii.data[:,:,:,0] nii.data = data3d nii.save() if method_type == 'coord': # parse to get coordinate coord = map(int, method_val.split('x')) if method_type == 'point': # get file name fname_point = method_val # extract coordinate of point sct.printv('\nExtract coordinate of point...', param.verbose) # TODO: change this way to remove dependence to sct.run. ProcessLabels.display_voxel returns list of coordinates status, output = sct.run('sct_label_utils -i point_RPI.nii.gz -display', param.verbose) # parse to get coordinate coord = output[output.find('Position=')+10:-17].split(',') if method_type == 'center': # set coordinate at center of FOV coord = round(float(nx)/2), round(float(ny)/2) if method_type == 'centerline': # get name of centerline from user argument fname_centerline = 'centerline_RPI.nii.gz' else: # generate volume with line along Z at coordinates 'coord' sct.printv('\nCreate line...', param.verbose) fname_centerline = create_line('data_RPI.nii', coord, nz) # create mask sct.printv('\nCreate mask...', param.verbose) centerline = nibabel.load(fname_centerline) # open centerline hdr = centerline.get_header() # get header hdr.set_data_dtype('uint8') # set imagetype to uint8 spacing = hdr.structarr['pixdim'] data_centerline = centerline.get_data() # get centerline z_centerline_not_null = [iz for iz in range(0, nz, 1) if data_centerline[:, :, iz].any()] # get center of mass of the centerline cx = [0] * nz cy = [0] * nz for iz in range(0, nz, 1): if iz in z_centerline_not_null: cx[iz], cy[iz] = ndimage.measurements.center_of_mass(numpy.array(data_centerline[:, :, iz])) # create 2d masks file_mask = 'data_mask' for iz in range(nz): if iz not in z_centerline_not_null: # write an empty nifty volume img = nibabel.Nifti1Image(data_centerline[:, :, iz], None, hdr) nibabel.save(img, (file_mask + str(iz) + '.nii')) else: center = numpy.array([cx[iz], cy[iz]]) mask2d = create_mask2d(center, param.shape, param.size, nx, ny, even=param.even, spacing=spacing) # Write NIFTI volumes img = nibabel.Nifti1Image(mask2d, None, hdr) nibabel.save(img, (file_mask+str(iz)+'.nii')) # merge along Z # cmd = 'fslmerge -z mask ' # CHANGE THAT CAN IMPACT SPEED: # related to issue #755, we cannot open more than 256 files at one time. # to solve this issue, we do not open more than 100 files ''' im_list = [] im_temp = [] for iz in range(nz_not_null): if iz != 0 and iz % 100 == 0: im_temp.append(concat_data(im_list, 2)) im_list = [Image(file_mask + str(iz) + '.nii')] else: im_list.append(Image(file_mask+str(iz)+'.nii')) if im_temp: im_temp.append(concat_data(im_list, 2)) im_out = concat_data(im_temp, 2, no_expand=True) else: im_out = concat_data(im_list, 2) ''' fname_list = [file_mask + str(iz) + '.nii' for iz in range(nz)] im_out = concat_data(fname_list, dim=2) im_out.setFileName('mask_RPI.nii.gz') im_out.save() # reorient if necessary # if not orientation_input == 'RPI': sct.run('sct_image -i mask_RPI.nii.gz -o mask.nii.gz -setorient ' + orientation_input, param.verbose) # copy header input --> mask im_dat = Image('data.nii') im_mask = Image('mask.nii.gz') im_mask = copy_header(im_dat, im_mask) im_mask.save() # come back to parent folder os.chdir('..') # Generate output files sct.printv('\nGenerate output files...', param.verbose) sct.generate_output_file(path_tmp+'mask.nii.gz', param.fname_out) # Remove temporary files if param.remove_tmp_files == 1: sct.printv('\nRemove temporary files...', param.verbose) sct.run('rm -rf '+path_tmp, param.verbose, error_exit='warning') # to view results sct.printv('\nDone! To view results, type:', param.verbose) sct.printv('fslview '+param.fname_data+' '+param.fname_out+' -l Red -t 0.5 &', param.verbose, 'info') print
def main(): # Initialization fname_data = '' fname_landmarks = '' fname_seg = '' folder_template = param.folder_template file_template = param.file_template file_template_label = param.file_template_label file_template_seg = param.file_template_seg output_type = param.output_type speed = param.speed remove_temp_files = param.remove_temp_files verbose = param.verbose smoothing_sigma = param.smoothing_sigma # start timer start_time = time.time() # get path of the toolbox status, path_sct = commands.getstatusoutput('echo $SCT_DIR') # # get path of the template # path_template = path_sct+folder_template # get fname of the template + template objects fname_template = path_sct+folder_template+'/'+file_template fname_template_label = path_sct+folder_template+'/'+file_template_label fname_template_seg = path_sct+folder_template+'/'+file_template_seg # Parameters for debug mode if param.debug: print '\n*** WARNING: DEBUG MODE ON ***\n' fname_data = path_sct+'/testing/data/errsm_23/t2/t2.nii.gz' fname_landmarks = path_sct+'/testing/data/errsm_23/t2/t2_landmarks_C2_T2_center.nii.gz' fname_seg = path_sct+'/testing/data/errsm_23/t2/t2_segmentation_PropSeg.nii.gz' speed = 'superfast' # Check input parameters try: opts, args = getopt.getopt(sys.argv[1:],'hi:l:m:o:r:s:') except getopt.GetoptError: usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ("-i"): fname_data = arg elif opt in ('-l'): fname_landmarks = arg elif opt in ("-m"): fname_seg = arg elif opt in ("-o"): output_type = int(arg) elif opt in ("-r"): remove_temp_files = int(arg) elif opt in ("-s"): speed = arg # display usage if a mandatory argument is not provided if fname_data == '' or fname_landmarks == '' or fname_seg == '': usage() # print arguments print '\nCheck parameters:' print '.. Data: '+fname_data print '.. Landmarks: '+fname_landmarks print '.. Segmentation: '+fname_seg print '.. Output type: '+str(output_type) print '.. Speed: '+speed print '.. Remove temp files: '+str(remove_temp_files) # Check speed parameter and create registration mode: slow 50x30, normal 50x15, fast 10x3 (default) print('\nAssign number of iterations based on speed...') if speed == "slow": nb_iterations = "50x30" elif speed == "normal": nb_iterations = "50x15" elif speed == "fast": nb_iterations = "10x3" elif speed == "superfast": nb_iterations = "3x1" # only for debugging purpose-- do not inform the user about this option else: print 'ERROR: Wrong input registration speed {slow, normal, fast}.' sys.exit(2) print '.. '+nb_iterations # Get full path # fname_data = os.path.abspath(fname_data) # fname_landmarks = os.path.abspath(fname_landmarks) # fname_seg = os.path.abspath(fname_seg) # check existence of input files print('\nCheck existence of input files...') sct.check_file_exist(fname_data,verbose) sct.check_file_exist(fname_landmarks,verbose) sct.check_file_exist(fname_seg,verbose) path_data, file_data, ext_data = sct.extract_fname(fname_data) # create temporary folder print('\nCreate temporary folder...') path_tmp = 'tmp.'+time.strftime("%y%m%d%H%M%S") status, output = sct.run('mkdir '+path_tmp) # copy files to temporary folder print('\nCopy files...') status, output = sct.run('c3d '+fname_data+' -o '+path_tmp+'/data.nii') status, output = sct.run('c3d '+fname_landmarks+' -o '+path_tmp+'/landmarks.nii.gz') status, output = sct.run('c3d '+fname_seg+' -o '+path_tmp+'/segmentation.nii.gz') # go to tmp folder os.chdir(path_tmp) # Change orientation of input images to RPI print('\nChange orientation of input images to RPI...') status, output = sct.run('sct_orientation -i data.nii -o data_rpi.nii -orientation RPI') status, output = sct.run('sct_orientation -i landmarks.nii.gz -o landmarks_rpi.nii.gz -orientation RPI') status, output = sct.run('sct_orientation -i segmentation.nii.gz -o segmentation_rpi.nii.gz -orientation RPI') # Straighten the spinal cord using centerline/segmentation print('\nStraighten the spinal cord using centerline/segmentation...') status, output = sct.run('sct_straighten_spinalcord.py -i data_rpi.nii -c segmentation_rpi.nii.gz -r '+str(remove_temp_files)) # Apply straightening to segmentation print('\nApply straightening to segmentation...') sct.run('WarpImageMultiTransform 3 segmentation_rpi.nii.gz segmentation_rpi_straight.nii.gz -R data_rpi_straight.nii warp_curve2straight.nii.gz') # Smoothing along centerline to improve accuracy and remove step effects print('\nSmoothing along centerline to improve accuracy and remove step effects...') sct.run('c3d data_rpi_straight.nii -smooth 0x0x'+str(smoothing_sigma)+'vox -o data_rpi_straight.nii') sct.run('c3d segmentation_rpi_straight.nii.gz -smooth 0x0x'+str(smoothing_sigma)+'vox -o segmentation_rpi_straight.nii.gz') # Label preparation: # -------------------------------------------------------------------------------- # Remove unused label on template. Keep only label present in the input label image print('\nRemove unused label on template. Keep only label present in the input label image...') status, output = sct.run('sct_label_utils.py -t remove -i '+fname_template_label+' -o template_label.nii.gz -r landmarks_rpi.nii.gz') # Create a cross for the template labels - 5 mm print('\nCreate a 5 mm cross for the template labels...') status, output = sct.run('sct_label_utils.py -t cross -i template_label.nii.gz -o template_label_cross.nii.gz -c 5') # Create a cross for the input labels and dilate for straightening preparation - 5 mm print('\nCreate a 5mm cross for the input labels and dilate for straightening preparation...') status, output = sct.run('sct_label_utils.py -t cross -i landmarks_rpi.nii.gz -o landmarks_rpi_cross3x3.nii.gz -c 5 -d') # Push the input labels in the template space print('\nPush the input labels to the straight space...') status, output = sct.run('WarpImageMultiTransform 3 landmarks_rpi_cross3x3.nii.gz landmarks_rpi_cross3x3_straight.nii.gz -R data_rpi_straight.nii warp_curve2straight.nii.gz --use-NN') # Convert landmarks from FLOAT32 to INT print '\nConvert landmarks from FLOAT32 to INT...' sct.run('c3d landmarks_rpi_cross3x3_straight.nii.gz -type int -o landmarks_rpi_cross3x3_straight.nii.gz') # Estimate affine transfo: straight --> template (landmark-based)' print '\nEstimate affine transfo: straight anat --> template (landmark-based)...' sct.run('ANTSUseLandmarkImagesToGetAffineTransform template_label_cross.nii.gz landmarks_rpi_cross3x3_straight.nii.gz affine straight2templateAffine.txt') # Apply affine transformation: straight --> template print '\nApply affine transformation: straight --> template...' sct.run('WarpImageMultiTransform 3 data_rpi_straight.nii data_rpi_straight2templateAffine.nii straight2templateAffine.txt -R '+fname_template) sct.run('WarpImageMultiTransform 3 segmentation_rpi_straight.nii.gz segmentation_rpi_straight2templateAffine.nii.gz straight2templateAffine.txt -R '+fname_template) # now threshold at 0.5 (for partial volume interpolation) # do not do that anymore-- better to estimate transformation using trilinear interp image to avoid step effect. See issue #31 on github. # sct.run('c3d segmentation_rpi_straight2templateAffine.nii.gz -threshold -inf 0.5 0 1 -o segmentation_rpi_straight2templateAffine.nii.gz') # Registration straight spinal cord to template print('\nRegister straight spinal cord to template...') nb_iterations = '50x15' # TODO: nb iteration for step 2 sct.run('sct_register_multimodal.py -i data_rpi_straight2templateAffine.nii -d '+fname_template+' -s segmentation_rpi_straight2templateAffine.nii.gz -t '+fname_template_seg+' -r '+str(remove_temp_files)+' -n '+nb_iterations+' -v '+str(verbose)+' -x 1',verbose) # status, output = sct.run('sct_register_straight_spinalcord_to_template.py -i data_rpi_straight.nii.gz -l landmarks_rpi_cross3x3_straight.nii.gz -t '+path_template+'/MNI-Poly-AMU_T2.nii.gz -f template_label_cross.nii.gz -m '+path_template+'/mask_gaussian_templatespace_sigma20.nii.gz -r 1 -n '+nb_iterations+' -v 1') # Concatenate warping fields: template2anat & anat2template print('\nConcatenate warping fields: template2anat & anat2template...') cmd = 'ComposeMultiTransform 3 warp_template2anat.nii.gz -R data.nii warp_straight2curve.nii.gz -i straight2templateAffine.txt warp_dest2src.nii.gz' print '>> '+cmd commands.getstatusoutput(cmd) cmd = 'ComposeMultiTransform 3 warp_anat2template.nii.gz -R '+fname_template+' warp_src2dest.nii.gz straight2templateAffine.txt warp_curve2straight.nii.gz' print '>> '+cmd commands.getstatusoutput(cmd) # Apply warping fields to anat and template if output_type == 1: sct.run('WarpImageMultiTransform 3 '+fname_template+' template2anat.nii.gz -R data.nii warp_template2anat.nii.gz') sct.run('WarpImageMultiTransform 3 data.nii.gz anat2template.nii.gz -R '+fname_template+' warp_anat2template.nii.gz') # come back to parent folder os.chdir('..') # Generate output files print('\nGenerate output files...') sct.generate_output_file(path_tmp+'/warp_template2anat.nii.gz','','warp_template2anat','.nii.gz') sct.generate_output_file(path_tmp+'/warp_anat2template.nii.gz','','warp_anat2template','.nii.gz') if output_type == 1: sct.generate_output_file(path_tmp+'/template2anat.nii.gz','','template2anat',ext_data) sct.generate_output_file(path_tmp+'/anat2template.nii.gz','','anat2template',ext_data) # Delete temporary files if remove_temp_files == 1: print '\nDelete temporary files...' sct.run('rm -rf '+path_tmp) # display elapsed time elapsed_time = time.time() - start_time print '\nFinished! Elapsed time: '+str(int(round(elapsed_time)))+'s' # to view results print '\nTo view results, type:' print 'fslview template2anat '+fname_data+' &' print 'fslview anat2template '+fname_template+' &\n'
def main(): #Initialization fname = '' verbose = param.verbose output_name = param.output_name smoothness = param.smoothness try: opts, args = getopt.getopt(sys.argv[1:],'hi:o:s:v:') except getopt.GetoptError: usage() for opt, arg in opts : if opt == '-h': usage() elif opt in ("-i"): fname = arg elif opt in ("-o"): output_name = arg elif opt in ("-s"): smoothness = arg elif opt in ('-v'): verbose = int(arg) # display usage if a mandatory argument is not provided if fname == '' : usage() # check existence of input files print'\nCheck if file exists ...' sct.check_file_exist(fname) # check if RPI sct.check_if_rpi(fname) # Display arguments print'\nCheck input arguments...' print' Input volume ...................... '+fname print' Verbose ........................... '+str(verbose) file = load(fname) data = file.get_data() hdr = file.get_header() X,Y,Z = (data>0).nonzero() Z_new = linspace(min(Z),max(Z),(max(Z)-min(Z)+1)) # tck1 = interpolate.splrep(Z, X, s=200) # X_fit = interpolate.splev(Z_new, tck1) # # tck2 = interpolate.splrep(Z, Y, s=200) # Y_fit = interpolate.splev(Z_new, tck2) # f1 = interpolate.interp1d(Z, X, kind='cubic') # f2 = interpolate.interp1d(Z,Y, kind='cubic') # # sort X and Y arrays using Z X = [X[i] for i in Z[:].argsort()] Y = [Y[i] for i in Z[:].argsort()] Z = [Z[i] for i in Z[:].argsort()] print X, Y, Z # NURBS! #X_fit, Y_fit, Z_fit, x_deriv, y_deriv, z_deriv = b_spline_nurbs(X, Y, Z, degree=3, point_number=3000) #f_opt_x, f_opt_y = opt_f(X,Y,Z) #print "f_opt = "+str(f_opt_x)+" "+str(f_opt_y) #f1 = non_parametric(Z,X,f=0.8) #f2 = non_parametric(Z,Y,f=0.8) f1 = interpolate.UnivariateSpline(Z, X) f2 = interpolate.UnivariateSpline(Z, Y) #f1 = polynomial_fit(Z,X,smoothness) #f2 = polynomial_fit(Z,Y,smoothness) X_fit = f1(Z_new) Y_fit = f2(Z_new) print X_fit print Y_fit if verbose==2 : import matplotlib.pyplot as plt plt.figure() plt.plot(Z_new,X_fit) plt.plot(Z,X,'o',linestyle = 'None') plt.show() plt.figure() plt.plot(Z_new,Y_fit) plt.plot(Z,Y,'o',linestyle = 'None') plt.show() data =data*0 for i in xrange(len(X_fit)): data[X_fit[i],Y_fit[i],Z_new[i]] = 1 print '\nSave volume ...' hdr.set_data_dtype('float32') # set imagetype to uint8 # save volume #data = data.astype(float32, copy =False) img = Nifti1Image(data, None, hdr) file_name = output_name save(img,file_name) print '\nFile created : ' + output_name del data
def main(): # Initialization fname_warp_list = '' # list of warping fields fname_dest = '' # destination image (fix) fname_warp_final = '' # concatenated transformations verbose = 1 # Parameters for debug mode if param.debug: sct.printv('\n*** WARNING: DEBUG MODE ON ***\n') status, path_sct_data = getstatusoutput('echo $SCT_TESTING_DATA_DIR') fname_warp_list = path_sct_data + '/t2/warp_template2anat.nii.gz,-' + path_sct_data + '/mt/warp_template2mt.nii.gz' fname_dest = path_sct_data + '/mt/mtr.nii.gz' verbose = 1 else: # Check input parameters parser = get_parser() arguments = parser.parse(sys.argv[1:]) fname_dest = arguments['-d'] fname_warp_list = arguments['-w'] if '-o' in arguments: fname_warp_final = arguments['-o'] verbose = int(arguments['-v']) # Parse list of warping fields sct.printv('\nParse list of transformations...', verbose) use_inverse = [] fname_warp_list_invert = [] for i in range(len(fname_warp_list)): # Check if inverse matrix is specified with '-' at the beginning of file name if fname_warp_list[i].find('-') == 0: use_inverse.append('-i ') fname_warp_list[i] = fname_warp_list[i][1:] # remove '-' else: use_inverse.append('') sct.printv( ' Transfo #' + str(i) + ': ' + use_inverse[i] + fname_warp_list[i], verbose) fname_warp_list_invert.append(use_inverse[i] + fname_warp_list[i]) # Check file existence sct.printv('\nCheck file existence...', verbose) sct.check_file_exist(fname_dest, verbose) for i in range(len(fname_warp_list)): sct.check_file_exist(fname_warp_list[i], verbose) # Get output folder and file name if fname_warp_final == '': path_out, file_out, ext_out = sct.extract_fname(param.fname_warp_final) else: path_out, file_out, ext_out = sct.extract_fname(fname_warp_final) # Check dimension of destination data (cf. issue #1419, #1429) im_dest = Image(fname_dest) if im_dest.dim[2] == 1: dimensionality = '2' else: dimensionality = '3' # Concatenate warping fields sct.printv('\nConcatenate warping fields...', verbose) # N.B. Here we take the inverse of the warp list fname_warp_list_invert.reverse() cmd = 'isct_ComposeMultiTransform ' + dimensionality + ' warp_final' + ext_out + ' -R ' + fname_dest + ' ' + ' '.join( fname_warp_list_invert) sct.printv('>> ' + cmd, verbose) status, output = getstatusoutput( cmd ) # here cannot use sct.run() because of wrong output status in isct_ComposeMultiTransform # check if output was generated if not os.path.isfile('warp_final' + ext_out): sct.printv('ERROR: Warping field was not generated.\n' + output, 1, 'error') # Generate output files sct.printv('\nGenerate output files...', verbose) sct.generate_output_file('warp_final' + ext_out, path_out + file_out + ext_out)
def main(): # Initialization fname_data = '' suffix_out = '_crop' remove_temp_files = param.remove_temp_files verbose = param.verbose fsloutput = 'export FSLOUTPUTTYPE=NIFTI; ' # for faster processing, all outputs are in NIFTI remove_temp_files = param.remove_temp_files # Parameters for debug mode if param.debug: print '\n*** WARNING: DEBUG MODE ON ***\n' fname_data = path_sct + '/testing/data/errsm_23/t2/t2.nii.gz' remove_temp_files = 0 else: # Check input parameters try: opts, args = getopt.getopt(sys.argv[1:], 'hi:r:v:') except getopt.GetoptError: usage() if not opts: usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ('-i'): fname_data = arg elif opt in ('-r'): remove_temp_files = int(arg) elif opt in ('-v'): verbose = int(arg) # display usage if a mandatory argument is not provided if fname_data == '': usage() # Check file existence sct.printv('\nCheck file existence...', verbose) sct.check_file_exist(fname_data, verbose) # Get dimensions of data sct.printv('\nGet dimensions of data...', verbose) nx, ny, nz, nt, px, py, pz, pt = Image(fname_data).dim sct.printv('.. ' + str(nx) + ' x ' + str(ny) + ' x ' + str(nz), verbose) # check if 4D data if not nt == 1: sct.printv( '\nERROR in ' + os.path.basename(__file__) + ': Data should be 3D.\n', 1, 'error') sys.exit(2) # print arguments print '\nCheck parameters:' print ' data ................... ' + fname_data print # Extract path/file/extension path_data, file_data, ext_data = sct.extract_fname(fname_data) path_out, file_out, ext_out = '', file_data + suffix_out, ext_data # create temporary folder path_tmp = 'tmp.' + time.strftime("%y%m%d%H%M%S") + '/' sct.run('mkdir ' + path_tmp) # copy files into tmp folder sct.run('isct_c3d ' + fname_data + ' -o ' + path_tmp + 'data.nii') # go to tmp folder os.chdir(path_tmp) # change orientation sct.printv('\nChange orientation to RPI...', verbose) set_orientation('data.nii', 'RPI', 'data_rpi.nii') # get image of medial slab sct.printv('\nGet image of medial slab...', verbose) image_array = nibabel.load('data_rpi.nii').get_data() nx, ny, nz = image_array.shape scipy.misc.imsave('image.jpg', image_array[math.floor(nx / 2), :, :]) # Display the image sct.printv('\nDisplay image and get cropping region...', verbose) fig = plt.figure() # fig = plt.gcf() # ax = plt.gca() ax = fig.add_subplot(111) img = mpimg.imread("image.jpg") implot = ax.imshow(img.T) implot.set_cmap('gray') plt.gca().invert_yaxis() # mouse callback ax.set_title( 'Left click on the top and bottom of your cropping field.\n Right click to remove last point.\n Close window when your done.' ) line, = ax.plot([], [], 'ro') # empty line cropping_coordinates = LineBuilder(line) plt.show() # disconnect callback # fig.canvas.mpl_disconnect(line) # check if user clicked two times if len(cropping_coordinates.xs) != 2: sct.printv('\nERROR: You have to select two points. Exit program.\n', 1, 'error') sys.exit(2) # convert coordinates to integer zcrop = [int(i) for i in cropping_coordinates.ys] # sort coordinates zcrop.sort() # crop image sct.printv('\nCrop image...', verbose) nii = Image('data_rpi.nii') data_crop = nii.data[:, :, zcrop[0]:zcrop[1]] nii.data = data_crop nii.setFileName('data_rpi_crop.nii') nii.save() # come back to parent folder os.chdir('..') sct.printv('\nGenerate output files...', verbose) sct.generate_output_file(path_tmp + 'data_rpi_crop.nii', path_out + file_out + ext_out) # Remove temporary files if remove_temp_files == 1: print('\nRemove temporary files...') sct.run('rm -rf ' + path_tmp) # to view results print '\nDone! To view results, type:' print 'fslview ' + path_out + file_out + ext_out + ' &' print
def main(): # Initialization fname_src = '' # source image (moving) fname_warp_list = '' # list of warping fields fname_dest = '' # destination image (fix) fname_src_reg = '' verbose = 1 fsloutput = 'export FSLOUTPUTTYPE=NIFTI; ' # for faster processing, all outputs are in NIFTI crop_reference = 0 # if = 1, put 0 everywhere around warping field, if = 2, real crop # Parameters for debug mode if param.debug: print '\n*** WARNING: DEBUG MODE ON ***\n' # get path of the testing data status, path_sct_data = commands.getstatusoutput('echo $SCT_TESTING_DATA_DIR') fname_src = path_sct_data+'/template/MNI-Poly-AMU_T2.nii.gz' fname_warp_list = path_sct_data+'/t2/warp_template2anat.nii.gz' fname_dest = path_sct_data+'/t2/t2.nii.gz' verbose = 1 else: # Check input parameters try: opts, args = getopt.getopt(sys.argv[1:], 'hi:d:o:v:w:x:c:') except getopt.GetoptError: usage() if not opts: usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ('-i'): fname_src = arg elif opt in ('-d'): fname_dest = arg elif opt in ('-o'): fname_src_reg = arg elif opt in ('-x'): param.interp = arg elif opt in ('-v'): verbose = int(arg) elif opt in ('-w'): fname_warp_list = arg elif opt in ('-c'): crop_reference = int(arg) # display usage if a mandatory argument is not provided if fname_src == '' or fname_warp_list == '' or fname_dest == '': usage() # get the right interpolation field depending on method interp = sct.get_interpolation('isct_antsApplyTransforms', param.interp) # Parse list of warping fields sct.printv('\nParse list of warping fields...', verbose) use_inverse = [] fname_warp_list_invert = [] fname_warp_list = fname_warp_list.replace(' ', '') # remove spaces fname_warp_list = fname_warp_list.split(",") # parse with comma for i in range(len(fname_warp_list)): # Check if inverse matrix is specified with '-' at the beginning of file name if fname_warp_list[i].find('-') == 0: use_inverse.append('-i ') fname_warp_list[i] = fname_warp_list[i][1:] # remove '-' else: use_inverse.append('') sct.printv(' Transfo #'+str(i)+': '+use_inverse[i]+fname_warp_list[i], verbose) fname_warp_list_invert.append(use_inverse[i]+fname_warp_list[i]) # need to check if last warping field is an affine transfo isLastAffine = False path_fname, file_fname, ext_fname = sct.extract_fname(fname_warp_list_invert[-1]) if ext_fname in ['.txt','.mat']: isLastAffine = True # Check file existence sct.printv('\nCheck file existence...', verbose) sct.check_file_exist(fname_src) sct.check_file_exist(fname_dest) for i in range(len(fname_warp_list)): # check if file exist sct.check_file_exist(fname_warp_list[i]) for i in range(len(fname_warp_list_invert)): sct.check_file_exist(fname_warp_list_invert[i]) # check if destination file is 3d sct.check_if_3d(fname_dest) # N.B. Here we take the inverse of the warp list, because sct_WarpImageMultiTransform concatenates in the reverse order fname_warp_list_invert.reverse() # Extract path, file and extension # path_src, file_src, ext_src = sct.extract_fname(os.path.abspath(fname_src)) # fname_dest = os.path.abspath(fname_dest) path_src, file_src, ext_src = sct.extract_fname(fname_src) # fname_dest = os.path.abspath(fname_dest # Get output folder and file name if fname_src_reg == '': path_out = '' # output in user's current directory file_out = file_src+'_reg' ext_out = ext_src fname_out = path_out+file_out+ext_out else: # path_out, file_out, ext_out = sct.extract_fname(fname_src_reg) fname_out = fname_src_reg # Get dimensions of data sct.printv('\nGet dimensions of data...', verbose) nx, ny, nz, nt, px, py, pz, pt = sct.get_dimension(fname_src) sct.printv(' ' + str(nx) + ' x ' + str(ny) + ' x ' + str(nz)+ ' x ' + str(nt), verbose) # if 3d if nt == 1: # Apply transformation sct.printv('\nApply transformation...', verbose) sct.run('isct_antsApplyTransforms -d 3 -i '+fname_src+' -o '+fname_out+' -t '+' '.join(fname_warp_list_invert)+' -r '+fname_dest+interp, verbose) # if 4d, loop across the T dimension else: # create temporary folder sct.printv('\nCreate temporary folder...', verbose) path_tmp = sct.slash_at_the_end('tmp.'+time.strftime("%y%m%d%H%M%S"), 1) sct.run('mkdir '+path_tmp, verbose) # Copying input data to tmp folder # NB: cannot use c3d here because c3d cannot convert 4D data. sct.printv('\nCopying input data to tmp folder and convert to nii...', verbose) sct.run('cp '+fname_src+' '+path_tmp+'data'+ext_src, verbose) # go to tmp folder os.chdir(path_tmp) # convert to nii format sct.run('fslchfiletype NIFTI data', verbose) # split along T dimension sct.printv('\nSplit along T dimension...', verbose) sct.run(fsloutput+'fslsplit data data_T', verbose) # apply transfo sct.printv('\nApply transformation to each 3D volume...', verbose) for it in range(nt): file_data_split = 'data_T'+str(it).zfill(4)+'.nii' file_data_split_reg = 'data_reg_T'+str(it).zfill(4)+'.nii' sct.run('isct_antsApplyTransforms -d 3 -i '+file_data_split+' -o '+file_data_split_reg+' -t '+' '.join(fname_warp_list_invert)+' -r '+fname_dest+interp, verbose) # Merge files back sct.printv('\nMerge file back...', verbose) cmd = fsloutput+'fslmerge -t '+fname_out for it in range(nt): file_data_split_reg = 'data_reg_T'+str(it).zfill(4)+'.nii' cmd = cmd+' '+file_data_split_reg sct.run(cmd, param.verbose) # come back to parent folder os.chdir('..') # 2. crop the resulting image using dimensions from the warping field warping_field = fname_warp_list_invert[-1] # if last warping field is an affine transfo, we need to compute the space of the concatenate warping field: if isLastAffine: sct.printv('WARNING: the resulting image could have wrong apparent results. You should use an affine transformation as last transformation...',1,'warning') elif crop_reference == 1: sct.run('sct_crop_image -i '+fname_out+' -o '+fname_out+' -ref '+warping_field+' -b 0') elif crop_reference == 2: sct.run('sct_crop_image -i '+fname_out+' -o '+fname_out+' -ref '+warping_field) # display elapsed time sct.printv('\nDone! To view results, type:', verbose) sct.printv('fslview '+fname_dest+' '+fname_out+' &\n', verbose, 'info')
def create_mask(): fsloutput = 'export FSLOUTPUTTYPE=NIFTI; ' # for faster processing, all outputs are in NIFTI # parse argument for method method_type = param.process[0] # check method val if not method_type == 'center': method_val = param.process[1] # check existence of input files if method_type == 'centerline': sct.check_file_exist(method_val, param.verbose) # Extract path/file/extension path_data, file_data, ext_data = sct.extract_fname(param.fname_data) # Get output folder and file name if param.fname_out == '': param.fname_out = param.file_prefix + file_data + ext_data # create temporary folder sct.printv('\nCreate temporary folder...', param.verbose) path_tmp = sct.tmp_create(param.verbose) # )sct.slash_at_the_end('tmp.'+time.strftime("%y%m%d%H%M%S"), 1) # sct.run('mkdir '+path_tmp, param.verbose) sct.printv('\nCheck orientation...', param.verbose) orientation_input = get_orientation(Image(param.fname_data)) sct.printv('.. ' + orientation_input, param.verbose) reorient_coordinates = False # copy input data to tmp folder convert(param.fname_data, path_tmp + 'data.nii') if method_type == 'centerline': convert(method_val, path_tmp + 'centerline.nii.gz') if method_type == 'point': convert(method_val, path_tmp + 'point.nii.gz') # go to tmp folder os.chdir(path_tmp) # reorient to RPI sct.printv('\nReorient to RPI...', param.verbose) # if not orientation_input == 'RPI': sct.run('sct_image -i data.nii -o data_RPI.nii -setorient RPI -v 0', verbose=False) if method_type == 'centerline': sct.run( 'sct_image -i centerline.nii.gz -o centerline_RPI.nii.gz -setorient RPI -v 0', verbose=False) if method_type == 'point': sct.run( 'sct_image -i point.nii.gz -o point_RPI.nii.gz -setorient RPI -v 0', verbose=False) # # if method_type == 'centerline': # orientation_centerline = get_orientation_3d(method_val, filename=True) # if not orientation_centerline == 'RPI': # sct.run('sct_image -i ' + method_val + ' -o ' + path_tmp + 'centerline.nii.gz' + ' -setorient RPI -v 0', verbose=False) # else: # convert(method_val, path_tmp+'centerline.nii.gz') # Get dimensions of data sct.printv('\nGet dimensions of data...', param.verbose) nx, ny, nz, nt, px, py, pz, pt = Image('data_RPI.nii').dim sct.printv( ' ' + str(nx) + ' x ' + str(ny) + ' x ' + str(nz) + ' x ' + str(nt), param.verbose) # in case user input 4d data if nt != 1: sct.printv( 'WARNING in ' + os.path.basename(__file__) + ': Input image is 4d but output mask will 3D.', param.verbose, 'warning') # extract first volume to have 3d reference nii = Image('data_RPI.nii') data3d = nii.data[:, :, :, 0] nii.data = data3d nii.save() if method_type == 'coord': # parse to get coordinate coord = map(int, method_val.split('x')) if method_type == 'point': # get file name fname_point = method_val # extract coordinate of point sct.printv('\nExtract coordinate of point...', param.verbose) # TODO: change this way to remove dependence to sct.run. ProcessLabels.display_voxel returns list of coordinates status, output = sct.run( 'sct_label_utils -i point_RPI.nii.gz -display', param.verbose) # parse to get coordinate coord = output[output.find('Position=') + 10:-17].split(',') if method_type == 'center': # set coordinate at center of FOV coord = round(float(nx) / 2), round(float(ny) / 2) if method_type == 'centerline': # get name of centerline from user argument fname_centerline = 'centerline_RPI.nii.gz' else: # generate volume with line along Z at coordinates 'coord' sct.printv('\nCreate line...', param.verbose) fname_centerline = create_line('data_RPI.nii', coord, nz) # create mask sct.printv('\nCreate mask...', param.verbose) centerline = nibabel.load(fname_centerline) # open centerline hdr = centerline.get_header() # get header hdr.set_data_dtype('uint8') # set imagetype to uint8 spacing = hdr.structarr['pixdim'] data_centerline = centerline.get_data() # get centerline # if data is 2D, reshape with empty third dimension if len(data_centerline.shape) == 2: data_centerline_shape = list(data_centerline.shape) data_centerline_shape.append(1) data_centerline = data_centerline.reshape(data_centerline_shape) z_centerline_not_null = [ iz for iz in range(0, nz, 1) if data_centerline[:, :, iz].any() ] # get center of mass of the centerline cx = [0] * nz cy = [0] * nz for iz in range(0, nz, 1): if iz in z_centerline_not_null: cx[iz], cy[iz] = ndimage.measurements.center_of_mass( numpy.array(data_centerline[:, :, iz])) # create 2d masks file_mask = 'data_mask' for iz in range(nz): if iz not in z_centerline_not_null: # write an empty nifty volume img = nibabel.Nifti1Image(data_centerline[:, :, iz], None, hdr) nibabel.save(img, (file_mask + str(iz) + '.nii')) else: center = numpy.array([cx[iz], cy[iz]]) mask2d = create_mask2d(center, param.shape, param.size, nx, ny, even=param.even, spacing=spacing) # Write NIFTI volumes img = nibabel.Nifti1Image(mask2d, None, hdr) nibabel.save(img, (file_mask + str(iz) + '.nii')) # merge along Z # cmd = 'fslmerge -z mask ' # CHANGE THAT CAN IMPACT SPEED: # related to issue #755, we cannot open more than 256 files at one time. # to solve this issue, we do not open more than 100 files ''' im_list = [] im_temp = [] for iz in range(nz_not_null): if iz != 0 and iz % 100 == 0: im_temp.append(concat_data(im_list, 2)) im_list = [Image(file_mask + str(iz) + '.nii')] else: im_list.append(Image(file_mask+str(iz)+'.nii')) if im_temp: im_temp.append(concat_data(im_list, 2)) im_out = concat_data(im_temp, 2, no_expand=True) else: im_out = concat_data(im_list, 2) ''' fname_list = [file_mask + str(iz) + '.nii' for iz in range(nz)] im_out = concat_data(fname_list, dim=2) im_out.setFileName('mask_RPI.nii.gz') im_out.save() # reorient if necessary # if not orientation_input == 'RPI': sct.run( 'sct_image -i mask_RPI.nii.gz -o mask.nii.gz -setorient ' + orientation_input, param.verbose) # copy header input --> mask im_dat = Image('data.nii') im_mask = Image('mask.nii.gz') im_mask = copy_header(im_dat, im_mask) im_mask.save() # come back to parent folder os.chdir('..') # Generate output files sct.printv('\nGenerate output files...', param.verbose) sct.generate_output_file(path_tmp + 'mask.nii.gz', param.fname_out) # Remove temporary files if param.remove_tmp_files == 1: sct.printv('\nRemove temporary files...', param.verbose) sct.run('rm -rf ' + path_tmp, param.verbose, error_exit='warning') # to view results sct.printv('\nDone! To view results, type:', param.verbose) sct.printv( 'fslview ' + param.fname_data + ' ' + param.fname_out + ' -l Red -t 0.5 &', param.verbose, 'info') print
def main(args=None): """ Main function :param args: :return: """ # get parser args if args is None: args = None if sys.argv[1:] else ['--help'] else: # flatten the list of input arguments because -w and -winv carry a nested list lst = [] for line in args: lst.append(line) if isinstance(line, str) else lst.extend(line) args = lst parser = get_parser() arguments = parser.parse_args(args=args) # Initialization fname_warp_final = '' # concatenated transformations fname_dest = arguments.d fname_warp_list = arguments.w warpinv_filename = arguments.winv if arguments.o is not None: fname_warp_final = arguments.o verbose = arguments.v sct.init_sct(log_level=verbose, update=True) # Update log level # Parse list of warping fields sct.printv('\nParse list of warping fields...', verbose) use_inverse = [] fname_warp_list_invert = [] # list_warp = list_warp.replace(' ', '') # remove spaces # list_warp = list_warp.split(",") # parse with comma for idx_warp, path_warp in enumerate(fname_warp_list): # Check if this transformation should be inverted if path_warp in warpinv_filename: use_inverse.append('-i') # list_warp[idx_warp] = path_warp[1:] # remove '-' fname_warp_list_invert += [[ use_inverse[idx_warp], fname_warp_list[idx_warp] ]] else: use_inverse.append('') fname_warp_list_invert += [[path_warp]] path_warp = fname_warp_list[idx_warp] if path_warp.endswith((".nii", ".nii.gz")) \ and Image(fname_warp_list[idx_warp]).header.get_intent()[0] != 'vector': raise ValueError("Displacement field in {} is invalid: should be encoded" \ " in a 5D file with vector intent code" \ " (see https://nifti.nimh.nih.gov/pub/dist/src/niftilib/nifti1.h" \ .format(path_warp)) # need to check if last warping field is an affine transfo isLastAffine = False path_fname, file_fname, ext_fname = sct.extract_fname( fname_warp_list_invert[-1][-1]) if ext_fname in ['.txt', '.mat']: isLastAffine = True # check if destination file is 3d if not sct.check_if_3d(fname_dest): sct.printv('ERROR: Destination data must be 3d') # Here we take the inverse of the warp list, because sct_WarpImageMultiTransform concatenates in the reverse order fname_warp_list_invert.reverse() fname_warp_list_invert = functools.reduce(lambda x, y: x + y, fname_warp_list_invert) # Check file existence sct.printv('\nCheck file existence...', verbose) sct.check_file_exist(fname_dest, verbose) for i in range(len(fname_warp_list)): sct.check_file_exist(fname_warp_list[i], verbose) # Get output folder and file name if fname_warp_final == '': path_out, file_out, ext_out = sct.extract_fname(param.fname_warp_final) else: path_out, file_out, ext_out = sct.extract_fname(fname_warp_final) # Check dimension of destination data (cf. issue #1419, #1429) im_dest = Image(fname_dest) if im_dest.dim[2] == 1: dimensionality = '2' else: dimensionality = '3' cmd = [ 'isct_ComposeMultiTransform', dimensionality, 'warp_final' + ext_out, '-R', fname_dest ] + fname_warp_list_invert status, output = sct.run(cmd, verbose=verbose, is_sct_binary=True) # check if output was generated if not os.path.isfile('warp_final' + ext_out): sct.printv('ERROR: Warping field was not generated.\n' + output, 1, 'error') # Generate output files sct.printv('\nGenerate output files...', verbose) sct.generate_output_file('warp_final' + ext_out, os.path.join(path_out, file_out + ext_out))