def createImage(self, ex_len=64):

        example_img_len = ex_len
        dim = 2
        szEx = np.tile(example_img_len,
                       dim)  # size of the desired images: (sz)^dim
        I0, I1, self.spacing = eg.CreateSquares(dim).create_image_pair(
            szEx,
            self.params)  # create a default image size with two sample squares
        self.sz = np.array(I0.shape)

        # create the source and target image as pyTorch variables
        self.ISource = AdaptVal(torch.from_numpy(I0.copy()))
        self.ITarget = AdaptVal(torch.from_numpy(I1))

        # smooth both a little bit
        self.params[('image_smoothing', {}, 'image smoothing settings')]
        self.params['image_smoothing'][(
            'smooth_images', True,
            '[True|False]; smoothes the images before registration')]
        self.params['image_smoothing'][('smoother', {},
                                        'settings for the image smoothing')]
        self.params['image_smoothing']['smoother'][(
            'gaussian_std', 0.05, 'how much smoothing is done')]
        self.params['image_smoothing']['smoother'][(
            'type', 'gaussian', "['gaussianSpatial'|'gaussian'|'diffusion']")]

        cparams = self.params['image_smoothing']
        s = SF.SmootherFactory(self.sz[2::],
                               self.spacing).create_smoother(cparams)
        self.ISource = s.smooth(self.ISource)
        self.ITarget = s.smooth(self.ITarget)
Пример #2
0
# We select if we want to create synthetic image pairs or would prefer a real image pair
use_synthetic_test_case = True

# Desired dimension (mermaid supports 1D, 2D, and 3D registration)
dim = 2

# If we want to add some noise to the background (for synthetic examples)
add_noise_to_bg = True

# and now create it
if use_synthetic_test_case:
    length = 64
    # size of the desired images: (sz)^dim
    szEx = np.tile(length, dim )
    # create a default image size with two sample squares
    I0, I1, spacing = EG.CreateSquares(dim,add_noise_to_bg).create_image_pair(szEx, params)
else:
    # return a real image example
    I0, I1, spacing = EG.CreateRealExampleImages(dim).create_image_pair() # create a default image size with two sample squares

##################################
# Creating the registration algorithm
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# We simply instantiate a simple interface object for the registration of image pairs.
# We can then query it as to what models registration models are currently supported.
#

# create a simple interface object for pair-wise image registration
si = SI.RegisterImagePair()
    settingFile = 'test_custom_registration_' + model_name + '_settings.json'
    params.load_JSON(settingFile)

if ds.use_real_images:
    I0, I1, spacing = eg.CreateRealExampleImages(ds.dim).create_image_pair()

else:
    szEx = np.tile(50, ds.dim)  # size of the desired images: (sz)^dim

    params['square_example_images'] = ({},
                                       'Settings for example image generation')
    params['square_example_images']['len_s'] = int(szEx.min() // 6)
    params['square_example_images']['len_l'] = int(szEx.max() // 4)

    # create a default image size with two sample squares
    I0, I1, spacing = eg.CreateSquares(ds.dim).create_image_pair(szEx, params)

sz = np.array(I0.shape)

assert (len(sz) == ds.dim + 2)

print('Spacing = ' + str(spacing))

# create the source and target image as pyTorch variables
ISource = AdaptVal(torch.from_numpy(I0.copy()))
ITarget = AdaptVal(torch.from_numpy(I1))

# if desired we smooth them a little bit
if ds.smooth_images:
    # smooth both a little bit
    params['image_smoothing'] = ds.par_algconf['image_smoothing']
Пример #4
0
from mermaid.data_wrapper import AdaptVal
import numpy as np
import mermaid.module_parameters as MP
import mermaid.example_generation as eg
import mermaid.utils as utils

import mermaid.smoother_factory as SF

import matplotlib.pyplot as plt

params = MP.ParameterDict()

dim = 2

szEx = np.tile(64, dim)
I0, I1, spacing = eg.CreateSquares(dim).create_image_pair(
    szEx, params)  # create a default image size with two sample squares
sz = np.array(I0.shape)

# create the source and target image as pyTorch variables
ISource = AdaptVal(torch.from_numpy(I0.copy()))

smoother = SF.MySingleGaussianFourierSmoother(sz[2:], spacing, params)

g_std = smoother.get_gaussian_std()

ISmooth = smoother.smooth_scalar_field(ISource)
ISmooth.backward(torch.ones_like(ISmooth))
#ISmooth.backward(torch.zeros_like(ISmooth))

print('g_std.grad')
print(g_std.grad)
#
# So let's first create an empty *mermaid* parameter object

# first we create a parameter structure to keep track of all registration settings
params = pars.ParameterDict()

#############################
# Generating registration example data
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Now we create some example data (source and target image examples for a two-dimensional square,
# of size 64x64) and keep track of the generated settings via this parameter object.

# and let's create two-dimensional squares
I0, I1, spacing = EG.CreateSquares(
    dim=2, add_noise_to_bg=True).create_image_pair(np.array([64, 64]),
                                                   params=params)

###############################
# Writing out parameters
# ^^^^^^^^^^^^^^^^^^^^^^
#
# Parameters can easily be written to a file (or displayed via print). We can write it out including comments for what these settings are as follows. The first command writes out the actual json configuration, the second one comments that explain what the settings are (as json does not allow commented files by default).

params.write_JSON('step_by_step_example_data.json')
params.write_JSON_comments('step_by_step_example_data_with_comments.json')

###############################
# Resulting parameters after the example generation
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#