def shear_y(image, level, fill_value): """Equivalent of PIL Shearing in Y dimension.""" # Shear parallel to y axis is a projective transform # with a matrix form of: # [1 0 # level 1]. image = tfi.transform(wrap(image), [1., 0., 0., level, 1., 0., 0., 0.]) return unwrap(image, fill_value)
def shear_y(image: TensorLike, level: float, replace: int) -> TensorLike: """Perform shear operation on an image (y-axis) Args: image: A 3D image Tensor. level: A float denoting shear element along x-axis replace: A one or three value 1D tensor to fill empty pixels. Returns: Transformed image along X or Y axis, with space outside image filled with replace. """ # Shear parallel to y axis is a projective transform # with a matrix form of: # [1 0 # level 1]. image = transform(wrap(image), [1.0, 0.0, 0.0, level, 1.0, 0.0, 0.0, 0.0]) return unwrap(image, replace)
def translate_xy(image: TensorLike, translate_to: TensorLike, replace: int) -> TensorLike: """Translates image in X or Y dimension. Args: image: A 3D image Tensor. translate_to: A 1D tensor to translate [x, y] replace: A one or three value 1D tensor to fill empty pixels. Returns: Translated image along X or Y axis, with space outside image filled with replace. Raises: ValueError: if axis is neither 0 nor 1.""" image = wrap(image) trans = tf.convert_to_tensor(translate_to) image = translate(image, [trans.numpy()[0], 0]) image = translate(image, [0, trans.numpy()[1]]) return unwrap(image, replace)
def rotate(image, degrees, fill_value, interpolation='BILINEAR'): """Rotates the image by degrees either clockwise or counterclockwise. Args: image: An image Tensor of type uint8. degrees: Float, a scalar angle in degrees to rotate all images by. If degrees is positive the image will be rotated clockwise otherwise it will be rotated counterclockwise. fill_value: A one or three value 1D tensor to fill empty pixels caused by the rotate operation. interpolation: Interpolation method Returns: The rotated version of image. """ # Convert from degrees to radians. degrees_to_radians = math.pi / 180.0 radians = degrees * degrees_to_radians # In practice, we should randomize the rotation degrees by flipping # it negatively half the time, but that's done on 'degrees' outside # of the function. image = tfi.rotate(wrap(image), radians, interpolation=interpolation) return unwrap(image, fill_value)
def translate_y(image, pixels, fill_value): """Equivalent of PIL Translate in Y dimension.""" image = tfi.translate(wrap(image), [0, -pixels]) return unwrap(image, fill_value)