def correlation_test(): I = plt.imread('../data/t1_demo.tif') Th = util.t2h(reg.identity(), np.array([10, 20])) J, _ = reg.image_transform(I, Th) K, _ = reg.image_transform(J, Th) C1 = reg.correlation(I, I) C2 = reg.correlation(J, J) # the self correlation should be very close to 1 assert abs( C1 - 1 ) < 10e-10, "Correlation function is incorrectly implemented (self correlation test)" assert abs( C2 - 1 ) < 10e-10, "Correlation function is incorrectly implemented (self correlation test)" C3 = reg.correlation(I, J) C4 = reg.correlation(I, K) print("Correlation between images with slight translocation:", C3) print("Correlation between images with big translocation:", C4, "\n") print('Test successful!') ######## print("\n\nAnd now for a test on the joint probability histogram: \n") p = reg.joint_histogram(I, J) assert abs(1 - float(np.sum(p))) < 0.01, "Mass probability function error..." print("Test successful!")
def correlation_test(): I = plt.imread('../data/cameraman.tif') Th = util.t2h(reg.identity(), np.array([10, 20])) J, _ = reg.image_transform(I, Th) C1 = reg.correlation(I, I) # the self correlation should be very close to 1 assert abs(C1 - 1) < 10e-10, "Correlation function is incorrectly implemented (self correlation test)" inv_lin_tr = np.array([[0,1],[1,0]]) T = util.t2h(reg.identity(),np.array([0,0])) K,_ = reg.image_transform(I,T) C2 = reg.correlation(I,K) assert abs(C2-1) < 10e-10, "Correlation function is incorrectly implemented (self correlation test)" print('Test successful!')
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 affine_corr(I, Im, x, MI): # Computes normalized cross-correlation between a fixed and # a moving image transformed with an affine transformation. # Input: # I - fixed image # Im - moving image # x - parameters of the rigid transform: the first element # is the rotation angle, the second and third are the # scaling parameters, the fourth and fifth are the # shearing parameters and the remaining two elements # are the translation # Output: # C - normalized cross-correlation between I and T(Im) # Im_t - transformed moving image T(Im) Tro = reg.rotate(x[0]) Tsc = reg.scale(x[1], x[2]) Tsh = reg.shear(x[3], x[4]) Trss = Tro.dot(Tsc).dot(Tsh) Th = util.t2h(Trss, [x[5], x[6]]) Im_t, Xt = reg.image_transform(Im, Th) if MI: p = joint_histogram(I, Im_t) S = reg.mutual_information(p) else: S = reg.correlation(I, Im_t) return S, Im_t, Th
def rigid_corr(I, Im, x, MI): # Computes normalized cross-correlation between a fixed and # a moving image transformed with a rigid transformation. # Input: # I - fixed image # Im - moving image # x - parameters of the rigid transform: the first element # is the rotation angle and the remaining two elements # are the translation # Output: # C - normalized cross-correlation between I and T(Im) # Im_t - transformed moving image T(Im) SCALING = 100 # the first element is the rotation angle T = reg.rotate(x[0]) Th = util.t2h(T, x[1:] * SCALING) # transform the moving image Im_t, Xt = reg.image_transform(Im, Th) # compute the similarity between the fixed and transformed moving image if MI: p = joint_histogram(I, Im_t) S = reg.mutual_information(p) else: S = reg.correlation(I, Im_t) return S, Im_t, Th
def correlation_test(): I = plt.imread('../data/cameraman.tif') Th = util.t2h(reg.identity(), np.array([10, 20])) J, _ = reg.image_transform(I, Th) C1 = reg.correlation(I, I) # the self correlation should be very close to 1 assert abs( C1 - 1 ) < 10e-10, "Correlation function is incorrectly implemented (self correlation test)" #------------------------------------------------------------------# # TODO: Implement a few more tests of the correlation definition #correlation when two signals are exaclty opposite should be very close to -1 C2 = reg.correlation(I, -I) assert abs( C2 + 1 ) < 10e-10, "Correlation function is incorrectly implemented (opposite self correlation test)" #detect corrrelation of two signals with different amplitudes should be 1 C3 = reg.correlation(I, I / 2) assert abs( C3 - 1 ) < 10e-10, "Correlation function is incorrectly implemented (different amplitude self correlation test)" C4 = reg.correlation(I, J) print(C4) assert abs( C4 - 1 ) < 10e-1, "Correlation function is incorrectly implemented (two diff images no correlation)" #------------------------------------------------------------------# print('Test successful!')
def point_based_registration(I_path, Im_path, X, Xm): #read/load the images I and Im imageI = plt.imread(I_path) imageIm = plt.imread(Im_path) #convert to homogenous coordinates using c2h X_h = util.c2h(X) Xm_h = util.c2h(Xm) #compute affine transformation and make a homogenous transformation matrix Th = reg.ls_affine(X_h, Xm_h) #transfrom the moving image using the transformation matrix It, Xt = reg.image_transform(imageIm, Th) #plotting the results fig = plt.figure(figsize=(20, 30)) ax1 = fig.add_subplot(131) im1 = ax1.imshow(imageI) #plot first image (fixed or T1) image ax2 = fig.add_subplot(132) im2 = ax2.imshow(imageIm) #plot second image (moving or T2) image ax3 = fig.add_subplot(133) im3 = ax3.imshow(It) #plot (T1 moving or T2) transformed image ax1.set_title('T1 (fixed)') ax2.set_title('T1 moving or T2') ax3.set_title('Transformed (T1 moving or T2) image') return Th
def affine_mi_adapted(I, Im, x): # Computes mutual information between a fixed and # a moving image transformed with an affine transformation. # Input: # I - fixed image # Im - moving image # x - parameters of the rigid transform: the first element # is the rotation angle, the second and third are the # scaling parameters, the fourth and fifth are the # shearing parameters and the remaining two elements # are the translation # Output: # MI - mutual information between I and T(Im) # Im_t - transformed moving image T(Im) NUM_BINS = 64 SCALING = 100 #------------------------------------------------------------------# # TODO: Implement the missing functionality T_rotate = reg.rotate(x[0]) T_scaled = reg.scale(x[1], x[2]) T_shear = reg.shear(x[3], x[4]) t = np.array(x[5:]) * SCALING T_total = T_shear.dot((T_scaled).dot(T_rotate)) Th = util.t2h(T_total, t) Im_t, Xt = reg.image_transform(Im, Th) p = reg.joint_histogram(Im, Im_t) MI = reg.mutual_information(p) #------------------------------------------------------------------# return MI
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 image_transform_test(): I = plt.imread('8dc00-mia/data/cameraman.tif') # 45 deg. rotation around the image center T_1 = util.t2h(reg.identity(), 128 * np.ones(2)) T_2 = util.t2h(reg.rotate(np.pi / 4), np.zeros(2)) T_3 = util.t2h(reg.identity(), -128 * np.ones(2)) T_rot = T_1.dot(T_2).dot(T_3) # 45 deg. rotation around the image center followed by shearing T_shear = util.t2h(reg.shear(0.0, 0.5), np.zeros(2)).dot(T_rot) # scaling in the x direction and translation T_scale = util.t2h(reg.scale(1.5, 1), np.array([10, 20])) It1, Xt1 = reg.image_transform(I, T_rot) It2, Xt2 = reg.image_transform(I, T_shear) It3, Xt3 = reg.image_transform(I, T_scale) fig = plt.figure(figsize=(12, 5)) ax1 = fig.add_subplot(131) im11 = ax1.imshow(I) im12 = ax1.imshow(It1, alpha=0.7) ax2 = fig.add_subplot(132) im21 = ax2.imshow(I) im22 = ax2.imshow(It2, alpha=0.7) ax3 = fig.add_subplot(133) im31 = ax3.imshow(I) im32 = ax3.imshow(It3, alpha=0.7) ax1.set_title('Rotation') ax2.set_title('Shearing') ax3.set_title('Scaling') plt.show()
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 mutual_information_test(): I = plt.imread('../data/cameraman.tif') I_mirror, _ = reg.image_transform(I, util.t2h(reg.reflect(-1, 1))) error_msg = "Mutual Information function is incorrectly implemented" # mutual information of an image with itself p1 = reg.joint_histogram(I, I) p2 = reg.joint_histogram(I, I_mirror) MI1 = reg.mutual_information(p1) MI2 = reg.mutual_information(p2) assert 1 - MI1 < 10e-10, error_msg + " (self MI should be 1)" assert MI2 < 0.8, error_msg + " (mirrored image gives MI above 0.8 (strange))" print('MI test successful!')
def correlation_test(): I = plt.imread('../data/cameraman.tif') Th = util.t2h(reg.identity(), np.array([10,20])) J, _ = reg.image_transform(I, Th) C1 = reg.correlation(I, I) # the self correlation should be very close to 1 assert abs(C1 - 1) < 10e-10, "Correlation function is incorrectly implemented (self correlation test)" #------------------------------------------------------------------# # TODO: Implement a few more tests of the correlation definition #------------------------------------------------------------------# print('Test successful!')
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 correlation_test(): I = plt.imread('../data/cameraman.tif') Th = util.t2h(reg.identity(), np.array([10, 20])) J, _ = reg.image_transform(I, Th) error_msg = "Correlation function is incorrectly implemented" C1 = reg.correlation(I, I) C2 = reg.correlation(I, J) C3 = reg.correlation(I, np.ones_like(I)) # the self correlation should be very close to 1 assert abs(C1 - 1) < 10e-10, error_msg + " (self correlation should be 1)" assert C1 >= -1, error_msg + " (self correlation is should be more than -1)" assert C2 <= 1, error_msg + " (translation correlation should be less than 1)" assert C2 >= -1, error_msg + " (translation correlation should be more than -1)" print('Correlation test successful!')
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
def optim_affine_corr(x, I, Im, MI): # Identical to affine_corr in functionality but the x variable is placed # in front when calling for use in scipy.optimize function. # Output is changed to only give the similarity because scipy.optimize # uses this metric to optimize the parameters Tro = reg.rotate(x[0]) Tsc = reg.scale(x[1], x[2]) Tsh = reg.shear(x[3], x[4]) Trss = Tro.dot(Tsc).dot(Tsh) Th = util.t2h(Trss, [x[5], x[6]]) Im_t, Xt = reg.image_transform(Im, Th) if MI: p = joint_histogram(I, Im_t) S = reg.mutual_information(p) else: S = reg.correlation(I, Im_t) return S
def optim_rigid_corr(x, I, Im, MI): # Identical to rigid_corr in functionality but the x variable is placed # in front when calling for use in scipy.optimize function. # Output is changed to only give the similarity because scipy.optimize # uses this metric to optimize the parameters SCALING = 100 # the first element is the rotation angle T = reg.rotate(x[0]) Th = util.t2h(T, x[1:] * SCALING) # transform the moving image Im_t, Xt = reg.image_transform(Im, Th) # compute the similarity between the fixed and transformed moving image if MI: p = joint_histogram(I, Im_t) S = reg.mutual_information(p) else: S = reg.correlation(I, Im_t) return S
def registration_metrics_demo(use_t2=False): # read a T1 image I = plt.imread('../data/t1_demo.tif') if use_t2: # read the corresponding T2 image # note that the T1 and T2 images are already registered I_t2 = plt.imread('../data/t2_demo.tif') # create a linear space of rotation angles - 101 angles between 0 and 360 deg. angles = np.linspace(-np.pi, np.pi, 101, endpoint=True) CC = np.full(angles.shape, np.nan) MI = np.full(angles.shape, np.nan) # visualization fig = plt.figure(figsize=(14,6)) # correlation ax1 = fig.add_subplot(131, xlim=(-np.pi, np.pi), ylim=(-1.1, 1.1)) line1, = ax1.plot(angles, CC, lw=2) ax1.set_xlabel('Rotation angle') ax1.set_ylabel('Correlation coefficient') ax1.grid() # mutual mutual_information ax2 = fig.add_subplot(132, xlim=(-np.pi, np.pi), ylim=(0, 2)) line2, = ax2.plot(angles, MI, lw=2) ax2.set_xlabel('Rotation angle') ax2.set_ylabel('Mutual information') ax2.grid() # images ax3 = fig.add_subplot(133) im1 = ax3.imshow(I) im2 = ax3.imshow(I, alpha=0.7) # used for rotation around image center t = np.array([I.shape[0], I.shape[1]])/2 + 0.5 T_1 = util.t2h(reg.identity(), t) T_3 = util.t2h(reg.identity(), -t) # loop over the rotation angles for k, ang in enumerate(angles): # transformation matrix for rotating the image # I by angles(k) around its center point T_2 = util.t2h(reg.rotate(ang), np.zeros(2)) T_rot = T_1.dot(T_2).dot(T_3) if use_t2: # rotate the T2 image J, Xt = reg.image_transform(I_t2, T_rot) else: # rotate the T1 image J, Xt = reg.image_transform(I, T_rot) # compute the joint histogram with 16 bins p = reg.joint_histogram(I, J, 16, [0, 255]) CC[k] = reg.correlation(I, J) MI[k] = reg.mutual_information(p) clear_output(wait = True) # visualize the results line1.set_ydata(CC) line2.set_ydata(MI) im2.set_data(J) display(fig)
full_path = path+"/transformation_matrix_T1_and_T1_moving_"+str(numberofpoints)+".npy" else: full_path = path+"/transformation_matrix_T1_and_T2_"+str(numberofpoints)+".npy" #compute affine transformation and make a homogenous transformation matrix Th = np.load(full_path) #transfrom the moving image using the transformation matrix It, Xt = reg.image_transform(imageIm, Th) #plotting the results fig = plt.figure(figsize = (20,30)) ax1 = fig.add_subplot(131) im1 = ax1.imshow(imageI) #plot first image (fixed or T1) image ax2 = fig.add_subplot(132) im2 = ax2.imshow(imageIm) #plot second image (moving or T2) image ax3 = fig.add_subplot(133) im3 = ax3.imshow(It) #plot (T1 moving or T2) transformed image if set_of_images == 1: ax1.set_title('T1') ax2.set_title('T1 moving') ax3.set_title('Transformed T1 moving')
def affine_corr_adapted(I, Im, x): #ADAPTED: In order to determine the gradient of the parameters, you only #need the correlation value. So, the outputs: Th and Im_t are removed. #Nothing else is changed. #Attention this function will be used for ngradient in the function: #intensity_based_registration_rigid_adapted. # Computes normalized cross-corrleation between a fixed and # a moving image transformed with an affine transformation. # Input: # I - fixed image # Im - moving image # x - parameters of the rigid transform: the first element # is the roation angle, the second and third are the # scaling parameters, the fourth and fifth are the # shearing parameters and the remaining two elements # are the translation # Output: # C - normalized cross-corrleation between I and T(Im) # Im_t - transformed moving image T(Im) NUM_BINS = 64 SCALING = 100 #------------------------------------------------------------------# # TODO: Implement the missing functionality T_rotate = reg.rotate(x[0]) #make rotation matrix (2x2 matrix) T_scaled = reg.scale(x[1], x[2]) #make scale matrix (2x2 matrix) T_shear = reg.shear(x[3], x[4]) # make shear matrix (2x2 matrix) t = np.array(x[5:]) * SCALING #scale translation vector T_total = T_shear.dot( (T_scaled).dot(T_rotate) ) #multiply the matrices to get the transformation matrix (2x2) Th = util.t2h( T_total, t) #convert to homogeneous transformation matrix (3x3 matrix) Im_t, Xt = reg.image_transform(Im, Th) #apply transformation to moving image C = reg.correlation( Im, Im_t ) #determine the correlation between the moving and transformed moving image #------------------------------------------------------------------# return C
def rigid_corr_adapted(I, Im, x): #ADAPTED: In order to determine the gradient of the parameters, you only #need the correlation value. So, the outputs: Th and Im_t are removed. #Nothing else is changed. #Attention this function will be used for ngradient in the function: #intensity_based_registration_rigid_adapted. # Computes normalized cross-correlation between a fixed and # a moving image transformed with a rigid transformation. # Input: # I - fixed image # Im - moving image # x - parameters of the rigid transform: the first element # is the rotation angle and the remaining two elements # are the translation # Output: # C - normalized cross-correlation between I and T(Im) # Im_t - transformed moving image T(Im) SCALING = 100 # the first element is the rotation angle T = reg.rotate(x[0]) # the remaining two element are the translation # # the gradient ascent/descent method work best when all parameters # of the function have approximately the same range of values # this is not the case for the parameters of rigid registration # where the transformation matrix usually takes much smaller # values compared to the translation vector this is why we pass a # scaled down version of the translation vector to this function # and then scale it up when computing the transformation matrix Th = util.t2h(T, x[1:] * SCALING) # transform the moving image Im_t, Xt = reg.image_transform(Im, Th) # compute the similarity between the fixed and transformed # moving image C = reg.correlation(I, Im_t) return C