Example #1
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 #2
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()
Example #3
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()
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()
Example #5
0
def affine_mi(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

    Tro = rotate(x[0])
    Tsc = scale(x[1], x[2])
    Tsh = shear(x[3], x[4])
    Trss = Tro.dot(Tsc).dot(Tsh)
    Th = util.t2h(Trss, [x[5], x[6]])

    Im_t = Th.dot(Im)

    p = joint_histogram(I, Im_t, NUM_BINS)
    MI = mutual_information(p)

    return MI, Im_t, Th
Example #6
0
def affine_corr(I, Im, x):
    # 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)

    NUM_BINS = 64
    SCALING = 100

    Tro = rotate(x[0])
    Tsc = scale(x[1], x[2])
    Tsh = shear(x[3], x[4])
    Trss = Tro.dot(Tsc).dot(Tsh)
    Th = util.t2h(Trss, [x[5], x[6]])

    Im_t, Xt = image_transform(Im, Th)

    C = correlation(I, Im_t)

    return C, Im_t, Th
Example #7
0
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
Example #8
0
def rigid_corr(I, Im, x, MI):
    # Computes normalized cross-correlation between a fixed and
    # a moving image transformed with a rigid transformation.
    # Input:
    # I - fixed image
    # Im - moving image
    # x - parameters of the rigid transform: the first element
    #     is the rotation angle 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)

    SCALING = 100

    # the first element is the rotation angle
    T = reg.rotate(x[0])
    Th = util.t2h(T, x[1:] * SCALING)

    # transform the moving image
    Im_t, Xt = reg.image_transform(Im, Th)

    # compute the similarity between the fixed and transformed moving image
    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
Example #9
0
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!')
Example #10
0
def affine_mi(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
    
    T_rot = rotate(x[0])
    T_scale = scale(x[1],x[2])
    T_shear = shear(x[3],x[4])
    
    T = T_rot.dot(T_scale).dot(T_shear)
    
    Th = util.t2h(T, x[5:]*SCALING)
    
    Im_t, Xt = image_transform(Im, Th)
    
    p = joint_histogram(I,Im_t)
    
    MI = mutual_information(p)

    return MI, Im_t, Th
Example #11
0
def affine_corr(I, Im, x):
    # 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

    #------------------------------------------------------------------#
    rot = x[0]
    sx, sy = x[1], x[2]
    cx, cy = x[3], x[4]
    transl = x[5:7]

    T_rot = rotate(rot)
    T_scal = scale(sx, sy)
    T_shear = shear(cx, cy)

    T = T_shear.dot(T_scal.dot(T_rot))
    Th = util.t2h(T, transl * SCALING)
    Im_t = Th.dot(Im)
    C = correlation(I, Im_t)
    #------------------------------------------------------------------#

    return C, Im_t, Th
Example #12
0
def affine_corr(I, Im, x):
    # 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_rot = rotate(x[0])
    T_scale = scale(x[1], x[2])
    T_shear = shear(x[3], x[4])
    T = T_rot.dot(T_scale.dot(T_shear))

    Th = util.t2h(T, x[5:] * SCALING)

    Im_t, Xt = image_transform(Im, Th)

    C = correlation(I, Im_t)
    #------------------------------------------------------------------#

    return C, Im_t, Th
Example #13
0
def affine_mi(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
    
    #------------------------------------------------------------------#
    T = rotate(x[0])
    S = scale(x[1],x[2]).dot(T)
    Sh = shear(x[3],x[4]).dot(S)

    Th = util.t2h(T, np.array(x[5:])*SCALING)

    Im_t, Xt = image_transform(Im, Th)
    C = correlation(I, Im_t)
    p = joint_histogram(I, Im, NUM_BINS)
    MI = mutual_information(p)
    plt.imshow(Im_t)
    #------------------------------------------------------------------#

    return MI, Im_t, Th
Example #14
0
def affine_mi(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(x[0]).dot(scale(x[1],x[2])).dot(shear(x[3],x[4]))
    Th=util.t2h(T,SCALING*np.array([x[5],x[6]]))
    Im_t=image_transform(Im,Th)[0]
    MI=mutual_information(joint_histogram(I,Im_t,NUM_BINS))
    #------------------------------------------------------------------#

    return MI, Im_t, Th
def affine_corr(I, Im, x):
    # 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 affine 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)

    NUM_BINS = 64
    SCALING = 100

    # Assemble transformation matrix (according to parameter sequence)
    T_rot = rotate(x[0])
    T_scale = scale(x[1], x[2])
    T_shear = shear(x[3], x[4])
    T = T_shear.dot(T_scale.dot(T_rot))

    Th = util.t2h(T, x[5:] * SCALING)

    #Transform moving image
    Im_t, Xt = image_transform(Im, Th)

    #Calculate correlation between images
    C = correlation(I,Im_t)

    return C, Im_t, Th
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 #17
0
def affine_corr(I, Im, x):
    # 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
    #X[0]: rotation angle
    #X[1] and X[2]: scaling parameters
    #X[3] and X[4]: shearing parameters
    #X[5] and X[6]: translation
    T= rotate(x[0]).dot(scale(x[1],x[2])).dot(shear(x[3],x[4]))
    Th=util.t2h(T,SCALING*np.array([x[5],x[6]]))
    Im_t=image_transform(Im,Th)[0]
    C=correlation(I,Im_t)
    
    #------------------------------------------------------------------#

    return C, Im_t, Th
Example #18
0
def affine_corr(I, Im, x, return_transform=True):
    """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 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
    return_transform: Flag for controlling the return values
    Output:
    C - normalized cross-correlation between I and T(Im)
    Im_t - transformed moving image T(Im)
    Th - transformation matrix (only returned if return_transform=True)
    """

    NUM_BINS = 64
    SCALING = 100

    #------------------------------------------------------------------#
    # TODO: Implement the missing functionality
    Th = rotate(x[0]).dot(scale(x[1], x[2])).dot(shear(x[3], x[4]))
    Th = util.t2h(Th, x[5:7] * SCALING)

    Im_t, Xt = np.array(image_transform(Im, Th))
    C = correlation(I, Im_t)

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

    if return_transform:
        return C, Im_t, Th
    else:
        return C
Example #19
0
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 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
Example #22
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 #23
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 #24
0
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()
Example #25
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()
Example #26
0
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!')
Example #27
0
def mutual_information_test():
    I = plt.imread('../data/cameraman.tif')
    I_mirror, _ = reg.image_transform(I, util.t2h(reg.reflect(-1, 1)))
    error_msg = "Mutual Information function is incorrectly implemented"

    # mutual information of an image with itself
    p1 = reg.joint_histogram(I, I)
    p2 = reg.joint_histogram(I, I_mirror)
    MI1 = reg.mutual_information(p1)
    MI2 = reg.mutual_information(p2)

    assert 1 - MI1 < 10e-10, error_msg + " (self MI should be 1)"
    assert MI2 < 0.8, error_msg + " (mirrored image gives MI above 0.8 (strange))"

    print('MI test successful!')
Example #28
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 #29
0
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!')
Example #30
0
def rigid_corr(I, Im, x, return_transform=True):
    """Computes normalized cross-correlation between a fixed and
    a moving image transformed with a rigid transformation.
    Input:
    I - fixed image
    Im - moving image
    x - parameters of the rigid transform: the first element
        is the rotation angle and the remaining two elements
        are the translation
    return_transform: Flag for controlling the return values
    Output:
    C - normalized cross-correlation between I and T(Im)
    Im_t - transformed moving image T(Im)
    Th - transformation matrix (only returned if return_transform=True)
    """

    SCALING = 100

    # the first element is the rotation angle
    T = rotate(x[0])

    # the remaining two elements are the translation
    #
    # the gradient ascent/descent method work best when all parameters
    # of the function have approximately the same range of values
    # this is  not the case for the parameters of rigid registration
    # where the transformation matrix usually takes  much smaller
    # values compared to the translation vector this is why we pass a
    # scaled down version of the translation vector to this function
    # and then scale it up when computing the transformation matrix
    Th = util.t2h(T, x[1:] * SCALING)

    # transform the moving image
    Im_t, Xt = image_transform(Im, Th)

    # compute the similarity between the fixed and transformed
    # moving image
    C = correlation(I, Im_t)

    if return_transform:
        return C, Im_t, Th
    else:
        return C