Unverified Commit 27e9ede5 authored by LXXXXR's avatar LXXXXR Committed by GitHub
Browse files

[Fix]: fix bugs in some photometric transforms (#863)

parent 6719cde4
...@@ -166,7 +166,7 @@ def imequalize(img): ...@@ -166,7 +166,7 @@ def imequalize(img):
s2 = _scale_channel(img, 1) s2 = _scale_channel(img, 1)
s3 = _scale_channel(img, 2) s3 = _scale_channel(img, 2)
equalized_img = np.stack([s1, s2, s3], axis=-1) equalized_img = np.stack([s1, s2, s3], axis=-1)
return equalized_img return equalized_img.astype((img.dtype))
def adjust_brightness(img, factor=1.): def adjust_brightness(img, factor=1.):
...@@ -196,6 +196,7 @@ def adjust_brightness(img, factor=1.): ...@@ -196,6 +196,7 @@ def adjust_brightness(img, factor=1.):
brightened_img = cv2.addWeighted( brightened_img = cv2.addWeighted(
img.astype(np.float32), factor, degenerated.astype(np.float32), img.astype(np.float32), factor, degenerated.astype(np.float32),
1 - factor, 0) 1 - factor, 0)
brightened_img = np.clip(brightened_img, 0, 255)
return brightened_img.astype(img.dtype) return brightened_img.astype(img.dtype)
...@@ -224,6 +225,7 @@ def adjust_contrast(img, factor=1.): ...@@ -224,6 +225,7 @@ def adjust_contrast(img, factor=1.):
contrasted_img = cv2.addWeighted( contrasted_img = cv2.addWeighted(
img.astype(np.float32), factor, degenerated.astype(np.float32), img.astype(np.float32), factor, degenerated.astype(np.float32),
1 - factor, 0) 1 - factor, 0)
contrasted_img = np.clip(contrasted_img, 0, 255)
return contrasted_img.astype(img.dtype) return contrasted_img.astype(img.dtype)
......
...@@ -132,7 +132,7 @@ class TestPhotometric: ...@@ -132,7 +132,7 @@ class TestPhotometric:
# test equalize with randomly sampled image. # test equalize with randomly sampled image.
for _ in range(nb_rand_test): for _ in range(nb_rand_test):
img = np.clip( img = np.clip(
np.random.uniform(0, 1, (1000, 1200, 3)) * 260, 0, np.random.normal(0, 1, (1000, 1200, 3)) * 260, 0,
255).astype(np.uint8) 255).astype(np.uint8)
equalized_img = mmcv.imequalize(img) equalized_img = mmcv.imequalize(img)
assert_array_equal(equalized_img, _imequalize(img)) assert_array_equal(equalized_img, _imequalize(img))
...@@ -160,7 +160,7 @@ class TestPhotometric: ...@@ -160,7 +160,7 @@ class TestPhotometric:
img = np.clip( img = np.clip(
np.random.uniform(0, 1, (1000, 1200, 3)) * 260, 0, np.random.uniform(0, 1, (1000, 1200, 3)) * 260, 0,
255).astype(np.uint8) 255).astype(np.uint8)
factor = np.random.uniform() factor = np.random.uniform() + np.random.choice([0, 1])
np.testing.assert_allclose( np.testing.assert_allclose(
mmcv.adjust_brightness(img, factor).astype(np.int32), mmcv.adjust_brightness(img, factor).astype(np.int32),
_adjust_brightness(img, factor).astype(np.int32), _adjust_brightness(img, factor).astype(np.int32),
...@@ -192,7 +192,7 @@ class TestPhotometric: ...@@ -192,7 +192,7 @@ class TestPhotometric:
img = np.clip( img = np.clip(
np.random.uniform(0, 1, (1200, 1000, 3)) * 260, 0, np.random.uniform(0, 1, (1200, 1000, 3)) * 260, 0,
255).astype(np.uint8) 255).astype(np.uint8)
factor = np.random.uniform() factor = np.random.uniform() + np.random.choice([0, 1])
# Note the gap (less_equal 1) between PIL.ImageEnhance.Contrast # Note the gap (less_equal 1) between PIL.ImageEnhance.Contrast
# and mmcv.adjust_contrast comes from the gap that converts from # and mmcv.adjust_contrast comes from the gap that converts from
# a color image to gray image using mmcv or PIL. # a color image to gray image using mmcv or PIL.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment