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

uint8 overflow in exposure.adjust_gamma #5413

Closed
florent-cd opened this issue May 31, 2021 · 2 comments · Fixed by #5507
Closed

uint8 overflow in exposure.adjust_gamma #5413

florent-cd opened this issue May 31, 2021 · 2 comments · Fixed by #5507

Comments

@florent-cd
Copy link

Description

When using the function skimage.exposure.adjust_gamma with an uint8 image with a gain superior to 1, the integers can overflow and return false results. A value bigger than 255 should be set to 255.

Way to reproduce

from skimage.exposure import adjust_gamma
import numpy as np

img = 255 * np.ones((3, 3), dtype=np.uint8)
transformed_img = adjust_gamma(img, gamma=1, gain=1.1)
print(transformed_img[0, 0])

This returns a value of 24 which is wrong.

The problem is in the definition of the lut in the function _adjust_gamma_u8 (l443):

    lut = (255 * gain * (np.linspace(0, 1, 256) ** gamma)).astype('uint8')

Before recasting as 'uint8' a max(255, lut) should probably be applied.

Version information

# Paste the output of the following python commands
from __future__ import print_function
import sys; print(sys.version)
import platform; print(platform.platform())
import skimage; print("scikit-image version: {}".format(skimage.__version__))
import numpy; print("numpy version: {}".format(numpy.__version__))
3.9.5 (default, May 18 2021, 19:34:48) 
[GCC 7.3.0]
Linux-4.15.0-143-generic-x86_64-with-glibc2.27
scikit-image version: 0.18.1
numpy version: 1.20.2
@hmaarrfk
Copy link
Member

hmaarrfk commented Jun 1, 2021

Thank you for the very detailed and complete bug report.

Would you be willing to provide the pull request to fix this issue?

I would suggest you include your failing example as a test and assert that the result is correct.

@florent-cd
Copy link
Author

Yes, I can do the PR (probably this week-end)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants