def my_main(X,Y, escala): callfun=1 if callfun==1: fig = plt.figure() fig.add_axes([0, 0, 1, 1]) callback = partial(visualize, ax=fig.axes[0]) reg = affine_registration(X, Y, tolerance=0.0001, w=0.0001, escala=escala) reg.register(callback) #plt.show() elif callfun==0: reg = affine_registration(X, Y, tolerance=0.0001, w=0.0001, escala=escala) reg.register(None) return reg
def main(): X = np.loadtxt('C:/DS_git/pycpd/data/fish_target.txt') Y = np.loadtxt('C:/DS_git/pycpd/data/fish_source.txt') fig = plt.figure() fig.add_axes([0, 0, 1, 1]) callback = partial(visualize, ax=fig.axes[0]) reg = affine_registration(**{'X': X, 'Y': Y}) reg.register(callback) plt.show()
def main(): fish = loadmat('../data/fish.mat') X = fish['X'] Y = fish['Y'] fig = plt.figure() fig.add_axes([0, 0, 1, 1]) callback = partial(visualize, ax=fig.axes[0]) reg = affine_registration(X, Y) reg.register(callback) plt.show()
def test_2D(): B = np.array([[1.0, 0.5], [0, 1.0]]) t = np.array([0.5, 1.0]) Y = np.loadtxt('data/fish_target.txt') X = np.dot(Y, B) + np.tile(t, (np.shape(Y)[0], 1)) reg = affine_registration(**{'X': X, 'Y': Y}) TY, (B_reg, t_reg) = reg.register() assert_array_almost_equal(B, B_reg) assert_array_almost_equal(t, t_reg) assert_array_almost_equal(X, TY)
def Update(self, target, source, mode='affine'): # registration import pycpd as cpd self.source = source if mode == 'affine': self.reg = cpd.affine_registration(target, self.source, tolerance=1e-3) else: self.reg = cpd.rigid_registration(target, self.source, tolerance=1e-3) self.reg.register(callback=None)
def test_3D(): B = np.array([[1.0, 0.5, 0.0], [0, 1.0, 0.0], [0.0, 0.0, 1.0]]) t = np.array([0.5, 1.0, -2.0]) fish_target = np.loadtxt('data/fish_target.txt') Y1 = np.zeros((fish_target.shape[0], fish_target.shape[1] + 1)) Y1[:, :-1] = fish_target Y2 = np.ones((fish_target.shape[0], fish_target.shape[1] + 1)) Y2[:, :-1] = fish_target Y = np.vstack((Y1, Y2)) X = np.dot(Y, B) + np.tile(t, (np.shape(Y)[0], 1)) reg = affine_registration(**{'X': X, 'Y': Y}) TY, (B_reg, t_reg) = reg.register() assert_array_almost_equal(B, B_reg) assert_array_almost_equal(t, t_reg) assert_array_almost_equal(X, TY)
def Update(self, target, source, mode='affine'): # registration import pycpd as cpd from functools import partial self.source = source nodes_before = np.array(self.source.GetNodesPos()) if mode == 'affine': self.reg = cpd.affine_registration(np.array(target.GetNodesPos()), nodes_before, tolerance=1e-3) else: self.reg = cpd.rigid_registration(np.array(target.GetNodesPos()), nodes_before, tolerance=1e-3) self.reg.register(callback=None) self.reg.updateTransform()
def main(): fish_target = np.loadtxt('data/fish_target.txt') X1 = np.zeros((fish_target.shape[0], fish_target.shape[1] + 1)) X1[:, :-1] = fish_target X2 = np.ones((fish_target.shape[0], fish_target.shape[1] + 1)) X2[:, :-1] = fish_target X = np.vstack((X1, X2)) fish_source = np.loadtxt('data/fish_source.txt') Y1 = np.zeros((fish_source.shape[0], fish_source.shape[1] + 1)) Y1[:, :-1] = fish_source Y2 = np.ones((fish_source.shape[0], fish_source.shape[1] + 1)) Y2[:, :-1] = fish_source Y = np.vstack((Y1, Y2)) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') callback = partial(visualize, ax=ax) reg = affine_registration(**{'X': X, 'Y': Y}) reg.register(callback) plt.show()
def main(): fish = loadmat('../data/fish.mat') X1 = np.zeros((fish['X'].shape[0], fish['X'].shape[1] + 1)) X1[:, :-1] = fish['X'] X2 = np.ones((fish['X'].shape[0], fish['X'].shape[1] + 1)) X2[:, :-1] = fish['X'] X = np.vstack((X1, X2)) Y1 = np.zeros((fish['Y'].shape[0], fish['Y'].shape[1] + 1)) Y1[:, :-1] = fish['Y'] Y2 = np.ones((fish['Y'].shape[0], fish['Y'].shape[1] + 1)) Y2[:, :-1] = fish['Y'] Y = np.vstack((Y1, Y2)) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') callback = partial(visualize, ax=ax) reg = affine_registration(X, Y) reg.register(callback) plt.show()
def RegisterGraph(target, source, mode='affine', tolerance=1e-3): # registration import pycpd as cpd from functools import partial nodes_before = np.array(source.GetNodesPos()) if mode == 'affine': new_pos = cpd.affine_registration(np.array(target.GetNodesPos()), nodes_before, tolerance=tolerance) else: new_pos = cpd.rigid_registration(np.array(target.GetNodesPos()), nodes_before, tolerance=tolerance) new_pos.register(callback=None) r = new_pos.updateTransform() nodes_after = new_pos.TY for idx, i in zip(source.GetNodes(), nodes_after): source.node[idx]['pos'] = i return source
# calculate intial rotation matrix based on anchor cells source_anchor -= np.mean(source_anchor, axis=0) target_anchor -= np.mean(target_anchor, axis=0) rot_mat = calc_rot(source_anchor, target_anchor) print rot_mat cell_coords -= np.mean(cell_coords, axis=0) transformed_init = np.matmul(rot_mat, cell_coords.transpose()).transpose() # calculate affine transform using coherent point drift fig = plt.figure() ax = fig.add_subplot(111, projection='3d') callback = partial(visualize, ax=ax) reg = affine_registration(**{ 'X': target_coords, 'Y': transformed_init, 'max_iterations': 10000 }) reg.register(callback) if plot_process: plt.show() transformed_coords = reg.TY new_transformed_coords, new_target_coords, new_tcID, new_tcID = get_relay_only( transformed_coords, target_coords, cell_ID, tc_ID) # get nearest neighbor assignments only (iterations=1), could also get affine transform using icp icp1 = get_icp(new_transformed_coords, new_target_coords, 'Similarity') transformed_coords = get_transformed(icp1, transformed_coords) if plot_process:
surfaceVoxels_flo2_red = surfaceVoxels_flo2[np.random.choice(surfaceVoxels_flo2.shape[0], 4000, replace = False)] print len(surfaceVoxels_flo2) ### Applying CPD Registration of Floating Image 1 on Reference ### print("Calculating CPD Affine registration for Floating 1 on Reference") init_transform = np.matmul(ijk_to_xyz_flo1,inv(ijk_to_xyz_ref)) surfaceVoxels_flo1m_red = apply_affine(surfaceVoxels_flo1_red, init_transform) surfaceVoxels_flo1m = apply_affine(surfaceVoxels_flo1,init_transform) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') callback = partial(visualize, ax=ax) reg = affine_registration(surfaceVoxels_ref_red, surfaceVoxels_flo1m_red,maxIterations=100) Treg = reg.register(callback) TY = Treg[0] affine_mat = np.concatenate((Treg[1],np.atleast_2d(Treg[2]).T),axis = 1) T1 = np.concatenate((affine_mat,np.array([[0,0,0,1]])),axis = 0) print (affine_mat) print("") print("You may now please exit the visulization.") #plt.show() ### Applying CPD Registration of Floating Image 2 on Reference ### print("Calculating CPD Affine registration for Floating 2 on Reference") init_transform = np.matmul(ijk_to_xyz_flo2,inv(ijk_to_xyz_ref)) surfaceVoxels_flo2m_red = apply_affine(surfaceVoxels_flo2_red, init_transform) surfaceVoxels_flo2m = apply_affine(surfaceVoxels_flo2,init_transform)
target_anchor -= np.mean(target_anchor, axis=0) rot_mat = calc_rot(source_anchor, target_anchor) print rot_mat pr_coords -= np.mean(pr_coords, axis=0) transformed_init = np.matmul(rot_mat, pr_coords.transpose()).transpose() else: transformed_init = pr_coords # calculate affine transform using coherent point drift fig = plt.figure() ax = fig.add_subplot(111, projection='3d') callback = partial(visualize, ax=ax) reg = affine_registration(**{ 'X': tp_coords, 'Y': transformed_init, 'max_iterations': 10000 }) reg.register(callback) if plot_process: plt.show() transformed_coords = reg.TY new_transformed_coords, new_tp_coords, new_pr_ID, new_tp_ID = get_pr_only( transformed_coords, tp_coords, pr_ID, tp_ID) # This is just to get nearest neighbors, but could be used to calculate the similarity transformation using icp icp1 = get_icp(new_transformed_coords, new_tp_coords, 'Similarity') transformed_coords = get_transformed(icp1, transformed_coords) pr_map, ind_map = create_coord_map(transformed_coords, tp_coords, pr_ID, tp_ID)