tuples are valid inputs

This commit is contained in:
Matt Terry
2013-08-05 12:43:42 -07:00
parent dc692dddcc
commit efe51c92a9
3 changed files with 42 additions and 1 deletions
+1
View File
@@ -1136,6 +1136,7 @@ def _prepare_lab_array(arr):
Arrays must be in floating point and have at least 3 elements in
last dimension. Return a new array.
"""
arr = np.asarray(arr)
shape = arr.shape
assert shape[-1] >= 3
return dtype.img_as_float(arr, force_copy=True)
+17 -1
View File
@@ -45,6 +45,8 @@ def deltaE_cie76(lab1, lab2):
.. [2] A. R. Robertson, "The CIE 1976 color-difference formulae,"
Color Res. Appl. 2, 7-11 (1977).
"""
lab1 = np.asarray(lab1)
lab2 = np.asarray(lab2)
L1, a1, b1 = np.rollaxis(lab1, -1)[:3]
L2, a2, b2 = np.rollaxis(lab2, -1)[:3]
return np.sqrt((L2 - L1) ** 2 + (a2 - a1) ** 2 + (b2 - b1) ** 2)
@@ -99,6 +101,8 @@ def deltaE_ciede94(lab1, lab2, kH=1, kC=1, kL=1, k1=0.045, k2=0.015):
.. [1] http://en.wikipedia.org/wiki/Color_difference
.. [2] http://www.brucelindbloom.com/index.html?Eqn_DeltaE_CIE94.html
"""
lab1 = np.asarray(lab1)
lab2 = np.asarray(lab2)
L1, C1, h1 = np.rollaxis(lab2lch(lab1), -1)[:3]
L2, C2, h2 = np.rollaxis(lab2lch(lab2), -1)[:3]
@@ -156,6 +160,15 @@ def deltaE_ciede2000(lab1, lab2, kL=1, kC=1, kH=1):
color metrics tested with an accurate color-difference tolerance
dataset," Appl. Opt. 33, 8069-8077 (1994).
"""
lab1 = np.asarray(lab1)
lab2 = np.asarray(lab2)
unroll = False
if lab1.ndim == 1 and lab2.ndim == 1:
unroll = True
if lab1.ndim == 1:
lab1 = lab1[None, :]
if lab2.ndim == 1:
lab2 = lab2[None, :]
L1, a1, b1 = np.rollaxis(lab1, -1)[:3]
L2, a2, b2 = np.rollaxis(lab2, -1)[:3]
@@ -227,7 +240,10 @@ def deltaE_ciede2000(lab1, lab2, kL=1, kC=1, kH=1):
dE2 += C_term ** 2
dE2 += H_term ** 2
dE2 += R_term
return np.sqrt(dE2)
ans = np.sqrt(dE2)
if unroll:
ans = ans[0]
return ans
def deltaE_cmc(lab1, lab2, kL=1, kC=1):
+24
View File
@@ -138,6 +138,30 @@ def test_cmc():
assert_allclose(dE2, oracle, rtol=1.e-8)
def test_single_color_cie76():
lab1 = (0.5, 0.5, 0.5)
lab2 = (0.4, 0.4, 0.4)
deltaE_cie76(lab1, lab2)
def test_single_color_cidede94():
lab1 = (0.5, 0.5, 0.5)
lab2 = (0.4, 0.4, 0.4)
deltaE_ciede94(lab1, lab2)
def test_single_color_ciede2000():
lab1 = (0.5, 0.5, 0.5)
lab2 = (0.4, 0.4, 0.4)
deltaE_ciede2000(lab1, lab2)
def test_single_color_cmc():
lab1 = (0.5, 0.5, 0.5)
lab2 = (0.4, 0.4, 0.4)
deltaE_cmc(lab1, lab2)
if __name__ == "__main__":
from numpy.testing import run_module_suite
run_module_suite()