From 7324b69e7af6b85109642d20196b490353422917 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Sun, 22 Nov 2026 21:07:13 +1000 Subject: [PATCH] fix: use the primaries argument of shadow_invariant Passing primaries without an angle raised UnboundLocalError, because the projection direction was only worked out inside the branch for primaries=None, where it used the default Sony sensor values and ignored any given. It now works the direction out from whichever primaries are in use, the given ones or the default. Only their ratios matter, so nanometres and metres agree. The xfail test that recorded the bug is now an ordinary test, and a second test checks the default primaries. Co-Authored-By: Claude Sonnet 5.5 --- src/machinevisiontoolbox/base/color.py | 15 +++++++-------- tests/base/test_color_shadow.py | 26 ++++++++++++++++++++------ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/machinevisiontoolbox/base/color.py b/src/machinevisiontoolbox/base/color.py index d930eceb..1cf64a64 100755 --- a/src/machinevisiontoolbox/base/color.py +++ b/src/machinevisiontoolbox/base/color.py @@ -1697,7 +1697,7 @@ def shadow_invariant( :type exp: bool, optional :param sharpen: a sharpening transform, defaults to None :type sharpen: ndarray(3,3), optional - :param primaries: camera peak filter responses (nm), defaults to None + :param primaries: peak wavelengths of the camera filters, in any units since only their ratios matter, defaults to None :type primaries: array_like(3), optional :return: greyscale shadow invariant image :rtype: ndarray(H,W) @@ -1765,16 +1765,15 @@ def shadow_invariant( if θ is None: # compute direction from the spectral peaks of the camera filters if primaries is None: - # Planckian illumination constant - c2 = 1.4388e-2 - # spectral peaks of the Sony ICX204 sensor used in BB2 camera primaries = np.array([610, 538, 460]) * 1e-9 - e_r = -c2 / primaries[0] - e_b = -c2 / primaries[2] - c = np.r_[e_b, -e_r] - c /= np.linalg.norm(c) + # Planckian illumination constant + c2 = 1.4388e-2 + e_r = -c2 / primaries[0] + e_b = -c2 / primaries[2] + c = np.r_[e_b, -e_r] + c /= np.linalg.norm(c) else: # otherwise use the provided angle c = np.array([np.cos(θ), np.sin(θ)]) diff --git a/tests/base/test_color_shadow.py b/tests/base/test_color_shadow.py index c6daafb8..3b366387 100644 --- a/tests/base/test_color_shadow.py +++ b/tests/base/test_color_shadow.py @@ -89,14 +89,28 @@ def test_default_angle_comes_from_the_camera_filters(self): d = np.log(np.stack([rgb[:, 0] / denom, rgb[:, 2] / denom])) nt.assert_allclose(shadow_invariant(im).ravel(), d.T @ c) - @pytest.mark.xfail( - strict=True, - raises=UnboundLocalError, - reason="passing primaries without theta leaves the projection direction unset", - ) def test_primaries_without_an_angle(self): + # the direction is worked out from the filters' peak wavelengths, in + # any units, since only their ratio matters im, _ = synthetic() - shadow_invariant(im, primaries=np.array([600, 540, 450]) * 1e-9) + primaries = np.array([600, 540, 450]) * 1e-9 + out = shadow_invariant(im, primaries=primaries) + c = np.array([-1 / primaries[2], 1 / primaries[0]]) + c /= np.linalg.norm(c) + d = shadow_invariant(im, np.arctan2(c[1], c[0])) + nt.assert_allclose(out, d) + # a different set of peaks gives a different direction + other = shadow_invariant(im, primaries=np.array([650, 540, 420]) * 1e-9) + assert not np.allclose(out, other) + # nanometres give the same answer as metres + nt.assert_allclose(shadow_invariant(im, primaries=primaries * 1e9), out) + + def test_default_primaries_are_the_sony_sensor(self): + im, _ = synthetic() + nt.assert_allclose( + shadow_invariant(im), + shadow_invariant(im, primaries=np.array([610, 538, 460]) * 1e-9), + ) def test_not_a_color_image_raises(self): with pytest.raises(ValueError):