def sequence_registration_affine(list_images): #--- check SpatialImage sequence conds_init = isinstance(list_images, list) and len(list_images)>=3 conds_list_img = [0 if isinstance(sp_img, SpatialImage) else 1 for sp_img in list_images] #--- end check if conds_init and 1 not in conds_list_img: #--- pairwise registration pairwise_trsf = [] for ind, sp_img in enumerate(list_images): if ind < len(list_images)-1: # --- rigid registration trsf_rig, res_rig = blockmatching(list_images[ind], list_images[ind+1], param_str_2='-trsf-type rigid -py-ll 1') # --- affine registration, initialisation by a rigid transformation trsf_aff, res_aff = blockmatching(list_images[ind], list_images[ind+1], left_transformation=trsf_rig, param_str_2='-trsf-type affine') res_trsf = compose_trsf([trsf_rig, trsf_aff]) #--- composition of transformations pairwise_trsf.append(res_trsf) #--- case affine registration trsf_rig.free() #--- add #--- composition of transformations list_compo_trsf, list_res_img = [], [] for ind, trsf in enumerate(pairwise_trsf): if ind < len(pairwise_trsf)-1: tmp_trsf = compose_trsf(pairwise_trsf[ind:]) # matrix multiplication list_compo_trsf.append(tmp_trsf) elif ind == pairwise_trsf.index(pairwise_trsf[-1]): list_compo_trsf.append(pairwise_trsf[-1]) #--- displacements compensation (whole sequence) for ind, trsf in enumerate(list_compo_trsf): tmp_img = apply_trsf(list_images[ind], trsf, template_img=list_images[-1]) list_res_img.append(tmp_img) list_res_img.append(list_images[-1]) # add reference image return list_compo_trsf, list_res_img
def deformable_registration(floating_img, reference_img): trsf_rig, res_rig = blockmatching(floating_img, reference_img) param_str_2 = '-trsf-type vectorfield' trsf_def, res_def = blockmatching(floating_img, reference_img, init_result_transformation=trsf_rig, param_str_2=param_str_2) return trsf_def, res_def
def sequence_registration_rigid(list_images): #--- check SpatialImage sequence conds_init = isinstance(list_images, list) and len(list_images)>=3 conds_list_img = [0 if isinstance(sp_img, SpatialImage) else 1 for sp_img in list_images] #--- end check if conds_init and 1 not in conds_list_img: #--- pairwise registration pairwise_trsf = [] for ind, sp_img in enumerate(list_images): if ind < len(list_images)-1: # --- rigid registration trsf_rig, res_rig = blockmatching(list_images[ind], list_images[ind+1], param_str_2='-trsf-type rigid') pairwise_trsf.append(trsf_rig) #--- case rigid registration #--- composition of transformations list_compo_trsf, list_res_img = [], [] for ind, trsf in enumerate(pairwise_trsf): if ind < len(pairwise_trsf)-1: comp_trsf = compose_trsf(pairwise_trsf[ind:]) # matrix multiplication list_compo_trsf.append(comp_trsf) elif ind == len(pairwise_trsf)-1: list_compo_trsf.append(pairwise_trsf[-1]) #--- displacements compensation (whole sequence) for ind, trsf in enumerate(list_compo_trsf): tmp_img = apply_trsf(list_images[ind], trsf, template_img=list_images[-1]) # global reference : list_images[-1] list_res_img.append(tmp_img) list_res_img.append(list_images[-1]) # add global reference image return list_compo_trsf, list_res_img
def affine_registration(floating_img, reference_img): trsf_rig, res_rig = rigid_registration(floating_img, reference_img) param_str_2 = '-trsf-type affine' trsf_aff, res_aff = blockmatching(floating_img, reference_img, left_transformation=trsf_rig, param_str_2=param_str_2) return trsf_aff, res_aff
def sequence_registration_affine(list_images): #--- check SpatialImage sequence conds_init = isinstance(list_images, list) and len(list_images) >= 3 conds_list_img = [ 0 if isinstance(sp_img, SpatialImage) else 1 for sp_img in list_images ] #--- end check if conds_init and 1 not in conds_list_img: #--- pairwise registration pairwise_trsf = [] for ind, sp_img in enumerate(list_images): if ind < len(list_images) - 1: # --- rigid registration trsf_rig, res_rig = blockmatching( list_images[ind], list_images[ind + 1], param_str_2='-trsf-type rigid -py-ll 1') # --- affine registration, initialisation by a rigid transformation trsf_aff, res_aff = blockmatching( list_images[ind], list_images[ind + 1], left_transformation=trsf_rig, param_str_2='-trsf-type affine') res_trsf = compose_trsf( [trsf_rig, trsf_aff]) #--- composition of transformations pairwise_trsf.append(res_trsf) #--- case affine registration trsf_rig.free() #--- add #--- composition of transformations list_compo_trsf, list_res_img = [], [] for ind, trsf in enumerate(pairwise_trsf): if ind < len(pairwise_trsf) - 1: tmp_trsf = compose_trsf( pairwise_trsf[ind:]) # matrix multiplication list_compo_trsf.append(tmp_trsf) elif ind == pairwise_trsf.index(pairwise_trsf[-1]): list_compo_trsf.append(pairwise_trsf[-1]) #--- displacements compensation (whole sequence) for ind, trsf in enumerate(list_compo_trsf): tmp_img = apply_trsf(list_images[ind], trsf, template_img=list_images[-1]) list_res_img.append(tmp_img) list_res_img.append(list_images[-1]) # add reference image return list_compo_trsf, list_res_img
def test_blockmatching_basic(self): img_src = imread(data_path('filtering_src.inr')) trsf_out, res = blockmatching(img_src, img_src) identity_trsf = create_trsf(param_str_2='-identity') np.testing.assert_array_almost_equal(trsf_out.mat.to_np_array(), identity_trsf.mat.to_np_array(), decimal=8) np.testing.assert_array_almost_equal(res, img_src, decimal=8) identity_trsf.free() trsf_out.free() return
def test_blockmatching_rigid(self): #--- rigid registration floating_img = imread(data_path('time_0_cut.inr')) reference_img = imread(data_path('time_1_cut.inr')) rigid_trsf = data_path("rigid_0_1.trsf") trsf_out, res = blockmatching(floating_img, reference_img) trsf_inp = BalTransformation() trsf_inp.read(rigid_trsf) np.testing.assert_array_almost_equal(trsf_out.mat.to_np_array(), trsf_inp.mat.to_np_array(), decimal=6) trsf_out.free() trsf_inp.free() return
def test_blockmatching_deformable(self): #--- deformable registration floating_img = imread(data_path('time_0_cut.inr')) reference_img = imread(data_path('time_1_cut.inr')) rigid_trsf = data_path("rigid_0_1.trsf") deformable_trsf = data_path("deformable_0_1.trsf") init_result_transformation = BalTransformation() init_result_transformation.read(rigid_trsf) param_str_2 = '-trsf-type vectorfield' trsf_out, res = blockmatching(floating_img, reference_img, init_result_transformation=init_result_transformation, left_transformation=None, param_str_2=param_str_2) trsf_inp = BalTransformation() trsf_inp.read(deformable_trsf) np.testing.assert_array_almost_equal(trsf_out.mat.to_np_array(), trsf_inp.mat.to_np_array(), decimal=6) trsf_out.free() trsf_inp.free() return
def test_blockmatching_deformable(self): #--- deformable registration floating_img = imread(data_path('time_0_cut.inr')) reference_img = imread(data_path('time_1_cut.inr')) rigid_trsf = data_path("rigid_0_1.trsf") deformable_trsf = data_path("deformable_0_1.trsf") init_result_transformation = BalTransformation() init_result_transformation.read(rigid_trsf) param_str_2 = '-trsf-type vectorfield' trsf_out, res = blockmatching( floating_img, reference_img, init_result_transformation=init_result_transformation, left_transformation=None, param_str_2=param_str_2) trsf_inp = BalTransformation() trsf_inp.read(deformable_trsf) np.testing.assert_array_almost_equal(trsf_out.mat.to_np_array(), trsf_inp.mat.to_np_array(), decimal=6) trsf_out.free() trsf_inp.free() return
def sequence_registration_rigid(list_images): #--- check SpatialImage sequence conds_init = isinstance(list_images, list) and len(list_images) >= 3 conds_list_img = [ 0 if isinstance(sp_img, SpatialImage) else 1 for sp_img in list_images ] #--- end check if conds_init and 1 not in conds_list_img: #--- pairwise registration pairwise_trsf = [] for ind, sp_img in enumerate(list_images): if ind < len(list_images) - 1: # --- rigid registration trsf_rig, res_rig = blockmatching( list_images[ind], list_images[ind + 1], param_str_2='-trsf-type rigid') pairwise_trsf.append(trsf_rig) #--- case rigid registration #--- composition of transformations list_compo_trsf, list_res_img = [], [] for ind, trsf in enumerate(pairwise_trsf): if ind < len(pairwise_trsf) - 1: comp_trsf = compose_trsf( pairwise_trsf[ind:]) # matrix multiplication list_compo_trsf.append(comp_trsf) elif ind == len(pairwise_trsf) - 1: list_compo_trsf.append(pairwise_trsf[-1]) #--- displacements compensation (whole sequence) for ind, trsf in enumerate(list_compo_trsf): tmp_img = apply_trsf(list_images[ind], trsf, template_img=list_images[-1] ) # global reference : list_images[-1] list_res_img.append(tmp_img) list_res_img.append(list_images[-1]) # add global reference image return list_compo_trsf, list_res_img
# -- 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') # --- deformable registration, initialisation by a rigid transformation print "\nPerforming deformable registration of t{} on t{}:".format(ind, ind + 1) trsf_vf, res_vf = blockmatching(sp_img, list_iso_img[ind + 1], left_transformation=trsf_rig, param_str_2='-trsf-type vectorfield') # --- composition of transformations print "\nPerforming composition of rigid et deformable registration..." res_trsf = compose_trsf([trsf_rig, trsf_vf], template_img=list_iso_img[ind+1]) list_res_trsf.append(res_trsf) # -- save the DEFORMABLE consecutive transformation: print "\nSaving {} transformation file: {}".format(trsf_type, res_trsf_fname) res_trsf.write(res_path + res_trsf_fname) # - Intensity image: if not exists(res_path + res_img_fname) or force:
def fusion(list_images, iterations=None): """ Multiview reconstruction (registration) Parameters ---------- :param list list_images: list of input ``SpatialImage`` :param int iterations: number of iterations, optional. Default: 5 Returns ---------- :return: ``SpatialImage`` instance -- image and metadata Example ------- >>> from timagetk.util import data_path >>> from timagetk.components import imread >>> from timagetk.algorithms import fusion >>> vues = [0, 1, 2] >>> list_images = [imread(data_path('fusion_img_' + str(vue) + '.inr')) for vue in vues] >>> fus_img = fusion(list_images) """ if iterations is None: iterations = 5 else: iterations = int(abs(iterations)) #--- check: list of SpatialImage images conds_init = isinstance(list_images, list) and len(list_images)>=2 conds_list_img = [0 if isinstance(sp_img, SpatialImage) else 1 for sp_img in list_images] #--- end check if conds_init and 1 not in conds_list_img: succ_ref_img = [] vox_list = [sp_img.get_voxelsize() for sp_img in list_images] vox_list = [i for i in itertools.chain.from_iterable(vox_list)] # voxel list ext_list = [sp_img.get_extent() for sp_img in list_images] ext_list = [i for i in itertools.chain.from_iterable(ext_list)] # extent list if list_images[0].get_dim() == 3: min_vox, val = np.min(vox_list), int(np.max(ext_list)/np.min(vox_list)) tmp_arr = np.zeros((val, val, val), dtype=list_images[0].dtype) template_img = SpatialImage(tmp_arr, voxelsize=[min_vox, min_vox, min_vox]) init_ref = apply_trsf(list_images[0], bal_transformation=None, template_img=template_img) succ_ref_img.append(init_ref) init_trsf_list, init_img_list = [], [] for ind, sp_img in enumerate(list_images): if ind>0: trsf_rig, res_rig = blockmatching(sp_img, init_ref, param_str_2='-trsf-type rigid -py-ll 1') trsf_aff, res_aff = blockmatching(sp_img, init_ref, left_transformation=trsf_rig, param_str_2='-trsf-type affine') tmp_trsf = compose_trsf([trsf_rig, trsf_aff]) trsf_def, res_def = blockmatching(sp_img, init_ref, init_result_transformation=tmp_trsf, param_str_2='-trsf-type vectorfield') out_trsf = BalTransformation(c_bal_trsf=trsf_def) init_trsf_list.append(out_trsf) init_img_list.append(res_def) init_img_list.append(init_ref) mean_ref = mean_images(init_img_list) mean_trsf = mean_trsfs(init_trsf_list) mean_trsf_inv = inv_trsf(mean_trsf) mean_ref_update = apply_trsf(mean_ref, mean_trsf_inv, template_img=template_img) succ_ref_img.append(mean_ref_update) for index in range(0, iterations): init_trsf_list, init_img_list = [], [] for ind, sp_img in enumerate(list_images): trsf_rig, res_rig = blockmatching(sp_img, mean_ref_update, param_str_2='-trsf-type rigid -py-ll 1') trsf_aff, res_aff = blockmatching(sp_img, mean_ref_update, left_transformation=trsf_rig, param_str_2='-trsf-type affine') tmp_trsf = compose_trsf([trsf_rig, trsf_aff]) trsf_def, res_def = blockmatching(sp_img, mean_ref_update, init_result_transformation=tmp_trsf, param_str_2='-trsf-type vectorfield') out_trsf = BalTransformation(c_bal_trsf=trsf_def) init_trsf_list.append(out_trsf) init_img_list.append(res_def) init_img_list.append(mean_ref_update) mean_ref = mean_images(init_img_list) mean_trsf = mean_trsfs(init_trsf_list) mean_trsf_inv = inv_trsf(mean_trsf) mean_ref_update = apply_trsf(mean_ref, mean_trsf_inv, template_img=template_img) succ_ref_img.append(mean_ref_update) return succ_ref_img[-1] else: print('Incorrect specification') return
def rigid_registration(floating_img, reference_img): trsf_rig, res_rig = blockmatching(floating_img, reference_img) return trsf_rig, res_rig
def fusion(list_images, iterations=None): """ Multiview reconstruction (registration) Parameters ---------- :param list list_images: list of input ``SpatialImage`` :param int iterations: number of iterations, optional. Default: 5 Returns ---------- :return: ``SpatialImage`` instance -- image and metadata Example ------- >>> from timagetk.util import data_path >>> from timagetk.components import imread >>> from timagetk.algorithms import fusion >>> vues = [0, 1, 2] >>> list_images = [imread(data_path('fusion_img_' + str(vue) + '.inr')) for vue in vues] >>> fus_img = fusion(list_images) """ if iterations is None: iterations = 5 else: iterations = int(abs(iterations)) #--- check: list of SpatialImage images conds_init = isinstance(list_images, list) and len(list_images) >= 2 conds_list_img = [ 0 if isinstance(sp_img, SpatialImage) else 1 for sp_img in list_images ] #--- end check if conds_init and 1 not in conds_list_img: succ_ref_img = [] vox_list = [sp_img.get_voxelsize() for sp_img in list_images] vox_list = [i for i in itertools.chain.from_iterable(vox_list) ] # voxel list ext_list = [sp_img.get_extent() for sp_img in list_images] ext_list = [i for i in itertools.chain.from_iterable(ext_list) ] # extent list if list_images[0].get_dim() == 3: min_vox, val = np.min(vox_list), int( np.max(ext_list) / np.min(vox_list)) tmp_arr = np.zeros((val, val, val), dtype=list_images[0].dtype) template_img = SpatialImage(tmp_arr, voxelsize=[min_vox, min_vox, min_vox]) init_ref = apply_trsf(list_images[0], bal_transformation=None, template_img=template_img) succ_ref_img.append(init_ref) init_trsf_list, init_img_list = [], [] for ind, sp_img in enumerate(list_images): if ind > 0: trsf_rig, res_rig = blockmatching( sp_img, init_ref, param_str_2='-trsf-type rigid -py-ll 1') trsf_aff, res_aff = blockmatching( sp_img, init_ref, left_transformation=trsf_rig, param_str_2='-trsf-type affine') tmp_trsf = compose_trsf([trsf_rig, trsf_aff]) trsf_def, res_def = blockmatching( sp_img, init_ref, init_result_transformation=tmp_trsf, param_str_2='-trsf-type vectorfield') out_trsf = BalTransformation(c_bal_trsf=trsf_def) init_trsf_list.append(out_trsf) init_img_list.append(res_def) init_img_list.append(init_ref) mean_ref = mean_images(init_img_list) mean_trsf = mean_trsfs(init_trsf_list) mean_trsf_inv = inv_trsf(mean_trsf) mean_ref_update = apply_trsf(mean_ref, mean_trsf_inv, template_img=template_img) succ_ref_img.append(mean_ref_update) for index in range(0, iterations): init_trsf_list, init_img_list = [], [] for ind, sp_img in enumerate(list_images): trsf_rig, res_rig = blockmatching( sp_img, mean_ref_update, param_str_2='-trsf-type rigid -py-ll 1') trsf_aff, res_aff = blockmatching( sp_img, mean_ref_update, left_transformation=trsf_rig, param_str_2='-trsf-type affine') tmp_trsf = compose_trsf([trsf_rig, trsf_aff]) trsf_def, res_def = blockmatching( sp_img, mean_ref_update, init_result_transformation=tmp_trsf, param_str_2='-trsf-type vectorfield') out_trsf = BalTransformation(c_bal_trsf=trsf_def) init_trsf_list.append(out_trsf) init_img_list.append(res_def) init_img_list.append(mean_ref_update) mean_ref = mean_images(init_img_list) mean_trsf = mean_trsfs(init_trsf_list) mean_trsf_inv = inv_trsf(mean_trsf) mean_ref_update = apply_trsf(mean_ref, mean_trsf_inv, template_img=template_img) succ_ref_img.append(mean_ref_update) return succ_ref_img[-1] else: print('Incorrect specification') return