def estimateNewMonomodalDiffeomorphicField2D(moving, fixed, lambdaParam,
                                             maxOuterIter,
                                             previousDisplacement,
                                             previousDisplacementInverse):
    '''
    Warning: in the monomodal case, the parameter lambda must be significantly lower than in the multimodal case. Try lambdaParam=1,
    as opposed as lambdaParam=150 used in the multimodal case
    '''
    innerTolerance = 1e-4
    displacement = np.zeros(shape=(moving.shape) + (2, ), dtype=np.float64)
    gradientField = np.empty(shape=(moving.shape) + (2, ), dtype=np.float64)
    totalDisplacement = np.zeros(shape=(moving.shape) + (2, ),
                                 dtype=np.float64)
    totalDisplacementInverse = np.zeros(shape=(moving.shape) + (2, ),
                                        dtype=np.float64)
    if (previousDisplacement != None):
        totalDisplacement[...] = previousDisplacement
        totalDisplacementInverse[...] = previousDisplacementInverse
    outerIter = 0
    framesToCapture = 5
    maxOuterIter = framesToCapture * (
        (maxOuterIter + framesToCapture - 1) / framesToCapture)
    itersPerCapture = maxOuterIter / framesToCapture
    plt.figure()
    while (outerIter < maxOuterIter):
        outerIter += 1
        print 'Outer iter:', outerIter
        warped = np.array(tf.warp_image(moving, totalDisplacement, None))
        if ((outerIter == 1) or (outerIter % itersPerCapture == 0)):
            plt.subplot(1, framesToCapture + 1,
                        1 + outerIter / itersPerCapture)
            rcommon.overlayImages(warped, fixed, False)
            plt.title('Iter:' + str(outerIter - 1))
        sigmaField = np.ones_like(warped, dtype=np.float64)
        deltaField = fixed - warped
        gradientField[:, :, 0], gradientField[:, :, 1] = sp.gradient(warped)
        maxVariation = 1 + innerTolerance
        innerIter = 0
        displacement[...] = 0
        maxInnerIter = 200
        while ((maxVariation > innerTolerance) and (innerIter < maxInnerIter)):
            innerIter += 1
            maxVariation = tf.iterateDisplacementField2DCYTHON(
                deltaField, sigmaField, gradientField, lambdaParam,
                displacement, None)
        #maxDisplacement=np.max(np.abs(displacement))
        expd, invexpd = tf.vector_field_exponential(displacement, True)
        totalDisplacement, stats = tf.compose_vector_fields(
            displacement, totalDisplacement)
        #totalDisplacement=np.array(totalDisplacement)
        totalDisplacementInverse, stats = tf.compose_vector_fields(
            totalDisplacementInverse, invexpd)
        #totalDisplacementInverse=np.array(totalDisplacementInverse)
        #if(maxDisplacement<outerTolerance):
        #break
    print "Iter: ", innerIter, "Max variation:", maxVariation
    return totalDisplacement, totalDisplacementInverse
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 estimateNewMonomodalDiffeomorphicField2D(moving, fixed, lambdaParam, maxOuterIter, previousDisplacement, previousDisplacementInverse):
    '''
    Warning: in the monomodal case, the parameter lambda must be significantly lower than in the multimodal case. Try lambdaParam=1,
    as opposed as lambdaParam=150 used in the multimodal case
    '''
    innerTolerance=1e-4
    displacement     =np.zeros(shape=(moving.shape)+(2,), dtype=np.float64)
    gradientField    =np.empty(shape=(moving.shape)+(2,), dtype=np.float64)
    totalDisplacement=np.zeros(shape=(moving.shape)+(2,), dtype=np.float64)
    totalDisplacementInverse=np.zeros(shape=(moving.shape)+(2,), dtype=np.float64)
    if(previousDisplacement!=None):
        totalDisplacement[...]=previousDisplacement
        totalDisplacementInverse[...]=previousDisplacementInverse
    outerIter=0
    framesToCapture=5
    maxOuterIter=framesToCapture*((maxOuterIter+framesToCapture-1)/framesToCapture)
    itersPerCapture=maxOuterIter/framesToCapture
    plt.figure()
    while(outerIter<maxOuterIter):
        outerIter+=1
        print 'Outer iter:', outerIter
        warped=np.array(tf.warp_image(moving, totalDisplacement, None))
        if((outerIter==1) or (outerIter%itersPerCapture==0)):
            plt.subplot(1,framesToCapture+1, 1+outerIter/itersPerCapture)
            rcommon.overlayImages(warped, fixed, False)
            plt.title('Iter:'+str(outerIter-1))
        sigmaField=np.ones_like(warped, dtype=np.float64)
        deltaField=fixed-warped
        gradientField[:,:,0], gradientField[:,:,1]=sp.gradient(warped)
        maxVariation=1+innerTolerance
        innerIter=0
        displacement[...]=0
        maxInnerIter=200
        while((maxVariation>innerTolerance)and(innerIter<maxInnerIter)):
            innerIter+=1
            maxVariation=tf.iterateDisplacementField2DCYTHON(deltaField, sigmaField, gradientField,  lambdaParam, displacement, None)
        #maxDisplacement=np.max(np.abs(displacement))
        expd, invexpd=tf.vector_field_exponential(displacement, True)
        totalDisplacement, stats=tf.compose_vector_fields(displacement, totalDisplacement)
        #totalDisplacement=np.array(totalDisplacement)
        totalDisplacementInverse, stats=tf.compose_vector_fields(totalDisplacementInverse, invexpd)
        #totalDisplacementInverse=np.array(totalDisplacementInverse)
        #if(maxDisplacement<outerTolerance):
            #break
    print "Iter: ",innerIter, "Max variation:",maxVariation
    return totalDisplacement, totalDisplacementInverse
예제 #4
0
 def update(new_displacement, current_displacement):
     expd, invexpd = tf.vector_field_exponential(new_displacement, True)
     updated, stats = tf.compose_vector_fields(expd, current_displacement)
     return updated, stats[0]
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 estimateNewMultimodalDiffeomorphicField2D(moving, fixed,
                                              lambdaDisplacement,
                                              quantizationLevels, maxOuterIter,
                                              previousDisplacement,
                                              previousDisplacementInverse):
    innerTolerance = 1e-4
    outerTolerance = 1e-3
    sh = moving.shape
    X0, X1 = np.mgrid[0:sh[0], 0:sh[1]]
    displacement = np.empty(shape=(moving.shape) + (2, ), dtype=np.float64)
    residuals = np.zeros(shape=(moving.shape), dtype=np.float64)
    gradientField = np.empty(shape=(moving.shape) + (2, ), dtype=np.float64)
    totalDisplacement = np.zeros(shape=(moving.shape) + (2, ),
                                 dtype=np.float64)
    totalDisplacementInverse = np.zeros(shape=(moving.shape) + (2, ),
                                        dtype=np.float64)
    if (previousDisplacement != None):
        totalDisplacement[...] = previousDisplacement
        totalDisplacementInverse[...] = previousDisplacementInverse
    fixedQ = None
    grayLevels = None
    fixedQ, grayLevels, hist = tf.quantizePositiveImageCYTHON(
        fixed, quantizationLevels)
    fixedQ = np.array(fixedQ, dtype=np.int32)
    finished = False
    outerIter = 0
    maxDisplacement = None
    maxVariation = None
    maxResidual = 0
    while ((not finished) and (outerIter < maxOuterIter)):
        outerIter += 1
        #---E step---
        warped = ndimage.map_coordinates(
            moving,
            [X0 + totalDisplacement[..., 0], X1 + totalDisplacement[..., 1]],
            prefilter=True)
        movingMask = ((moving > 0) * 1.0) * ((fixed > 0) * 1.0)
        warpedMovingMask = ndimage.map_coordinates(
            movingMask,
            [X0 + totalDisplacement[..., 0], X1 + totalDisplacement[..., 1]],
            order=0,
            prefilter=False)
        warpedMovingMask = warpedMovingMask.astype(np.int32)
        means, variances = tf.computeMaskedImageClassStatsCYTHON(
            warpedMovingMask, warped, quantizationLevels, fixedQ)
        means[0] = 0
        means = np.array(means)
        variances = np.array(variances)
        sigmaField = variances[fixedQ]
        deltaField = means[
            fixedQ] - warped  #########Delta-field using Arce's rule
        #--M step--
        g0, g1 = sp.gradient(warped)
        gradientField[:, :, 0] = g0
        gradientField[:, :, 1] = g1
        maxVariation = 1 + innerTolerance
        innerIter = 0
        maxInnerIter = 1000
        displacement[...] = 0
        while ((maxVariation > innerTolerance) and (innerIter < maxInnerIter)):
            innerIter += 1
            maxVariation = tf.iterateDisplacementField2DCYTHON(
                deltaField, sigmaField, gradientField, lambdaDisplacement,
                totalDisplacement, displacement, residuals)
            opt = np.max(residuals)
            if (maxResidual < opt):
                maxResidual = opt
        #--accumulate displacement--
        expd, invexpd = tf.vector_field_exponential(displacement)
        totalDisplacement = tf.compose_vector_fields(expd, totalDisplacement)
        totalDisplacementInverse = tf.compose_vector_fields(
            totalDisplacementInverse, invexpd)
        #--check stop condition--
        nrm = np.sqrt(displacement[..., 0]**2 + displacement[..., 1]**2)
        #maxDisplacement=np.max(nrm)
        maxDisplacement = np.mean(nrm)
        if ((maxDisplacement < outerTolerance) or (outerIter >= maxOuterIter)):
            finished = True


#            plt.figure()
#            plt.subplot(1,3,1)
#            plt.imshow(means[fixedQ],cmap=plt.cm.gray)
#            plt.title("Estimated warped modality")
#            plt.subplot(1,3,2)
#            plt.imshow(fixedQ,cmap=plt.cm.gray)
#            plt.title("Quantized")
#            plt.subplot(1,3,3)
#            plt.plot(means)
#            plt.title("Means")
    print "Iter: ", outerIter, "Mean displacement:", maxDisplacement, "Max variation:", maxVariation, "Max residual:", maxResidual
    if (previousDisplacement != None):
        return totalDisplacement - previousDisplacement, totalDisplacementInverse
    return totalDisplacement, totalDisplacementInverse
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 estimateNewMultimodalDiffeomorphicField2D(moving, fixed, lambdaDisplacement, quantizationLevels, maxOuterIter, previousDisplacement, previousDisplacementInverse):
    innerTolerance=1e-4
    outerTolerance=1e-3
    sh=moving.shape
    X0,X1=np.mgrid[0:sh[0], 0:sh[1]]
    displacement     =np.empty(shape=(moving.shape)+(2,), dtype=np.float64)
    residuals=np.zeros(shape=(moving.shape), dtype=np.float64)
    gradientField    =np.empty(shape=(moving.shape)+(2,), dtype=np.float64)
    totalDisplacement=np.zeros(shape=(moving.shape)+(2,), dtype=np.float64)
    totalDisplacementInverse=np.zeros(shape=(moving.shape)+(2,), dtype=np.float64)
    if(previousDisplacement!=None):
        totalDisplacement[...]=previousDisplacement
        totalDisplacementInverse[...]=previousDisplacementInverse
    fixedQ=None
    grayLevels=None
    fixedQ, grayLevels, hist=tf.quantizePositiveImageCYTHON(fixed, quantizationLevels)
    fixedQ=np.array(fixedQ, dtype=np.int32)
    finished=False
    outerIter=0
    maxDisplacement=None
    maxVariation=None
    maxResidual=0
    while((not finished) and (outerIter<maxOuterIter)):
        outerIter+=1
        #---E step---
        warped=ndimage.map_coordinates(moving, [X0+totalDisplacement[...,0], X1+totalDisplacement[...,1]], prefilter=True)
        movingMask=((moving>0)*1.0)*((fixed>0)*1.0)
        warpedMovingMask=ndimage.map_coordinates(movingMask, [X0+totalDisplacement[...,0], X1+totalDisplacement[...,1]], order=0, prefilter=False)
        warpedMovingMask=warpedMovingMask.astype(np.int32)
        means, variances=tf.computeMaskedImageClassStatsCYTHON(warpedMovingMask, warped, quantizationLevels, fixedQ)            
        means[0]=0
        means=np.array(means)
        variances=np.array(variances)
        sigmaField=variances[fixedQ]
        deltaField=means[fixedQ]-warped#########Delta-field using Arce's rule
        #--M step--
        g0, g1=sp.gradient(warped)
        gradientField[:,:,0]=g0
        gradientField[:,:,1]=g1
        maxVariation=1+innerTolerance
        innerIter=0
        maxInnerIter=1000
        displacement[...]=0
        while((maxVariation>innerTolerance)and(innerIter<maxInnerIter)):
            innerIter+=1
            maxVariation=tf.iterateDisplacementField2DCYTHON(deltaField, sigmaField, gradientField,  lambdaDisplacement, totalDisplacement, displacement, residuals)
            opt=np.max(residuals)
            if(maxResidual<opt):
                maxResidual=opt
        #--accumulate displacement--
        expd, invexpd=tf.vector_field_exponential(displacement)
        totalDisplacement=tf.compose_vector_fields(expd, totalDisplacement)
        totalDisplacementInverse=tf.compose_vector_fields(totalDisplacementInverse, invexpd)
        #--check stop condition--
        nrm=np.sqrt(displacement[...,0]**2+displacement[...,1]**2)
        #maxDisplacement=np.max(nrm)
        maxDisplacement=np.mean(nrm)
        if((maxDisplacement<outerTolerance)or(outerIter>=maxOuterIter)):
            finished=True
#            plt.figure()
#            plt.subplot(1,3,1)
#            plt.imshow(means[fixedQ],cmap=plt.cm.gray)    
#            plt.title("Estimated warped modality")
#            plt.subplot(1,3,2)
#            plt.imshow(fixedQ,cmap=plt.cm.gray)
#            plt.title("Quantized")
#            plt.subplot(1,3,3)
#            plt.plot(means)
#            plt.title("Means")
    print "Iter: ",outerIter, "Mean displacement:", maxDisplacement, "Max variation:",maxVariation, "Max residual:", maxResidual
    if(previousDisplacement!=None):
        return totalDisplacement-previousDisplacement, totalDisplacementInverse
    return totalDisplacement, totalDisplacementInverse