From 7c371e16623c1e0fb96e43e573ee72a8abc9c43f Mon Sep 17 00:00:00 2001 From: spotter Date: Fri, 8 Mar 2013 11:48:07 +0100 Subject: [PATCH] FIX: img_as_ubyte function threw ValueError due to floating point errors in hed2rgb function. Clipping the output of the hed2rgb function between 0 and 1 seems to correct the bug without affecting the conversion quality. --- skimage/color/colorconv.py | 4 +++- skimage/color/tests/test_colorconv.py | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index 8f44880c..73beba14 100644 --- a/skimage/color/colorconv.py +++ b/skimage/color/colorconv.py @@ -807,7 +807,9 @@ def hed2rgb(hed): >>> ihc_hed = rgb2hed(ihc) >>> ihc_rgb = hed2rgb(ihc_hed) """ + from ..exposure import rescale_intensity + hed = dtype.img_as_float(hed) logrgb1 = np.dot(-np.reshape(hed, (-1, 3)), rgb_from_hed) rgb1 = np.exp(logrgb1) - return np.reshape(rgb1 - 1, hed.shape) + return rescale_intensity(np.reshape(rgb1 - 1, hed.shape), in_range=(0, 1)) diff --git a/skimage/color/tests/test_colorconv.py b/skimage/color/tests/test_colorconv.py index 3171028d..ddbfa594 100644 --- a/skimage/color/tests/test_colorconv.py +++ b/skimage/color/tests/test_colorconv.py @@ -16,7 +16,7 @@ import os.path import numpy as np from numpy.testing import * -from skimage import img_as_float +from skimage import img_as_float, img_as_ubyte from skimage.io import imread from skimage.color import ( rgb2hsv, hsv2rgb, @@ -125,7 +125,7 @@ class TestColorconv(TestCase): # RGB<->HED roundtrip with ubyte image def test_hed_rgb_roundtrip(self): img_rgb = self.img_rgb - assert_array_almost_equal(hed2rgb(rgb2hed(img_rgb)), img_as_float(img_rgb)) + assert_equal(img_as_ubyte(hed2rgb(rgb2hed(img_rgb))), img_rgb) # RGB<->HED roundtrip with float image def test_hed_rgb_float_roundtrip(self):