def estimateNewMultimodalRigidTransformation2D_ecqmmf(moving, fixed, probs, nclasses, previousBeta=None):
    epsilon=1e-9
    sh=moving.shape
    center=(np.array(sh)-1)/2.0
    X0,X1=np.mgrid[0:sh[0], 0:sh[1]]
    X0=X0-center[0]
    X1=X1-center[1]
    mask=np.ones_like(X0, dtype=np.int32)
    if((previousBeta!=None) and (np.max(np.abs(previousBeta))>epsilon)):
        R=rcommon.getRotationMatrix2D(previousBeta[0])
        X0new,X1new=(R[0,0]*X0 + R[0,1]*X1 + center[0] + 2.0*previousBeta[1],
                     R[1,0]*X0 + R[1,1]*X1 + center[1] + 2.0*previousBeta[2])
        moving=ndimage.map_coordinates(moving, [X0new, X1new], prefilter=const_prefilter_map_coordinates)
        mask[...]=(X0new<0) + (X0new>(sh[0]-1))
        mask[...]=mask + (X1new<0) + (X1new>(sh[1]-1))
        mask[...]=1-mask
    means, variances=tf.computeMaskedVolumeClassStatsProbsCYTHON(mask, moving, probs)
    means=np.array(means)
    weights=np.array([1.0/x if(x>0) else 0 for x in variances], dtype=np.float64)
    g0, g1=sp.gradient(moving)
    q=np.empty(shape=(X0.shape)+(3,), dtype=np.float64)
    q[...,0]=g1*X0-g0*X1
    q[...,1]=g0
    q[...,2]=g1
    expected=probs.dot(means)
    diff=expected-moving
    Aw, bw=tf.integrateMaskedWeightedTensorFieldProductsCYTHON(mask, q, diff, numLevels, rightQ, weights)
    beta=linalg.solve(Aw,bw)
    return beta
def estimateNewMultimodalRigidTransformation3D(left, right, rightQ, numLevels, previousBeta=None):
    epsilon=1e-9
    sh=left.shape
    center=(np.array(sh)-1)/2.0
    X0,X1,X2=np.mgrid[0:sh[0], 0:sh[1], 0:sh[2]]
    X0=X0-center[0]
    X1=X1-center[1]
    X2=X2-center[2]
    mask=np.ones_like(X0, dtype=np.int32)
    if((previousBeta!=None) and (np.max(np.abs(previousBeta))>epsilon)):
        R=rcommon.getRotationMatrix(previousBeta[0:3])
        X0new,X1new,X2new=(R[0,0]*X0 + R[0,1]*X1 + R[0,2]*X2 + center[0] + 2.0*previousBeta[3], 
                        R[1,0]*X0 + R[1,1]*X1 + R[1,2]*X2 + center[1] + 2.0*previousBeta[4], 
                        R[2,0]*X0 + R[2,1]*X1 + R[2,2]*X2 + center[2] + 2.0*previousBeta[5])
        left=ndimage.map_coordinates(left, [X0new, X1new, X2new], prefilter=const_prefilter_map_coordinates)
        mask[...]=(X0new<0) + (X0new>(sh[0]-1))
        mask[...]=mask + (X1new<0) + (X1new>(sh[1]-1))
        mask[...]=mask + (X2new<0) + (X2new>(sh[2]-1))
        mask[...]=1-mask
    means, variances=tf.computeMaskedVolumeClassStatsCYTHON(mask, left, numLevels, rightQ)
    means=np.array(means)
    weights=np.array([1.0/x if(x>0) else 0 for x in variances], dtype=np.float64)
    g0, g1, g2=sp.gradient(left)
    q=np.empty(shape=(X0.shape)+(6,), dtype=np.float64)
    q[...,0]=g2*X1-g1*X2
    q[...,1]=g0*X2-g2*X0
    q[...,2]=g1*X0-g0*X1
    q[...,3]=g0
    q[...,4]=g1
    q[...,5]=g2
    diff=means[rightQ]-left
    Aw, bw=tf.integrateMaskedWeightedTensorFieldProductsCYTHON(mask, q, diff, numLevels, rightQ, weights)
    beta=linalg.solve(Aw,bw)
    return beta
示例#3
0
def estimateNewMultimodalRigidTransformation3D(left,
                                               right,
                                               rightQ,
                                               numLevels,
                                               previousBeta=None):
    epsilon = 1e-9
    sh = left.shape
    center = (np.array(sh) - 1) / 2.0
    X0, X1, X2 = np.mgrid[0:sh[0], 0:sh[1], 0:sh[2]]
    X0 = X0 - center[0]
    X1 = X1 - center[1]
    X2 = X2 - center[2]
    mask = np.ones_like(X0, dtype=np.int32)
    if ((previousBeta != None) and (np.max(np.abs(previousBeta)) > epsilon)):
        R = rcommon.getRotationMatrix(previousBeta[0:3])
        X0new, X1new, X2new = (R[0, 0] * X0 + R[0, 1] * X1 + R[0, 2] * X2 +
                               center[0] + 2.0 * previousBeta[3],
                               R[1, 0] * X0 + R[1, 1] * X1 + R[1, 2] * X2 +
                               center[1] + 2.0 * previousBeta[4],
                               R[2, 0] * X0 + R[2, 1] * X1 + R[2, 2] * X2 +
                               center[2] + 2.0 * previousBeta[5])
        left = ndimage.map_coordinates(
            left, [X0new, X1new, X2new],
            prefilter=const_prefilter_map_coordinates)
        mask[...] = (X0new < 0) + (X0new > (sh[0] - 1))
        mask[...] = mask + (X1new < 0) + (X1new > (sh[1] - 1))
        mask[...] = mask + (X2new < 0) + (X2new > (sh[2] - 1))
        mask[...] = 1 - mask
    means, variances = tf.computeMaskedVolumeClassStatsCYTHON(
        mask, left, numLevels, rightQ)
    means = np.array(means)
    weights = np.array([1.0 / x if (x > 0) else 0 for x in variances],
                       dtype=np.float64)
    g0, g1, g2 = sp.gradient(left)
    q = np.empty(shape=(X0.shape) + (6, ), dtype=np.float64)
    q[..., 0] = g2 * X1 - g1 * X2
    q[..., 1] = g0 * X2 - g2 * X0
    q[..., 2] = g1 * X0 - g0 * X1
    q[..., 3] = g0
    q[..., 4] = g1
    q[..., 5] = g2
    diff = means[rightQ] - left
    Aw, bw = tf.integrateMaskedWeightedTensorFieldProductsCYTHON(
        mask, q, diff, numLevels, rightQ, weights)
    beta = linalg.solve(Aw, bw)
    return beta
示例#4
0
def estimateNewMultimodalRigidTransformation2D_ecqmmf(moving,
                                                      fixed,
                                                      probs,
                                                      nclasses,
                                                      previousBeta=None):
    epsilon = 1e-9
    sh = moving.shape
    center = (np.array(sh) - 1) / 2.0
    X0, X1 = np.mgrid[0:sh[0], 0:sh[1]]
    X0 = X0 - center[0]
    X1 = X1 - center[1]
    mask = np.ones_like(X0, dtype=np.int32)
    if ((previousBeta != None) and (np.max(np.abs(previousBeta)) > epsilon)):
        R = rcommon.getRotationMatrix2D(previousBeta[0])
        X0new, X1new = (R[0, 0] * X0 + R[0, 1] * X1 + center[0] +
                        2.0 * previousBeta[1], R[1, 0] * X0 + R[1, 1] * X1 +
                        center[1] + 2.0 * previousBeta[2])
        moving = ndimage.map_coordinates(
            moving, [X0new, X1new], prefilter=const_prefilter_map_coordinates)
        mask[...] = (X0new < 0) + (X0new > (sh[0] - 1))
        mask[...] = mask + (X1new < 0) + (X1new > (sh[1] - 1))
        mask[...] = 1 - mask
    means, variances = tf.computeMaskedVolumeClassStatsProbsCYTHON(
        mask, moving, probs)
    means = np.array(means)
    weights = np.array([1.0 / x if (x > 0) else 0 for x in variances],
                       dtype=np.float64)
    g0, g1 = sp.gradient(moving)
    q = np.empty(shape=(X0.shape) + (3, ), dtype=np.float64)
    q[..., 0] = g1 * X0 - g0 * X1
    q[..., 1] = g0
    q[..., 2] = g1
    expected = probs.dot(means)
    diff = expected - moving
    Aw, bw = tf.integrateMaskedWeightedTensorFieldProductsCYTHON(
        mask, q, diff, numLevels, rightQ, weights)
    beta = linalg.solve(Aw, bw)
    return beta