def combining_transforms(): X = util.test_object(1) #------------------------------------------------------------------# # Experiment with combining transformation matrices. X1_1 = reg.rotate(np.pi / 4).dot(reg.reflect(-1, 1).dot(X)) X1_2 = reg.reflect(-1, 1).dot(reg.rotate(np.pi / 4).dot(X)) X2_1 = reg.shear(0.6, 0.3).dot(reg.reflect(-1, -1).dot(X)) X2_2 = reg.shear(0.6, 0.3).dot(reg.reflect(-1, 1).dot(X)) X3_1 = reg.reflect(-1, 1).dot(reg.shear(0.6, 0.3).dot(X)) X3_2 = reg.shear(0.6, 0.3).dot(reg.reflect(-1, 1).dot(X)) fig = plt.figure(figsize=(12, 5)) ax1 = fig.add_subplot(141, xlim=(-4, 4), ylim=(-4, 4)) ax2 = fig.add_subplot(142, xlim=(-4, 4), ylim=(-4, 4)) ax3 = fig.add_subplot(143, xlim=(-4, 4), ylim=(-4, 4)) ax4 = fig.add_subplot(144, xlim=(-4, 4), ylim=(-4, 4)) util.plot_object(ax1, X) util.plot_object(ax2, X1_1) util.plot_object(ax2, X1_2) util.plot_object(ax3, X2_1) util.plot_object(ax3, X2_2) util.plot_object(ax4, X3_1) util.plot_object(ax4, X3_2) ax1.grid() ax2.grid() ax3.grid() ax4.grid()
def combining_transforms(): X = util.test_object(1) #------------------------------------------------------------------# # TODO: Experiment with combining transformation matrices. X_t = reg.reflect(-1, 1).dot(reg.rotate(np.pi / 2)).dot(X) X_u = reg.shear(2, 1).dot(reg.reflect(1, -1)).dot(X) X_v = reg.reflect(1, -1).dot(reg.shear(2, 1)).dot(X) X_w = reg.rotate(np.pi / 2).dot(reg.shear(2, 1)).dot(X) fig = plt.figure(figsize=(12, 5)) ax1 = fig.add_subplot(141, xlim=(-8, 8), ylim=(-8, 8)) ax2 = fig.add_subplot(142, xlim=(-8, 8), ylim=(-8, 8)) ax3 = fig.add_subplot(143, xlim=(-8, 8), ylim=(-8, 8)) ax4 = fig.add_subplot(144, xlim=(-8, 8), ylim=(-8, 8)) util.plot_object(ax1, X_t) util.plot_object(ax2, X_u) util.plot_object(ax3, X_v) util.plot_object(ax4, X_w) ax1.set_title('reflect, rotate') ax2.set_title('shear, reflect') ax3.set_title('reflect, shear') ax4.set_title('rotate, shear') ax1.grid() ax2.grid() ax3.grid() ax4.grid()
def combining_transforms(): X = util.test_object(1) X_rot = reg.rotate(3 * np.pi / 7).dot(X) X_shear = reg.shear(0.3, 0.22).dot(X) X_rot_shear = reg.shear(0.1, 0.3).dot(X_rot) X_shear_reflect = reg.reflect(-1, 1).dot(X_shear) X_multicom = reg.rotate(np.pi / 3).dot(X_rot_shear) fig = plt.figure(figsize=(12, 5)) ax1 = fig.add_subplot(141, xlim=(-4, 4), ylim=(-4, 4)) ax2 = fig.add_subplot(142, xlim=(-4, 4), ylim=(-4, 4)) ax3 = fig.add_subplot(143, xlim=(-4, 4), ylim=(-4, 4)) ax4 = fig.add_subplot(144, xlim=(-4, 4), ylim=(-4, 4)) util.plot_object(ax1, X) util.plot_object(ax2, X_rot_shear) util.plot_object(ax3, X_shear_reflect) util.plot_object(ax4, X_multicom) ax1.set_title('Original') ax2.set_title('Combination 1') ax3.set_title('Combination 2') ax4.set_title('Combination 3') ax1.grid() ax2.grid() ax3.grid() ax4.grid()
def combining_transforms(): X = util.test_object(1) X_shear_reflect = reg.reflect(-1, 1).dot(reg.shear(0.5, 0.2).dot(X)) X_reflect_shear = reg.shear(0.5, 0.2).dot(reg.reflect(-1, 1).dot(X)) fig = plt.figure(figsize=(12, 5)) ax1 = fig.add_subplot(141, xlim=(-4, 4), ylim=(-4, 4)) ax2 = fig.add_subplot(142, xlim=(-4, 4), ylim=(-4, 4)) ax3 = fig.add_subplot(143, xlim=(-4, 4), ylim=(-4, 4)) ax4 = fig.add_subplot(144, xlim=(-4, 4), ylim=(-4, 4)) util.plot_object(ax1, X) util.plot_object(ax2, X_shear_reflect) util.plot_object(ax3, X_reflect_shear) util.plot_object(ax4, X) ax1.set_title('Original') ax2.set_title('shear_reflect') ax3.set_title('reflect_shear') ax4.set_title('Original') ax1.grid() ax2.grid() ax3.grid() ax4.grid() fig.show()
def combining_transforms(): X = util.test_object(1) X_combined = (reg.rotate(3 * np.pi / 5) * reg.shear(0.2, 0.2) * reg.shear(2, 8)).dot(X) fig = plt.figure(figsize=(12, 5)) ax1 = fig.add_subplot(141, xlim=(-4, 4), ylim=(-4, 4)) util.plot_object(ax1, X_combined)
def transforms_test(): X = util.test_object(1) X_rot = reg.rotate(3*np.pi/4).dot(X) X_shear = reg.shear(0.1, 0.2).dot(X) X_reflect = reg.reflect(-1, -1).dot(X) fig = plt.figure(figsize=(12,5)) ax1 = fig.add_subplot(141, xlim=(-4,4), ylim=(-4,4)) ax2 = fig.add_subplot(142, xlim=(-4,4), ylim=(-4,4)) ax3 = fig.add_subplot(143, xlim=(-4,4), ylim=(-4,4)) ax4 = fig.add_subplot(144, xlim=(-4,4), ylim=(-4,4)) util.plot_object(ax1, X) util.plot_object(ax2, X_rot) util.plot_object(ax3, X_shear) util.plot_object(ax4, X_reflect) ax1.set_title('Original') ax2.set_title('Rotation') ax3.set_title('Shear') ax4.set_title('Reflection') ax1.grid() ax2.grid() ax3.grid() ax4.grid()
def combining_transforms(): X = util.test_object(1) X_1 = X X_2 = reg.rotate(3 * np.pi / 4).dot(X) X_3 = reg.reflect(-1, 1).dot(reg.rotate(3 * np.pi / 4).dot(X)) X_4 = reg.shear(0.1, 0.2).dot( reg.reflect(-1, 1).dot(reg.rotate(3 * np.pi / 4).dot(X))) fig = plt.figure(figsize=(12, 5)) ax1 = fig.add_subplot(141, xlim=(-4, 4), ylim=(-4, 4)) ax2 = fig.add_subplot(142, xlim=(-4, 4), ylim=(-4, 4)) ax3 = fig.add_subplot(143, xlim=(-4, 4), ylim=(-4, 4)) ax4 = fig.add_subplot(144, xlim=(-4, 4), ylim=(-4, 4)) util.plot_object(ax1, X_1) util.plot_object(ax2, X_2) util.plot_object(ax3, X_3) util.plot_object(ax4, X_4) ax1.set_title('Original') ax2.set_title('Then rotate') ax3.set_title('Now reflect over x') ax4.set_title('Lastly, shear') for ax_obj in [ax1, ax2, ax3, ax4]: ax_obj.grid()
def ls_affine_test(): X = util.test_object(1) # convert to homogeneous coordinates Xh = util.c2h(X) T_rot = reg.rotate(np.pi / 4) T_scale = reg.scale(1.2, 0.9) T_shear = reg.shear(0.2, 0.1) T = util.t2h(T_rot.dot(T_scale).dot(T_shear), [10, 20]) Xm = T.dot(Xh) Te = reg.ls_affine(Xh, Xm) Xmt = Te.dot(Xm) fig = plt.figure(figsize=(12, 5)) ax1 = fig.add_subplot(131) ax2 = fig.add_subplot(132) ax3 = fig.add_subplot(133) util.plot_object(ax1, Xh) util.plot_object(ax2, Xm) util.plot_object(ax3, Xmt) ax1.set_title('Test shape') ax2.set_title('Arbitrary transformation') ax3.set_title('Retrieved test shape') ax1.grid() ax2.grid() ax3.grid()
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 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 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 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 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