| 156 | |
| 157 | |
| 158 | class CropTransform(Transform): |
| 159 | def __init__( |
| 160 | self, |
| 161 | x0: int, |
| 162 | y0: int, |
| 163 | w: int, |
| 164 | h: int, |
| 165 | orig_w: Optional[int] = None, |
| 166 | orig_h: Optional[int] = None, |
| 167 | ): |
| 168 | """ |
| 169 | Args: |
| 170 | x0, y0, w, h (int): crop the image(s) by img[y0:y0+h, x0:x0+w]. |
| 171 | orig_w, orig_h (int): optional, the original width and height |
| 172 | before cropping. Needed to make this transform invertible. |
| 173 | """ |
| 174 | super().__init__() |
| 175 | self._set_attributes(locals()) |
| 176 | |
| 177 | def apply_image(self, img: np.ndarray) -> np.ndarray: |
| 178 | """ |
| 179 | Crop the image(s). |
| 180 | |
| 181 | Args: |
| 182 | img (ndarray): of shape NxHxWxC, or HxWxC or HxW. The array can be |
| 183 | of type uint8 in range [0, 255], or floating point in range |
| 184 | [0, 1] or [0, 255]. |
| 185 | Returns: |
| 186 | ndarray: cropped image(s). |
| 187 | """ |
| 188 | if len(img.shape) <= 3: |
| 189 | return img[self.y0 : self.y0 + self.h, self.x0 : self.x0 + self.w] |
| 190 | else: |
| 191 | return img[..., self.y0 : self.y0 + self.h, self.x0 : self.x0 + self.w, :] |
| 192 | |
| 193 | def apply_coords(self, coords: np.ndarray) -> np.ndarray: |
| 194 | """ |
| 195 | Apply crop transform on coordinates. |
| 196 | |
| 197 | Args: |
| 198 | coords (ndarray): floating point array of shape Nx2. Each row is |
| 199 | (x, y). |
| 200 | Returns: |
| 201 | ndarray: cropped coordinates. |
| 202 | """ |
| 203 | coords[:, 0] -= self.x0 |
| 204 | coords[:, 1] -= self.y0 |
| 205 | return coords |
| 206 | |
| 207 | def apply_polygons(self, polygons: list) -> list: |
| 208 | """ |
| 209 | Apply crop transform on a list of polygons, each represented by a Nx2 array. |
| 210 | It will crop the polygon with the box, therefore the number of points in the |
| 211 | polygon might change. |
| 212 | |
| 213 | Args: |
| 214 | polygon (list[ndarray]): each is a Nx2 floating point array of |
| 215 | (x, y) format in absolute coordinates. |
no outgoing calls
no test coverage detected