Repository: scikit-image/scikit-image

Issue:
uint8 artifacts in adjust_gamma?
## Description
Not sure if this is a bug or not, but if I apply a gamma correction of 1.0 (which should leave the image unchanged IIUC) to a `uint8` image, what I get is some weird behavior on the resulting image histogram, see below. In principle, I would expect the image to remain unchanged upon this adjustment.
<!--
(Note: for guidance on how to use `scikit-image`, please post instead on https://forum.image.sc/tag/scikit-image)
-->

## Way to reproduce
```python
import matplotlib.pyplot as plt
import numpy as np
from skimage.exposure import adjust_gamma, histogram

gen = np.random.default_rng()

image = gen.normal(loc=200.0, scale=10, size=(1024, 1024)).astype(np.uint8)

out_image = adjust_gamma(image, gamma=1.0)

freq_in, bins_in = histogram(image)
freq_out, bins_out = histogram(out_image)

plt.plot(bins_in, freq_in, c="r", label="original")
plt.plot(bins_out, freq_out, c="b", label="adjusted")
plt.legend()
```
![image](https://user-images.githubusercontent.com/89844771/158585460-79cd372b-8490-4abe-9384-ff937cecd17d.png)


## Version information
```python
# 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(f'scikit-image version: {skimage.__version__}')
import numpy; print(f'numpy version: {numpy.__version__}')
```

```python
3.10.2 | packaged by conda-forge | (main, Feb  1 2022, 19:26:10) [MSC v.1929 64 bit (AMD64)]
Windows-10-10.0.19044-SP0
scikit-image version: 0.19.2
numpy version: 1.22.2

```



Discussion:
Thank you @aqc-carlodri for the report, I can reproduce the bug! It is caused by a round off error :cry::
Gamma adjustment of `uint8` arrays is made using a look-up-table
https://github.com/scikit-image/scikit-image/blob/fd943c6183537a5f27860559807a6ddd6bf0ce9b/skimage/exposure/exposure.py#L611-L617

When `gain` and `gamma` are equal to 1, `lut` is supposed to be equal to `np.arange(256)`, but this is not true because of round-off errors:

```python
>>> gain = 1
>>> gamma = 1
>>> lut = 255 * gain * (np.linspace(0, 1, 256) ** gamma)
>>> err_val = lut[lut != np.arange(256)]
>>> np.set_printoptions(precision=15)
>>> print(err_val)
[ 32.99999999999999  36.99999999999999  40.99999999999999
  44.99999999999999  48.99999999999999  52.99999999999999
  56.99999999999999  60.99999999999999  65.99999999999999
  73.99999999999999  81.99999999999999  89.99999999999999
  97.99999999999999 105.99999999999999 113.99999999999999
 121.99999999999999 131.99999999999997 147.99999999999997
 163.99999999999997 179.99999999999997 195.99999999999997
 211.99999999999997 227.99999999999997 243.99999999999997]
```
A simple `np.round` can fix this :wink: 

Thanks for the quick reply @rfezzani! Actually, I just realized that the output histogram behaves similarly also for `gamma` and `gain` with values different from `1.0`: can you please test and see if you can reproduce?

For example, this is what happens with `gamma=0.9`:
![image](https://user-images.githubusercontent.com/89844771/158605102-f9b779d3-583a-4be1-91cf-8ddfe135d464.png)


and `gamma=1.1`:
![image](https://user-images.githubusercontent.com/89844771/158605292-a1dd0d56-952e-4525-8973-34a088e0daa8.png)

> Thanks for the quick reply @rfezzani! Actually, I just realized that the output histogram behaves similarly also for `gamma` and `gain` with values different from `1.0`: can you please test and see if you can reproduce?

After closer look, I think this is an expected result! For `gamma != 1` I mean  :wink:.
In fact, the look-up table with `uint8` values is an approximation of a continuous function and some integral values can never be reached by rounding the floating point values!

The `×` points in the following figure represents the missed 8bit integers by rounding the floating point values

![Figure_1](https://user-images.githubusercontent.com/3438227/158815022-7fc6d180-b07f-46fa-ab8b-26396f09910d.png)

The figure is obtained using this script
```python
import matplotlib.pyplot as plt
import numpy as np

def get_lut(gamma):
    lut = 255 * (np.linspace(0, 1, 256) ** gamma)
    return np.minimum(lut, 255)


x = np.arange(256)

for gamma in [0.5, 0.9, 1.0, 1.1, 1.5]:
    print(f"Continuous (gamma={gamma})")
    lut = get_lut(gamma)
    lut_u8 = (lut + 0.5).astype('uint8')
    plt.plot(x, lut, '-', label=f"gamma={gamma}")
    plt.plot(x, lut_u8, ':', label=f"LUT (gamma={gamma})")

    miss = np.flatnonzero(np.absolute(np.diff(lut_u8)) > 1)

    miss_val = [range(i0 + 1, i1)
                for i0, i1 in zip(lut_u8[miss], lut_u8[miss + 1])]

    lut_miss = [np.full(len(r), i0)
                for i0, r in zip(miss, miss_val)]
    if miss_val:
        miss_val = np.concatenate(miss_val)
        lut_miss = np.concatenate(lut_miss)

    plt.plot(lut_miss, miss_val, 'x',
             label=f"Missing values (gamma={gamma})")

plt.legend()

plt.show()
```


Resolving patch:
diff --git a/skimage/exposure/exposure.py b/skimage/exposure/exposure.py
--- a/skimage/exposure/exposure.py
+++ b/skimage/exposure/exposure.py
@@ -612,8 +612,8 @@ def _adjust_gamma_u8(image, gamma, gain):
     """LUT based implementation of gamma adjustment.
 
     """
-    lut = (255 * gain * (np.linspace(0, 1, 256) ** gamma))
-    lut = np.minimum(lut, 255).astype('uint8')
+    lut = 255 * gain * (np.linspace(0, 1, 256) ** gamma)
+    lut = np.minimum(np.rint(lut), 255).astype('uint8')
     return lut[image]
 
 


Test patch:
diff --git a/skimage/exposure/tests/test_exposure.py b/skimage/exposure/tests/test_exposure.py
--- a/skimage/exposure/tests/test_exposure.py
+++ b/skimage/exposure/tests/test_exposure.py
@@ -598,7 +598,7 @@ def test_adjust_gamma_1x1_shape():
 
 def test_adjust_gamma_one():
     """Same image should be returned for gamma equal to one"""
-    image = np.random.uniform(0, 255, (8, 8))
+    image = np.arange(0, 256, dtype=np.uint8).reshape((16, 16))
     result = exposure.adjust_gamma(image, 1)
     assert_array_equal(result, image)
 
@@ -616,16 +616,37 @@ def test_adjust_gamma_zero(dtype):
 def test_adjust_gamma_less_one():
     """Verifying the output with expected results for gamma
     correction with gamma equal to half"""
-    image = np.arange(0, 255, 4, np.uint8).reshape((8, 8))
-    expected = np.array([
-        [  0,  31,  45,  55,  63,  71,  78,  84],
-        [ 90,  95, 100, 105, 110, 115, 119, 123],
-        [127, 131, 135, 139, 142, 146, 149, 153],
-        [156, 159, 162, 165, 168, 171, 174, 177],
-        [180, 183, 186, 188, 191, 194, 196, 199],
-        [201, 204, 206, 209, 211, 214, 216, 218],
-        [221, 223, 225, 228, 230, 232, 234, 236],
-        [238, 241, 243, 245, 247, 249, 251, 253]], dtype=np.uint8)
+    image = np.arange(0, 256, dtype=np.uint8).reshape((16, 16))
+
+    expected = np.array([0, 16, 23, 28, 32, 36, 39, 42, 45, 48, 50,
+                         53, 55, 58, 60, 62, 64, 66, 68, 70, 71, 73,
+                         75, 77, 78, 80, 81, 83, 84, 86, 87, 89, 90,
+                         92, 93, 94, 96, 97, 98, 100, 101, 102, 103,
+                         105, 106, 107, 108, 109, 111, 112, 113, 114,
+                         115, 116, 117, 118, 119, 121, 122, 123, 124,
+                         125, 126, 127, 128, 129, 130, 131, 132, 133,
+                         134, 135, 135, 136, 137, 138, 139, 140, 141,
+                         142, 143, 144, 145, 145, 146, 147, 148, 149,
+                         150, 151, 151, 152, 153, 154, 155, 156, 156,
+                         157, 158, 159, 160, 160, 161, 162, 163, 164,
+                         164, 165, 166, 167, 167, 168, 169, 170, 170,
+                         171, 172, 173, 173, 174, 175, 176, 176, 177,
+                         178, 179, 179, 180, 181, 181, 182, 183, 183,
+                         184, 185, 186, 186, 187, 188, 188, 189, 190,
+                         190, 191, 192, 192, 193, 194, 194, 195, 196,
+                         196, 197, 198, 198, 199, 199, 200, 201, 201,
+                         202, 203, 203, 204, 204, 205, 206, 206, 207,
+                         208, 208, 209, 209, 210, 211, 211, 212, 212,
+                         213, 214, 214, 215, 215, 216, 217, 217, 218,
+                         218, 219, 220, 220, 221, 221, 222, 222, 223,
+                         224, 224, 225, 225, 226, 226, 227, 228, 228,
+                         229, 229, 230, 230, 231, 231, 232, 233, 233,
+                         234, 234, 235, 235, 236, 236, 237, 237, 238,
+                         238, 239, 240, 240, 241, 241, 242, 242, 243,
+                         243, 244, 244, 245, 245, 246, 246, 247, 247,
+                         248, 248, 249, 249, 250, 250, 251, 251, 252,
+                         252, 253, 253, 254, 254, 255],
+                        dtype=np.uint8).reshape((16, 16))
 
     result = exposure.adjust_gamma(image, 0.5)
     assert_array_equal(result, expected)
@@ -634,16 +655,33 @@ def test_adjust_gamma_less_one():
 def test_adjust_gamma_greater_one():
     """Verifying the output with expected results for gamma
     correction with gamma equal to two"""
-    image = np.arange(0, 255, 4, np.uint8).reshape((8, 8))
-    expected = np.array([
-        [  0,   0,   0,   0,   1,   1,   2,   3],
-        [  4,   5,   6,   7,   9,  10,  12,  14],
-        [ 16,  18,  20,  22,  25,  27,  30,  33],
-        [ 36,  39,  42,  45,  49,  52,  56,  60],
-        [ 64,  68,  72,  76,  81,  85,  90,  95],
-        [100, 105, 110, 116, 121, 127, 132, 138],
-        [144, 150, 156, 163, 169, 176, 182, 189],
-        [196, 203, 211, 218, 225, 233, 241, 249]], dtype=np.uint8)
+    image = np.arange(0, 256, dtype=np.uint8).reshape((16, 16))
+
+    expected = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
+                         1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
+                         4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8,
+                         8, 8, 9, 9, 9, 10, 10, 11, 11, 11, 12, 12,
+                         13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18,
+                         18, 19, 19, 20, 20, 21, 21, 22, 23, 23, 24,
+                         24, 25, 26, 26, 27, 28, 28, 29, 30, 30, 31,
+                         32, 32, 33, 34, 35, 35, 36, 37, 38, 38, 39,
+                         40, 41, 42, 42, 43, 44, 45, 46, 47, 47, 48,
+                         49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58,
+                         59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
+                         70, 71, 73, 74, 75, 76, 77, 78, 79, 80, 81,
+                         82, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94,
+                         95, 97, 98, 99, 100, 102, 103, 104, 105,
+                         107, 108, 109, 111, 112, 113, 115, 116, 117,
+                         119, 120, 121, 123, 124, 126, 127, 128, 130,
+                         131, 133, 134, 136, 137, 139, 140, 142, 143,
+                         145, 146, 148, 149, 151, 152, 154, 155, 157,
+                         158, 160, 162, 163, 165, 166, 168, 170, 171,
+                         173, 175, 176, 178, 180, 181, 183, 185, 186,
+                         188, 190, 192, 193, 195, 197, 199, 200, 202,
+                         204, 206, 207, 209, 211, 213, 215, 217, 218,
+                         220, 222, 224, 226, 228, 230, 232, 233, 235,
+                         237, 239, 241, 243, 245, 247, 249, 251, 253,
+                         255] , dtype=np.uint8).reshape((16, 16))
 
     result = exposure.adjust_gamma(image, 2)
     assert_array_equal(result, expected)