def shear(x,
          shear,
          row_axis=0,
          col_axis=1,
          channel_axis=2,
          fill_mode='nearest',
          cval=0.):
    shear_matrix = np.array([[1, -np.sin(shear), 0], [0, np.cos(shear), 0],
                             [0, 0, 1]])
    h, w = x.shape[row_axis], x.shape[col_axis]
    transform_matrix = image.transform_matrix_offset_center(shear_matrix, h, w)
    x = np.rollaxis(x, channel_axis, 0)
    final_affine_matrix = transform_matrix[:2, :2]
    final_offset = transform_matrix[:2, 2]

    channel_images = [
        scipy.ndimage.interpolation.affine_transform(x_channel,
                                                     final_affine_matrix,
                                                     final_offset,
                                                     order=1,
                                                     mode=fill_mode,
                                                     cval=cval)
        for x_channel in x
    ]
    x = np.stack(channel_images, axis=0)
    x = np.rollaxis(x, 0, channel_axis + 1)
    return x
def apply_transform_matrix(self, img: np.ndarray, transform_matrix):
    """
    Apply transformation matrix to image img
    :param self: self
    :param img: image
    :param transform_matrix: transformation matrix
    :return: transformed image
    """
    h, w = img.shape[0], img.shape[1]
    transform_matrix = transform_matrix_offset_center(transform_matrix, h, w)
    img = np.rollaxis(img, 2, 0)
    final_affine_matrix = transform_matrix[:2, :2]
    final_offset = transform_matrix[:2, 2]

    channel_images = [scipy.ndimage.interpolation.affine_transform(
        x_channel,
        final_affine_matrix,
        final_offset,
        order=1,
        mode=self.fill_mode,
        cval=self.cval) for x_channel in img]
    img = np.stack(channel_images, axis=0)
    img = np.rollaxis(img, 0, 2 + 1)
    # img = apply_affine_transform(img, transform_matrix, channel_axis=2, fill_mode=self.fill_mode, cval=self.cval) # apply_transform
    return img
Esempio n. 3
0
def rotate(x, row_axis=0, col_axis=1, channel_axis=2, fill_mode='nearest', cval=0.):
    rotate_limit=(-90, 90)
    theta = np.pi / 180 * np.random.uniform(rotate_limit[0], rotate_limit[1])
    rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0],[np.sin(theta), np.cos(theta), 0],[0, 0, 1]])
    h, w = x.shape[row_axis], x.shape[col_axis]
    transform_matrix = image.transform_matrix_offset_center(rotation_matrix, h, w)
    x = image.apply_transform(x, transform_matrix, channel_axis, fill_mode, cval)
    return x
Esempio n. 4
0
def random_rotation(x, y, rg, row_index=0, col_index=1, channel_index=2,
                    fill_mode='nearest', cval=0.):
    theta = np.pi / 180 * np.random.uniform(-rg, rg)
    rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0],
                                [np.sin(theta), np.cos(theta), 0],
                                [0, 0, 1]])

    h, w = x.shape[row_index], x.shape[col_index]
    transform_matrix = transform_matrix_offset_center(rotation_matrix, h, w)
    x = apply_transform(x, transform_matrix, channel_index, fill_mode, cval)
    y = apply_transform(y, transform_matrix, channel_index, fill_mode, cval)
    return x, y
Esempio n. 5
0
def random_zoom(x, y, zoom_range, row_index=0, col_index=1, channel_index=2,
                fill_mode='nearest', cval=0.):
    if len(zoom_range) != 2:
        raise Exception('zoom_range should be a tuple or list of two floats. '
                        'Received arg: ', zoom_range)

    if zoom_range[0] == 1 and zoom_range[1] == 1:
        zx, zy = 1, 1
    else:
        zx, zy = np.random.uniform(zoom_range[0], zoom_range[1], 2)
    zoom_matrix = np.array([[zx, 0, 0],
                            [0, zy, 0],
                            [0, 0, 1]])

    h, w = x.shape[row_index], x.shape[col_index]
    transform_matrix = transform_matrix_offset_center(zoom_matrix, h, w)
    x = apply_transform(x, transform_matrix, channel_index, fill_mode, cval)
    y = apply_transform(y, transform_matrix, channel_index, fill_mode, cval)
    return x, y
def rotate(x,
           theta,
           row_axis=0,
           col_axis=1,
           channel_axis=2,
           fill_mode='nearest',
           cval=0.):
    rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0],
                                [np.sin(theta),
                                 np.cos(theta), 0], [0, 0, 1]])
    h, w = x.shape[row_axis], x.shape[col_axis]
    transform_matrix = image.transform_matrix_offset_center(
        rotation_matrix, h, w)
    x = np.rollaxis(x, channel_axis, 0)
    final_affine_matrix = transform_matrix[:2, :2]
    final_offset = transform_matrix[:2, 2]
    if len(x.shape) == 3:
        x = np.rollaxis(x, channel_axis, 0)
        channel_images = [
            scipy.ndimage.interpolation.affine_transform(x_channel,
                                                         final_affine_matrix,
                                                         final_offset,
                                                         order=1,
                                                         mode=fill_mode,
                                                         cval=cval)
            for x_channel in x
        ]

        x = np.stack(channel_images, axis=0)
        x = np.rollaxis(x, 0, channel_axis + 1)
    else:
        x = scipy.ndimage.interpolation.affine_transform(x,
                                                         final_affine_matrix,
                                                         final_offset,
                                                         order=1,
                                                         mode=fill_mode,
                                                         cval=cval)
    return x
    def random_transform_extension(self, x, y, seed=None):
        """Randomly augment a single image tensor.

        # Arguments
            x: 3D tensor, single image.
            seed: random seed.

        # Returns
            A randomly transformed version of the input (same shape).
        """
        # x is a single image, so it doesn't have image number at index 0
        img_row_axis = self.row_axis - 1
        img_col_axis = self.col_axis - 1
        img_channel_axis = self.channel_axis - 1

        if seed is not None:
            np.random.seed(seed)

        # use composition of homographies
        # to generate final transform that needs to be applied
        if self.rotation_range:
            theta = np.pi / 180 * np.random.uniform(-self.rotation_range,
                                                    self.rotation_range)
        else:
            theta = 0

        if self.height_shift_range:
            tx = np.random.uniform(
                -self.height_shift_range,
                self.height_shift_range) * x.shape[img_row_axis]
        else:
            tx = 0

        if self.width_shift_range:
            ty = np.random.uniform(
                -self.width_shift_range,
                self.width_shift_range) * x.shape[img_col_axis]
        else:
            ty = 0

        if self.shear_range:
            shear = np.random.uniform(-self.shear_range, self.shear_range)
        else:
            shear = 0

        if self.zoom_range[0] == 1 and self.zoom_range[1] == 1:
            zx, zy = 1, 1
        else:
            zx, zy = np.random.uniform(self.zoom_range[0], self.zoom_range[1],
                                       2)

        transform_matrix = None
        if theta != 0:
            rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0],
                                        [np.sin(theta),
                                         np.cos(theta), 0], [0, 0, 1]])
            transform_matrix = rotation_matrix

        if tx != 0 or ty != 0:
            shift_matrix = np.array([[1, 0, tx], [0, 1, ty], [0, 0, 1]])
            transform_matrix = shift_matrix if transform_matrix is None else np.dot(
                transform_matrix, shift_matrix)

        if shear != 0:
            shear_matrix = np.array([[1, -np.sin(shear), 0],
                                     [0, np.cos(shear), 0], [0, 0, 1]])
            transform_matrix = shear_matrix if transform_matrix is None else np.dot(
                transform_matrix, shear_matrix)

        if zx != 1 or zy != 1:
            zoom_matrix = np.array([[zx, 0, 0], [0, zy, 0], [0, 0, 1]])
            transform_matrix = zoom_matrix if transform_matrix is None else np.dot(
                transform_matrix, zoom_matrix)

        if transform_matrix is not None:
            hx, wx = x.shape[img_row_axis], x.shape[img_col_axis]
            hy, wy = y.shape[img_row_axis], y.shape[img_col_axis]
            transform_matrix_x = kimage.transform_matrix_offset_center(
                transform_matrix, hx, wx)
            transform_matrix_y = kimage.transform_matrix_offset_center(
                transform_matrix, hy, wy)
            x = apply_transform(x,
                                transform_matrix_x,
                                img_channel_axis,
                                fill_mode=self.fill_mode,
                                cval=self.cval)

            y = apply_transform(y,
                                transform_matrix_y,
                                img_channel_axis,
                                fill_mode=self.fill_mode,
                                cval=self.cval)

        if self.channel_shift_range != 0:
            x = image.random_channel_shift(x, self.channel_shift_range,
                                           img_channel_axis)

        if self.horizontal_flip:
            if np.random.random() < 0.5:
                x = kimage.flip_axis(x, img_col_axis)
                y = kimage.flip_axis(y, img_col_axis)

        if self.vertical_flip:
            if np.random.random() < 0.5:
                x = kimage.flip_axis(x, img_row_axis)
                y = kimage.flip_axis(y, img_row_axis)
        return x, y