r"""Applies remap transformation to batched 2D images. Remap is an operation that relocates pixels in a image to another location in a new image. The input images are transformed to the output images by the tensor ``map_xy``. The output's H and W are same as ``map_xy``'s H and W. Args:
(
inp: Tensor,
map_xy: Tensor,
border_mode: str = "replicate",
scalar: float = 0.0,
interp_mode: str = "linear",
)
| 298 | |
| 299 | |
| 300 | def remap( |
| 301 | inp: Tensor, |
| 302 | map_xy: Tensor, |
| 303 | border_mode: str = "replicate", |
| 304 | scalar: float = 0.0, |
| 305 | interp_mode: str = "linear", |
| 306 | ) -> Tensor: |
| 307 | r"""Applies remap transformation to batched 2D images. Remap is an operation that relocates pixels in a image to another location in a new image. |
| 308 | |
| 309 | The input images are transformed to the output images by the tensor ``map_xy``. |
| 310 | The output's H and W are same as ``map_xy``'s H and W. |
| 311 | |
| 312 | Args: |
| 313 | inp: input image, its shape represents ``[b, c, in_h, in_w]``. |
| 314 | map_xy: transformation matrix, its shape shoule be ``[b, o_h, o_w, 2]``. The shape of output is determined by o_h and o_w. |
| 315 | For each element in output, its value is determined by inp and ``map_xy``. |
| 316 | ``map_xy[..., 0]`` and ``map_xy[..., 1]`` are the positions of |
| 317 | the current element in inp, respectively. Therefore, their ranges are ``[0, in_w - 1]`` and ``[0, in_h - 1]``. |
| 318 | border_mode: pixel extrapolation method. Default: "replicate". Currently also support "constant", "reflect", "reflect_101", "wrap". |
| 319 | "replicate": repeatedly fills the edge pixel values of the duplicate image, expanding the new boundary pixel values with |
| 320 | the edge pixel values. |
| 321 | "constant": fills the edges of the image with a fixed numeric value. |
| 322 | scalar: value used in case of a constant border. Default: 0 |
| 323 | interp_mode: interpolation methods. Default: "linear". Currently also support "nearest" mode. |
| 324 | |
| 325 | Returns: |
| 326 | output tensor. [b, c, o_h, o_w] |
| 327 | |
| 328 | Examples: |
| 329 | >>> import numpy as np |
| 330 | >>> inp_shape = (1, 1, 4, 4) |
| 331 | >>> inp = Tensor(np.arange(16, dtype=np.float32).reshape(inp_shape)) |
| 332 | >>> map_xy_shape = (1, 2, 2, 2) |
| 333 | >>> map_xy = Tensor(np.array([[[1., 0.],[0., 1.]], |
| 334 | ... [[0., 1.],[0., 1.]]], |
| 335 | ... dtype=np.float32).reshape(map_xy_shape)) |
| 336 | >>> out = F.vision.remap(inp, map_xy) |
| 337 | >>> out.numpy() |
| 338 | array([[[[1., 4.], |
| 339 | [4., 4.]]]], dtype=float32) |
| 340 | """ |
| 341 | format = "NCHW" |
| 342 | |
| 343 | op = builtin.Remap( |
| 344 | imode=interp_mode, border_type=border_mode, format=format, scalar=scalar |
| 345 | ) |
| 346 | assert isinstance(inp, (Tensor, megbrain_graph.VarNode)), "inp must be Tensor type" |
| 347 | (result,) = apply(op, inp, map_xy) |
| 348 | return result |
| 349 | |
| 350 | |
| 351 | def warp_affine( |