Skip to content

Commit 4d3f0d8

Browse files
committed
Fix bug where gamma="sRGB" returns a float image. Also simplified the code. Minor tidyup.
1 parent bfe11b6 commit 4d3f0d8

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

machinevisiontoolbox/base/color.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,6 @@ def name2color(name, colorspace="RGB", dtype="float"):
732732
colorspace = colorspace.lower()
733733

734734
def csconvert(name, cs):
735-
736735
rgb = colors.to_rgb(name)
737736

738737
if cs == "rgb":
@@ -1247,7 +1246,6 @@ def colorspace_convert(image, src, dst):
12471246

12481247

12491248
def _convertflag(src, dst):
1250-
12511249
src = src.replace(":", "").lower()
12521250
dst = dst.replace(":", "").lower()
12531251

@@ -1367,7 +1365,6 @@ def gamma_encode(image, gamma="sRGB"):
13671365
raise ValueError("gamma must be string or scalar")
13681366

13691367
if isinstance(gamma, str) and gamma.lower() == "srgb":
1370-
13711368
imagef = float_image(image)
13721369

13731370
if imagef.ndim == 2:
@@ -1438,47 +1435,43 @@ def gamma_decode(image, gamma="sRGB"):
14381435
if not (base.isscalar(gamma) or isinstance(gamma, str)):
14391436
raise ValueError("gamma must be string or scalar")
14401437

1441-
if isinstance(gamma, str) and gamma.lower() == "srgb":
1438+
imagef = float_image(image) # convert to float image
14421439

1443-
imagef = float_image(image)
1440+
if isinstance(gamma, str) and gamma.lower() == "srgb":
1441+
# sRGB gamma decode
14441442

14451443
if imagef.ndim == 2:
14461444
# greyscale
1447-
return _srgb_inverse(imagef)
1445+
out = _srgb_inverse(imagef)
1446+
14481447
elif imagef.ndim == 3:
14491448
# multi-dimensional
14501449
out = np.empty(imagef.shape, dtype=imagef.dtype)
1451-
for p in range(imagef.ndim):
1450+
for p in range(imagef.shape[2]):
14521451
out[:, :, p] = _srgb_inverse(imagef[:, :, p])
14531452
else:
14541453
raise ValueError("expecting 2d or 3d image")
14551454

1456-
if np.issubdtype(image.dtype, np.integer):
1457-
# original image was integer, convert back
1458-
return int_image(out)
1459-
else:
1460-
return out
1461-
14621455
else:
1456+
# normal power law decoding
14631457

1464-
# normal power law:
1465-
if np.issubdtype(image.dtype, np.floating):
1466-
return image**gamma
1467-
else:
1468-
# int image
1469-
maxg = np.float32((np.iinfo(image.dtype).max))
1470-
return ((image.astype(np.float32) / maxg) ** gamma) * maxg # original
1471-
# return ((image.astype(np.float32) / maxg) ** gamma) * maxg
1458+
out = imagef**gamma
1459+
1460+
if np.issubdtype(image.dtype, np.integer):
1461+
# original image was integer, convert back to int
1462+
return int_image(out)
1463+
else:
1464+
return out
14721465

14731466

14741467
def _srgb_inverse(Rg):
14751468
"""
14761469
Inverse sRGB gamma correction
14771470
14781471
:param Rg: 2D image
1479-
:type Rg: numpy array, shape (N,M)
1472+
:type Rg: float ndarray(N,M)
14801473
:return: R
1481-
:rtype: numpy array
1474+
:rtype: float ndarray(N,M)
14821475
14831476
- ``_srgb_imverse(Rg)`` maps an sRGB gamma encoded image to linear
14841477
tristimulus values.
@@ -1698,7 +1691,6 @@ def esttheta(im, sharpen=None):
16981691
"""
16991692

17001693
def pickregion(im):
1701-
17021694
im.disp()
17031695

17041696
clicks = plt.ginput(n=-1)
@@ -1727,7 +1719,6 @@ def pickregion(im):
17271719

17281720

17291721
if __name__ == "__main__": # pragma: no cover
1730-
17311722
import pathlib
17321723
import os.path
17331724

0 commit comments

Comments
 (0)