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 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 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 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, ')'