diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index 38eab995..123e547c 100644 --- a/skimage/color/colorconv.py +++ b/skimage/color/colorconv.py @@ -379,8 +379,14 @@ def xyz2rgb(xyz): >>> lena_xyz = rgb2xyz(lena) >>> lena_rgb = xyz2rgb(lena_xyz) """ - return _convert(rgb_from_xyz, xyz) - + # Follow the algorithm from http://www.easyrgb.com/index.php?X=MATH&H=01#text1 + # except we don't multiply/divide by 100 in the conversion + arr = _prepare_colorarray(xyz) + arr = _convert(rgb_from_xyz, arr) + mask = arr>0.0031308 + arr[mask] = 1.055 * np.power(arr[mask], 1/2.4) - 0.055 + arr[~mask] *= 12.92 + return arr def rgb2xyz(rgb): """RGB to XYZ color space conversion. @@ -415,7 +421,13 @@ def rgb2xyz(rgb): >>> lena = data.lena() >>> lena_xyz = rgb2xyz(lena) """ - return _convert(xyz_from_rgb, rgb) + # Follow the algorithm from http://www.easyrgb.com/index.php?X=MATH&H=02#text2 + # except we don't multiply/divide by 100 in the conversion + arr = _prepare_colorarray(rgb).copy() + mask = arr>0.04045 + arr[mask] = np.power((arr[mask]+0.055)/1.055, 2.4) + arr[~mask] /= 12.92 + return _convert(xyz_from_rgb, arr) def rgb2rgbcie(rgb): @@ -594,7 +606,7 @@ def xyz2lab(xyz): >>> lena_xyz = rgb2xyz(lena) >>> lena_lab = xyz2lab(lena_xyz) """ - arr = _prepare_colorarray(xyz) + arr = _prepare_colorarray(xyz).copy() # scale by CIE XYZ tristimulus values of the reference white point arr = arr / lab_ref_white diff --git a/skimage/color/tests/test_colorconv.py b/skimage/color/tests/test_colorconv.py index c09c9d22..d1edf47f 100644 --- a/skimage/color/tests/test_colorconv.py +++ b/skimage/color/tests/test_colorconv.py @@ -113,10 +113,14 @@ class TestColorconv(TestCase): # XYZ to RGB def test_xyz2rgb_conversion(self): - # only roundtrip test, we checked rgb2xyz above already assert_almost_equal(xyz2rgb(rgb2xyz(self.colbars_array)), self.colbars_array) + # RGB<->XYZ roundtrip on another image + def test_xyz_rgb_roundtrip(self): + img_rgb = img_as_float(self.img_rgb) + assert_array_almost_equal(xyz2rgb(rgb2xyz(img_rgb)), img_rgb) + # RGB to RGB CIE def test_rgb2rgbcie_conversion(self): gt = np.array([[[ 0.1488856 , 0.18288098, 0.19277574],