def testMultimodalRigidTransformationMultiScale3D_ecqmmf( betaGT, level, nclasses, lambdaParam, mu, maxIter, tolerance): betaGTRads = np.array(betaGT, dtype=np.float64) betaGTRads[0:3] = np.copy(np.pi * betaGTRads[0:3] / 180.0) movingName = 'data/t2/t2_icbm_normal_1mm_pn0_rf0_peeled.nii.gz' fixedName = 'data/t1/t1_icbm_normal_1mm_pn0_rf0_peeled.nii.gz' print 'Loading data...' moving = nib.load(movingName) moving = moving.get_data().squeeze() moving = moving.astype(np.float64) fixed = nib.load(fixedName) fixed = fixed.get_data().squeeze() fixed = fixed.astype(np.float64) moving = (moving - moving.min()) / (moving.max() - moving.min()) fixed = (fixed - fixed.min()) / (fixed.max() - fixed.min()) print 'Applying GT transform...' fixed = rcommon.applyRigidTransformation3D(fixed, betaGTRads) sh = moving.shape #leftPyramid=[i for i in rcommon.pyramid_gaussian_3D(moving, level)] #rightPyramid=[i for i in rcommon.pyramid_gaussian_3D(fixed, level)] fixedImage = fixed[:, sh[1] // 2, :].copy() movingImage = moving[:, sh[1] // 2, :].copy() print 'Initializing registration...' meansFixed, variancesFixed, meansMoving, variancesMoving, joint = initializeECQMMFRegistration( fixedImage, movingImage, nclasses, lambdaParam, mu, maxIter, tolerance) joint = np.array(joint) negLogLikelihood = np.zeros_like(joint) ecqmmf_reg.compute_registration_neg_log_likelihood_constant_models( fixedImage, movingImage, joint, meansFixed, meansMoving, variancesFixed, variancesMoving, negLogLikelihood) #ecqmmf_reg.initialize_registration_maximum_likelihood_probs(negLogLikelihood, joint): ecqmmf.initialize_normalized_likelihood(negLogLikelihood, joint) bufferN = np.array(range(nclasses * nclasses)) bufferD = np.array(range(nclasses * nclasses)) for iter_count in range(maxIter): print 'Iter:', iter_count, '/', maxIter ecqmmf_reg.iterate_marginals(negLogLikelihood, joint, lambdaParam, mu, bufferN, bufferD) #ecqmmf_reg.integrate_registration_probabilistic_weighted_tensor_field_products(double[:,:,:] q, double[:,:] diff, double[:,:,:,:] probs, double[:] weights) ecqmmf_reg.compute_registration_neg_log_likelihood_constant_models( fixedImage, movingImage, joint, meansFixed, meansMoving, variancesFixed, variancesMoving, negLogLikelihood) #----plot joint probability maps--- print 'Plotting joint probability maps...' plt.figure() plt.title('Joint probability maps') for i in range(nclasses): for j in range(nclasses): plt.subplot(nclasses, nclasses, 1 + i * nclasses + j) plt.imshow(joint[:, :, i, j], cmap=plt.cm.gray) plt.title("F=" + str(i) + ", M=" + str(j)) #----plot negLogLikelihood maps--- print 'Plotting negLogLikelihood maps...' plt.figure() plt.title('neg-log-likelihood maps') for i in range(nclasses): for j in range(nclasses): plt.subplot(nclasses, nclasses, 1 + i * nclasses + j) plt.imshow(negLogLikelihood[:, :, i, j], cmap=plt.cm.gray) plt.title("F=" + str(i) + ", M=" + str(j))
def testMultimodalRigidTransformationMultiScale3D_ecqmmf(betaGT, level, nclasses, lambdaParam, mu, maxIter, tolerance): betaGTRads=np.array(betaGT, dtype=np.float64) betaGTRads[0:3]=np.copy(np.pi*betaGTRads[0:3]/180.0) movingName='data/t2/t2_icbm_normal_1mm_pn0_rf0_peeled.nii.gz' fixedName ='data/t1/t1_icbm_normal_1mm_pn0_rf0_peeled.nii.gz' print 'Loading data...' moving=nib.load(movingName) moving=moving.get_data().squeeze() moving=moving.astype(np.float64) fixed=nib.load(fixedName) fixed=fixed.get_data().squeeze() fixed=fixed.astype(np.float64) moving=(moving-moving.min())/(moving.max()-moving.min()) fixed=(fixed-fixed.min())/(fixed.max()-fixed.min()) print 'Applying GT transform...' fixed=rcommon.applyRigidTransformation3D(fixed, betaGTRads) sh=moving.shape #leftPyramid=[i for i in rcommon.pyramid_gaussian_3D(moving, level)] #rightPyramid=[i for i in rcommon.pyramid_gaussian_3D(fixed, level)] fixedImage=fixed[:,sh[1]//2,:].copy() movingImage=moving[:,sh[1]//2,:].copy() print 'Initializing registration...' meansFixed, variancesFixed, meansMoving, variancesMoving, joint=initializeECQMMFRegistration(fixedImage, movingImage, nclasses, lambdaParam, mu, maxIter, tolerance) joint=np.array(joint) negLogLikelihood=np.zeros_like(joint) ecqmmf_reg.compute_registration_neg_log_likelihood_constant_models(fixedImage, movingImage, joint, meansFixed, meansMoving, variancesFixed, variancesMoving, negLogLikelihood) #ecqmmf_reg.initialize_registration_maximum_likelihood_probs(negLogLikelihood, joint): ecqmmf.initialize_normalized_likelihood(negLogLikelihood, joint) bufferN=np.array(range(nclasses*nclasses)) bufferD=np.array(range(nclasses*nclasses)) for iter_count in range(maxIter): print 'Iter:',iter_count,'/',maxIter ecqmmf_reg.iterate_marginals(negLogLikelihood, joint, lambdaParam, mu, bufferN, bufferD) #ecqmmf_reg.integrate_registration_probabilistic_weighted_tensor_field_products(double[:,:,:] q, double[:,:] diff, double[:,:,:,:] probs, double[:] weights) ecqmmf_reg.compute_registration_neg_log_likelihood_constant_models(fixedImage, movingImage, joint, meansFixed, meansMoving, variancesFixed, variancesMoving, negLogLikelihood) #----plot joint probability maps--- print 'Plotting joint probability maps...' plt.figure() plt.title('Joint probability maps') for i in range(nclasses): for j in range(nclasses): plt.subplot(nclasses,nclasses,1+i*nclasses+j) plt.imshow(joint[:,:,i,j], cmap=plt.cm.gray) plt.title("F="+str(i)+", M="+str(j)) #----plot negLogLikelihood maps--- print 'Plotting negLogLikelihood maps...' plt.figure() plt.title('neg-log-likelihood maps') for i in range(nclasses): for j in range(nclasses): plt.subplot(nclasses,nclasses,1+i*nclasses+j) plt.imshow(negLogLikelihood[:,:,i,j], cmap=plt.cm.gray) plt.title("F="+str(i)+", M="+str(j))
def estimateNewECQMMFMultimodalDeformationField2D(fixed, moving, nclasses, lambdaMeasureField, lambdaDisplacement, mu, maxOuterIter, maxInnerIter, tolerance, previousDisplacement=None): sh=fixed.shape X0,X1=np.mgrid[0:sh[0], 0:sh[1]] displacement =np.empty(shape=(fixed.shape)+(2,), dtype=np.float64) gradientField =np.empty(shape=(fixed.shape)+(2,), dtype=np.float64) totalDisplacement=np.zeros(shape=(fixed.shape)+(2,), dtype=np.float64) gradientField =np.empty(shape=(fixed.shape)+(2,), dtype=np.float64) residuals =np.zeros_like(fixed) warped=None if(previousDisplacement!=None): totalDisplacement[...]=previousDisplacement warped=ndimage.map_coordinates(moving, [X0+totalDisplacement[...,0], X1+totalDisplacement[...,1]], prefilter=True) else: warped=moving #run soft segmentation on the fixed image meansFixed, variancesFixed=ecqmmf.initialize_constant_models(fixed, nclasses) meansFixed=np.array(meansFixed) variancesFixed=np.array(variancesFixed) segFixed, meansFixed, variancesFixed, probsFixed=ecqmmf.ecqmmf(fixed, nclasses, lambdaMeasureField, mu, maxOuterIter, maxInnerIter, tolerance) meansFixed=np.array(meansFixed) probsFixed=np.array(probsFixed) #run soft segmentation on the warped image meansWarped, variancesWarped=ecqmmf.initialize_constant_models(warped, nclasses) meansWarped=np.array(meansWarped) variancesWarped=np.array(variancesWarped) segWarped, meansWarped, variancesWarped, probsWarped=ecqmmf.ecqmmf(warped, nclasses, lambdaMeasureField, mu, maxOuterIter, maxInnerIter, tolerance) meansWarped=np.array(meansWarped) probsWarped=np.array(probsWarped) #inicialize the joint models (solve assignment problem) ecqmmf_reg.initialize_coupled_constant_models(probsFixed, probsWarped, meansWarped) #start optimization outerIter=0 negLogLikelihood=np.zeros_like(probsFixed) while(outerIter<maxOuterIter): outerIter+=1 print "Outer:", outerIter if(outerIter>1):#avoid warping twice at the first iteration warped=ndimage.map_coordinates(moving, [X0+totalDisplacement[...,0], X1+totalDisplacement[...,1]], prefilter=True) movingMask=(moving>0)*1.0 warpedMask=ndimage.map_coordinates(movingMask, [X0+totalDisplacement[...,0], X1+totalDisplacement[...,1]], order=0, prefilter=False) warpedMask=warpedMask.astype(np.int32) #--- optimize the measure field and the intensity models --- ecqmmf_reg.compute_registration_neg_log_likelihood_constant_models(fixed, warped, meansFixed, meansWarped, negLogLikelihood) #ecqmmf.initialize_normalized_likelihood(negLogLikelihood, probsWarped) ecqmmf.initialize_maximum_likelihood(negLogLikelihood, probsWarped); innerIter=0 mse=0 while(innerIter<maxInnerIter): innerIter+=1 print "\tInner:",innerIter ecqmmf.optimize_marginals(negLogLikelihood, probsWarped, lambdaMeasureField, mu, maxInnerIter, tolerance) mseFixed=ecqmmf.update_constant_models(fixed, probsWarped, meansFixed, variancesFixed) mseWarped=ecqmmf.update_constant_models(warped, probsWarped, meansWarped, variancesWarped) mse=np.max([mseFixed, mseWarped]) if(mse<tolerance): break #---given the intensity models and the measure field, compute the displacement deltaField=meansWarped[None, None, :]-warped[:,:,None] gradientField[:,:,0], gradientField[:,:,1]=sp.gradient(warped) maxDisplacement=ecqmmf_reg.optimize_ECQMMF_displacement_field_2D(deltaField, gradientField, probsWarped, lambdaDisplacement, displacement, residuals, maxInnerIter, tolerance) totalDisplacement+=displacement if(maxDisplacement<tolerance): break plt.figure() plt.subplot(2,2,1) plt.imshow(fixed, cmap=plt.cm.gray) plt.title("fixed") plt.subplot(2,2,2) plt.imshow(warped, cmap=plt.cm.gray) plt.title("moving") plt.subplot(2,2,3) plt.imshow(probsFixed.dot(meansFixed), cmap=plt.cm.gray) plt.title("E[fixed]") plt.subplot(2,2,4) plt.imshow(probsWarped.dot(meansWarped), cmap=plt.cm.gray) plt.title("E[moving]") return totalDisplacement
def estimateNewECQMMFMultimodalDeformationField2D(fixed, moving, nclasses, lambdaMeasureField, lambdaDisplacement, mu, maxOuterIter, maxInnerIter, tolerance, previousDisplacement=None): sh = fixed.shape X0, X1 = np.mgrid[0:sh[0], 0:sh[1]] displacement = np.empty(shape=(fixed.shape) + (2, ), dtype=np.float64) gradientField = np.empty(shape=(fixed.shape) + (2, ), dtype=np.float64) totalDisplacement = np.zeros(shape=(fixed.shape) + (2, ), dtype=np.float64) gradientField = np.empty(shape=(fixed.shape) + (2, ), dtype=np.float64) residuals = np.zeros_like(fixed) warped = None if (previousDisplacement != None): totalDisplacement[...] = previousDisplacement warped = ndimage.map_coordinates( moving, [X0 + totalDisplacement[..., 0], X1 + totalDisplacement[..., 1]], prefilter=True) else: warped = moving #run soft segmentation on the fixed image meansFixed, variancesFixed = ecqmmf.initialize_constant_models( fixed, nclasses) meansFixed = np.array(meansFixed) variancesFixed = np.array(variancesFixed) segFixed, meansFixed, variancesFixed, probsFixed = ecqmmf.ecqmmf( fixed, nclasses, lambdaMeasureField, mu, maxOuterIter, maxInnerIter, tolerance) meansFixed = np.array(meansFixed) probsFixed = np.array(probsFixed) #run soft segmentation on the warped image meansWarped, variancesWarped = ecqmmf.initialize_constant_models( warped, nclasses) meansWarped = np.array(meansWarped) variancesWarped = np.array(variancesWarped) segWarped, meansWarped, variancesWarped, probsWarped = ecqmmf.ecqmmf( warped, nclasses, lambdaMeasureField, mu, maxOuterIter, maxInnerIter, tolerance) meansWarped = np.array(meansWarped) probsWarped = np.array(probsWarped) #inicialize the joint models (solve assignment problem) ecqmmf_reg.initialize_coupled_constant_models(probsFixed, probsWarped, meansWarped) #start optimization outerIter = 0 negLogLikelihood = np.zeros_like(probsFixed) while (outerIter < maxOuterIter): outerIter += 1 print "Outer:", outerIter if (outerIter > 1): #avoid warping twice at the first iteration warped = ndimage.map_coordinates(moving, [ X0 + totalDisplacement[..., 0], X1 + totalDisplacement[..., 1] ], prefilter=True) movingMask = (moving > 0) * 1.0 warpedMask = ndimage.map_coordinates( movingMask, [X0 + totalDisplacement[..., 0], X1 + totalDisplacement[..., 1]], order=0, prefilter=False) warpedMask = warpedMask.astype(np.int32) #--- optimize the measure field and the intensity models --- ecqmmf_reg.compute_registration_neg_log_likelihood_constant_models( fixed, warped, meansFixed, meansWarped, negLogLikelihood) #ecqmmf.initialize_normalized_likelihood(negLogLikelihood, probsWarped) ecqmmf.initialize_maximum_likelihood(negLogLikelihood, probsWarped) innerIter = 0 mse = 0 while (innerIter < maxInnerIter): innerIter += 1 print "\tInner:", innerIter ecqmmf.optimize_marginals(negLogLikelihood, probsWarped, lambdaMeasureField, mu, maxInnerIter, tolerance) mseFixed = ecqmmf.update_constant_models(fixed, probsWarped, meansFixed, variancesFixed) mseWarped = ecqmmf.update_constant_models(warped, probsWarped, meansWarped, variancesWarped) mse = np.max([mseFixed, mseWarped]) if (mse < tolerance): break #---given the intensity models and the measure field, compute the displacement deltaField = meansWarped[None, None, :] - warped[:, :, None] gradientField[:, :, 0], gradientField[:, :, 1] = sp.gradient(warped) maxDisplacement = ecqmmf_reg.optimize_ECQMMF_displacement_field_2D( deltaField, gradientField, probsWarped, lambdaDisplacement, displacement, residuals, maxInnerIter, tolerance) totalDisplacement += displacement if (maxDisplacement < tolerance): break plt.figure() plt.subplot(2, 2, 1) plt.imshow(fixed, cmap=plt.cm.gray) plt.title("fixed") plt.subplot(2, 2, 2) plt.imshow(warped, cmap=plt.cm.gray) plt.title("moving") plt.subplot(2, 2, 3) plt.imshow(probsFixed.dot(meansFixed), cmap=plt.cm.gray) plt.title("E[fixed]") plt.subplot(2, 2, 4) plt.imshow(probsWarped.dot(meansWarped), cmap=plt.cm.gray) plt.title("E[moving]") return totalDisplacement