def testCircleToCMonomodalDiffeomorphic(lambdaParam): import numpy as np import tensorFieldUtils as tf import matplotlib.pyplot as plt import registrationCommon as rcommon fname0='data/circle.png' #fname0='data/C_trans.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) X1,X0=np.mgrid[0:displacement.shape[0], 0:displacement.shape[1]] detJacobian=rcommon.computeJacobianField(displacement) plt.figure() plt.imshow(detJacobian) CS=plt.contour(X0,X1,detJacobian,levels=[0.0], colors='b') plt.clabel(CS, inline=1, fontsize=10) plt.title('det(J(displacement))') print 'J range:', '[', detJacobian.min(), detJacobian.max(),']' #directInverse=np.array(tf.invert_vector_field(displacement, 0.5, 1000, 1e-7)) directInverse=np.array(tf.invert_vector_field_fixed_point(displacement, 100, 1e-7)) detJacobianInverse=rcommon.computeJacobianField(directInverse) plt.figure() plt.imshow(detJacobianInverse) CS=plt.contour(X0,X1,detJacobianInverse, levels=[0.0],colors='w') plt.clabel(CS, inline=1, fontsize=10) plt.title('det(J(displacement^-1))') print 'J^-1 range:', '[', detJacobianInverse.min(), detJacobianInverse.max(),']' #directInverse=rcommon.invert_vector_field_fixed_point(displacement, 1000, 1e-7) residual, stats=tf.compose_vector_fields(displacement, inverse) residual=np.array(residual) directResidual,stats=tf.compose_vector_fields(displacement, directInverse) directResidual=np.array(directResidual) # 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, 'inv-joint', 7) rcommon.plotDiffeomorphism(displacement, directInverse, directResidual, 'inv-direct', 7) tf.write_double_buffer(displacement.reshape(-1), 'displacement.bin')
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(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 testCircleToCMonomodalDiffeomorphic(lambdaParam): import numpy as np import tensorFieldUtils as tf import matplotlib.pyplot as plt import registrationCommon as rcommon fname0 = 'data/circle.png' #fname0='data/C_trans.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) X1, X0 = np.mgrid[0:displacement.shape[0], 0:displacement.shape[1]] detJacobian = rcommon.computeJacobianField(displacement) plt.figure() plt.imshow(detJacobian) CS = plt.contour(X0, X1, detJacobian, levels=[0.0], colors='b') plt.clabel(CS, inline=1, fontsize=10) plt.title('det(J(displacement))') print 'J range:', '[', detJacobian.min(), detJacobian.max(), ']' #directInverse=np.array(tf.invert_vector_field(displacement, 0.5, 1000, 1e-7)) directInverse = np.array( tf.invert_vector_field_fixed_point(displacement, 100, 1e-7)) detJacobianInverse = rcommon.computeJacobianField(directInverse) plt.figure() plt.imshow(detJacobianInverse) CS = plt.contour(X0, X1, detJacobianInverse, levels=[0.0], colors='w') plt.clabel(CS, inline=1, fontsize=10) plt.title('det(J(displacement^-1))') print 'J^-1 range:', '[', detJacobianInverse.min(), detJacobianInverse.max( ), ']' #directInverse=rcommon.invert_vector_field_fixed_point(displacement, 1000, 1e-7) residual, stats = tf.compose_vector_fields(displacement, inverse) residual = np.array(residual) directResidual, stats = tf.compose_vector_fields(displacement, directInverse) directResidual = np.array(directResidual) # 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, 'inv-joint', 7) rcommon.plotDiffeomorphism(displacement, directInverse, directResidual, 'inv-direct', 7) tf.write_double_buffer(displacement.reshape(-1), 'displacement.bin')