Beispiel #1
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
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()
Beispiel #3
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
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
def point_based_registration_demo():
    # ---------------------------------
    # Append the following code in jupiter to run:
    #   %matplotlib inline
    #   import sys
    #   sys.path.append("C:/Users/s163666/Documents/2019-2020/Medical Image Analysis/Mini_project_mia")
    #   from project_fout import point_based_registration_demo
    # point_based_registration_demo()
    # ---------------------------------

    # read the fixed and moving images, 2x T1 or T1&T2
    # change these in order to read different images
    I_path = './data/image_data/3_1_t1.tif'
    Im_path = './data/image_data/3_1_t2.tif'

    #Select set of corresponding points using my_cpselect
    X0, Xm0 = util.my_cpselect(I_path, Im_path)

    #Compute the affine transformation between the pair of images
    Xm = np.ones((3, Xm0.shape[1]))
    Xm[0, :] = Xm0[0, :]
    Xm[1, :] = Xm0[1, :]
    X = np.ones((3, X0.shape[1]))
    X[0, :] = X0[0, :]
    X[1, :] = X0[1, :]
    T, Etrain = reg.ls_affine(X, Xm)

    Etest = reg.point_based_error(I_path, Im_path, T)

    #read image
    Im = plt.imread(Im_path)

    #Apply the affine transformation to the moving image
    It, Xt = reg.image_transform(Im, T)

    #plot figure
    fig = plt.figure(figsize=(12, 5))
    #fig, ax = plt.subplots()

    ax = fig.add_subplot(121)
    im = ax.imshow(It)

    ax.set_title("Transformed image")
    ax.set_xlabel('x')
    ax.set_ylabel('y')

    print(Etrain)
    print(Etest)

    display(fig)
    fig.savefig('./data/image_results/3_1_t1 + 3_1_t2.png')
def my_point_based_registration():
    I = '../data/image_data/3_2_t1.tif'
    I_d = '../data/image_data/3_2_t1_d.tif'
    I_plt = plt.imread('../data/image_data/3_2_t1.tif')
    I_d_plt = plt.imread('../data/image_data/3_2_t1_d.tif')

    X, Xm = util.my_cpselect(I, I_d)
    affine_transformation = reg.ls_affine(X, Xm)
    transformed_moving_image, transformed_vector = reg.image_transform(
        I_d_plt, affine_transformation)

    fig = plt.figure(figsize=(12, 5))
    ax1 = fig.add_subplot(131)
    Im1 = ax1.imshow(I_plt)  # Fixed image or Transformed image
    Im2 = ax1.imshow(transformed_moving_image,
                     alpha=0.7)  # Transformed image or Fixed image
def my_point_based_registration_2():

    I2 = '../data/image_data/3_2_t1.tif'  # Tweede opdracht T1 slide
    I_d_2 = '../data/image_data/3_2_t2.tif'  # T2 slice
    I_plt_2 = plt.imread('../data/image_data/3_2_t1.tif')
    I_d_plt_2 = plt.imread('../data/image_data/3_2_t1_d.tif')

    X2, Xm2 = util.my_cpselect(I2, I_d_2)
    affine_transformation_2 = reg.ls_affine(X2, Xm2)
    transformed_moving_image_2, transformed_vector_2 = reg.image_transform(
        I_d_plt_2, affine_transformation_2)

    fig = plt.figure(figsize=(12, 5))
    ax1 = fig.add_subplot(131)
    Im1 = ax1.imshow(
        I_plt_2)  # Fixed image or Transformed image, Je mag wisselen
    Im2 = ax1.imshow(
        transformed_moving_image_2,
        alpha=0.7)  # Transformed image or Fixed image  Mag wisselen