Example #1
0
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()
Example #2
0
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()
Example #3
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()
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 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()
Example #6
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()
Example #7
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()
Example #8
0
def combining_transforms():

    X = util.test_object(1)

    #------------------------------------------------------------------#
    # TODO: Experiment with combining transformation matrices.
    Example_1_temp = reg.rotate(5 * np.pi / 3).dot(X)
    Example_1_result = reg.reflect(-1, 1).dot(Example_1_temp)

    Example2 = reg.reflect(-1, 1).dot(X)
    Example2_result = reg.rotate(np.pi / 2).dot(Example2)

    Example2_reversed = reg.rotate(np.pi / 2).dot(X)
    Example2_reversed_result = reg.rotate(5 * np.pi / 3).dot(X)

    #plotting
    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))

    util.plot_object(ax1, X)
    util.plot_object(ax2, Example2_result)
    util.plot_object(ax3, Example2_reversed_result)

    ax1.set_title('Original')
    ax2.set_title('Reflection, rotation')
    ax3.set_title('Reversed')

    ax1.grid()
    ax2.grid()
    ax3.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 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()
Example #11
0
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()
Example #12
0
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()
Example #13
0
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()
Example #14
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 combining_transforms():

    X = util.test_object(1)

    #------------------------------------------------------------------#
    # TODO: Experiment with combining transformation matrices.
    X_t = reg.rotate(np.pi/2).dot(X)*reg.reflect(-1,1).dot(X)
    return X_t
Example #16
0
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 combining_transforms():

    X = util.test_object(1)
    T = reg.reflect(-1, 1).dot(reg.rotate(np.pi / 2))
    X_t = T.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_t)
Example #18
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()
Example #19
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()
Example #20
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()
Example #21
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()
Example #22
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()
Example #23
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()
Example #25
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()
Example #26
0
def combining_transforms():

    X = util.test_object(1)

    #------------------------------------------------------------------#
    X_t = reg.reflect(-1,1).dot(reg.rotate(2)).dot(X)
# -*- 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)
Example #28
0
def combining_transforms():

    X = util.test_object(1)