Perform horizontal flip.
| 27 | |
| 28 | |
| 29 | class HFlipTransform(Transform): |
| 30 | """ |
| 31 | Perform horizontal flip. |
| 32 | """ |
| 33 | |
| 34 | def __init__(self, width: int): |
| 35 | super().__init__() |
| 36 | self._set_attributes(locals()) |
| 37 | |
| 38 | def apply_image(self, img: np.ndarray) -> np.ndarray: |
| 39 | """ |
| 40 | Flip the image(s). |
| 41 | |
| 42 | Args: |
| 43 | img (ndarray): of shape HxW, HxWxC, or NxHxWxC. The array can be |
| 44 | of type uint8 in range [0, 255], or floating point in range |
| 45 | [0, 1] or [0, 255]. |
| 46 | Returns: |
| 47 | ndarray: the flipped image(s). |
| 48 | """ |
| 49 | # NOTE: opencv would be faster: |
| 50 | # https://github.com/pytorch/pytorch/issues/16424#issuecomment-580695672 |
| 51 | if img.ndim <= 3: # HxW, HxWxC |
| 52 | return np.flip(img, axis=1) |
| 53 | else: |
| 54 | return np.flip(img, axis=-2) |
| 55 | |
| 56 | def apply_coords(self, coords: np.ndarray) -> np.ndarray: |
| 57 | """ |
| 58 | Flip the coordinates. |
| 59 | |
| 60 | Args: |
| 61 | coords (ndarray): floating point array of shape Nx2. Each row is |
| 62 | (x, y). |
| 63 | Returns: |
| 64 | ndarray: the flipped coordinates. |
| 65 | |
| 66 | Note: |
| 67 | The inputs are floating point coordinates, not pixel indices. |
| 68 | Therefore they are flipped by `(W - x, H - y)`, not |
| 69 | `(W - 1 - x, H - 1 - y)`. |
| 70 | """ |
| 71 | coords[:, 0] = self.width - coords[:, 0] |
| 72 | return coords |
| 73 | |
| 74 | def inverse(self) -> Transform: |
| 75 | """ |
| 76 | The inverse is to flip again |
| 77 | """ |
| 78 | return self |
| 79 | |
| 80 | |
| 81 | class VFlipTransform(Transform): |
no outgoing calls
no test coverage detected