Applies the given transform(s) to the image(s). Args: images: A tensor of shape (num_images, num_rows, num_columns, num_channels) (NHWC), (num_rows, num_columns, num_channels) (HWC), or (num_rows, num_columns) (HW). The rank must be statically known (the shape is not `Ten
(images,
transforms,
interpolation="NEAREST",
output_shape=None,
name=None)
| 220 | |
| 221 | |
| 222 | def transform(images, |
| 223 | transforms, |
| 224 | interpolation="NEAREST", |
| 225 | output_shape=None, |
| 226 | name=None): |
| 227 | """Applies the given transform(s) to the image(s). |
| 228 | |
| 229 | Args: |
| 230 | images: A tensor of shape (num_images, num_rows, num_columns, num_channels) |
| 231 | (NHWC), (num_rows, num_columns, num_channels) (HWC), or |
| 232 | (num_rows, num_columns) (HW). The rank must be statically known (the |
| 233 | shape is not `TensorShape(None)`. |
| 234 | transforms: Projective transform matrix/matrices. A vector of length 8 or |
| 235 | tensor of size N x 8. If one row of transforms is |
| 236 | [a0, a1, a2, b0, b1, b2, c0, c1], then it maps the *output* point |
| 237 | `(x, y)` to a transformed *input* point |
| 238 | `(x', y') = ((a0 x + a1 y + a2) / k, (b0 x + b1 y + b2) / k)`, |
| 239 | where `k = c0 x + c1 y + 1`. The transforms are *inverted* compared to |
| 240 | the transform mapping input points to output points. Note that gradients |
| 241 | are not backpropagated into transformation parameters. |
| 242 | interpolation: Interpolation mode. Supported values: "NEAREST", "BILINEAR". |
| 243 | output_shape: Output dimesion after the transform, [height, width]. |
| 244 | If None, output is the same size as input image. |
| 245 | |
| 246 | name: The name of the op. |
| 247 | |
| 248 | Returns: |
| 249 | Image(s) with the same type and shape as `images`, with the given |
| 250 | transform(s) applied. Transformed coordinates outside of the input image |
| 251 | will be filled with zeros. |
| 252 | |
| 253 | Raises: |
| 254 | TypeError: If `image` is an invalid type. |
| 255 | ValueError: If output shape is not 1-D int32 Tensor. |
| 256 | """ |
| 257 | with ops.name_scope(name, "transform"): |
| 258 | image_or_images = ops.convert_to_tensor(images, name="images") |
| 259 | transform_or_transforms = ops.convert_to_tensor( |
| 260 | transforms, name="transforms", dtype=dtypes.float32) |
| 261 | if image_or_images.dtype.base_dtype not in _IMAGE_DTYPES: |
| 262 | raise TypeError("Invalid dtype %s." % image_or_images.dtype) |
| 263 | elif image_or_images.get_shape().ndims is None: |
| 264 | raise TypeError("image_or_images rank must be statically known") |
| 265 | elif len(image_or_images.get_shape()) == 2: |
| 266 | images = image_or_images[None, :, :, None] |
| 267 | elif len(image_or_images.get_shape()) == 3: |
| 268 | images = image_or_images[None, :, :, :] |
| 269 | elif len(image_or_images.get_shape()) == 4: |
| 270 | images = image_or_images |
| 271 | else: |
| 272 | raise TypeError("Images should have rank between 2 and 4.") |
| 273 | |
| 274 | if output_shape is None: |
| 275 | output_shape = array_ops.shape(images)[1:3] |
| 276 | if not context.executing_eagerly(): |
| 277 | output_shape_value = tensor_util.constant_value(output_shape) |
| 278 | if output_shape_value is not None: |
| 279 | output_shape = output_shape_value |