def transform(img, scale, angle, bbox_center, output_shape): tx = bbox_center[0] - output_shape[1] * scale / 2 ty = bbox_center[1] - output_shape[0] * scale / 2 # for offsetting translations caused by rotation: # https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html rx = (1 - tf.cos(angle)) * output_shape[1] * scale / 2 - tf.sin(angle) * output_shape[0] * scale / 2 ry = tf.sin(angle) * output_shape[1] * scale / 2 + (1 - tf.cos(angle)) * output_shape[0] * scale / 2 transform = [scale * tf.cos(angle), scale * tf.sin(angle), rx + tx, -scale * tf.sin(angle), scale * tf.cos(angle), ry + ty, 0., 0.] img = image_ops.transform(tf.expand_dims(img, axis=0), tf.expand_dims(transform, axis=0), fill_mode='constant', output_shape=output_shape[:2]) img = tf.squeeze(img) # transform for keypoints alpha = 1 / scale * tf.cos(-angle) beta = 1 / scale * tf.sin(-angle) rx_xy = (1 - alpha) * bbox_center[0] - beta * bbox_center[1] ry_xy = beta * bbox_center[0] + (1 - alpha) * bbox_center[1] transform_xy = [[alpha, beta], [-beta, alpha]] tx_xy = bbox_center[0] - output_shape[1] / 2 ty_xy = bbox_center[1] - output_shape[0] / 2 M = tf.concat([transform_xy, [[rx_xy - tx_xy], [ry_xy - ty_xy]]], axis=1) return img, M
def transform(image: tf.Tensor, transforms) -> tf.Tensor: """Prepares input data for `image_ops.transform`.""" original_ndims = tf.rank(image) transforms = tf.convert_to_tensor(transforms, dtype=tf.float32) if transforms.shape.rank == 1: transforms = transforms[None] image = to_4d(image) image = image_ops.transform( images=image, transforms=transforms, interpolation='nearest') return from_4d(image, original_ndims)
def _run_random_transform_with_mock(self, transform_matrix, expected_output, mode, interpolation='bilinear'): inp = np.arange(15).reshape((1, 5, 3, 1)).astype(np.float32) with self.cached_session(use_gpu=True): output = image_preprocessing.transform( inp, transform_matrix, fill_mode=mode, interpolation=interpolation) self.assertAllClose(expected_output, output)
def rotate(inputs): """rotate image.""" inputs_shape = array_ops.shape(inputs) batch_size = inputs_shape[0] img_hd = math_ops.cast(inputs_shape[1], dtypes.float32) img_wd = math_ops.cast(inputs_shape[2], dtypes.float32) min_angle = LOWER * 2. * np.pi max_angle = UPPER * 2. * np.pi angles = random_ops.random_uniform( shape=[batch_size], minval=min_angle, maxval=max_angle) return image_preprocessing.transform( inputs, image_preprocessing.get_rotation_matrix(angles, img_hd, img_wd))
def zoom(inputs): """zoom image.""" inputs_shape = array_ops.shape(inputs) batch_size = inputs_shape[0] img_hd = math_ops.cast(inputs_shape[1], dtypes.float32) img_wd = math_ops.cast(inputs_shape[2], dtypes.float32) height_zoom = random_ops.random_uniform( shape=[batch_size, 1], minval=1. + LOWER, maxval=1. + UPPER) width_zoom = random_ops.random_uniform( shape=[batch_size, 1], minval=1. + LOWER, maxval=1. + UPPER) zooms = math_ops.cast( array_ops.concat([width_zoom, height_zoom], axis=1), dtype=dtypes.float32) return image_preprocessing.transform( inputs, image_preprocessing.get_zoom_matrix(zooms, img_hd, img_wd))