def arbitrary_rotation(): X = util.test_object(1) # The object that is rotated Xh = util.c2h(X) # Convert the object vectors to homogeneous coords t = X[:, 0] # First vertex to rotate around # Define separate transformations in homogeneous coords T_ftrans = util.t2h(reg.identity(), -1 * t) # Translate to ensure first vertex is in the origin T_rot = util.t2h(reg.rotate(np.pi / 4), np.array([0, 0])) # Rotate 45 degrees T_btrans = util.t2h(reg.identity(), t) # Translate back to the original position # Create the final transformation matrix T = T_btrans.dot(T_rot.dot(T_ftrans)) #------------------------------------------------------------------# # TODO: TODO: Perform rotation of the test shape around the first vertex #------------------------------------------------------------------# # Transform X_rot = T.dot(Xh) # Plot fig = plt.figure(figsize=(5, 5)) ax1 = fig.add_subplot(111) util.plot_object(ax1, X) util.plot_object(ax1, X_rot) ax1.set_xlim(ax1.get_ylim()) ax1.grid()
def arbitrary_rotation(): X = util.test_object(1) Xh = util.c2h(X) #------------------------------------------------------------------# # TODO: Perform rotation of the test shape around the first vertex Xt = X[:, 0] phi = (45 / 180) * np.pi #matrix to move the figure to new origin translate_matrix = util.t2h(reg.identity(), -Xt) translate_matrix[2][ 2] = 1 #add the 1 in de third row and column to get 'ones'on the diagonal rotate_matrix = np.array([[np.cos(phi), -np.sin(phi), 0], [np.sin(phi), np.cos(phi), 0], [0, 0, 1]]) #matrix to move the figure back to original origin translate_matrix_2 = util.t2h(reg.identity(), Xt) translate_matrix_2[2][ 2] = 1 #add the 1 in de third row and column to get 'ones'on the diagonal T = (rotate_matrix.dot(translate_matrix)) #rotate the moved figure T = translate_matrix_2.dot(T) #move the figure back #------------------------------------------------------------------# X_rot = T.dot(Xh) fig = plt.figure(figsize=(5, 5)) ax1 = fig.add_subplot(111) util.plot_object(ax1, X) util.plot_object(ax1, X_rot) ax1.set_xlim(ax1.get_ylim()) ax1.grid()
def arbitrary_rotation(): X = util.test_object(1) Xh = util.c2h(X) #the object in homogeneous form #------------------------------------------------------------------# Xt = np.array(X[:, 0]) T_rot = reg.rotate(np.pi / 4) Xt_down = util.t2h( reg.identity(), -Xt) #with Xt being the translation vector set into homogeneous form Xt_up = util.t2h(reg.identity(), Xt) T_rot = util.t2h(T_rot, np.array( [0, 0])) #with T being the rotational vector set into homogeneous form T = Xt_up.dot(T_rot).dot(Xt_down) X_fin = T.dot(Xh) #------------------------------------------------------------------# fig = plt.figure(figsize=(5, 5)) ax1 = fig.add_subplot(111) util.plot_object(ax1, X) util.plot_object(ax1, X_fin) ax1.set_xlim(ax1.get_ylim()) ax1.grid()
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 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 arbitrary_rotation(): X = util.test_object(1) Xh = util.c2h(X) #------------------------------------------------------------------# # Perform rotation of the test shape around the first vertex t = -X[:, 0] Trot = reg.rotate(np.pi / 4) Throt = util.t2h(Trot, np.zeros([1, np.size(Trot, axis=1)])) th = util.t2h(reg.identity(), t) thin = np.linalg.inv(th) T = thin.dot(Throt.dot(th)) #------------------------------------------------------------------# X_rot = T.dot(Xh) fig = plt.figure(figsize=(5, 5)) ax1 = fig.add_subplot(111) util.plot_object(ax1, X) util.plot_object(ax1, X_rot) ax1.set_xlim(ax1.get_ylim()) ax1.grid()
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 arbitrary_rotation(): X = util.test_object(1) Xh = util.c2h(X) p_rot = X[:, 0] T_to_zero = util.t2h(reg.identity(), -p_rot) T_return = util.t2h(reg.identity(), p_rot) T_rot = util.t2h(reg.rotate(45)) T = T_return.dot(T_rot.dot(T_to_zero)) X_rot = T.dot(Xh) fig = plt.figure(figsize=(5, 5)) ax1 = fig.add_subplot(111) util.plot_object(ax1, X) util.plot_object(ax1, X_rot) ax1.set_xlim(ax1.get_ylim()) ax1.grid() fig.show()
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 arbitrary_rotation(): X = util.test_object(1) Xh = util.c2h(X) # ------------------------------------------------------------------# Tlat = util.t2h(reg.identity(), np.array(X[:, 0])) Tlat_down = util.t2h(reg.identity(), -np.array(X[:, 0])) T_rot = reg.rotate(np.pi / 4) T_rot_hom = util.t2h(T_rot, np.array([0, 0])) T_tot = Tlat.dot(T_rot_hom).dot(Tlat_down) X_fin = T_tot.dot(Xh) # ------------------------------------------------------------------# fig = plt.figure(figsize=(5, 5)) ax1 = fig.add_subplot(111) util.plot_object(ax1, X) util.plot_object(ax1, X_fin) ax1.set_xlim(ax1.get_ylim()) ax1.grid()
def arbitrary_rotation(): X = util.test_object(1) Xh = util.c2h(X) #------------------------------------------------------------------# # TODO: TODO: Perform rotation of the test shape around the first vertex #------------------------------------------------------------------# tdown = X[:, 0] * -1 tup = X[:, 0] tdown = util.t2h(reg.identity(), tdown) tup = util.t2h(reg.identity(), tup) r = util.t2h(reg.rotate(45), [0, 0]) T = tup.dot(r.dot(tdown)) X_rot = T.dot(Xh) fig = plt.figure(figsize=(5, 5)) ax1 = fig.add_subplot(111) util.plot_object(ax1, X) util.plot_object(ax1, X_rot) ax1.set_xlim(ax1.get_ylim()) ax1.grid() plt.show()
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 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 arbitrary_rotation(): X = util.test_object(1) Xh = util.c2h(X) #------------------------------------------------------------------# Xt= X[:0] T_trans = util.t2h(reg.identity(), Xt) T_rot = reg.rotate(0.25*np.pi) T_trans_inv = np.linalg.inv(T_trans) T = T_trans_inv.dot(T_rot.dot(T_trans)) #------------------------------------------------------------------# X_rot = T.dot(Xh) fig = plt.figure(figsize=(5,5)) ax1 = fig.add_subplot(111) util.plot_object(ax1, X) util.plot_object(ax1, X_rot) ax1.set_xlim(ax1.get_ylim()) ax1.grid()
def arbitrary_rotation(): X = util.test_object(1) Xh = util.c2h(X) #------------------------------------------------------------------# # TODO: Perform rotation of the test shape around the first vertex Xt = X[:, 0] T = util.t2h(reg.rotate(np.pi / 4), Xt).dot(util.t2h(reg.identity(), -Xt)) #------------------------------------------------------------------# X_rot = T.dot(Xh) fig = plt.figure(figsize=(5, 5)) ax1 = fig.add_subplot(111) util.plot_object(ax1, X) util.plot_object(ax1, X_rot) ax1.set_xlim(ax1.get_ylim()) ax1.grid()
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)