def testMultimodalRigidTransformationMultiScale3D_ecqmmf(betaGT, level): leftName = 'data/t2/t2_icbm_normal_1mm_pn0_rf0.rawb' rightName = 'data/t1/t1_icbm_normal_1mm_pn0_rf0.rawb' #rightName='data/t2/t2_icbm_normal_1mm_pn0_rf0.rawb' #leftName='data/t1/t1_icbm_normal_1mm_pn0_rf0.rawb' betaGTRads = np.array(betaGT, dtype=np.float64) betaGTRads[0:3] = np.copy(np.pi * betaGTRads[0:3] / 180.0) ns = 181 nr = 217 nc = 181 print 'Loading volume...' left = np.fromfile(leftName, dtype=np.ubyte).reshape(ns, nr, nc) left = left.astype(np.float64) print 'Generating pyramid at level', level, '...' right = np.fromfile(rightName, dtype=np.ubyte).reshape(ns, nr, nc) right = right.astype(np.float64) right = rcommon.applyRigidTransformation3D(right, betaGTRads) leftPyramid = [i for i in rcommon.pyramid_gaussian_3D(left, level)] rightPyramid = [i for i in rcommon.pyramid_gaussian_3D(right, level)] print 'Estimation started.' beta = estimateMultiModalRigidTransformationMultiscale3D( leftPyramid, rightPyramid) print 'Estimation finished.' print 'Ground truth:', betaGT plotSlicePyramidsAxial(leftPyramid, rightPyramid) return beta
def testMultimodalRigidTransformationMultiScale3D_ecqmmf(betaGT, level): leftName='data/t2/t2_icbm_normal_1mm_pn0_rf0.rawb' rightName='data/t1/t1_icbm_normal_1mm_pn0_rf0.rawb' #rightName='data/t2/t2_icbm_normal_1mm_pn0_rf0.rawb' #leftName='data/t1/t1_icbm_normal_1mm_pn0_rf0.rawb' betaGTRads=np.array(betaGT, dtype=np.float64) betaGTRads[0:3]=np.copy(np.pi*betaGTRads[0:3]/180.0) ns=181 nr=217 nc=181 print 'Loading volume...' left=np.fromfile(leftName, dtype=np.ubyte).reshape(ns,nr,nc) left=left.astype(np.float64) print 'Generating pyramid at level',level,'...' right=np.fromfile(rightName, dtype=np.ubyte).reshape(ns,nr,nc) right=right.astype(np.float64) right=rcommon.applyRigidTransformation3D(right, betaGTRads) leftPyramid=[i for i in rcommon.pyramid_gaussian_3D(left, level)] rightPyramid=[i for i in rcommon.pyramid_gaussian_3D(right, level)] print 'Estimation started.' beta=estimateMultiModalRigidTransformationMultiscale3D(leftPyramid, rightPyramid) print 'Estimation finished.' print 'Ground truth:', betaGT plotSlicePyramidsAxial(leftPyramid, rightPyramid) return beta
def testEstimateMonomodalDiffeomorphicField3DMultiScale(lambdaParam): fnameMoving = 'data/affineRegistered/templateT1ToIBSR01T1.nii.gz' fnameFixed = 'data/t1/IBSR18/IBSR_01/IBSR_01_ana_strip.nii.gz' moving = nib.load(fnameMoving) fixed = nib.load(fnameFixed) 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 = 3 #maskMoving=np.ones_like(moving) #maskFixed=np.ones_like(fixed) movingPyramid = [ img for img in rcommon.pyramid_gaussian_3D(moving, level, np.ones_like(moving)) ] fixedPyramid = [ img for img in rcommon.pyramid_gaussian_3D(fixed, level, np.ones_like(fixed)) ] rcommon.plotOverlaidPyramids3DCoronal(movingPyramid, fixedPyramid) #maxOuterIter=[100,100,100,100,100,100,100,100,100] maxOuterIter = [3, 3, 3, 3, 3, 3, 3, 3, 3] #maxOuterIter=[10,20,50,100, 100, 100] displacement = estimateMonomodalDiffeomorphicField3DMultiScale( movingPyramid, fixedPyramid, lambdaParam, maxOuterIter, 0, None) warped = tf.warp_volume(movingPyramid[0], displacement) np.save('displacement_templateT1ToIBSR01T1_diff.npy', displacement) np.save('warped_templateT1ToIBSR01T1_diff.npy', warped)
def testRigidTransformationMultiScale3D(betaGT, level): betaGTRads=np.array(betaGT, dtype=np.float64) betaGTRads[0:3]=np.copy(np.pi*betaGTRads[0:3]/180.0) ns=181 nr=217 nc=181 print 'Loading volume...' inImg=np.fromfile('data/t2/t2_icbm_normal_1mm_pn0_rf0.rawb', dtype=np.ubyte).reshape(ns,nr,nc) inImg=inImg.astype(np.float64) print 'Generating pyramid at level',level,'...' left=inImg right=rcommon.applyRigidTransformation3D(inImg, betaGTRads) leftPyramid=[i for i in rcommon.pyramid_gaussian_3D(left, level)] rightPyramid=[i for i in rcommon.pyramid_gaussian_3D(right, level)] plotSlicePyramidsAxial(leftPyramid, rightPyramid) print 'Estimation started.' beta=estimateRigidTransformationMultiscale3D(leftPyramid, rightPyramid) print 'Estimation finished.' print 'Ground truth:', betaGT return beta
def testIntersubjectRigidRegistration(fname0, fname1, level, outfname): nib_left = nib.load(fname0) nib_right = nib.load(fname1) left=nib_left.get_data().astype(np.double).squeeze() right=nib_right.get_data().astype(np.double).squeeze() leftPyramid=[i for i in rcommon.pyramid_gaussian_3D(left, level)] rightPyramid=[i for i in rcommon.pyramid_gaussian_3D(right, level)] plotSlicePyramidsAxial(leftPyramid, rightPyramid) print 'Estimation started.' beta=estimateRigidTransformationMultiscale3D(leftPyramid, rightPyramid) print 'Estimation finished.' rcommon.applyRigidTransformation3D(left, beta) sl=np.array(left.shape)//2 sr=np.array(right.shape)//2 rcommon.overlayImages(left[sl[0],:,:], leftPyramid[0][sr[0],:,:]) rcommon.overlayImages(left[sl[0],:,:], right[sr[0],:,:]) affine_transform=AffineTransform('ijk', ['aligned-z=I->S','aligned-y=P->A', 'aligned-x=L->R'], np.eye(4)) left=Image(left, affine_transform) nipy.save_image(left,outfname) return beta
def testRigidTransformationMultiScale3D(betaGT, level): betaGTRads = np.array(betaGT, dtype=np.float64) betaGTRads[0:3] = np.copy(np.pi * betaGTRads[0:3] / 180.0) ns = 181 nr = 217 nc = 181 print 'Loading volume...' inImg = np.fromfile('data/t2/t2_icbm_normal_1mm_pn0_rf0.rawb', dtype=np.ubyte).reshape(ns, nr, nc) inImg = inImg.astype(np.float64) print 'Generating pyramid at level', level, '...' left = inImg right = rcommon.applyRigidTransformation3D(inImg, betaGTRads) leftPyramid = [i for i in rcommon.pyramid_gaussian_3D(left, level)] rightPyramid = [i for i in rcommon.pyramid_gaussian_3D(right, level)] plotSlicePyramidsAxial(leftPyramid, rightPyramid) print 'Estimation started.' beta = estimateRigidTransformationMultiscale3D(leftPyramid, rightPyramid) print 'Estimation finished.' print 'Ground truth:', betaGT return beta
def testIntersubjectRigidRegistration(fname0, fname1, level, outfname): nib_left = nib.load(fname0) nib_right = nib.load(fname1) left = nib_left.get_data().astype(np.double).squeeze() right = nib_right.get_data().astype(np.double).squeeze() leftPyramid = [i for i in rcommon.pyramid_gaussian_3D(left, level)] rightPyramid = [i for i in rcommon.pyramid_gaussian_3D(right, level)] plotSlicePyramidsAxial(leftPyramid, rightPyramid) print 'Estimation started.' beta = estimateRigidTransformationMultiscale3D(leftPyramid, rightPyramid) print 'Estimation finished.' rcommon.applyRigidTransformation3D(left, beta) sl = np.array(left.shape) // 2 sr = np.array(right.shape) // 2 rcommon.overlayImages(left[sl[0], :, :], leftPyramid[0][sr[0], :, :]) rcommon.overlayImages(left[sl[0], :, :], right[sr[0], :, :]) affine_transform = AffineTransform( 'ijk', ['aligned-z=I->S', 'aligned-y=P->A', 'aligned-x=L->R'], np.eye(4)) left = Image(left, affine_transform) nipy.save_image(left, outfname) return beta
def testEstimateMonomodalDiffeomorphicField3DMultiScale(lambdaParam): fnameMoving='data/affineRegistered/templateT1ToIBSR01T1.nii.gz' fnameFixed='data/t1/IBSR18/IBSR_01/IBSR_01_ana_strip.nii.gz' moving = nib.load(fnameMoving) fixed= nib.load(fnameFixed) 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=3 #maskMoving=np.ones_like(moving) #maskFixed=np.ones_like(fixed) movingPyramid=[img for img in rcommon.pyramid_gaussian_3D(moving, level, np.ones_like(moving))] fixedPyramid=[img for img in rcommon.pyramid_gaussian_3D(fixed, level, np.ones_like(fixed))] rcommon.plotOverlaidPyramids3DCoronal(movingPyramid, fixedPyramid) #maxOuterIter=[100,100,100,100,100,100,100,100,100] maxOuterIter=[3,3,3,3,3,3,3,3,3] #maxOuterIter=[10,20,50,100, 100, 100] displacement=estimateMonomodalDiffeomorphicField3DMultiScale(movingPyramid, fixedPyramid, lambdaParam, maxOuterIter, 0,None) warped=tf.warp_volume(movingPyramid[0], displacement) np.save('displacement_templateT1ToIBSR01T1_diff.npy', displacement) np.save('warped_templateT1ToIBSR01T1_diff.npy', warped)
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()
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()