Example #1
0
def test_apply_affine_transform():
    image = np.zeros((5, 5, 2))
    image[..., 0] = [[0, 0, 1, 0, 0], [0, 1, 0, 1, 0], [1, 0, 0, 0, 1],
                     [0, 1, 0, 1, 0], [0, 0, 1, 0, 0]]
    image[..., 1] = [[1, 1, 1, 1, 1], [0, 0, 1, 0, 0], [1, 1, 1, 1, 1],
                     [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]]
    res = apply_affine_transform(image, theta=90, mode='constant', cval=0)

    expected = np.zeros((5, 5, 2))

    expected[..., 0] = [[0, 0, 1, 0, 0], [0, 1, 0, 1, 0], [1, 0, 0, 0, 1],
                        [0, 1, 0, 1, 0], [0, 0, 1, 0, 0]]
    expected[..., 1] = [[0, 0, 1, 0, 1], [0, 0, 1, 0, 1], [1, 1, 1, 1, 1],
                        [0, 0, 1, 0, 1], [0, 0, 1, 0, 1]]

    assert np.allclose(res, expected)

    image = np.zeros((5, 5, 2))
    image[..., 0] = [[0, 0, 1, 0, 0], [0, 1, 0, 1, 0], [1, 0, 0, 0, 1],
                     [0, 1, 0, 1, 0], [0, 0, 1, 0, 0]]
    image[..., 1] = [[1, 1, 1, 1, 1], [0, 0, 1, 0, 0], [1, 1, 1, 1, 1],
                     [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]]
    res = apply_affine_transform(image, shift=(1, 0), mode='constant', cval=0)

    expected = np.zeros((5, 5, 2))

    expected[..., 0] = [[0, 1, 0, 1, 0], [1, 0, 0, 0, 1], [0, 1, 0, 1, 0],
                        [0, 0, 1, 0, 0], [0, 0, 0, 0, 0]]
    expected[..., 1] = [[0, 0, 1, 0, 0], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0],
                        [0, 0, 1, 0, 0], [0, 0, 0, 0, 0]]

    assert np.allclose(res, expected)
    assert np.all(np.rint(res) == expected)
Example #2
0
    def transform(self, images, targets):
        transformed_images = images.copy()
        transformed_targets = targets.copy()

        # loop through
        for i in range(len(images)):
            # apply affine transform if possible
            if self.affine_transform:
                # After affine transform, the pixel intensity may change
                # the image should clip back to original range
                reduced_ax = tuple(range(len(transformed_images[i].shape) - 1))
                vmin = transformed_images[i].min(axis=reduced_ax)
                vmax = transformed_images[i].max(axis=reduced_ax)

                transformed_images[i] = apply_affine_transform(
                    transformed_images[i],
                    mode=self.fill_mode,
                    cval=self.cval,
                    theta=self.rotation_degree,
                    rotation_axis=self.rotation_axis,
                    zoom_factor=self.zoom_factor,
                    shift=self.shift).clip(vmin, vmax)

                transformed_targets[i] = apply_affine_transform(
                    transformed_targets[i],
                    mode=self.fill_mode,
                    cval=self.cval,
                    theta=self.rotation_degree,
                    rotation_axis=self.rotation_axis,
                    zoom_factor=self.zoom_factor,
                    shift=self.shift)

                # round the target label back to integer
                transformed_targets[i] = np.rint(transformed_targets[i])

            # flip image
            if self.flip_axis is not None:
                transformed_images[i] = apply_flip(transformed_images[i],
                                                   self.flip_axis)

                transformed_targets[i] = apply_flip(transformed_targets[i],
                                                    self.flip_axis)

        return transformed_images, transformed_targets
Example #3
0
def test_apply_affine_transform_3d_correct():
    image = np.random.random((4, 4, 4, 2))
    res = apply_affine_transform(image,
                                 theta=30,
                                 rotation_axis=0,
                                 shift=[-1, 1, 2],
                                 mode='constant',
                                 cval=0)
    expected = apply_affine_transform(image,
                                      theta=30,
                                      rotation_axis=0,
                                      shift=[-1, 1, 2],
                                      mode='constant',
                                      cval=0,
                                      use_3d_transform=True)
    assert np.allclose(res, expected)

    image = np.random.random((4, 4, 4, 2))
    res = apply_affine_transform(image,
                                 theta=-30,
                                 rotation_axis=1,
                                 shift=[2, -1, 1],
                                 mode='constant',
                                 cval=0)
    expected = apply_affine_transform(image,
                                      theta=-30,
                                      rotation_axis=1,
                                      shift=[2, -1, 1],
                                      mode='constant',
                                      cval=0,
                                      use_3d_transform=True)
    assert np.allclose(res, expected)

    image = np.random.random((4, 4, 4, 2))
    res = apply_affine_transform(image,
                                 theta=90,
                                 rotation_axis=2,
                                 shift=[1, 2, -1],
                                 mode='constant',
                                 cval=0)
    expected = apply_affine_transform(image,
                                      theta=90,
                                      rotation_axis=2,
                                      shift=[1, 2, -1],
                                      mode='constant',
                                      cval=0,
                                      use_3d_transform=True)
    assert np.allclose(res, expected)
Example #4
0
def test_apply_affine_transform_3d_nosplit():
    image = np.zeros((3, 3, 3, 2))
    # 3d T in the first channel
    image[0][..., 0] = [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
    image[1][..., 0] = [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
    image[2][..., 0] = [[0, 0, 0], [1, 1, 1], [0, 0, 0]]
    # 3d H in the second channel
    image[0][..., 1] = [[1, 0, 1], [0, 0, 0], [0, 0, 0]]
    image[1][..., 1] = [[1, 1, 1], [0, 0, 0], [0, 0, 0]]
    image[2][..., 1] = [[1, 0, 1], [0, 0, 0], [0, 0, 0]]
    res = apply_affine_transform(image,
                                 theta=90,
                                 rotation_axis=0,
                                 mode='constant',
                                 cval=0,
                                 use_3d_transform=True)

    expected = np.zeros((3, 3, 3, 2))
    # 3d T in the first channel
    expected[0][..., 0] = [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
    expected[1][..., 0] = [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
    expected[2][..., 0] = [[0, 1, 0], [0, 1, 0], [0, 1, 0]]
    # 3d H in the second channel
    expected[0][..., 1] = [[0, 0, 1], [0, 0, 0], [0, 0, 1]]
    expected[1][..., 1] = [[0, 0, 1], [0, 0, 1], [0, 0, 1]]
    expected[2][..., 1] = [[0, 0, 1], [0, 0, 0], [0, 0, 1]]

    assert np.allclose(res, expected)
    assert np.all(np.rint(res) == expected)

    image = np.zeros((3, 3, 3, 2))
    # 3d T in the first channel
    image[0][..., 0] = [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
    image[1][..., 0] = [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
    image[2][..., 0] = [[0, 0, 0], [1, 1, 1], [0, 0, 0]]
    # 3d H in the second channel
    image[0][..., 1] = [[1, 0, 1], [0, 0, 0], [0, 0, 0]]
    image[1][..., 1] = [[1, 1, 1], [0, 0, 0], [0, 0, 0]]
    image[2][..., 1] = [[1, 0, 1], [0, 0, 0], [0, 0, 0]]
    res = apply_affine_transform(image,
                                 shift=(1, 0, 0),
                                 mode='constant',
                                 cval=0,
                                 use_3d_transform=True)

    expected = np.zeros((3, 3, 3, 2))
    # 3d T in the first channel
    # expected[0][..., 0] = [[0, 0, 0],
    #                        [0, 1, 0],
    #                        [0, 0, 0]]
    expected[0][..., 0] = [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
    expected[1][..., 0] = [[0, 0, 0], [1, 1, 1], [0, 0, 0]]
    # 3d H in the second channel
    expected[0][..., 1] = [[1, 1, 1], [0, 0, 0], [0, 0, 0]]
    expected[1][..., 1] = [[1, 0, 1], [0, 0, 0], [0, 0, 0]]

    assert np.allclose(res, expected)
    assert np.all(np.rint(res) == expected)

    res = apply_affine_transform(image,
                                 shift=(0, 1, 0),
                                 mode='constant',
                                 cval=0,
                                 use_3d_transform=True)

    expected = np.zeros((3, 3, 3, 2))
    expected[0][..., 0] = [
        [0, 1, 0],
        [0, 0, 0],
        [0, 0, 0],
    ]
    expected[1][..., 0] = [[0, 1, 0], [0, 0, 0], [0, 0, 0]]
    expected[2][..., 0] = [[1, 1, 1], [0, 0, 0], [0, 0, 0]]

    assert np.allclose(res, expected)
    assert np.all(np.rint(res) == expected)

    res = apply_affine_transform(image,
                                 shift=(0, 0, 1),
                                 mode='constant',
                                 cval=0,
                                 use_3d_transform=True)

    expected = np.zeros((3, 3, 3, 2))
    # 3d T in the first channel
    expected[0][..., 0] = [[0, 0, 0], [1, 0, 0], [0, 0, 0]]
    expected[1][..., 0] = [[0, 0, 0], [1, 0, 0], [0, 0, 0]]
    expected[2][..., 0] = [[0, 0, 0], [1, 1, 0], [0, 0, 0]]
    # 3d H in the second channel
    expected[0][..., 1] = [[0, 1, 0], [0, 0, 0], [0, 0, 0]]
    expected[1][..., 1] = [[1, 1, 0], [0, 0, 0], [0, 0, 0]]
    expected[2][..., 1] = [[0, 1, 0], [0, 0, 0], [0, 0, 0]]

    assert np.allclose(res, expected)
    assert np.all(np.rint(res) == expected)
    return normalize(image), target


if __name__ == "__main__":
    theta = 30
    zoom = 1
    rotation_axis = 0
    shift = (0, 0, 0)

    image, target = load_images()
    shape = image.shape[:-1]

    transformed = apply_affine_transform(image,
                                         mode='constant',
                                         rotation_axis=rotation_axis,
                                         theta=theta,
                                         zoom_factor=zoom,
                                         shift=shift).clip(0, 1)

    transformed_label = (apply_affine_transform(target,
                                                mode='constant',
                                                rotation_axis=rotation_axis,
                                                theta=theta,
                                                zoom_factor=zoom,
                                                shift=shift).clip(0, 1) >
                         0.5).astype(int)

    fig, axes = plt.subplots(2, 2)

    for ax in axes.flatten():
        ax.axis('off')
Example #6
0
    nrow = 5
    ncol = 8
    i = 0

    i += 1
    plt.subplot(nrow, ncol, i)
    plt.axis('off')
    plt.imshow(img_slice[..., 0])
    plt.title('Original')

    i += 1
    plt.subplot(nrow, ncol, i)
    plt.axis('off')
    plt.imshow(img_slice[..., 1])

    transformed = apply_affine_transform(img_slice, theta=-16, rotation_axis=2)

    transformed[..., 0] = transformed[..., 0].clip(-100, 100)
    transformed[..., 1] = transformed[..., 1].clip(0, img_slice[..., 1].max())

    i += 1
    plt.subplot(nrow, ncol, i)
    plt.axis('off')
    plt.imshow(transformed[..., 0])
    plt.title('Rotate -16')

    i += 1
    plt.subplot(nrow, ncol, i)
    plt.axis('off')
    plt.imshow(transformed[..., 1])
def affine_transform():
    img = load_image('imgs/cat.jpg')
    nrow = 6
    ncol = 10

    rotate = [0, -30, 15]
    zoom = [1, 0.5, 0.8, 1.2, 2]
    shift = [(0, 0), (0, 50), (70, 0), (70, 50)]

    mode = 'constant'
    cval = 0.0

    s = 0
    i = 1
    pos = 1
    for row, r in enumerate(rotate):
        for col, z in enumerate(zoom):
            transformed = apply_affine_transform(img,
                                                 theta=r,
                                                 zoom_factor=z,
                                                 shift=shift[s],
                                                 mode=mode, cval=cval)
            plt.subplot(nrow, ncol, pos+row*10+col)
            plt.axis('off')
            plt.imshow(transformed)
            plt.title(f'theta={r}, z={z},s={shift[s]}', fontsize=6)

    s = 1
    i = 1
    pos = 6
    for row, r in enumerate(rotate):
        for col, z in enumerate(zoom):
            transformed = apply_affine_transform(img,
                                                 theta=r,
                                                 zoom_factor=z,
                                                 shift=shift[s],
                                                 mode=mode, cval=cval)
            plt.subplot(nrow, ncol, pos+row*10+col)
            plt.axis('off')
            plt.imshow(transformed)
            plt.title(f'theta={r}, z={z},s={shift[s]}', fontsize=6)

    s = 2
    i = 1
    pos = 31
    for row, r in enumerate(rotate):
        for col, z in enumerate(zoom):
            transformed = apply_affine_transform(img,
                                                 theta=r,
                                                 zoom_factor=z,
                                                 shift=shift[s],
                                                 mode=mode, cval=cval)
            plt.subplot(nrow, ncol, pos+row*10+col)
            plt.axis('off')
            plt.imshow(transformed)
            plt.title(f'theta={r}, z={z},s={shift[s]}', fontsize=6)

    s = 3
    i = 1
    pos = 36
    for row, r in enumerate(rotate):
        for col, z in enumerate(zoom):
            transformed = apply_affine_transform(img,
                                                 theta=r,
                                                 zoom_factor=z,
                                                 shift=shift[s],
                                                 mode=mode, cval=cval)

            plt.subplot(nrow, ncol, pos+row*10+col)
            plt.axis('off')
            plt.imshow(transformed)
            plt.title(f'theta={r}, z={z},s={shift[s]}', fontsize=6)

    plt.show()