未验证 提交 56a673a0 编写于 作者: W wangxinxin08 提交者: GitHub

[Dygraph]replace RandomDistortOp with ColorDistort (#1841)

* replace RandomDistortOp with ColorDistort

* modify RandomDistortOp

* remove hsv_format
上级 f545a541
......@@ -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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册