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() fig.show()
def image_transform_test(): I = plt.imread('../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')
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
# -*- coding: utf-8 -*- """ Created on Tue Sep 10 15:36:11 2019 @author: 20161879 """ import sys sys.path.append("../code") import numpy as np import matplotlib.pyplot as plt import registration as reg import registration_util as util X = util.test_object(1) X_scaled = reg.scale(2, 3).dot(X) fig = plt.figure(figsize=(5, 5)) ax1 = fig.add_subplot(111) ax1.grid() util.plot_object(ax1, X) util.plot_object(ax1, X_scaled)