Rescale the image in a sample to a given size. Args: output_size (tuple or int): Desired output size. If tuple, output is matched to output_size. If int, smaller of image edges is matched to output_size keeping aspect ratio the same.
| 206 | # |
| 207 | |
| 208 | class Rescale(object): |
| 209 | """Rescale the image in a sample to a given size. |
| 210 | |
| 211 | Args: |
| 212 | output_size (tuple or int): Desired output size. If tuple, output is |
| 213 | matched to output_size. If int, smaller of image edges is matched |
| 214 | to output_size keeping aspect ratio the same. |
| 215 | """ |
| 216 | |
| 217 | def __init__(self, output_size): |
| 218 | assert isinstance(output_size, (int, tuple)) |
| 219 | self.output_size = output_size |
| 220 | |
| 221 | def __call__(self, sample): |
| 222 | image, landmarks = sample['image'], sample['landmarks'] |
| 223 | |
| 224 | h, w = image.shape[:2] |
| 225 | if isinstance(self.output_size, int): |
| 226 | if h > w: |
| 227 | new_h, new_w = self.output_size * h / w, self.output_size |
| 228 | else: |
| 229 | new_h, new_w = self.output_size, self.output_size * w / h |
| 230 | else: |
| 231 | new_h, new_w = self.output_size |
| 232 | |
| 233 | new_h, new_w = int(new_h), int(new_w) |
| 234 | |
| 235 | img = transform.resize(image, (new_h, new_w)) |
| 236 | |
| 237 | # h and w are swapped for landmarks because for images, |
| 238 | # x and y axes are axis 1 and 0 respectively |
| 239 | landmarks = landmarks * [new_w / w, new_h / h] |
| 240 | |
| 241 | return {'image': img, 'landmarks': landmarks} |
| 242 | |
| 243 | |
| 244 | class RandomCrop(object): |
no outgoing calls
no test coverage detected