Example #1
0
def callback_CC(sdr, status):
    #Status indicates at which stage of the optimization we currently are
    #For now, we will only react at the end of each resolution of the scale
    #space
    if status == imwarp.RegistrationStages.SCALE_END:
        #get the current images from the metric
        wmoving = sdr.metric.moving_image
        wstatic = sdr.metric.static_image
        #draw the images on top of each other with different colors
        regtools.overlay_images(wmoving, wstatic, 'Warped moving', 'Overlay', 'Warped static')
def callback_CC(sdr, status):
    #Status indicates at which stage of the optimization we currently are
    #For now, we will only react at the end of each resolution of the scale
    #space
    if status == imwarp.RegistrationStages.SCALE_END:
        #get the current images from the metric
        wmoving = sdr.metric.moving_image
        wstatic = sdr.metric.static_image
        #draw the images on top of each other with different colors
        regtools.overlay_images(wmoving, wstatic, 'Warped moving', 'Overlay', 'Warped static')
Example #3
0
def plot_together(image1, image2):
    """Plot a comparison of two images."""
    new_shape1 = list(image1.shape)
    new_shape2 = list(image2.shape)

    if image1.shape[0] < image2.shape[0]:
        new_shape1[0] = image2.shape[0]
    elif image1.shape[0] > image2.shape[0]:
        new_shape2[0] = image1.shape[0]

    if image1.shape[1] < image2.shape[1]:
        new_shape1[1] = image2.shape[1]
    elif image1.shape[1] > image2.shape[1]:
        new_shape2[1] = image1.shape[1]

    if new_shape1 != image1.shape:
        new_image1 = np.zeros(new_shape1, dtype=image1.dtype)
        new_image1[:image1.shape[0], :image1.shape[1]] = image1
        image1 = new_image1

    if new_shape2 != image2.shape:
        new_image2 = np.zeros(new_shape2, dtype=image2.dtype)
        new_image2[:image2.shape[0], :image2.shape[1]] = image2
        image2 = new_image2

    fig = overlay_images(image1, image2)
    fig.set_size_inches([12, 10])
    return new_image1, new_image2
Example #4
0
def alignImage(static, moving, level_iters):
    """The Symmetric Normalization algorithm uses a multi-resolution approach by building a Gaussian Pyramid.
    The static image is used a reference; a transformation that transforms the moving image into the
    static image is found and applied to the static image.  Returns overlay images, error image, and transformation matrix. 
    Uses CrossCorrelation metric which makes sense for aligning AFM topography. 
    
    INPUT: static and moving are both 2d arrays of the same dimension.
    Level_iters is a list (typically 4 entries) defines
    the number of iterations a user wants at each level of the pyramid, with the 0th entry referring to the 
    finest resolution.
    
    OUTPUT: overlayimages: an image with the static, moving, and overlay images
            errorimage: the difference between the warped_moving image and the static reference. THe flatter 
            the better.
            transformmatrix: the matrix corresponding to the forward transformation of a matrix."""
    
    from dipy.align.imwarp import SymmetricDiffeomorphicRegistration
    from dipy.align.metrics import SSDMetric, CCMetric, EMMetric
    from dipy.viz import regtools

    dim = static.ndim
    metric = CCMetric(dim)
    sdr = SymmetricDiffeomorphicRegistration(metric, level_iters, inv_iter = 50)
    mapping = sdr.optimize(static, moving)
    warped_moving = mapping.transform(moving, 'linear')
    overlayimages = regtools.overlay_images(static, warped_moving, 'Static','Overlay','Warped moving',
   'direct_warp_result.png')
    errorimage = plt.matshow(warped_moving-static, cmap='viridis')
    transformmatrix=mapping.forward
    
    
    return overlayimages, errorimage, transformmatrix
Example #5
0
def plot_together(image1, image2):
    """Plot a comparison of two images

    Parameters
    ----------
    image1 : numpy.ndarray
        First image to compare
    image2 : nump.ndarray
        Second image to compare

    Returns
    -------
    new_image1 : numpy.ndarray
        Modified version of image1 with same size as image2
    new_image2 : numpy.ndarray
        Modified version of image2 with same saze as image1

    """
    new_shape1 = list(image1.shape)
    new_shape2 = list(image2.shape)

    if image1.shape[0] < image2.shape[0]:
        new_shape1[0] = image2.shape[0]
    elif image1.shape[0] > image2.shape[0]:
        new_shape2[0] = image1.shape[0]

    if image1.shape[1] < image2.shape[1]:
        new_shape1[1] = image2.shape[1]
    elif image1.shape[1] > image2.shape[1]:
        new_shape2[1] = image1.shape[1]

    if new_shape1 != image1.shape:
        new_image1 = np.zeros(new_shape1, dtype=image1.dtype)
        new_image1[:image1.shape[0], :image1.shape[1]] = image1
        image1 = new_image1

    if new_shape2 != image2.shape:
        new_image2 = np.zeros(new_shape2, dtype=image2.dtype)
        new_image2[:image2.shape[0], :image2.shape[1]] = image2
        image2 = new_image2

    fig = overlay_images(image1, image2)
    fig.set_size_inches([12, 10])
    return new_image1, new_image2
Example #6
0
moving_grid2world = nib_syn_b0.affine

static = static[:, :, 37]
moving = moving[:, :, 37]
static_grid2world = np.delete(static_grid2world, 2, 0)
static_grid2world = np.delete(static_grid2world, 2, 1)
moving_grid2world = np.delete(moving_grid2world, 2, 0)
moving_grid2world = np.delete(moving_grid2world, 2, 1)

# resample moving image on a grid of same dimemsions as static image
identity = np.eye(3)
affine_map = AffineMap(identity, static.shape, static_grid2world, moving.shape,
                       moving_grid2world)
resampled = affine_map.transform(moving)
with Xvfb() as xvfb:
    regtools.overlay_images(static, resampled, "Static", "Overlay", "Moving",
                            "resampled.png")

# align the centers of mass of two images
c_of_mass = transform_centers_of_mass(static, static_grid2world, moving,
                                      moving_grid2world)
transformed = c_of_mass.transform(moving)
with Xvfb() as xvfb:
    regtools.overlay_images(static, resampled, "Static", "Overlay", "Moving",
                            "transformed_com.png")

# create similarity metric (Mutual Information)
nbins = 32  # number of bins to discretize joint & marginal PDF
sampling_prop = 0.8  # percentage of voxels for computing PDFs, None: 100%
metric = MutualInformationMetric(nbins, sampling_prop)

# multi-resolution strategy, building Guassian Pyramid
Example #7
0
from dipy.viz import regtools


fname_moving = get_fnames('reg_o')
fname_static = get_fnames('reg_c')

moving = np.load(fname_moving)
static = np.load(fname_static)

"""
To visually check the overlap of the static image with the transformed moving
image, we can plot them on top of each other with different channels to see
where the differences are located
"""

regtools.overlay_images(static, moving, 'Static', 'Overlay', 'Moving', 'input_images.png')

"""
.. figure:: input_images.png
   :align: center

   Input images.
"""

"""
We want to find an invertible map that transforms the moving image (circle)
into the static image (the C letter).

The first decision we need to make is what similarity metric is appropriate
for our problem. In this example we are using two binary images, so the Sum
of Squared Differences (SSD) is a good choice.
from dipy.viz import regtools


fname_moving = get_fnames('reg_o')
fname_static = get_fnames('reg_c')

moving = np.load(fname_moving)
static = np.load(fname_static)

"""
To visually check the overlap of the static image with the transformed moving
image, we can plot them on top of each other with different channels to see
where the differences are located
"""

regtools.overlay_images(static, moving, 'Static', 'Overlay', 'Moving', 'input_images.png')

"""
.. figure:: input_images.png
   :align: center

   Input images.
"""

"""
We want to find an invertible map that transforms the moving image (circle)
into the static image (the C letter).

The first decision we need to make is what similarity metric is appropriate
for our problem. In this example we are using two binary images, so the Sum
of Squared Differences (SSD) is a good choice.