Skip to content

Commit

Permalink
[Fix] Fix potential interger overflow in imequalize. (#1198)
Browse files Browse the repository at this point in the history
* Fix potential interger overflow in `imequalize`.

* Modify imequalize unit test image size to generate potential integer
overflow.
  • Loading branch information
mzr1996 authored Jul 14, 2021
1 parent 6659c38 commit 366c628
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 2 additions & 0 deletions mmcv/image/photometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ def _scale_channel(im, c):
lut = (np.cumsum(histo) + (step // 2)) // step
# Shift lut, prepending with 0.
lut = np.concatenate([[0], lut[:-1]], 0)
# handle potential integer overflow
lut[lut > 255] = 255
# If step is zero, return the original image.
# Otherwise, index from lut.
return np.where(np.equal(step, 0), im, lut[im])
Expand Down
5 changes: 2 additions & 3 deletions tests/test_image/test_photometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ def _imequalize(img):

# test equalize with randomly sampled image.
for _ in range(nb_rand_test):
img = np.clip(
np.random.normal(0, 1, (1000, 1200, 3)) * 260, 0,
255).astype(np.uint8)
img = np.clip(np.random.normal(0, 1, (256, 256, 3)) * 260, 0,
255).astype(np.uint8)
equalized_img = mmcv.imequalize(img)
assert_array_equal(equalized_img, _imequalize(img))

Expand Down

0 comments on commit 366c628

Please sign in to comment.