Augmentation from CenterNet
| 91 | |
| 92 | |
| 93 | class AffineTransform(Transform): |
| 94 | """ |
| 95 | Augmentation from CenterNet |
| 96 | """ |
| 97 | |
| 98 | def __init__(self, src, dst, output_size, border_value=[0, 0, 0]): |
| 99 | """ |
| 100 | output_size:(w, h) |
| 101 | """ |
| 102 | super().__init__() |
| 103 | affine = cv2.getAffineTransform(np.float32(src), np.float32(dst)) |
| 104 | self._set_attributes(locals()) |
| 105 | |
| 106 | def apply_image(self, img: np.ndarray) -> np.ndarray: |
| 107 | """ |
| 108 | Apply AffineTransform for the image(s). |
| 109 | |
| 110 | Args: |
| 111 | img (ndarray): of shape HxW, HxWxC, or NxHxWxC. The array can be |
| 112 | of type uint8 in range [0, 255], or floating point in range |
| 113 | [0, 1] or [0, 255]. |
| 114 | Returns: |
| 115 | ndarray: the image(s) after applying affine transform. |
| 116 | """ |
| 117 | return cv2.warpAffine(img, self.affine, self.output_size, flags=cv2.INTER_LINEAR, borderValue=self.border_value) |
| 118 | |
| 119 | def apply_coords(self, coords: np.ndarray) -> np.ndarray: |
| 120 | """ |
| 121 | Affine the coordinates. |
| 122 | |
| 123 | Args: |
| 124 | coords (ndarray): floating point array of shape Nx2. Each row is |
| 125 | (x, y). |
| 126 | Returns: |
| 127 | ndarray: the flipped coordinates. |
| 128 | |
| 129 | Note: |
| 130 | The inputs are floating point coordinates, not pixel indices. |
| 131 | Therefore they are flipped by `(W - x, H - y)`, not |
| 132 | `(W - 1 - x, H 1 - y)`. |
| 133 | """ |
| 134 | # aug_coord (N, 3) shape, self.affine (2, 3) shape |
| 135 | w, h = self.output_size |
| 136 | aug_coords = np.column_stack((coords, np.ones(coords.shape[0]))) |
| 137 | coords = np.dot(aug_coords, self.affine.T) |
| 138 | coords[..., 0] = np.clip(coords[..., 0], 0, w - 1) |
| 139 | coords[..., 1] = np.clip(coords[..., 1], 0, h - 1) |
| 140 | return coords |
| 141 | |
| 142 | def apply_segmentation(self, segmentation): |
| 143 | return NotImplemented |
| 144 | |
| 145 | def inverse(self): |
| 146 | # return AffineTransform(self.dst, self.src, self.output_size) |
| 147 | return NotImplemented |
| 148 | |
| 149 | class FairAffineTransform(Transform): |
| 150 | """ |
no outgoing calls
no test coverage detected