diff --git a/ppdet/data/transform/operator.py b/ppdet/data/transform/operator.py index 3609ca010d738409e084bdb2e5aa7a54948af508..e6943cd4b53cfb53b81b5c3f5285b267547e066a 100644 --- a/ppdet/data/transform/operator.py +++ b/ppdet/data/transform/operator.py @@ -360,9 +360,17 @@ class RandomDistortOp(BaseOperator): return img img = img.astype(np.float32) - img[..., 0] += random.uniform(low, high) - img[..., 0][img[..., 0] > 360] -= 360 - img[..., 0][img[..., 0] < 0] += 360 + # it works, but result differ from HSV version + delta = np.random.uniform(low, high) + u = np.cos(delta * np.pi) + w = np.sin(delta * np.pi) + bt = np.array([[1.0, 0.0, 0.0], [0.0, u, -w], [0.0, w, u]]) + tyiq = np.array([[0.299, 0.587, 0.114], [0.596, -0.274, -0.321], + [0.211, -0.523, 0.311]]) + ityiq = np.array([[1.0, 0.956, 0.621], [1.0, -0.272, -0.647], + [1.0, -1.107, 1.705]]) + t = np.dot(np.dot(ityiq, bt), tyiq).T + img = np.dot(img, t) return img def apply_saturation(self, img): @@ -371,7 +379,12 @@ class RandomDistortOp(BaseOperator): return img delta = np.random.uniform(low, high) img = img.astype(np.float32) - img[..., 1] *= delta + # it works, but result differ from HSV version + gray = img * np.array([[[0.299, 0.587, 0.114]]], dtype=np.float32) + gray = gray.sum(axis=2, keepdims=True) + gray *= (1.0 - delta) + img *= delta + img += gray return img def apply_contrast(self, img): @@ -396,10 +409,8 @@ class RandomDistortOp(BaseOperator): img = sample['image'] if self.random_apply: functions = [ - self.apply_brightness, - self.apply_contrast, - lambda img: cv2.cvtColor(self.apply_saturation(cv2.cvtColor(img, cv2.COLOR_RGB2HSV)), cv2.COLOR_HSV2RGB), - lambda img: cv2.cvtColor(self.apply_hue(cv2.cvtColor(img, cv2.COLOR_RGB2HSV)), cv2.COLOR_HSV2RGB), + self.apply_brightness, self.apply_contrast, + self.apply_saturation, self.apply_hue ] distortions = np.random.permutation(functions)[:self.count] for func in distortions: @@ -413,10 +424,8 @@ class RandomDistortOp(BaseOperator): if mode: img = self.apply_contrast(img) - img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV) img = self.apply_saturation(img) img = self.apply_hue(img) - img = cv2.cvtColor(img, cv2.COLOR_HSV2RGB) if not mode: img = self.apply_contrast(img)