Example #1
0
def testCircleToCMonomodalSyNEM(lambdaParam, maxOuterIter):
    fname0 = 'data/circle.png'
    #fname0='data/C_trans.png'
    fname1 = 'data/C.png'
    nib_moving = plt.imread(fname0)
    nib_fixed = plt.imread(fname1)
    moving = nib_moving[:, :, 0]
    fixed = nib_fixed[:, :, 1]
    moving = (moving - moving.min()) / (moving.max() - moving.min())
    fixed = (fixed - fixed.min()) / (fixed.max() - fixed.min())
    level = 3
    maskMoving = moving > 0
    maskFixed = fixed > 0
    movingPyramid = [
        img for img in rcommon.pyramid_gaussian_2D(moving, level, maskMoving)
    ]
    fixedPyramid = [
        img for img in rcommon.pyramid_gaussian_2D(fixed, level, maskFixed)
    ]
    rcommon.plotOverlaidPyramids(movingPyramid, fixedPyramid)
    displacementList = []
    displacement, dinv = estimateMonomodalSyNField2DMultiScale(
        movingPyramid, fixedPyramid, lambdaParam, maxOuterIter, 0,
        displacementList)
    inverse = np.array(tf.invert_vector_field(displacement, 0.75, 300, 1e-7))
    residual, stats = tf.compose_vector_fields(displacement, inverse)
    residual = np.array(residual)
    warpPyramid = [
        rcommon.warpImage(movingPyramid[i], displacementList[i])
        for i in range(level + 1)
    ]
    rcommon.plotOverlaidPyramids(warpPyramid, fixedPyramid)
    rcommon.overlayImages(warpPyramid[0], fixedPyramid[0])
    rcommon.plotDiffeomorphism(displacement, inverse, residual, '', 7)
Example #2
0
def testCircleToCMonomodalSyNEM(lambdaParam, maxOuterIter):
    fname0='data/circle.png'
    #fname0='data/C_trans.png'
    fname1='data/C.png'
    nib_moving=plt.imread(fname0)
    nib_fixed=plt.imread(fname1)
    moving=nib_moving[:,:,0]
    fixed=nib_fixed[:,:,1]
    moving=(moving-moving.min())/(moving.max() - moving.min())
    fixed=(fixed-fixed.min())/(fixed.max() - fixed.min())
    level=3
    maskMoving=moving>0
    maskFixed=fixed>0
    movingPyramid=[img for img in rcommon.pyramid_gaussian_2D(moving, level, maskMoving)]
    fixedPyramid=[img for img in rcommon.pyramid_gaussian_2D(fixed, level, maskFixed)]
    rcommon.plotOverlaidPyramids(movingPyramid, fixedPyramid)
    displacementList=[]
    displacement, dinv=estimateMonomodalSyNField2DMultiScale(movingPyramid, fixedPyramid, lambdaParam, maxOuterIter, 0,displacementList)
    inverse=np.array(tf.invert_vector_field(displacement, 0.75, 300, 1e-7))
    residual, stats=tf.compose_vector_fields(displacement, inverse)
    residual=np.array(residual)
    warpPyramid=[rcommon.warpImage(movingPyramid[i], displacementList[i]) for i in range(level+1)]
    rcommon.plotOverlaidPyramids(warpPyramid, fixedPyramid)
    rcommon.overlayImages(warpPyramid[0], fixedPyramid[0])
    rcommon.plotDiffeomorphism(displacement, inverse, residual, '',7)
def testInversion_invertible():
    displacement_clean=tf.create_invertible_displacement_field(256, 256, 0.5, 8)
    detJacobian=rcommon.computeJacobianField(displacement_clean)
    plt.figure()
    plt.imshow(detJacobian)
    print 'Range:', detJacobian.min(), detJacobian.max()
    X1,X0=np.mgrid[0:displacement_clean.shape[0], 0:displacement_clean.shape[1]]
    CS=plt.contour(X0,X1,detJacobian,levels=[0.0], colors='b')
    plt.clabel(CS, inline=1, fontsize=10)
    plt.title('det(J(displacement))')
    displacement=displacement_clean+np.random.normal(0.0, 1.1, displacement_clean.shape)
    #displacement=np.array(displacement_clean)
    #inverse=rcommon.invert_vector_field_fixed_point(displacement, 100, 1e-7)
    #inverse=np.array(tf.invert_vector_field(displacement, 0.1, 100, 1e-7))
    lambdaParam=5.0
    #########Jacobi##########
    inverse=np.array(tf.invert_vector_field(displacement, lambdaParam, 100, 1e-6))
    residual, stats=tf.compose_vector_fields(displacement_clean, inverse)
    residual=np.array(residual)
    print 'Jacobi. Max:',stats[0], '. Mean:',stats[1],'Std:',stats[2]
    [d,invd,res, detJ]=rcommon.plotDiffeomorphism(displacement, inverse, residual, 'Jacobi', 7)
    #########Fixed point######
    inverse=np.array(tf.invert_vector_field_fixed_point(displacement, 100, 1e-6))
    residual, stats=tf.compose_vector_fields(displacement_clean, inverse)
    residual=np.array(residual)
    print 'Fixed point. Max:',stats[0], '. Mean:',stats[1],'Std:',stats[2]
    [d,invd,res, detJ]=rcommon.plotDiffeomorphism(displacement, inverse, residual, 'Fixed point', 7)
    #########TV-L2###########
    inverse=np.array(tf.invert_vector_field_tv_l2(displacement, lambdaParam, 3000, 1e-6))
    residual, stats=tf.compose_vector_fields(displacement_clean, inverse)
    residual=np.array(residual)
    print 'TV-L2. Max:',stats[0], '. Mean:',stats[1],'Std:',stats[2]
    [d,invd,res, detJ]=rcommon.plotDiffeomorphism(displacement, inverse, residual, 'TV-L2', 7)
def testInversion(lambdaParam):
    fname0='data/circle.png'
    fname1='data/C.png'
    circleToCDisplacementName='circleToCDisplacement.npy'
    circleToCDisplacementInverseName='circleToCDisplacementInverse.npy'
    nib_moving=plt.imread(fname0)
    nib_fixed=plt.imread(fname1)
    moving=nib_moving[:,:,0]
    fixed=nib_fixed[:,:,1]
    moving=(moving-moving.min())/(moving.max() - moving.min())
    fixed=(fixed-fixed.min())/(fixed.max() - fixed.min())
    level=3
    maskMoving=moving>0
    maskFixed=fixed>0
    movingPyramid=[img for img in rcommon.pyramid_gaussian_2D(moving, level, np.ones_like(maskMoving))]
    fixedPyramid=[img for img in rcommon.pyramid_gaussian_2D(fixed, level, np.ones_like(maskFixed))]
    rcommon.plotOverlaidPyramids(movingPyramid, fixedPyramid)
    displacementList=[]
    maxOuterIter=[10,50,100,100,100,100,100,100,100]
    if(os.path.exists(circleToCDisplacementName)):
        displacement=np.load(circleToCDisplacementName)
        inverse=np.load(circleToCDisplacementInverseName)
    else:
        displacement, inverse=estimateMonomodalDiffeomorphicField2DMultiScale(movingPyramid, fixedPyramid, lambdaParam, maxOuterIter, 0,displacementList)
        np.save(circleToCDisplacementName, displacement)
        np.save(circleToCDisplacementInverseName, inverse)
    print 'vector field exponential'
    expd, invexpd=tf.vector_field_exponential(displacement, True)
    print 'vector field inversion'
    directInverse=tf.invert_vector_field(displacement, 1.0, 10000, 1e-7)
    print 'vector field inversion'
    directExpInverse=tf.invert_vector_field(expd, 1.0, 10000, 1e-7)
    ###Now compare inversions###
    residualJoint=np.array(tf.compose_vector_fields(displacement, inverse)[0])
    residualDirect=np.array(tf.compose_vector_fields(displacement, directInverse)[0])
    residualExpJoint=np.array(tf.compose_vector_fields(expd, invexpd)[0])
    residualExpDirect=np.array(tf.compose_vector_fields(expd, directExpInverse)[0])
    rcommon.plotDiffeomorphism(displacement, inverse, residualJoint, 'D-joint')
    rcommon.plotDiffeomorphism(expd, invexpd, residualExpJoint, 'expD-joint')
    d,invd,res,jacobian=rcommon.plotDiffeomorphism(displacement, directInverse, residualDirect, 'D-direct')
    rcommon.plotDiffeomorphism(expd, directExpInverse, residualExpDirect, 'expD-direct')
    sp.misc.imsave('circleToC_deformation.png', d)
    sp.misc.imsave('circleToC_inverse_deformation.png', invd)
    sp.misc.imsave('circleToC_residual_deformation.png', res)
    tf.write_double_buffer(np.array(displacement).reshape(-1), '../inverse/experiments/displacement.bin')
    tf.write_double_buffer(np.array(displacement).reshape(-1), '../inverse/experiments/displacement_clean.bin')
def testInversion_invertible():
    displacement_clean = tf.create_invertible_displacement_field(
        256, 256, 0.5, 8)
    detJacobian = rcommon.computeJacobianField(displacement_clean)
    plt.figure()
    plt.imshow(detJacobian)
    print 'Range:', detJacobian.min(), detJacobian.max()
    X1, X0 = np.mgrid[0:displacement_clean.shape[0],
                      0:displacement_clean.shape[1]]
    CS = plt.contour(X0, X1, detJacobian, levels=[0.0], colors='b')
    plt.clabel(CS, inline=1, fontsize=10)
    plt.title('det(J(displacement))')
    displacement = displacement_clean + np.random.normal(
        0.0, 1.1, displacement_clean.shape)
    #displacement=np.array(displacement_clean)
    #inverse=rcommon.invert_vector_field_fixed_point(displacement, 100, 1e-7)
    #inverse=np.array(tf.invert_vector_field(displacement, 0.1, 100, 1e-7))
    lambdaParam = 5.0
    #########Jacobi##########
    inverse = np.array(
        tf.invert_vector_field(displacement, lambdaParam, 100, 1e-6))
    residual, stats = tf.compose_vector_fields(displacement_clean, inverse)
    residual = np.array(residual)
    print 'Jacobi. Max:', stats[0], '. Mean:', stats[1], 'Std:', stats[2]
    [d, invd, res, detJ] = rcommon.plotDiffeomorphism(displacement, inverse,
                                                      residual, 'Jacobi', 7)
    #########Fixed point######
    inverse = np.array(
        tf.invert_vector_field_fixed_point(displacement, 100, 1e-6))
    residual, stats = tf.compose_vector_fields(displacement_clean, inverse)
    residual = np.array(residual)
    print 'Fixed point. Max:', stats[0], '. Mean:', stats[1], 'Std:', stats[2]
    [d, invd, res,
     detJ] = rcommon.plotDiffeomorphism(displacement, inverse, residual,
                                        'Fixed point', 7)
    #########TV-L2###########
    inverse = np.array(
        tf.invert_vector_field_tv_l2(displacement, lambdaParam, 3000, 1e-6))
    residual, stats = tf.compose_vector_fields(displacement_clean, inverse)
    residual = np.array(residual)
    print 'TV-L2. Max:', stats[0], '. Mean:', stats[1], 'Std:', stats[2]
    [d, invd, res, detJ] = rcommon.plotDiffeomorphism(displacement, inverse,
                                                      residual, 'TV-L2', 7)
def testInversion(lambdaParam):
    fname0 = 'data/circle.png'
    fname1 = 'data/C.png'
    circleToCDisplacementName = 'circleToCDisplacement.npy'
    circleToCDisplacementInverseName = 'circleToCDisplacementInverse.npy'
    nib_moving = plt.imread(fname0)
    nib_fixed = plt.imread(fname1)
    moving = nib_moving[:, :, 0]
    fixed = nib_fixed[:, :, 1]
    moving = (moving - moving.min()) / (moving.max() - moving.min())
    fixed = (fixed - fixed.min()) / (fixed.max() - fixed.min())
    level = 3
    maskMoving = moving > 0
    maskFixed = fixed > 0
    movingPyramid = [
        img for img in rcommon.pyramid_gaussian_2D(moving, level,
                                                   np.ones_like(maskMoving))
    ]
    fixedPyramid = [
        img for img in rcommon.pyramid_gaussian_2D(fixed, level,
                                                   np.ones_like(maskFixed))
    ]
    rcommon.plotOverlaidPyramids(movingPyramid, fixedPyramid)
    displacementList = []
    maxOuterIter = [10, 50, 100, 100, 100, 100, 100, 100, 100]
    if (os.path.exists(circleToCDisplacementName)):
        displacement = np.load(circleToCDisplacementName)
        inverse = np.load(circleToCDisplacementInverseName)
    else:
        displacement, inverse = estimateMonomodalDiffeomorphicField2DMultiScale(
            movingPyramid, fixedPyramid, lambdaParam, maxOuterIter, 0,
            displacementList)
        np.save(circleToCDisplacementName, displacement)
        np.save(circleToCDisplacementInverseName, inverse)
    print 'vector field exponential'
    expd, invexpd = tf.vector_field_exponential(displacement, True)
    print 'vector field inversion'
    directInverse = tf.invert_vector_field(displacement, 1.0, 10000, 1e-7)
    print 'vector field inversion'
    directExpInverse = tf.invert_vector_field(expd, 1.0, 10000, 1e-7)
    ###Now compare inversions###
    residualJoint = np.array(
        tf.compose_vector_fields(displacement, inverse)[0])
    residualDirect = np.array(
        tf.compose_vector_fields(displacement, directInverse)[0])
    residualExpJoint = np.array(tf.compose_vector_fields(expd, invexpd)[0])
    residualExpDirect = np.array(
        tf.compose_vector_fields(expd, directExpInverse)[0])
    rcommon.plotDiffeomorphism(displacement, inverse, residualJoint, 'D-joint')
    rcommon.plotDiffeomorphism(expd, invexpd, residualExpJoint, 'expD-joint')
    d, invd, res, jacobian = rcommon.plotDiffeomorphism(
        displacement, directInverse, residualDirect, 'D-direct')
    rcommon.plotDiffeomorphism(expd, directExpInverse, residualExpDirect,
                               'expD-direct')
    sp.misc.imsave('circleToC_deformation.png', d)
    sp.misc.imsave('circleToC_inverse_deformation.png', invd)
    sp.misc.imsave('circleToC_residual_deformation.png', res)
    tf.write_double_buffer(
        np.array(displacement).reshape(-1),
        '../inverse/experiments/displacement.bin')
    tf.write_double_buffer(
        np.array(displacement).reshape(-1),
        '../inverse/experiments/displacement_clean.bin')