| 54 | |
| 55 | |
| 56 | class LetterBoxTransform(Transform): |
| 57 | |
| 58 | def __init__(self, new_shape, dst_constant, ratio, padding, interpolation=None, border_type=None, color=(127.5, 127.5, 127.5)): |
| 59 | super().__init__() |
| 60 | self._set_attributes(locals()) |
| 61 | self.interpolation = cv2.INTER_AREA if interpolation is None else interpolation |
| 62 | self.border_type = cv2.BORDER_CONSTANT if border_type is None else border_type |
| 63 | self.color = color |
| 64 | |
| 65 | def apply_image(self, img, interp=None): |
| 66 | top, bottom, left, right = self.dst_constant |
| 67 | img = cv2.resize(img, self.new_shape, interpolation=self.interpolation) # resized, no border |
| 68 | ret = cv2.copyMakeBorder(img, top, bottom, left, right, self.border_type, value=self.color) # padded rectangular |
| 69 | return ret |
| 70 | |
| 71 | def apply_coords(self, coords): |
| 72 | pad_w, pad_h = self.padding |
| 73 | coords[:, 0] = self.ratio * coords[:, 0] + pad_w |
| 74 | coords[:, 1] = self.ratio * coords[:, 1] + pad_h |
| 75 | return coords |
| 76 | |
| 77 | def apply_box(self, coords): |
| 78 | padw, padh = self.padding |
| 79 | coords_out = coords.copy() |
| 80 | coords_out[:, 0] = self.ratio * coords[:, 0] + padw |
| 81 | coords_out[:, 1] = self.ratio * coords[:, 1] + padh |
| 82 | coords_out[:, 2] = self.ratio * coords[:, 2] + padw |
| 83 | coords_out[:, 3] = self.ratio * coords[:, 3] + padh |
| 84 | return coords_out |
| 85 | |
| 86 | def apply_segmentation(self, segmentation): |
| 87 | return NotImplemented |
| 88 | |
| 89 | def inverse(self): |
| 90 | return NotImplemented |
| 91 | |
| 92 | |
| 93 | class AffineTransform(Transform): |