コード例 #1
0
ファイル: test_ecqmmf_reg.py プロジェクト: zuzyc/registration
def initializeECQMMFRegistration(fixedImage, movingImage, nclasses,
                                 lambdaParam, mu, maxIter, tolerance):
    meansFixed, variancesFixed = ecqmmf.initialize_constant_models(
        fixedImage, nclasses)
    meansFixed = np.array(meansFixed)
    variancesFixed = np.array(variancesFixed)
    meansMoving, variancesMoving = ecqmmf.initialize_constant_models(
        movingImage, nclasses)
    meansMoving = np.array(meansMoving)
    variancesMoving = np.array(variancesMoving)
    segmentedFixed, meansFixed, variancesFixed, probsFixed = ecqmmf.ecqmmf(
        fixedImage, nclasses, lambdaParam, mu, maxIter, tolerance)
    segmentedMoving, meansMoving, variancesMoving, probsMoving = ecqmmf.ecqmmf(
        movingImage, nclasses, lambdaParam, mu, maxIter, tolerance)
    probsFixed = np.array(probsFixed)
    probsMoving = np.array(probsMoving)
    ecqmmf.update_variances(fixedImage, probsFixed, meansFixed, variancesFixed)
    ecqmmf.update_variances(movingImage, probsMoving, meansMoving,
                            variancesMoving)
    #show variances
    plt.figure()
    plt.subplot(2, 1, 1)
    plt.plot(variancesFixed)
    plt.subplot(2, 1, 2)
    plt.plot(variancesMoving)
    #show mean images
    fixedSmooth = probsFixed.dot(meansFixed)
    movingSmooth = probsMoving.dot(meansMoving)
    plt.figure()
    plt.subplot(1, 2, 1)
    plt.imshow(fixedSmooth, cmap=plt.cm.gray)
    plt.title('Mean fixed')
    plt.subplot(1, 2, 2)
    plt.imshow(movingSmooth, cmap=plt.cm.gray)
    plt.title('Mean moving')
    #show mode images
    plt.figure()
    plt.subplot(1, 2, 1)
    plt.imshow(segmentedFixed)
    plt.title('Seg. fixed')
    plt.subplot(1, 2, 2)
    plt.imshow(segmentedMoving)
    plt.title('Seg. moving')
    #--------------
    joint = probsFixed[:, :, :, None] * probsMoving[:, :, None, :]
    return meansFixed, variancesFixed, meansMoving, variancesMoving, joint
コード例 #2
0
def initializeECQMMFRegistration(fixedImage, movingImage, nclasses, lambdaParam, mu, maxIter, tolerance):
    meansFixed, variancesFixed=ecqmmf.initialize_constant_models(fixedImage, nclasses)
    meansFixed=np.array(meansFixed)
    variancesFixed=np.array(variancesFixed)
    meansMoving, variancesMoving=ecqmmf.initialize_constant_models(movingImage, nclasses)
    meansMoving=np.array(meansMoving)
    variancesMoving=np.array(variancesMoving)
    segmentedFixed, meansFixed, variancesFixed, probsFixed=ecqmmf.ecqmmf(
                            fixedImage, nclasses, lambdaParam, mu, maxIter, tolerance)
    segmentedMoving, meansMoving, variancesMoving, probsMoving=ecqmmf.ecqmmf(
                            movingImage, nclasses, lambdaParam, mu, maxIter, tolerance)
    probsFixed=np.array(probsFixed)
    probsMoving=np.array(probsMoving)
    ecqmmf.update_variances(fixedImage, probsFixed, meansFixed, variancesFixed)
    ecqmmf.update_variances(movingImage, probsMoving, meansMoving, variancesMoving)
    #show variances
    plt.figure()
    plt.subplot(2,1,1)
    plt.plot(variancesFixed)
    plt.subplot(2,1,2)
    plt.plot(variancesMoving)
    #show mean images
    fixedSmooth=probsFixed.dot(meansFixed)
    movingSmooth=probsMoving.dot(meansMoving)
    plt.figure()
    plt.subplot(1,2,1)
    plt.imshow(fixedSmooth, cmap=plt.cm.gray)
    plt.title('Mean fixed')
    plt.subplot(1,2,2)
    plt.imshow(movingSmooth,cmap=plt.cm.gray)
    plt.title('Mean moving')
    #show mode images
    plt.figure()
    plt.subplot(1,2,1)
    plt.imshow(segmentedFixed)
    plt.title('Seg. fixed')
    plt.subplot(1,2,2)
    plt.imshow(segmentedMoving)
    plt.title('Seg. moving')
    #--------------
    joint=probsFixed[:,:,:,None]*probsMoving[:,:,None,:]
    return meansFixed, variancesFixed, meansMoving, variancesMoving, joint
コード例 #3
0
#image=nib.load('data/t1/IBSR18/IBSR_01/IBSR_01_ana_strip.nii.gz')
image=nib.load('data/t1/t1_icbm_normal_1mm_pn0_rf0_peeled.nii.gz')
image=image.get_data().squeeze()
image=image.astype(np.float64)
image=(image-image.min())/(image.max()-image.min())
sh=image.shape
img=image[:,sh[1]//2,:].copy()

nclasses=16
lambdaParam=.05
mu=.01
outerIter=20
innerIter=50
tolerance=1e-6

means, variances=ecqmmf.initialize_constant_models(img, nclasses)
means=np.array(means)
variances=np.array(variances)
segmented, means, variances, probs=ecqmmf.ecqmmf(img, nclasses, lambdaParam, mu, outerIter, innerIter, tolerance)
segmented=np.array(segmented)
means=np.array(means)
variances=np.array(variances)
probs=np.array(probs)
meanEstimator=probs.dot(means)

plt.figure()
plt.subplot(1,3,1)
plt.imshow(img)
plt.title('Input')
plt.subplot(1,3,2)
plt.imshow(segmented)
コード例 #4
0
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
コード例 #5
0
ファイル: test_ecqmmf.py プロジェクト: zuzyc/registration
#image=nib.load('data/t1/IBSR18/IBSR_01/IBSR_01_ana_strip.nii.gz')
image = nib.load('data/t1/t1_icbm_normal_1mm_pn0_rf0_peeled.nii.gz')
image = image.get_data().squeeze()
image = image.astype(np.float64)
image = (image - image.min()) / (image.max() - image.min())
sh = image.shape
img = image[:, sh[1] // 2, :].copy()

nclasses = 16
lambdaParam = .05
mu = .01
outerIter = 20
innerIter = 50
tolerance = 1e-6

means, variances = ecqmmf.initialize_constant_models(img, nclasses)
means = np.array(means)
variances = np.array(variances)
segmented, means, variances, probs = ecqmmf.ecqmmf(img, nclasses, lambdaParam,
                                                   mu, outerIter, innerIter,
                                                   tolerance)
segmented = np.array(segmented)
means = np.array(means)
variances = np.array(variances)
probs = np.array(probs)
meanEstimator = probs.dot(means)

plt.figure()
plt.subplot(1, 3, 1)
plt.imshow(img)
plt.title('Input')
コード例 #6
0
ファイル: test_ecqmmf_reg.py プロジェクト: zuzyc/registration
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