コード例 #1
0
def mutual_information_test():

    I = plt.imread('../data/cameraman.tif')

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

    #------------------------------------------------------------------#
    # TODO: Implement a few tests of the mutual_information definition
    #first test

    random1 = np.random.randint(255, size=(512, 512))
    random2 = np.random.randint(255, size=(512, 512))
    p2 = reg.joint_histogram(random1, random2)
    MI2 = reg.mutual_information(p2)

    bound1 = MI2 < 10e-4
    bound2 = MI2 > -10e-4
    #assert (bound1 == True and bound2 ==True), "The mutual information implementation is wrong"

    print(MI2)
    #------------------------------------------------------------------#

    print('Test successful!')
コード例 #2
0
def mutual_information_test():

    I = plt.imread('../data/cameraman.tif')

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

    noise = np.random.randint(225, size=(512, 512))
    p2 = reg.joint_histogram(noise, noise)
    MI2 = reg.mutual_information(p2)

    print('Test successful!')
コード例 #3
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!')
コード例 #4
0
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!")
コード例 #5
0
def mutual_information_test():

    I = plt.imread('../data/cameraman.tif')

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

    J = np.random.randint(255, size=(512, 512))
    K = np.random.randint(255, size=(512, 512))
    p2 = reg.joint_histogram(J, K)
    MI2 = reg.mutual_information(p2)

    print(MI1, " ", MI2)
    assert MI1 > 0, "Mutual Information calculation error (MI<0).."
    assert MI2 > 0, "Mutual Information calculation error (MI<0).."

    print('Test successful!')
コード例 #6
0
def mutual_information_test():

    I = plt.imread('../data/cameraman.tif')

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

    #------------------------------------------------------------------#
    # Implement a few tests of the mutual_information definition
    print(MI1)
    In1 = np.random.randint(255, size=(512, 512))
    In2 = np.random.randint(255, size=(512, 512))
    MI2 = reg.mutual_information(reg.joint_histogram(In1, In2))
    print(MI2)
    #------------------------------------------------------------------#

    print('Test successful!')
コード例 #7
0
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
コード例 #8
0
def mutual_information_test():

    I = plt.imread('../data/cameraman.tif')

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

    #------------------------------------------------------------------#
    # TODO: Implement a few tests of the mutual_information definition
    x = np.random.randint(255, size=(512, 512))  #random image
    y = np.random.randint(255, size=(512, 512))  #random image

    h = reg.joint_histogram(x, y)  #joint histogram van x en y
    g = reg.mutual_information(h)  #mutual information van x en y
    print(MI1)
    #------------------------------------------------------------------#

    print('Test successful!')
コード例 #9
0
def mutual_information_test():

    I = plt.imread('../data/cameraman.tif')

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

    #------------------------------------------------------------------#
    # TODO: Implement a few tests of the mutual_information definition
    image_1 = np.random.randint(255, size=(512, 512))
    image_2 = np.random.randint(255, size=(512, 512))

    joint_hist = reg.joint_histogram(image_1, image_2)
    result = reg.mutual_information(joint_hist)
    print(MI1)
    print(result)

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

    print('Test successful!')
コード例 #10
0
def mutual_information_test():
    I = plt.imread('../data/cameraman.tif')

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

    # ------------------------------------------------------------------#
    # TODO: Implement a few tests of the mutual_information definition
    # ------------------------------------------------------------------#

    print('Test successful!')
コード例 #11
0
def mutual_information_e_test():

    I = plt.imread('../data/cameraman.tif')

    N1 = np.random.randint(255, size=(512, 512))
    N2 = np.random.randint(255, size=(512, 512))

    # mutual information of an image with itself
    p1 = reg.joint_histogram(I, I)
    MI1 = reg.mutual_information_e(p1)
    MI2 = reg.mutual_information(p1)
    assert abs(MI1-MI2) < 10e-3, "Mutual information function with entropy is incorrectly implemented (difference with reference implementation test)"

    print('Test successful!')
コード例 #12
0
def registration_metrics_demo(use_t2=False):

    # read a T1 image
    I = plt.imread('../data/t1_demo.tif')

    if use_t2:
        # read the corresponding T2 image
        # note that the T1 and T2 images are already registered
        I_t2 = plt.imread('../data/t2_demo.tif')

    # create a linear space of rotation angles - 101 angles between 0 and 360 deg.
    angles = np.linspace(-np.pi, np.pi, 101, endpoint=True)

    CC = np.full(angles.shape, np.nan)
    MI = np.full(angles.shape, np.nan)

    # visualization
    fig = plt.figure(figsize=(14,6))

    # correlation
    ax1 = fig.add_subplot(131, xlim=(-np.pi, np.pi), ylim=(-1.1, 1.1))
    line1, = ax1.plot(angles, CC, lw=2)
    ax1.set_xlabel('Rotation angle')
    ax1.set_ylabel('Correlation coefficient')
    ax1.grid()

    # mutual mutual_information
    ax2  = fig.add_subplot(132, xlim=(-np.pi, np.pi), ylim=(0, 2))
    line2, = ax2.plot(angles, MI, lw=2)
    ax2.set_xlabel('Rotation angle')
    ax2.set_ylabel('Mutual information')
    ax2.grid()

    # images
    ax3 = fig.add_subplot(133)
    im1 = ax3.imshow(I)
    im2 = ax3.imshow(I, alpha=0.7)

    # used for rotation around image center
    t = np.array([I.shape[0], I.shape[1]])/2 + 0.5
    T_1 = util.t2h(reg.identity(), t)
    T_3 = util.t2h(reg.identity(), -t)

    # loop over the rotation angles
    for k, ang in enumerate(angles):
        # transformation matrix for rotating the image
        # I by angles(k) around its center point
        T_2 = util.t2h(reg.rotate(ang), np.zeros(2))
        T_rot = T_1.dot(T_2).dot(T_3)

        if use_t2:
            # rotate the T2 image
            J, Xt = reg.image_transform(I_t2, T_rot)
        else:
            # rotate the T1 image
            J, Xt = reg.image_transform(I, T_rot)

        # compute the joint histogram with 16 bins
        p = reg.joint_histogram(I, J, 16, [0, 255])

        CC[k] = reg.correlation(I, J)
        MI[k] = reg.mutual_information(p)

        clear_output(wait = True)
        
        # visualize the results
        line1.set_ydata(CC)
        line2.set_ydata(MI)
        im2.set_data(J)

        display(fig)
コード例 #13
0
##choose fiducial points and save them for T1 and T1 moving
X1, X2 = util.my_cpselect(I1_path, I2_path)

Th2, fig2, It2 = proj.point_based_registration(I1_path, I2_path, X1, X2)
np.save(
    path + "/transformation_matrix_T1_and_T2_moving_" + str(numberofpoints),
    Th2)
plt.savefig(path + "/transformed_T2_image_" + str(numberofpoints) + ".png")

#calculate correlation
display("Calculate correlation")
corr_I2 = reg.correlation(I1, It2)
#calculate mutual information
display("Calculate mutual information")
p2 = reg.joint_histogram(I1, It2)
mi_I2 = reg.mutual_information(p2)
"""
Evaluation of point-based affine image registration
"""

#Calculate the registration error for T1 and T1 moving
#
##Select target points for T1 and T1 moving
#X1_target, Xm1_target = util.my_cpselect(I1_path, Im1_path)
#
#Reg_error1 = proj.Evaluate_point_based_registration(T1, X1_target, Xm1_target)
#print('Registration error for pair of T1 image slices:\n{}'.format(Reg_error1))
#
##Select target points for T1 and T2
#X1_target, X2_target = util.my_cpselect(I2_path, Im2_path)
コード例 #14
0
# assigning space
[
    cor_pbr, cor_pbr2, cor_cc_rig, cor_cc_rig2, cor_cc_af, cor_cc_af2, cor_mi,
    cor_mi2
] = [np.zeros(len(t1)) for i in range(8)]
[MI_pbr, MI_pbr2, MI_cc_rig, MI_cc_rig2, MI_cc_af, MI_cc_af2, MI_mi,
 MI_mi2] = [np.zeros(len(t1)) for i in range(8)]

countimages = len(t1)

# executing registration and saving coralation and mutual information
for i in range(countimages):
    I, Im, It = pbr.pbr(r'..\data\image_data\\' + t1[i],
                        r'..\data\image_data\\' + t1d[i])
    cor_pbr[i] = reg.correlation(I, It)
    MI_pbr[i] = reg.mutual_information(reg.joint_histogram(I, It))

for i in range(countimages):
    I, Im, It = pbr.pbr(r'..\data\image_data\\' + t1[i],
                        r'..\data\image_data\\' + t2[i])
    cor_pbr2[i] = reg.correlation(I, It)
    MI_pbr2[i] = reg.mutual_information(reg.joint_histogram(I, It))
print('Point-based: done')

# t1 to t1
for i in range(countimages):
    sim1, I_cc_rig, Im_cc_rig = proj.intensity_based_registration(
        r'..\data\image_data\\' + t1[i], r'..\data\image_data\\' + t1d[i], 0,
        0, 0)
    cor_cc_rig[i] = sim1[-1]
    MI_cc_rig[i] = reg.mutual_information(