list_iso_img = [isometric_resampling(im) for im in list_img] from timagetk.algorithms import blockmatching trsf_type = 'iso-deformable' list_res_trsf, list_res_img = [], [] for ind, sp_img in enumerate(list_iso_img): if ind < len(list_iso_img) - 1: # --- filenames to save: img_path, img_fname = split(list_img_fname[ind]) img_path += "/" # -- get the result image file name & path (output path), and create it if necessary: res_img_fname = get_res_img_fname(img_fname, index2time[ind+1], index2time[ind], trsf_type) res_path = img_path + '{}_registrations/'.format(trsf_type) # -- get DEFORMABLE registration result trsf filename and write trsf: res_trsf_fname = get_res_trsf_fname(img_fname, index2time[ind+1], index2time[ind], trsf_type) if exists(res_path + res_trsf_fname) and not force: print "Found saved {} registered image and transformation!".format(trsf_type) print "Loading BalTransformation:\n {}".format(res_path + res_img_fname) res_trsf = bal_trsf.BalTransformation() res_trsf.read(res_path + res_trsf_fname) list_res_trsf.append(res_trsf) else: if not exists(res_path): print "Creating folder: {}".format(res_path) mkdir(res_path) # --- rigid registration print "\nPerforming rigid registration of t{} on t{}:".format(ind, ind + 1) trsf_rig, res_rig = blockmatching(sp_img, list_iso_img[ind + 1], param_str_2='-trsf-type rigid -py-ll 1')
not_sequence = True if len(time_steps) == 2 else False # - Build the list of result transformation filenames to check if they exist (if, not they will be computed): # res_trsf_list = [] seq_res_trsf_list = [] for t_ref, t_float in time_reg_list: # 't' here refer to 't_float' float_img_path, float_img_fname = split( list_img_fname[time2index[t_float]]) float_img_path += "/" # - Get the result image file name & path (output path), and create it if necessary: res_img_fname = get_res_img_fname(float_img_fname, t_ref, t_float, trsf_type) res_path = float_img_path + '{}_registrations/'.format(trsf_type) # - Get sequence registration result trsf filename and write trsf: # res_trsf_list.append(res_path + get_res_trsf_fname(float_img_fname, t_ref, t_float, trsf_type)) seq_res_trsf_list.append(res_path + get_res_trsf_fname( float_img_fname, t_ref, t_float, "sequence_" + trsf_type)) print "" for f in seq_res_trsf_list: print "Existing tranformation file {}: {}\n".format(f, exists(f)) list_img = [] print "\n# - Loading list of images for which to apply registration process:" for n, img_fname in enumerate(list_img_fname): print " - Time-point {}, reading image {}...".format(n, img_fname) im = imread(img_fname) if ref_ch_name.find('raw') != -1: im = z_slice_contrast_stretch(im) else: pass list_img.append(im)
print ' - t_{}h reference fname: {}'.format(t_ref, ref_img_fname) im_ref = imread(image_dirname + ref_path_suffix + ref_img_fname) if ref_img_fname.find('iso') == -1: # if not isometric, resample! im_ref = isometric_resampling(im_ref) print "" res_trsf, res_im = registration(im_float, im_ref, method='rigid_registration', pyramid_highest_level=py_hl, pyramid_lowest_level=py_ll) print "" # - Save result image and tranformation: print "Writing image file: {}".format(rig_float_img_fname) imsave(res_path + rig_float_img_fname, res_im) # - Get result trsf filename: res_trsf_fname = get_res_trsf_fname(float_img_fname, t_ref, t_float, 'iso-rigid') print "Writing trsf file: {}".format(res_trsf_fname) res_trsf.write(res_path + res_trsf_fname) print "\nApplying estimated {} transformation on '{}' to segmented image:".format( 'rigid', ref_ch_name) if not exists(res_path + rig_seg_img_fname): print " - {}\n --> {}".format(seg_img_fname, rig_seg_img_fname) # --- Read the segmented image file: seg_im = imread(image_dirname + seg_path_suffix + seg_img_fname) res_seg_im = apply_trsf(seg_im, res_trsf, param_str_2=' -nearest -param') # --- Apply and save registered segmented image: imsave(res_path + rig_seg_img_fname, res_seg_im) else: print " - existing file: {}".format(rig_seg_img_fname)
# - Consecutive blockmatching registration: # Trsf are saved, registered might be saved, extra image will be saved if registered are. ################################################################################ sorted_time_steps = [time_steps[i] for i in t_index] time2index = {t: n for n, t in enumerate(time_steps)} out_trsf_fnames = [] # list of transformation matrix filenames for t_float, t_ref in zip(sorted_time_steps[:-1], sorted_time_steps[1:]): i_float = time2index[t_float] i_ref = time2index[t_ref] print "\n\n# -- Blockmatching registration t{}->t{}!".format(i_float, i_ref) # - Get the intensity image filenames corresponding to `t_ref` & `t_float`: ref_img_path, ref_img_fname = split(indexed_img_fnames[i_ref]) float_img_path, float_img_fname = split(indexed_img_fnames[i_float]) # - Defines the transformation matrix filename (output): out_trsf_fname = get_res_trsf_fname(float_img_fname, t_ref, t_float, trsf_type) # -- Add it to the list of transformation matrix filenames: out_trsf_fnames.append(out_trsf_fname) # - Read the reference and floating images: print "\n - Reading floating image (t{},{}{}): '{}'...".format(i_float, t_float, time_unit, float_img_fname) float_img = read_image(join(float_img_path, float_img_fname)) print "\n - Reading reference image (t{},{}{}): '{}'...".format(i_ref, t_ref, time_unit, ref_img_fname) ref_img = read_image(join(ref_img_path, ref_img_fname)) if not exists(join(out_folder, out_trsf_fname)) or force: print "\nComputing {} blockmatching registration t{}->t{}!".format(trsf_type.upper(), i_float, i_ref) # - Blockmatching registration: out_trsf, _ = registration(float_img, ref_img, method=trsf_type, pyramid_lowest_level=py_ll) # -- Save the transformation matrix: print "--> Saving {} transformation file: '{}'".format(trsf_type.upper(), out_trsf_fname) save_trsf(out_trsf, out_folder + out_trsf_fname) elif write_cons_img or extra_im is not None or seg_im is not None:
composed_trsf = zip(list_comp_tsrf, time_steps[:-1]) for trsf, t in composed_trsf: # 't' here refer to 't_float' # - Get the reference file name & path: ref_img_path, ref_img_fname = split(list_img_fname[-1]) # - Get the float file name & path: float_im = list_img[time2index[t]] float_img_path, float_img_fname = split(list_img_fname[time2index[t]]) float_img_path += "/" # - Get the result image file name & path (output path), and create it if necessary: res_img_fname = get_res_img_fname(float_img_fname, time_steps[-1], t, trsf_type) res_path = float_img_path + '{}_registrations/'.format(trsf_type) if not exists(res_path): mkdir(res_path) # - Get result trsf filename and write trsf: res_trsf_fname = get_res_trsf_fname(float_img_fname, time_steps[-1], t, trsf_type) if not exists(res_path + res_trsf_fname) or force: if t == time_steps[-2]: # -- No need to "adjust" for time_steps[-2]/time_steps[-1] registration since it is NOT a composition: print "\n# - Saving {} t{}/t{} registration:".format( trsf_type.upper(), time2index[t], time2index[time_steps[-1]]) res_trsf = trsf res_im = list_res_img[-1] else: # -- One last round of vectorfield using composed transformation as init_trsf: print "\n# - Final {} registration adjustment for t{}/t{} composed transformation:".format( trsf_type.upper(), time2index[t], time2index[time_steps[-1]]) py_hl = 1 # defines highest level of the blockmatching-pyramid py_ll = 0 # defines lowest level of the blockmatching-pyramid print ' - t_{}h floating fname: {}'.format(t, float_img_fname)