def test_interpolate_displacement_field(args=None):
    shape = (10, 5)
    test_img = np.random.rand(*shape)
    subsample = 3
    voxel_size = (3.0, 2.5)
    jitter_sigma = (5.0, ) * len(shape)
    jitter_sigma_voxels = tuple(j / vs
                                for j, vs in zip(jitter_sigma, voxel_size))

    identity = augment.create_identity_transformation(shape=shape,
                                                      subsample=subsample)
    np.random.seed(100)
    elastic = augment.create_elastic_transformation(
        shape=shape,
        control_point_spacing=(1, 1),
        jitter_sigma=jitter_sigma_voxels,
        subsample=subsample)
    np.random.seed(100)
    elastic_w = augment.create_elastic_transformation(
        shape=shape,
        control_point_spacing=(1, 1),
        jitter_sigma=jitter_sigma,
        subsample=subsample)
    transform = identity + elastic
    print(transform)
    print('-----')
    print(elastic)
    print(elastic_w)
    print(elastic_w / elastic)
    displacement = augment.upscale_transformation(elastic, shape)
    transform = augment.upscale_transformation(transform, shape)
    displace_w = augment.upscale_transformation(elastic_w, shape)

    tf1 = transform
    tf2 = displacement + augment.create_identity_transformation(shape=shape,
                                                                subsample=1)

    print()
    print('-----')
    print(tf1)
    print('-----')
    print(tf2)

    d1 = transform - augment.create_identity_transformation(shape=shape,
                                                            subsample=1)
    d2 = displacement
    print()
    print('-----')
    print(d1)
    print('-----')
    print(d2)

    print(np.abs(tf1 - tf2))
Beispiel #2
0
def _create_rotation_transformation(shape, angle, subsample=1, voxel_size=None):

    dims = len(shape)
    subsample_shape = tuple(max(1,int(s/subsample)) for s in shape)
    control_points = (2,)*dims

    if voxel_size is None:
        voxel_size = Coordinate((1,) * dims)

    # map control points to world coordinates
    control_point_scaling_factor = tuple(float(s-1) * vs for s, vs in zip(shape, voxel_size))

    # rotate control points
    center = np.array([0.5*(d-1)*vs for d, vs in zip(shape, voxel_size)])

    # print("Creating rotation transformation with:")
    # print("\tangle : " + str(angle))
    # print("\tcenter: " + str(center))

    control_point_offsets = np.zeros((dims,) + control_points, dtype=np.float32)
    for control_point in np.ndindex(control_points):

        point = np.array(control_point)*control_point_scaling_factor
        center_offset = np.array([p-c for c,p in zip(center, point)], dtype=np.float32)
        rotated_offset = np.array(center_offset)
        rotated_offset[-2:] = _rotate(center_offset[-2:], angle)
        displacement = rotated_offset - center_offset
        control_point_offsets[(slice(None),) + control_point] += displacement

    return augment.upscale_transformation(control_point_offsets, subsample_shape)
    shape=shape,
    control_point_spacing=(1, 1),
    jitter_sigma=jitter_sigma_voxels,
    subsample=subsample)
np.random.seed(100)
elastic_w = augment.create_elastic_transformation(shape=shape,
                                                  control_point_spacing=(1, 1),
                                                  jitter_sigma=jitter_sigma,
                                                  subsample=subsample)
transform = identity + elastic
print(transform)
print('-----')
print(elastic)
print(elastic_w)
print(elastic_w / elastic)
displacement = augment.upscale_transformation(elastic, shape)
transform = augment.upscale_transformation(transform, shape)
displace_w = augment.upscale_transformation(elastic_w, shape)

tf1 = transform
tf2 = displacement + augment.create_identity_transformation(shape=shape,
                                                            subsample=1)

print()
print('-----')
print(tf1)
print('-----')
print(tf2)

d1 = transform - augment.create_identity_transformation(shape=shape,
                                                        subsample=1)