def pbr(I_path, Im_path): I = plt.imread(I_path) Im = plt.imread(Im_path) X, Xm = util.my_cpselect(I_path, Im_path) if len(X[1]) == 1: print('x must be larger than 1') return X = util.c2h(X) Xm = util.c2h(Xm) T = reg.ls_affine(X, Xm) It, Xt = reg.image_transform(Im, T) fig = plt.figure(figsize=(30, 30)) ax1 = fig.add_subplot(121) ax1.set_title('Overlay of original images', fontsize=35) ax1.axis('off') im11 = ax1.imshow(I) im12 = ax1.imshow(Im, alpha=0.7) ax2 = fig.add_subplot(122) ax2.set_title('Overlay transformed image over the fixed image', fontsize=35) ax2.axis('off') im21 = ax2.imshow(I) im22 = ax2.imshow(It, alpha=0.7) return I, Im, It
def pbr(I_path, Im_path): # Load in images I = plt.imread(I_path) Im = plt.imread(Im_path) # Set control points X, Xm = util.my_cpselect(I_path, Im_path) Xh = util.c2h(X) Xmh = util.c2h(Xm) # Find optimal affine transformation T = ls_affine(Xh, Xmh) # Transform Im Im_t, X_t = image_transform(Im, T) # Display images fig = plt.figure(figsize=(12, 5)) ax1 = fig.add_subplot(131) im11 = ax1.imshow(I) ax2 = fig.add_subplot(132) im21 = ax2.imshow(Im) ax3 = fig.add_subplot(133) im31 = ax3.imshow(I) im32 = ax3.imshow(Im_t, alpha=0.7) ax1.set_title('Original image (I)') ax2.set_title('Warped image (Im)') ax3.set_title('Overlay with I and transformed Im')
def point_based_affine_registration(I, Im): # let the user selecte points X, Xm = util.my_cpselect(I, Im) # find affine transformation matrix T, reg_error = reg.ls_affine(util.c2h(X), util.c2h(Xm)) # transform the moving image according to T Im = plt.imread(Im) It, _ = reg.image_transform(Im, T) return It, reg_error
def point_based_registration_demo(): # --------------------------------- # Append the following code in jupiter to run: # %matplotlib inline # import sys # sys.path.append("C:/Users/s163666/Documents/2019-2020/Medical Image Analysis/Mini_project_mia") # from project_fout import point_based_registration_demo # point_based_registration_demo() # --------------------------------- # read the fixed and moving images, 2x T1 or T1&T2 # change these in order to read different images I_path = './data/image_data/3_1_t1.tif' Im_path = './data/image_data/3_1_t2.tif' #Select set of corresponding points using my_cpselect X0, Xm0 = util.my_cpselect(I_path, Im_path) #Compute the affine transformation between the pair of images Xm = np.ones((3, Xm0.shape[1])) Xm[0, :] = Xm0[0, :] Xm[1, :] = Xm0[1, :] X = np.ones((3, X0.shape[1])) X[0, :] = X0[0, :] X[1, :] = X0[1, :] T, Etrain = reg.ls_affine(X, Xm) Etest = reg.point_based_error(I_path, Im_path, T) #read image Im = plt.imread(Im_path) #Apply the affine transformation to the moving image It, Xt = reg.image_transform(Im, T) #plot figure fig = plt.figure(figsize=(12, 5)) #fig, ax = plt.subplots() ax = fig.add_subplot(121) im = ax.imshow(It) ax.set_title("Transformed image") ax.set_xlabel('x') ax.set_ylabel('y') print(Etrain) print(Etest) display(fig) fig.savefig('./data/image_results/3_1_t1 + 3_1_t2.png')
def my_point_based_registration(): I = '../data/image_data/3_2_t1.tif' I_d = '../data/image_data/3_2_t1_d.tif' I_plt = plt.imread('../data/image_data/3_2_t1.tif') I_d_plt = plt.imread('../data/image_data/3_2_t1_d.tif') X, Xm = util.my_cpselect(I, I_d) affine_transformation = reg.ls_affine(X, Xm) transformed_moving_image, transformed_vector = reg.image_transform( I_d_plt, affine_transformation) fig = plt.figure(figsize=(12, 5)) ax1 = fig.add_subplot(131) Im1 = ax1.imshow(I_plt) # Fixed image or Transformed image Im2 = ax1.imshow(transformed_moving_image, alpha=0.7) # Transformed image or Fixed image
def pbr(I_path, Im_path): # Load in images I = plt.imread(I_path) Im = plt.imread(Im_path) # Set control points X, Xm = util.my_cpselect(I_path, Im_path) Xh = util.c2h(X) Xmh = util.c2h(Xm) # Find optimal affine transformation T = ls_affine(Xh, Xmh) # Transform Im Im_t, X_t = image_transform(Im, T) return Im_t, X_t, X, Xm, T
def point_based_error(I_path,Im_path,T): #Select set of corresponding points using my_cpselect X0, Xm0 = util.my_cpselect(I_path, Im_path) Xm = np.ones((3, Xm0.shape[1])) Xm[0,:] = Xm0[0,:] Xm[1,:]= Xm0[1,:] X = np.ones((3, X0.shape[1])) X[0,:] = X0[0,:] X[1,:]= X0[1,:] b1 = np.transpose(X[0,:]) b2 = np.transpose(X[1,:]) #Compute the affine transformation between the pair of images _, Etestx = reg.ls_solve(T,b1) _, Etesty = reg.ls_solve(T,b2) Etest = np.array([[Etestx],[Etesty]]) return Etest
def my_point_based_registration_2(): I2 = '../data/image_data/3_2_t1.tif' # Tweede opdracht T1 slide I_d_2 = '../data/image_data/3_2_t2.tif' # T2 slice I_plt_2 = plt.imread('../data/image_data/3_2_t1.tif') I_d_plt_2 = plt.imread('../data/image_data/3_2_t1_d.tif') X2, Xm2 = util.my_cpselect(I2, I_d_2) affine_transformation_2 = reg.ls_affine(X2, Xm2) transformed_moving_image_2, transformed_vector_2 = reg.image_transform( I_d_plt_2, affine_transformation_2) fig = plt.figure(figsize=(12, 5)) ax1 = fig.add_subplot(131) Im1 = ax1.imshow( I_plt_2) # Fixed image or Transformed image, Je mag wisselen Im2 = ax1.imshow( transformed_moving_image_2, alpha=0.7) # Transformed image or Fixed image Mag wisselen
# # ##calculate correlation #display("Calculate correlation") #corr_I1 = reg.correlation(I1, It1) # ##calculate mutual information #display("Calculate mutual information") #p1 = reg.joint_histogram(I1, It1) #mi_I1 = reg.mutual_information(p1) """ Part 2: T1 and T2 """ ##choose fiducial points and save them for T1 and T1 moving X1, X2 = util.my_cpselect(I1_path, I2_path) Th2, fig2, It2 = proj.point_based_registration(I1_path, I2_path, X1, X2) np.save( path + "/transformation_matrix_T1_and_T2_moving_" + str(numberofpoints), Th2) plt.savefig(path + "/transformed_T2_image_" + str(numberofpoints) + ".png") #calculate correlation display("Calculate correlation") corr_I2 = reg.correlation(I1, It2) #calculate mutual information display("Calculate mutual information") p2 = reg.joint_histogram(I1, It2) mi_I2 = reg.mutual_information(p2) """
#DO NOT CHANGE FOLLOWING LINES #Select images: T1 and T1 moving and T2 slice #paths of the images I1_path = '../data/image_data/1_1_t1.tif' Im1_path = '../data/image_data/1_1_t1_d.tif' I2_path = '../data/image_data/1_1_t2.tif' #read the images I1 = plt.imread(I1_path) Im1 = plt.imread(Im1_path) Im2 = plt.imread(I2_path) if set_of_images == 1: full_path = path + "/transformation_matrix_T1_and_T1_moving_" + str( numberofpoints) + ".npy" X1_target, Xm1_target = util.my_cpselect(I1_path, Im1_path) else: full_path = path + "/transformation_matrix_T1_and_T2_" + str( numberofpoints) + ".npy" X1_target, Xm1_target = util.my_cpselect(I1_path, I2_path) transformation_matrix = np.load(full_path) """ Evaluation of point-based affine image registration """ Reg_error1 = proj.Evaluate_point_based_registration(transformation_matrix, X1_target, Xm1_target) if set_of_images == 1: print('Registration error for pair of T1 and T1 moving: {:.2f}'.format(