| 7 | |
| 8 | |
| 9 | class HueTransform(Transform): |
| 10 | def __init__(self, fraction, S_rand, V_rand, format='BGR'): |
| 11 | super().__init__() |
| 12 | self._set_attributes(locals()) |
| 13 | if format == 'BGR': |
| 14 | cvt_format_in = cv2.COLOR_BGR2HSV |
| 15 | cvt_format_out = cv2.COLOR_HSV2BGR |
| 16 | elif format == 'RGB': |
| 17 | cvt_format_in = cv2.COLOR_RGB2HSV |
| 18 | cvt_format_out = cv2.COLOR_HSV2RGB |
| 19 | else: |
| 20 | raise Exception("Image format only accepts BGR and RGB") |
| 21 | self.cvt_format_in = cvt_format_in |
| 22 | self.cvt_format_out = cvt_format_out |
| 23 | |
| 24 | def apply_coords(self, coords): |
| 25 | return coords |
| 26 | |
| 27 | def apply_segmentation(self, segmentation): |
| 28 | return segmentation |
| 29 | |
| 30 | def apply_image(self, img, interp=None): |
| 31 | return self.hue(img) |
| 32 | |
| 33 | def hue(self, img): |
| 34 | img_hsv = cv2.cvtColor(img, self.cvt_format_in) |
| 35 | S = img_hsv[:, :, 1].astype(np.float32) |
| 36 | V = img_hsv[:, :, 2].astype(np.float32) |
| 37 | |
| 38 | a = (self.S_rand * 2 - 1) * self.fraction + 1 |
| 39 | S *= a |
| 40 | if a > 1: |
| 41 | np.clip(S, a_min=0, a_max=255, out=S) |
| 42 | |
| 43 | a = (self.V_rand * 2 - 1) * self.fraction + 1 |
| 44 | V *= a |
| 45 | if a > 1: |
| 46 | np.clip(V, a_min=0, a_max=255, out=V) |
| 47 | |
| 48 | img_hsv[:, :, 1] = S.astype(np.uint8) |
| 49 | img_hsv[:, :, 2] = V.astype(np.uint8) |
| 50 | return cv2.cvtColor(img_hsv, self.cvt_format_out) |
| 51 | |
| 52 | def inverse(self): |
| 53 | return NotImplemented |
| 54 | |
| 55 | |
| 56 | class LetterBoxTransform(Transform): |