Perform vertical flip.
| 79 | |
| 80 | |
| 81 | class VFlipTransform(Transform): |
| 82 | """ |
| 83 | Perform vertical flip. |
| 84 | """ |
| 85 | |
| 86 | def __init__(self, height: int): |
| 87 | super().__init__() |
| 88 | self._set_attributes(locals()) |
| 89 | |
| 90 | def apply_image(self, img: np.ndarray) -> np.ndarray: |
| 91 | """ |
| 92 | Flip the image(s). |
| 93 | |
| 94 | Args: |
| 95 | img (ndarray): of shape HxW, HxWxC, or NxHxWxC. The array can be |
| 96 | of type uint8 in range [0, 255], or floating point in range |
| 97 | [0, 1] or [0, 255]. |
| 98 | Returns: |
| 99 | ndarray: the flipped image(s). |
| 100 | """ |
| 101 | tensor = torch.from_numpy(np.ascontiguousarray(img)) |
| 102 | if len(tensor.shape) == 2: |
| 103 | # For dimension of HxW. |
| 104 | tensor = tensor.flip((-2)) |
| 105 | elif len(tensor.shape) > 2: |
| 106 | # For dimension of HxWxC, NxHxWxC. |
| 107 | tensor = tensor.flip((-3)) |
| 108 | return tensor.numpy() |
| 109 | |
| 110 | def apply_coords(self, coords: np.ndarray) -> np.ndarray: |
| 111 | """ |
| 112 | Flip the coordinates. |
| 113 | |
| 114 | Args: |
| 115 | coords (ndarray): floating point array of shape Nx2. Each row is |
| 116 | (x, y). |
| 117 | Returns: |
| 118 | ndarray: the flipped coordinates. |
| 119 | |
| 120 | Note: |
| 121 | The inputs are floating point coordinates, not pixel indices. |
| 122 | Therefore they are flipped by `(W - x, H - y)`, not |
| 123 | `(W - 1 - x, H - 1 - y)`. |
| 124 | """ |
| 125 | coords[:, 1] = self.height - coords[:, 1] |
| 126 | return coords |
| 127 | |
| 128 | def inverse(self) -> Transform: |
| 129 | """ |
| 130 | The inverse is to flip again |
| 131 | """ |
| 132 | return self |
| 133 | |
| 134 | |
| 135 | class NoOpTransform(Transform): |
no outgoing calls
no test coverage detected