Beispiel #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)
def testEstimateMonomodalDiffeomorphicField2DMultiScale(lambdaParam):
    fname0='IBSR_01_to_02.nii.gz'
    fname1='data/t1/IBSR18/IBSR_02/IBSR_02_ana_strip.nii.gz'
    nib_moving = nib.load(fname0)
    nib_fixed= nib.load(fname1)
    moving=nib_moving.get_data().squeeze().astype(np.float64)
    fixed=nib_fixed.get_data().squeeze().astype(np.float64)
    moving=np.copy(moving, order='C')
    fixed=np.copy(fixed, order='C')
    sl=moving.shape
    sr=fixed.shape
    level=5
    #---sagital---
    moving=moving[sl[0]//2,:,:].copy()
    fixed=fixed[sr[0]//2,:,:].copy()
    #---coronal---
    #moving=moving[:,sl[1]//2,:].copy()
    #fixed=fixed[:,sr[1]//2,:].copy()
    #---axial---
    #moving=moving[:,:,sl[2]//2].copy()
    #fixed=fixed[:,:,sr[2]//2].copy()
    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=[]
    maxIter=200
    displacement, inverse=estimateMonomodalDiffeomorphicField2DMultiScale(movingPyramid, fixedPyramid, lambdaParam, maxIter, 0,displacementList)
    residual=tf.compose_vector_fields(displacement, inverse)
    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)
Beispiel #3
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 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 testEstimateECQMMFMultimodalDeformationField2DMultiScale_synthetic():
    ##################parameters############
    maxGTDisplacement=2
    maxPyramidLevel=0
    lambdaMeasureField=0.02
    lambdaDisplacement=200
    mu=0.001
    maxOuterIter=20
    maxInnerIter=50
    tolerance=1e-5
    displacementList=[]
    #######################################3
    #fname0='IBSR_01_to_02.nii.gz'
    #fname1='data/t1/IBSR18/IBSR_02/IBSR_02_ana_strip.nii.gz'
    fnameMoving='data/t2/t2_icbm_normal_1mm_pn0_rf0_peeled.nii.gz'
    fnameFixed='data/t1/t1_icbm_normal_1mm_pn0_rf0_peeled.nii.gz'
    nib_moving = nib.load(fnameMoving)
    nib_fixed = nib.load(fnameFixed)
    moving=nib_moving.get_data().squeeze().astype(np.float64)
    fixed=nib_fixed.get_data().squeeze().astype(np.float64)
    sm=moving.shape
    sf=fixed.shape
    #---coronal---
    moving=moving[:,sm[1]//2,:].copy()
    fixed=fixed[:,sf[1]//2,:].copy()
    moving=(moving-moving.min())/(moving.max()-moving.min())
    fixed=(fixed-fixed.min())/(fixed.max()-fixed.min())
    #----apply synthetic deformation field to fixed image
    GT=rcommon.createDeformationField_type2(fixed.shape[0], fixed.shape[1], maxGTDisplacement)
    fixed=rcommon.warpImage(fixed,GT)
    maskMoving=moving>0
    maskFixed=fixed>0
    movingPyramid=[img for img in rcommon.pyramid_gaussian_2D(moving, maxPyramidLevel, maskMoving)]
    fixedPyramid=[img for img in rcommon.pyramid_gaussian_2D(fixed, maxPyramidLevel, maskFixed)]
    plt.figure()
    plt.subplot(1,2,1)
    plt.imshow(moving, cmap=plt.cm.gray)
    plt.title('Moving')
    plt.subplot(1,2,2)
    plt.imshow(fixed, cmap=plt.cm.gray)
    plt.title('Fixed')
    rcommon.plotOverlaidPyramids(movingPyramid, fixedPyramid)
    displacement=estimateECQMMFMultimodalDeformationField2DMultiScale(fixedPyramid, movingPyramid, lambdaMeasureField, lambdaDisplacement, mu, maxOuterIter, maxInnerIter, tolerance, 0,displacementList)
    warpedPyramid=[rcommon.warpImage(movingPyramid[i], displacementList[i]) for i in range(maxPyramidLevel+1)]
    rcommon.plotOverlaidPyramids(warpedPyramid, fixedPyramid)
    rcommon.overlayImages(warpedPyramid[0], fixedPyramid[0])
    rcommon.plotDeformationField(displacement)
    displacement[...,0]*=(maskMoving + maskFixed)
    displacement[...,1]*=(maskMoving + maskFixed)
    nrm=np.sqrt(displacement[...,0]**2 + displacement[...,1]**2)
    maxNorm=np.max(nrm)
    rcommon.plotDeformationField(displacement)
    residual=((displacement-GT))**2
    meanDisplacementError=np.sqrt(residual.sum(2)*(maskMoving + maskFixed)).mean()
    stdevDisplacementError=np.sqrt(residual.sum(2)*(maskMoving + maskFixed)).std()
    print 'Max global displacement: ', maxNorm
    print 'Mean displacement error: ', meanDisplacementError,'(',stdevDisplacementError,')'
Beispiel #6
0
def testEstimateMonomodalSyNField2DMultiScale(lambdaParam):
    fname0 = 'IBSR_01_to_02.nii.gz'
    fname1 = 'data/t1/IBSR18/IBSR_02/IBSR_02_ana_strip.nii.gz'
    nib_moving = nib.load(fname0)
    nib_fixed = nib.load(fname1)
    moving = nib_moving.get_data().squeeze()
    fixed = nib_fixed.get_data().squeeze()
    moving = np.copy(moving, order='C')
    fixed = np.copy(fixed, order='C')
    sl = moving.shape
    sr = fixed.shape
    level = 5
    #---sagital---
    moving = moving[sl[0] // 2, :, :].copy()
    fixed = fixed[sr[0] // 2, :, :].copy()
    #---coronal---
    #moving=moving[:,sl[1]//2,:].copy()
    #fixed=fixed[:,sr[1]//2,:].copy()
    #---axial---
    #moving=moving[:,:,sl[2]//2].copy()
    #fixed=fixed[:,:,sr[2]//2].copy()
    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 = []
    maxIter = 200
    displacement = estimateMonomodalSyNField2DMultiScale(
        movingPyramid, fixedPyramid, lambdaParam, maxIter, 0, displacementList)
    warpPyramid = [
        rcommon.warpImage(movingPyramid[i], displacementList[i])
        for i in range(level + 1)
    ]
    rcommon.plotOverlaidPyramids(warpPyramid, fixedPyramid)
    rcommon.overlayImages(warpPyramid[0], fixedPyramid[0])
    rcommon.plotDeformationField(displacement)
    nrm = np.sqrt(displacement[..., 0]**2 + displacement[..., 1]**2)
    maxNorm = np.max(nrm)
    displacement[..., 0] *= (maskMoving + maskFixed)
    displacement[..., 1] *= (maskMoving + maskFixed)
    rcommon.plotDeformationField(displacement)
    #nrm=np.sqrt(displacement[...,0]**2 + displacement[...,1]**2)
    #plt.figure()
    #plt.imshow(nrm)
    print 'Max global displacement: ', maxNorm
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')
Beispiel #8
0
def testEstimateMonomodalSyNField2DMultiScale(lambdaParam):
    fname0='IBSR_01_to_02.nii.gz'
    fname1='data/t1/IBSR18/IBSR_02/IBSR_02_ana_strip.nii.gz'
    nib_moving = nib.load(fname0)
    nib_fixed= nib.load(fname1)
    moving=nib_moving.get_data().squeeze()
    fixed=nib_fixed.get_data().squeeze()
    moving=np.copy(moving, order='C')
    fixed=np.copy(fixed, order='C')
    sl=moving.shape
    sr=fixed.shape
    level=5
    #---sagital---
    moving=moving[sl[0]//2,:,:].copy()
    fixed=fixed[sr[0]//2,:,:].copy()
    #---coronal---
    #moving=moving[:,sl[1]//2,:].copy()
    #fixed=fixed[:,sr[1]//2,:].copy()
    #---axial---
    #moving=moving[:,:,sl[2]//2].copy()
    #fixed=fixed[:,:,sr[2]//2].copy()
    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=[]
    maxIter=200
    displacement=estimateMonomodalSyNField2DMultiScale(movingPyramid, fixedPyramid, lambdaParam, maxIter, 0,displacementList)
    warpPyramid=[rcommon.warpImage(movingPyramid[i], displacementList[i]) for i in range(level+1)]
    rcommon.plotOverlaidPyramids(warpPyramid, fixedPyramid)
    rcommon.overlayImages(warpPyramid[0], fixedPyramid[0])
    rcommon.plotDeformationField(displacement)
    nrm=np.sqrt(displacement[...,0]**2 + displacement[...,1]**2)
    maxNorm=np.max(nrm)
    displacement[...,0]*=(maskMoving + maskFixed)
    displacement[...,1]*=(maskMoving + maskFixed)
    rcommon.plotDeformationField(displacement)
    #nrm=np.sqrt(displacement[...,0]**2 + displacement[...,1]**2)
    #plt.figure()
    #plt.imshow(nrm)
    print 'Max global displacement: ', maxNorm
def testEstimateMonomodalDiffeomorphicField2DMultiScale(lambdaParam):
    fname0 = 'IBSR_01_to_02.nii.gz'
    fname1 = 'data/t1/IBSR18/IBSR_02/IBSR_02_ana_strip.nii.gz'
    nib_moving = nib.load(fname0)
    nib_fixed = nib.load(fname1)
    moving = nib_moving.get_data().squeeze().astype(np.float64)
    fixed = nib_fixed.get_data().squeeze().astype(np.float64)
    moving = np.copy(moving, order='C')
    fixed = np.copy(fixed, order='C')
    sl = moving.shape
    sr = fixed.shape
    level = 5
    #---sagital---
    moving = moving[sl[0] // 2, :, :].copy()
    fixed = fixed[sr[0] // 2, :, :].copy()
    #---coronal---
    #moving=moving[:,sl[1]//2,:].copy()
    #fixed=fixed[:,sr[1]//2,:].copy()
    #---axial---
    #moving=moving[:,:,sl[2]//2].copy()
    #fixed=fixed[:,:,sr[2]//2].copy()
    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 = []
    maxIter = 200
    displacement, inverse = estimateMonomodalDiffeomorphicField2DMultiScale(
        movingPyramid, fixedPyramid, lambdaParam, maxIter, 0, displacementList)
    residual = tf.compose_vector_fields(displacement, inverse)
    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)
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 runArcesExperiment(rootDir, lambdaParam, maxOuterIter):
    #---Load displacement field---
    dxName = rootDir + 'Vx.dat'
    dyName = rootDir + 'Vy.dat'
    dx = np.loadtxt(dxName)
    dy = np.loadtxt(dyName)
    GT_in = np.ndarray(shape=dx.shape + (2, ), dtype=np.float64)
    GT_in[..., 0] = dy
    GT_in[..., 1] = dx
    GT, GTinv = tf.vector_field_exponential(GT_in)
    GTres = tf.compose_vector_fields(GT, GTinv)
    #---Load input images---
    fnameT1 = rootDir + 't1.jpg'
    fnameT2 = rootDir + 't2.jpg'
    fnamePD = rootDir + 'pd.jpg'
    fnameMask = rootDir + 'Mascara.bmp'
    t1 = plt.imread(fnameT1)[..., 0].astype(np.float64)
    t2 = plt.imread(fnameT2)[..., 0].astype(np.float64)
    pd = plt.imread(fnamePD)[..., 0].astype(np.float64)
    t1 = (t1 - t1.min()) / (t1.max() - t1.min())
    t2 = (t2 - t2.min()) / (t2.max() - t2.min())
    pd = (pd - pd.min()) / (pd.max() - pd.min())
    mask = plt.imread(fnameMask).astype(np.float64)
    fixed = t1
    moving = t2
    maskMoving = mask > 0
    maskFixed = mask > 0
    fixed *= mask
    moving *= mask
    plt.figure()
    plt.subplot(1, 4, 1)
    plt.imshow(t1, cmap=plt.cm.gray)
    plt.title('Input T1')
    plt.subplot(1, 4, 2)
    plt.imshow(t2, cmap=plt.cm.gray)
    plt.title('Input T2')
    plt.subplot(1, 4, 3)
    plt.imshow(pd, cmap=plt.cm.gray)
    plt.title('Input PD')
    plt.subplot(1, 4, 4)
    plt.imshow(mask, cmap=plt.cm.gray)
    plt.title('Input Mask')
    #-------------------------
    warpedFixed = rcommon.warpImage(fixed, GT)
    print 'Registering T2 (template) to deformed T1 (template)...'
    level = 3
    movingPyramid = [
        img for img in rcommon.pyramid_gaussian_2D(moving, level, maskMoving)
    ]
    fixedPyramid = [
        img
        for img in rcommon.pyramid_gaussian_2D(warpedFixed, level, maskFixed)
    ]
    plt.figure()
    plt.subplot(1, 2, 1)
    plt.imshow(moving, cmap=plt.cm.gray)
    plt.title('Moving')
    plt.subplot(1, 2, 2)
    plt.imshow(warpedFixed, cmap=plt.cm.gray)
    plt.title('Fixed')
    rcommon.plotOverlaidPyramids(movingPyramid, fixedPyramid)
    displacementList = []
    displacement, inverse = estimateMultimodalDiffeomorphicField2DMultiScale(
        movingPyramid, fixedPyramid, lambdaParam, maxOuterIter, 0,
        displacementList)
    residual = tf.compose_vector_fields(displacement, inverse)
    warpPyramid = [
        rcommon.warpImage(movingPyramid[i], displacementList[i])
        for i in range(level + 1)
    ]
    rcommon.plotOverlaidPyramids(warpPyramid, fixedPyramid)
    rcommon.overlayImages(warpPyramid[0], fixedPyramid[0])
    displacement[..., 0] *= (maskFixed)
    displacement[..., 1] *= (maskFixed)
    #----plot deformations---
    rcommon.plotDiffeomorphism(GT, GTinv, GTres, 7)
    rcommon.plotDiffeomorphism(displacement, inverse, residual, 7)
    #----statistics---
    nrm = np.sqrt(displacement[..., 0]**2 + displacement[..., 1]**2)
    nrm *= maskFixed
    maxNorm = np.max(nrm)
    residual = ((displacement - GT))**2
    meanDisplacementError = np.sqrt(residual.sum(2) * (maskFixed)).mean()
    stdevDisplacementError = np.sqrt(residual.sum(2) * (maskFixed)).std()
    print 'Max global displacement: ', maxNorm
    print 'Mean displacement error: ', meanDisplacementError, '(', stdevDisplacementError, ')'
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')
Beispiel #13
0
def testEstimateECQMMFMultimodalDeformationField2DMultiScale_synthetic():
    ##################parameters############
    maxGTDisplacement = 2
    maxPyramidLevel = 0
    lambdaMeasureField = 0.02
    lambdaDisplacement = 200
    mu = 0.001
    maxOuterIter = 20
    maxInnerIter = 50
    tolerance = 1e-5
    displacementList = []
    #######################################3
    #fname0='IBSR_01_to_02.nii.gz'
    #fname1='data/t1/IBSR18/IBSR_02/IBSR_02_ana_strip.nii.gz'
    fnameMoving = 'data/t2/t2_icbm_normal_1mm_pn0_rf0_peeled.nii.gz'
    fnameFixed = 'data/t1/t1_icbm_normal_1mm_pn0_rf0_peeled.nii.gz'
    nib_moving = nib.load(fnameMoving)
    nib_fixed = nib.load(fnameFixed)
    moving = nib_moving.get_data().squeeze().astype(np.float64)
    fixed = nib_fixed.get_data().squeeze().astype(np.float64)
    sm = moving.shape
    sf = fixed.shape
    #---coronal---
    moving = moving[:, sm[1] // 2, :].copy()
    fixed = fixed[:, sf[1] // 2, :].copy()
    moving = (moving - moving.min()) / (moving.max() - moving.min())
    fixed = (fixed - fixed.min()) / (fixed.max() - fixed.min())
    #----apply synthetic deformation field to fixed image
    GT = rcommon.createDeformationField_type2(fixed.shape[0], fixed.shape[1],
                                              maxGTDisplacement)
    fixed = rcommon.warpImage(fixed, GT)
    maskMoving = moving > 0
    maskFixed = fixed > 0
    movingPyramid = [
        img for img in rcommon.pyramid_gaussian_2D(moving, maxPyramidLevel,
                                                   maskMoving)
    ]
    fixedPyramid = [
        img for img in rcommon.pyramid_gaussian_2D(fixed, maxPyramidLevel,
                                                   maskFixed)
    ]
    plt.figure()
    plt.subplot(1, 2, 1)
    plt.imshow(moving, cmap=plt.cm.gray)
    plt.title('Moving')
    plt.subplot(1, 2, 2)
    plt.imshow(fixed, cmap=plt.cm.gray)
    plt.title('Fixed')
    rcommon.plotOverlaidPyramids(movingPyramid, fixedPyramid)
    displacement = estimateECQMMFMultimodalDeformationField2DMultiScale(
        fixedPyramid, movingPyramid, lambdaMeasureField, lambdaDisplacement,
        mu, maxOuterIter, maxInnerIter, tolerance, 0, displacementList)
    warpedPyramid = [
        rcommon.warpImage(movingPyramid[i], displacementList[i])
        for i in range(maxPyramidLevel + 1)
    ]
    rcommon.plotOverlaidPyramids(warpedPyramid, fixedPyramid)
    rcommon.overlayImages(warpedPyramid[0], fixedPyramid[0])
    rcommon.plotDeformationField(displacement)
    displacement[..., 0] *= (maskMoving + maskFixed)
    displacement[..., 1] *= (maskMoving + maskFixed)
    nrm = np.sqrt(displacement[..., 0]**2 + displacement[..., 1]**2)
    maxNorm = np.max(nrm)
    rcommon.plotDeformationField(displacement)
    residual = ((displacement - GT))**2
    meanDisplacementError = np.sqrt(
        residual.sum(2) * (maskMoving + maskFixed)).mean()
    stdevDisplacementError = np.sqrt(
        residual.sum(2) * (maskMoving + maskFixed)).std()
    print 'Max global displacement: ', maxNorm
    print 'Mean displacement error: ', meanDisplacementError, '(', stdevDisplacementError, ')'
def runArcesExperiment(rootDir, lambdaParam, maxOuterIter):
    #---Load displacement field---
    dxName=rootDir+'Vx.dat'
    dyName=rootDir+'Vy.dat'
    dx=np.loadtxt(dxName)
    dy=np.loadtxt(dyName)
    GT_in=np.ndarray(shape=dx.shape+(2,), dtype=np.float64)
    GT_in[...,0]=dy
    GT_in[...,1]=dx
    GT, GTinv=tf.vector_field_exponential(GT_in)
    GTres=tf.compose_vector_fields(GT, GTinv)
    #---Load input images---
    fnameT1=rootDir+'t1.jpg'
    fnameT2=rootDir+'t2.jpg'
    fnamePD=rootDir+'pd.jpg'
    fnameMask=rootDir+'Mascara.bmp'
    t1=plt.imread(fnameT1)[...,0].astype(np.float64)
    t2=plt.imread(fnameT2)[...,0].astype(np.float64)
    pd=plt.imread(fnamePD)[...,0].astype(np.float64)
    t1=(t1-t1.min())/(t1.max()-t1.min())
    t2=(t2-t2.min())/(t2.max()-t2.min())
    pd=(pd-pd.min())/(pd.max()-pd.min())
    mask=plt.imread(fnameMask).astype(np.float64)
    fixed=t1
    moving=t2
    maskMoving=mask>0
    maskFixed=mask>0
    fixed*=mask
    moving*=mask
    plt.figure()
    plt.subplot(1,4,1)
    plt.imshow(t1, cmap=plt.cm.gray)
    plt.title('Input T1')
    plt.subplot(1,4,2)
    plt.imshow(t2, cmap=plt.cm.gray)
    plt.title('Input T2')
    plt.subplot(1,4,3)
    plt.imshow(pd, cmap=plt.cm.gray)
    plt.title('Input PD')
    plt.subplot(1,4,4)
    plt.imshow(mask, cmap=plt.cm.gray)
    plt.title('Input Mask')
    #-------------------------
    warpedFixed=rcommon.warpImage(fixed,GT)
    print 'Registering T2 (template) to deformed T1 (template)...'
    level=3
    movingPyramid=[img for img in rcommon.pyramid_gaussian_2D(moving, level, maskMoving)]
    fixedPyramid=[img for img in rcommon.pyramid_gaussian_2D(warpedFixed, level, maskFixed)]
    plt.figure()
    plt.subplot(1,2,1)
    plt.imshow(moving, cmap=plt.cm.gray)
    plt.title('Moving')
    plt.subplot(1,2,2)
    plt.imshow(warpedFixed, cmap=plt.cm.gray)
    plt.title('Fixed')
    rcommon.plotOverlaidPyramids(movingPyramid, fixedPyramid)
    displacementList=[]
    displacement, inverse=estimateMultimodalDiffeomorphicField2DMultiScale(movingPyramid, fixedPyramid, lambdaParam, maxOuterIter, 0, displacementList)
    residual=tf.compose_vector_fields(displacement, inverse)
    warpPyramid=[rcommon.warpImage(movingPyramid[i], displacementList[i]) for i in range(level+1)]
    rcommon.plotOverlaidPyramids(warpPyramid, fixedPyramid)
    rcommon.overlayImages(warpPyramid[0], fixedPyramid[0])
    displacement[...,0]*=(maskFixed)
    displacement[...,1]*=(maskFixed)
    #----plot deformations---
    rcommon.plotDiffeomorphism(GT, GTinv, GTres, 7)
    rcommon.plotDiffeomorphism(displacement, inverse, residual, 7)
    #----statistics---
    nrm=np.sqrt(displacement[...,0]**2 + displacement[...,1]**2)
    nrm*=maskFixed
    maxNorm=np.max(nrm)
    residual=((displacement-GT))**2
    meanDisplacementError=np.sqrt(residual.sum(2)*(maskFixed)).mean()
    stdevDisplacementError=np.sqrt(residual.sum(2)*(maskFixed)).std()
    print 'Max global displacement: ', maxNorm
    print 'Mean displacement error: ', meanDisplacementError,'(',stdevDisplacementError,')'