def test_plugin_deformable(self): #--- deformable registration floating_img = imread(data_path('time_0_cut.inr')) reference_img = imread(data_path('time_1_cut.inr')) deformable_trsf = data_path("deformable_0_1.trsf") trsf_inp = BalTransformation() trsf_inp.read(deformable_trsf) trsf_out, res = registration(floating_img, reference_img, method='deformable_registration') 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
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) list_comp_trsf, list_res_img = [], [] # if not np.all([exists(f) for f in res_trsf_list]) or force: if not np.all([exists(f) for f in seq_res_trsf_list]) or force: if not_sequence: list_comp_trsf, list_res_img = registration( list_img[0], list_img[1], method='{}_registration'.format(trsf_type)) list_comp_trsf = [list_comp_trsf] else: print "\n# - Computing sequence {} registration:".format( trsf_type.upper()) list_comp_trsf, list_res_img = sequence_registration( list_img, method='sequence_{}_registration'.format(trsf_type)) # - Save estimated tranformations: for seq_trsf, seq_trsf_fname in zip(list_comp_trsf, seq_res_trsf_list): print "Saving computed SEQUENCE {} transformation file: {}".format( trsf_type.upper(), seq_trsf_fname) save_trsf(seq_trsf, seq_trsf_fname) else: for seq_trsf_fname in seq_res_trsf_list: print "Loading existing SEQUENCE {} transformation file: {}".format(
L1_evaluations['Expert'] = L1_evaluation # RIGID Registration: ########################### trsfs = {} if image_registration: for filename in filenames: trsf_fname = image_dirname + filename[:-4] + "_rigid_to_expert.trsf" if exists(trsf_fname): trsfs[filename] = np.loadtxt(trsf_fname) else: float_image = imread(image_dirname + xp_filename) ref_image = imread(image_dirname + filename) trsfs[filename], res_img = registration( float_image, ref_image, method="rigid_registration", pyramid_lowest_level=1) del res_img mat = trsfs[filename].mat.to_np_array() np.savetxt(trsf_fname, mat, fmt='%1.8f') for filename in filenames: print "\n\n# Comparing expert seeds to auto seeds from {}...".format( filename) evaluations[filename] = [] L1_evaluations[filename] = [] suffix = "_" + filename topomesh_file = image_dirname + filename + "_seed_wat_detection_topomesh.ply" # reload image, might be necessary for size and voxelsize variables: image_filename = image_dirname + filename
raise ImportError('Import Error') out_path = './results/' # to save results # we consider two different times # time_1 is the floating image # time_2 is the reference image times = [1, 2] # list of SpatialImage instances list_images = [ imread(data_path('time_' + str(time) + '.inr')) for time in times ] floating_img, reference_img = list_images[0], list_images[1] # Rigid registration: trsf_rig, res_rig = registration(floating_img, reference_img, method='rigid_registration') # display the spatial transformation (4x4 matrix): trsf_rig.c_display() # save the spatial transformation: res_name = 'example_trsf_rigid.trsf' # filename trsf_rig.write(out_path + res_name) # save the result image: res_name = 'example_reg_rigid_1_2.tif' # filename # SpatialImage to .tif format imsave(out_path + res_name, res_rig) # Affine registration: trsf_aff, res_aff = registration(floating_img, reference_img, method='affine_registration')
from timagetk.io import imsave from timagetk.plugins import registration from timagetk.plugins import morphology raw_ref_img = imread("/data/GabriellaMosca/EM_C_140/EM_C_140- C=0.tif") raw_float_img = imread("/data/GabriellaMosca/EM_C_214/EM_C_214 C=0.tif") ref_img = imread("/data/GabriellaMosca/EM_C_140/EM_C_140-masked.tif") float_img = imread("/data/GabriellaMosca/EM_C_214/EM_C_214-masked.tif") init_trsf = BalTrsf() init_trsf.read('/data/GabriellaMosca/T140_214.trsf') trsf_def, img_def = registration(float_img, ref_img, method='deformable', init_trsf=init_trsf) imsave("/data/GabriellaMosca/EM_C_214/EM_C_214-masked_deformable.tif", img_def) from timagetk.visu.mplt import grayscale_imshow img2plot = [float_img, ref_img, img_rig, ref_img] img_titles = ["t0", "t1", "Registered t0 on t1", "t1"] grayscale_imshow(img2plot, "Effect of rigid registration", img_titles, vmin=0, vmax=255, max_per_line=2) img2plot = [ float_img.get_z_slice(40),
# - 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: print "\nFound existing tranformation t{}->t{}!".format(i_float, i_ref) print "--> Reading {} transformation file: '{}'".format(trsf_type.upper(), out_trsf_fname) out_trsf = read_trsf(out_folder + out_trsf_fname) else: print "\nFound existing tranformation t{}->t{}!".format(i_float, i_ref) print "--> Nothing left to do here..." if write_cons_img: print "\n{} registration of the float intensity image:".format(trsf_type.upper()) # - Defines the registered image filename (output): out_img_fname = get_res_img_fname(float_img_fname, t_ref, t_float, trsf_type) if not exists(join(out_folder, out_img_fname)) or force:
print "\n# - RIGID registration for t{}/t{}:".format( time2index[t_float], time2index[t_ref]) py_hl = 3 # defines highest level of the blockmatching-pyramid py_ll = 1 # defines lowest level of the blockmatching-pyramid print ' - t_{}h floating fname: {}'.format(t_float, float_img_fname) im_float = imread(image_dirname + float_path_suffix + float_img_fname) if float_img_fname.find('iso') == -1: # if not isometric, resample! im_float = isometric_resampling(im_float) 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)
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) print ' - t_{}h reference fname: {}'.format( time_steps[-1], ref_img_fname) print ' - {} t_{}h/t_{}h composed-trsf as initialisation'.format( trsf_type, t, time_steps[-1]) print "" res_trsf, res_im = registration( float_im, ref_im, method='{}_registration'.format(trsf_type), init_trsf=trsf, pyramid_highest_level=py_hl, pyramid_lowest_level=py_ll) print "" # - Save result image and tranformation: print "Writing image file: {}".format(res_img_fname) imsave(res_path + res_img_fname, res_im) print "Writing trsf file: {}".format(res_trsf_fname) res_trsf.write(res_path + res_trsf_fname) else: print "Existing image file: {}".format(res_img_fname) print "Loading existing {} transformation file: {}".format( trsf_type.upper(), res_trsf_fname) res_trsf = bal_trsf.BalTransformation()
list_img = [] print "\n# - Loading list of images to register:" for n, img_fname in enumerate(list_img_fname): print " - Time-point {}, reading image {}...".format(n, img_fname) im = read_image(img_fname) # print " ---> Contrast stretching...".format(n, img_fname) # im = z_slice_contrast_stretch(im) list_img.append(im) list_comp_trsf, list_res_img = [], [] # if not np.all([exists(f) for f in res_trsf_list]) or force: if not np.all([exists(f) for f in seq_res_trsf_list]) or force: if not_sequence: list_comp_trsf, list_res_img = registration( list_img[0], list_img[1], method=trsf_type, pyramid_highest_level=py_hl, pyramid_lowest_level=py_ll) list_comp_trsf = [list_comp_trsf] else: print "\n# - Computing SEQUENCE {} registration:".format( trsf_type.upper()) list_comp_trsf, list_res_img = sequence_registration( list_img, method=trsf_type, return_images=True, pyramid_highest_level=py_hl, pyramid_lowest_level=py_ll) # - Save estimated tranformations: for seq_trsf, seq_trsf_fname in zip(list_comp_trsf, seq_res_trsf_list): print "Saving computed SEQUENCE {} transformation file: {}".format(
if not os.path.isdir(out_path): new_fold = os.path.join(os.getcwd(),'results') os.mkdir(new_fold) # we consider two different times # time_1 is the floating image # time_2 is the reference image times = [1, 2] # list of SpatialImage instances list_images = [imread(data_path('time_' + str(time) + '.inr')) for time in times] floating_img, reference_img = list_images[0], list_images[1] # Rigid registration: trsf_rig, res_rig = registration(floating_img, reference_img, method='rigid_registration') # display the spatial transformation (4x4 matrix): trsf_rig.c_display() # save the spatial transformation: res_name = 'example_trsf_rigid.trsf' # filename trsf_rig.write(out_path+res_name) # save the result image: res_name = 'example_reg_rigid_1_2.tif' # filename # SpatialImage to .tif format imsave(out_path+res_name, res_rig) # Affine registration: trsf_aff, res_aff = registration(floating_img, reference_img, method='affine_registration')
def apply_mask(img, mask): return SpatialImage(img.get_array() * mask, origin=img.origin, voxelsize=img.voxelsize, metadata=img.metadata) float_img = apply_mask(raw_float_img, float_mask) imsave("/data/GabriellaMosca/EM_C_140/EM_C_140-masked.tif", float_img) ref_img = apply_mask(raw_ref_img, ref_mask) imsave("/data/GabriellaMosca/EM_C_214/EM_C_214-masked.tif", ref_img) trsf_rig, img_rig = registration(float_img, ref_img, method='rigid') imsave("/data/GabriellaMosca/EM_C_140/EM_C_140-masked_rigid.tif", img_rig) trsf_aff, img_aff = registration(float_img, ref_img, method='affine') imsave("/data/GabriellaMosca/EM_C_140/EM_C_140-masked_affine.tif", img_aff) trsf_def, img_def = registration(float_img, ref_img, method='deformable') imsave("/data/GabriellaMosca/EM_C_140/EM_C_140-masked_deformable.tif", img_def) from timagetk.visu.mplt import grayscale_imshow img2plot = [float_img, ref_img, img_rig, ref_img] img_titles = ["t0", "t1", "Registered t0 on t1", "t1"] grayscale_imshow(img2plot, "Effect of rigid registration", img_titles, vmin=0,