Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]Fix bugs in imequalize, adjust_brightness and adjust_contrast #863

Merged
merged 1 commit into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mmcv/image/photometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def _scale_channel(im, c):
s2 = _scale_channel(img, 1)
s3 = _scale_channel(img, 2)
equalized_img = np.stack([s1, s2, s3], axis=-1)
return equalized_img
return equalized_img.astype((img.dtype))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.astype(img.dtype)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LXXXXR may fix it in #864



def adjust_brightness(img, factor=1.):
Expand Down Expand Up @@ -196,6 +196,7 @@ def adjust_brightness(img, factor=1.):
brightened_img = cv2.addWeighted(
img.astype(np.float32), factor, degenerated.astype(np.float32),
1 - factor, 0)
brightened_img = np.clip(brightened_img, 0, 255)
return brightened_img.astype(img.dtype)


Expand Down Expand Up @@ -224,6 +225,7 @@ def adjust_contrast(img, factor=1.):
contrasted_img = cv2.addWeighted(
img.astype(np.float32), factor, degenerated.astype(np.float32),
1 - factor, 0)
contrasted_img = np.clip(contrasted_img, 0, 255)
return contrasted_img.astype(img.dtype)


Expand Down
6 changes: 3 additions & 3 deletions tests/test_image/test_photometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def _imequalize(img):
# test equalize with randomly sampled image.
for _ in range(nb_rand_test):
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)
equalized_img = mmcv.imequalize(img)
assert_array_equal(equalized_img, _imequalize(img))
Expand Down Expand Up @@ -160,7 +160,7 @@ def _adjust_brightness(img, factor):
img = np.clip(
np.random.uniform(0, 1, (1000, 1200, 3)) * 260, 0,
255).astype(np.uint8)
factor = np.random.uniform()
factor = np.random.uniform() + np.random.choice([0, 1])
np.testing.assert_allclose(
mmcv.adjust_brightness(img, factor).astype(np.int32),
_adjust_brightness(img, factor).astype(np.int32),
Expand Down Expand Up @@ -192,7 +192,7 @@ def _adjust_contrast(img, factor):
img = np.clip(
np.random.uniform(0, 1, (1200, 1000, 3)) * 260, 0,
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
# and mmcv.adjust_contrast comes from the gap that converts from
# a color image to gray image using mmcv or PIL.
Expand Down