Esempio n. 1
0
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')
Esempio n. 2
0
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
Esempio n. 3
0
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
Esempio n. 4
0
def Evaluate_point_based_registration(Th, X_target, Xm_target):
    X_target = util.c2h(X_target)
    Xmh_target = util.c2h(Xm_target)

    #transform the selected points
    Xm_target_transformed = Th.dot(Xmh_target)

    #average distance between points (= target registration error)
    Error_avgdist = calculateAvg_Distance(X_target, Xm_target_transformed)
    return Error_avgdist
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
Esempio n. 6
0
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)  #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()
Esempio n. 8
0
def arbitrary_rotation():

    X = util.test_object(1)
    Xh = util.c2h(X)

    #------------------------------------------------------------------#
    #perform rotation around the first vertex
    phi = 45
    c = np.cos(phi)
    s = np.sin(phi)

    coord_1 = X[:, 0]  #rotatie om dit punt
    x_r = coord_1[0]
    y_r = coord_1[1]

    T_1 = np.array([[1, 0, x_r], [0, 1, y_r], [0, 0, 1]])
    T_2 = np.array([[c, -s, 0], [s, c, 0], [0, 0, 1]])
    T_3 = np.array([[1, 0, -x_r], [0, 1, -y_r], [0, 0, 1]])

    T = T_1.dot(T_2).dot(T_3)

    #------------------------------------------------------------------#

    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()
Esempio n. 9
0
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()
Esempio n. 10
0
def image_transform(I, Th,  output_shape=None):
    # Image transformation by inverse mapping.
    # Input:
    # I - image to be transformed
    # Th - homogeneous transformation matrix
    # output_shape - size of the output image (default is same size as input)
    # Output:
    # It - transformed image
    # we want double precision for the interpolation, but we want the
    # output to have the same data type as the input - so, we will
    # convert to double and remember the original input type

    input_type = type(I);

    # default output size is same as input
    if output_shape is None:
        output_shape = I.shape

    # spatial coordinates of the transformed image
    x = np.arange(0, output_shape[1])
    y = np.arange(0, output_shape[0])
    xx, yy = np.meshgrid(x, y)

    # convert to a 2-by-p matrix (p is the number of pixels)
    X = np.concatenate((xx.reshape((1, xx.size)), yy.reshape((1, yy.size))))
    # convert to homogeneous coordinates
    Xh = util.c2h(X)

    #------------------------------------------------------------------#
    # TODO: Perform inverse coordinates mapping.
    #------------------------------------------------------------------#

    It = ndimage.map_coordinates(I, [Xt[1,:], Xt[0,:]], order=1, mode='constant').reshape(I.shape)

    return It, Xt
Esempio n. 11
0
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 arbitrary_rotation():

    X = util.test_object(1)
    Xh = util.c2h(X)

    #Define rotation angle 45degrees
    phi = np.pi / 4

    #Define seperate homogenous transformation matrices
    T_1 = np.array([[1, 0, -X[0, 0]], [0, 1, -X[1, 0]], [0, 0, 1]])
    T_rot = np.array([[cos(phi), -sin(phi), 0], [sin(phi),
                                                 cos(phi), 0], [0, 0, 1]])
    T_2 = np.array([[1, 0, X[0, 0]], [0, 1, X[1, 0]], [0, 0, 1]])

    #Combine them
    T = T_2.dot(T_rot.dot(T_1))

    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 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()
Esempio n. 14
0
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
Esempio n. 15
0
def arbitrary_rotation():

    X = util.test_object(1)
    Xh = util.c2h(X)

    #------------------------------------------------------------------#
    # TODO: Perform rotation of the test shape around the first vertex
    #------------------------------------------------------------------#

    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()
Esempio n. 16
0
def t2h_test():
    X = util.test_object(1)
    Xh = util.c2h(X)

    # translation vector
    t = np.array([10, 20])

    # rotation matrix
    T_rot = reg.rotate(np.pi / 4)

    Th = util.t2h(T_rot, t)

    X_rot_tran = Th.dot(Xh)

    fig = plt.figure(figsize=(5, 5))
    ax1 = fig.add_subplot(111)
    util.plot_object(ax1, X)
    util.plot_object(ax1, X_rot_tran)
    ax1.grid()
Esempio n. 17
0
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()
Esempio n. 18
0
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()
Esempio n. 19
0
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()
Esempio n. 20
0
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
    a_point = X[:,0]
    ax = a_point[0]
    ay = a_point[1]

    trans1 = np.array([[1,0,ax],[0,1,ay],[0,0,1]])              #translatiematrix 1 (zie slides)
    trans2 = np.array([[1, 0, -ax], [0, 1, -ay], [0, 0, 1]])    #translatiematrix 2

    phi = (np.pi/4)
    c = np.cos(phi)
    s = np.sin(phi)
    extra = ([[0,0]])
    extra_vertical = np.transpose(extra)
    extra_horizontal = ([[0,0,1]])
    r = np.array([[c, -s], [s, c]])
    rot1 = np.concatenate((r, extra_vertical), 1)
    rot2 = np.concatenate((rot1, extra_horizontal), 0)          #rotatiematrix

    Transformation = trans1.dot(rot2.dot(trans2))
    X_rot = Transformation.dot(Xh)

    #------------------------------------------------------------------#

    #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()
Esempio n. 22
0
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()