Beispiel #1
0
def testEstimateMultimodalSyN3DMultiScale(fnameMoving, fnameFixed, fnameAffine, warpDir, lambdaParam):
    '''
        testEstimateMultimodalDiffeomorphicField3DMultiScale('IBSR_01_ana_strip.nii.gz', 't1_icbm_normal_1mm_pn0_rf0_peeled.nii.gz', 'IBSR_01_ana_strip_t1_icbm_normal_1mm_pn0_rf0_peeledAffine.txt', 100)
    '''
    print 'Registering', fnameMoving, 'to', fnameFixed,'with lambda=',lambdaParam  
    sys.stdout.flush()
    moving = nib.load(fnameMoving)
    fixed= nib.load(fnameFixed)
    referenceShape=np.array(fixed.shape, dtype=np.int32)
    M=moving.get_affine()
    F=fixed.get_affine()
    if not fnameAffine:
        T=np.eye(4)
    else:
        T=rcommon.readAntsAffine(fnameAffine)
    initAffine=np.linalg.inv(M).dot(T.dot(F))
    print initAffine
    moving=moving.get_data().squeeze().astype(np.float64)
    fixed=fixed.get_data().squeeze().astype(np.float64)
    moving=np.copy(moving, order='C')
    fixed=np.copy(fixed, order='C')
    moving=(moving-moving.min())/(moving.max()-moving.min())
    fixed=(fixed-fixed.min())/(fixed.max()-fixed.min())
    level=2
    maskMoving=moving>0
    maskFixed=fixed>0
    movingPyramid=[img for img in rcommon.pyramid_gaussian_3D(moving, level, maskMoving)]
    fixedPyramid=[img for img in rcommon.pyramid_gaussian_3D(fixed, level, maskFixed)]
    #maxOuterIter=[25,50,100,100, 100, 100]
    maxOuterIter=[2,2,2,2,2,2]
    baseMoving=rcommon.getBaseFileName(fnameMoving)
    baseFixed=rcommon.getBaseFileName(fnameFixed)    
#    if(os.path.exists('disp_'+baseMoving+'_'+baseFixed+'.npy')):
#        displacement=np.load('disp_'+baseMoving+'_'+baseFixed+'.npy')
#    else:
    displacement, directInverse=estimateMultimodalSyN3DMultiScale(movingPyramid, fixedPyramid, initAffine, lambdaParam, maxOuterIter, 0)
    tf.prepend_affine_to_displacement_field(displacement, initAffine)
#    np.save('disp_'+baseMoving+'_'+baseFixed+'.npy', displacement)
    #####Warp all requested volumes
    #---first the target using tri-linear interpolation---
    moving=nib.load(fnameMoving).get_data().squeeze().astype(np.float64)
    moving=np.copy(moving, order='C')
    warped=np.array(tf.warp_volume(moving, displacement)).astype(np.int16)
    imgWarped=nib.Nifti1Image(warped, F)
    imgWarped.to_filename('warpedDiff_'+baseMoving+'_'+baseFixed+'.nii.gz')
    #---warp using affine only
    moving=nib.load(fnameMoving).get_data().squeeze().astype(np.int32)
    moving=np.copy(moving, order='C')
    warped=np.array(tf.warp_discrete_volumeNNAffine(moving, referenceShape, initAffine)).astype(np.int16)
    imgWarped=nib.Nifti1Image(warped, F)#The affine transformation is the reference's one
    imgWarped.to_filename('warpedAffine_'+baseMoving+'_'+baseFixed+'.nii.gz')
    #---now the rest of the targets using nearest neighbor
    names=[os.path.join(warpDir,name) for name in os.listdir(warpDir)]
    for name in names:
        #---warp using the non-linear deformation
        toWarp=nib.load(name).get_data().squeeze().astype(np.int32)
        toWarp=np.copy(toWarp, order='C')
        baseWarp=rcommon.getBaseFileName(name)
        warped=np.array(tf.warp_discrete_volumeNN(toWarp, displacement)).astype(np.int16)
        imgWarped=nib.Nifti1Image(warped, F)#The affine transformation is the reference's one
        imgWarped.to_filename('warpedDiff_'+baseWarp+'_'+baseFixed+'.nii.gz')
        #---warp using affine inly
        warped=np.array(tf.warp_discrete_volumeNNAffine(toWarp, referenceShape, initAffine)).astype(np.int16)
        imgWarped=nib.Nifti1Image(warped, F)#The affine transformation is the reference's one
        imgWarped.to_filename('warpedAffine_'+baseWarp+'_'+baseFixed+'.nii.gz')
    #---finally, the deformed lattices (forward, inverse and resdidual)---    
    lambdaParam=0.9
    maxIter=100
    tolerance=1e-4
    print 'Computing inverse...'
    inverse=np.array(tf.invert_vector_field3D(displacement, lambdaParam, maxIter, tolerance))
    residual=np.array(tf.compose_vector_fields3D(displacement, inverse))
    saveDeformedLattice3D(displacement, 'latticeDispDiff_'+baseMoving+'_'+baseFixed+'.nii.gz')
    saveDeformedLattice3D(inverse, 'latticeInvDiff_'+baseMoving+'_'+baseFixed+'.nii.gz')
    saveDeformedLattice3D(residual, 'latticeResdiff_'+baseMoving+'_'+baseFixed+'.nii.gz')
    residual=np.sqrt(np.sum(residual**2,3))
    print "Mean residual norm:", residual.mean()," (",residual.std(), "). Max residual norm:", residual.max()
Beispiel #2
0
def testEstimateMultimodalSyN3DMultiScale(fnameMoving, fnameFixed, fnameAffine,
                                          warpDir, lambdaParam):
    '''
        testEstimateMultimodalDiffeomorphicField3DMultiScale('IBSR_01_ana_strip.nii.gz', 't1_icbm_normal_1mm_pn0_rf0_peeled.nii.gz', 'IBSR_01_ana_strip_t1_icbm_normal_1mm_pn0_rf0_peeledAffine.txt', 100)
    '''
    print 'Registering', fnameMoving, 'to', fnameFixed, 'with lambda=', lambdaParam
    sys.stdout.flush()
    moving = nib.load(fnameMoving)
    fixed = nib.load(fnameFixed)
    referenceShape = np.array(fixed.shape, dtype=np.int32)
    M = moving.get_affine()
    F = fixed.get_affine()
    if not fnameAffine:
        T = np.eye(4)
    else:
        T = rcommon.readAntsAffine(fnameAffine)
    initAffine = np.linalg.inv(M).dot(T.dot(F))
    print initAffine
    moving = moving.get_data().squeeze().astype(np.float64)
    fixed = fixed.get_data().squeeze().astype(np.float64)
    moving = np.copy(moving, order='C')
    fixed = np.copy(fixed, order='C')
    moving = (moving - moving.min()) / (moving.max() - moving.min())
    fixed = (fixed - fixed.min()) / (fixed.max() - fixed.min())
    level = 2
    maskMoving = moving > 0
    maskFixed = fixed > 0
    movingPyramid = [
        img for img in rcommon.pyramid_gaussian_3D(moving, level, maskMoving)
    ]
    fixedPyramid = [
        img for img in rcommon.pyramid_gaussian_3D(fixed, level, maskFixed)
    ]
    #maxOuterIter=[25,50,100,100, 100, 100]
    maxOuterIter = [2, 2, 2, 2, 2, 2]
    baseMoving = rcommon.getBaseFileName(fnameMoving)
    baseFixed = rcommon.getBaseFileName(fnameFixed)
    #    if(os.path.exists('disp_'+baseMoving+'_'+baseFixed+'.npy')):
    #        displacement=np.load('disp_'+baseMoving+'_'+baseFixed+'.npy')
    #    else:
    displacement, directInverse = estimateMultimodalSyN3DMultiScale(
        movingPyramid, fixedPyramid, initAffine, lambdaParam, maxOuterIter, 0)
    tf.prepend_affine_to_displacement_field(displacement, initAffine)
    #    np.save('disp_'+baseMoving+'_'+baseFixed+'.npy', displacement)
    #####Warp all requested volumes
    #---first the target using tri-linear interpolation---
    moving = nib.load(fnameMoving).get_data().squeeze().astype(np.float64)
    moving = np.copy(moving, order='C')
    warped = np.array(tf.warp_volume(moving, displacement)).astype(np.int16)
    imgWarped = nib.Nifti1Image(warped, F)
    imgWarped.to_filename('warpedDiff_' + baseMoving + '_' + baseFixed +
                          '.nii.gz')
    #---warp using affine only
    moving = nib.load(fnameMoving).get_data().squeeze().astype(np.int32)
    moving = np.copy(moving, order='C')
    warped = np.array(
        tf.warp_discrete_volumeNNAffine(moving, referenceShape,
                                        initAffine)).astype(np.int16)
    imgWarped = nib.Nifti1Image(
        warped, F)  #The affine transformation is the reference's one
    imgWarped.to_filename('warpedAffine_' + baseMoving + '_' + baseFixed +
                          '.nii.gz')
    #---now the rest of the targets using nearest neighbor
    names = [os.path.join(warpDir, name) for name in os.listdir(warpDir)]
    for name in names:
        #---warp using the non-linear deformation
        toWarp = nib.load(name).get_data().squeeze().astype(np.int32)
        toWarp = np.copy(toWarp, order='C')
        baseWarp = rcommon.getBaseFileName(name)
        warped = np.array(tf.warp_discrete_volumeNN(
            toWarp, displacement)).astype(np.int16)
        imgWarped = nib.Nifti1Image(
            warped, F)  #The affine transformation is the reference's one
        imgWarped.to_filename('warpedDiff_' + baseWarp + '_' + baseFixed +
                              '.nii.gz')
        #---warp using affine inly
        warped = np.array(
            tf.warp_discrete_volumeNNAffine(toWarp, referenceShape,
                                            initAffine)).astype(np.int16)
        imgWarped = nib.Nifti1Image(
            warped, F)  #The affine transformation is the reference's one
        imgWarped.to_filename('warpedAffine_' + baseWarp + '_' + baseFixed +
                              '.nii.gz')
    #---finally, the deformed lattices (forward, inverse and resdidual)---
    lambdaParam = 0.9
    maxIter = 100
    tolerance = 1e-4
    print 'Computing inverse...'
    inverse = np.array(
        tf.invert_vector_field3D(displacement, lambdaParam, maxIter,
                                 tolerance))
    residual = np.array(tf.compose_vector_fields3D(displacement, inverse))
    saveDeformedLattice3D(
        displacement,
        'latticeDispDiff_' + baseMoving + '_' + baseFixed + '.nii.gz')
    saveDeformedLattice3D(
        inverse, 'latticeInvDiff_' + baseMoving + '_' + baseFixed + '.nii.gz')
    saveDeformedLattice3D(
        residual, 'latticeResdiff_' + baseMoving + '_' + baseFixed + '.nii.gz')
    residual = np.sqrt(np.sum(residual**2, 3))
    print "Mean residual norm:", residual.mean(), " (", residual.std(
    ), "). Max residual norm:", residual.max()
Beispiel #3
0
     if argc<3:
         print 'Displacement-field file name expected.'
         sys.exit(0)
     dispName=sys.argv[2]
     displacement=np.load(dispName)
     lambdaParam=0.9
     maxIter=100
     tolerance=1e-4
     if argc>3:
         lambdaParam=float(sys.argv[3])
     if argc>4:
         maxIter=int(sys.argv[4])
     if argc>5:
         tolerance=float(sys.argv[5])
     print 'Inverting displacement: ',dispName, '. With parameters: lambda=',lambdaParam, '. Maxiter=',maxIter, '. Tolerance=',tolerance,'...'
     inverse=np.array(tf.invert_vector_field3D(displacement, lambdaParam, maxIter, tolerance))
     invName="inv"+dispName
     print 'Saving inverse as:', invName
     np.save(invName, inverse)
     print 'Computing inversion error...'
     residual=np.array(tf.compose_vector_fields3D(displacement, inverse))
     residualName="res"+dispName
     print 'Saving residual as:', residualName
     np.save(residualName, residual)
     residual=np.sqrt(np.sum(residual**2,3))
     print "Mean residual norm:", residual.mean()," (",residual.std(), "). Max residual norm:", residual.max()
     sys.exit(0)
 elif(sys.argv[1]=='npy2nifti'):
     if argc<3:
         print 'File name expected.'
         sys.exit(0)