From fec8da5b443e73e8006eadb6d5c1f344ba600400 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Sun, 22 Nov 2026 21:09:57 +1000 Subject: [PATCH 1/2] fix: allow a minimum disparity of zero in stereo_simple stereo_simple shifted the right image with right_arr[:, :-drange[0]], which for a minimum disparity of 0 is right_arr[:, :-0], an empty slice, so any range starting at zero (drange=[0, 12], or an int, which becomes (0, n)) failed with a broadcast error. The slice now uses the width minus the minimum, which is right for zero as well. The xfail test that recorded the bug is replaced by tests of ranges starting at zero, one of which checks it agrees with a larger minimum. Co-Authored-By: Claude Sonnet 5.5 --- src/machinevisiontoolbox/ImageMultiview.py | 2 +- tests/test_image_multiview.py | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/machinevisiontoolbox/ImageMultiview.py b/src/machinevisiontoolbox/ImageMultiview.py index 54d8bca2..38bd758c 100644 --- a/src/machinevisiontoolbox/ImageMultiview.py +++ b/src/machinevisiontoolbox/ImageMultiview.py @@ -109,7 +109,7 @@ def window_stack(image, hw): right_arr = right_arr - right_arr.mean(axis=2)[..., np.newaxis] # shift right image to the right - right_arr = right_arr[:, : -drange[0], :] + right_arr = right_arr[:, : right_arr.shape[1] - drange[0], :] right_arr = np.pad( right_arr, ((0, 0), (drange[0], 0), (0, 0)), diff --git a/tests/test_image_multiview.py b/tests/test_image_multiview.py index 3bd72e2c..8bc8c0fd 100644 --- a/tests/test_image_multiview.py +++ b/tests/test_image_multiview.py @@ -70,18 +70,24 @@ def test_output_is_the_size_of_the_input(self, pair, result): assert similarity.shape == left.shape assert dsi.shape[:2] == left.shape - @pytest.mark.xfail( - strict=True, - raises=ValueError, - reason="an integer drange becomes (0, n) and the slice right_arr[:, :-0] " - "is then empty, so the arrays cannot be multiplied", - ) - def test_integer_range(self, pair): + @pytest.mark.parametrize("drange", [[0, 12], (0, 12)]) + def test_zero_minimum_disparity(self, pair, drange): + # a minimum of zero used to give the empty slice right_arr[:, :-0] left, right = pair left = Image(left.array.astype(np.float32)) right = Image(right.array.astype(np.float32)) - disparity, _, _ = left.stereo_simple(right, hw=2, drange=12) + disparity, similarity, dsi = left.stereo_simple(right, hw=2, drange=drange) + assert dsi.shape[2] == 12 nt.assert_array_equal(disparity.array[10:30, 30:80], DISPARITY) + nt.assert_allclose(similarity.array[10:30, 30:80], 1.0, atol=1e-5) + + def test_zero_minimum_agrees_with_a_larger_one(self, pair): + left, right = pair + left = Image(left.array.astype(np.float32)) + right = Image(right.array.astype(np.float32)) + a, _, _ = left.stereo_simple(right, hw=2, drange=[0, 12]) + b, _, _ = left.stereo_simple(right, hw=2, drange=[4, 12]) + nt.assert_array_equal(a.array[10:30, 30:80], b.array[10:30, 30:80]) class TestDSIRefine: From ab1e433509977c2e08d10afd7e50d9541eb0993b Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Sun, 22 Nov 2026 21:10:24 +1000 Subject: [PATCH 2/2] docs: document the integer disparity range of the stereo methods stereo_simple, stereo_BM and stereo_SGBM already accept a single integer n for drange, meaning (0, n), but it was in neither the signature nor the :type:. Add it to both, say what it means, and test it in stereo_simple. Co-Authored-By: Claude Sonnet 5.5 --- src/machinevisiontoolbox/ImageMultiview.py | 21 ++++++++++++--------- tests/test_image_multiview.py | 10 ++++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/machinevisiontoolbox/ImageMultiview.py b/src/machinevisiontoolbox/ImageMultiview.py index 38bd758c..93b20c3f 100644 --- a/src/machinevisiontoolbox/ImageMultiview.py +++ b/src/machinevisiontoolbox/ImageMultiview.py @@ -25,7 +25,7 @@ class ImageMultiviewMixin(ImageMixinBase): # ======================= stereo ================================== # def stereo_simple( - self, right: Image, hw: int, drange: tuple[int, int] | list[int] + self, right: Image, hw: int, drange: tuple[int, int] | list[int] | int ) -> tuple[Self, Self, np.ndarray]: """ Simple stereo matching @@ -34,8 +34,9 @@ def stereo_simple( :type right: :class:`Image` :param hw: window half width :type hw: int - :param drange: disparity range - :type drange: array_like(2) + :param drange: disparity range (minimum, maximum), or a single integer + ``n`` which means ``(0, n)`` + :type drange: array_like(2) or int :return: disparity image, similarity image, disparity space image :rtype: :class:`Image`, :class:`Image`, ndarray(H,W,D) @@ -233,7 +234,7 @@ def stereo_BM( self, right: Image, hw: int, - drange: tuple[int, int] | list[int], + drange: tuple[int, int] | list[int] | int, speckle: tuple[int, int] | None = None, ) -> Self: """ @@ -243,8 +244,9 @@ def stereo_BM( :type right: :class:`Image` :param hw: window half width :type hw: int - :param drange: disparity range - :type drange: array_like(2) + :param drange: disparity range (minimum, maximum), or a single integer + ``n`` which means ``(0, n)`` + :type drange: array_like(2) or int :param speckle: speckle filter parameters, defaults to None :type speckle: array_like(2), optional :raises ValueError: block size too small @@ -313,7 +315,7 @@ def stereo_SGBM( self, right: Image, hw: int, - drange: tuple[int, int] | list[int], + drange: tuple[int, int] | list[int] | int, speckle: tuple[int, int] | None = None, ) -> Self: """ @@ -323,8 +325,9 @@ def stereo_SGBM( :type right: :class:`Image` :param hw: window half width :type hw: int - :param drange: disparity range - :type drange: array_like(2) + :param drange: disparity range (minimum, maximum), or a single integer + ``n`` which means ``(0, n)`` + :type drange: array_like(2) or int :param speckle: speckle filter parameters, defaults to None :type speckle: array_like(2), optional :raises ValueError: block size too small diff --git a/tests/test_image_multiview.py b/tests/test_image_multiview.py index 8bc8c0fd..3ede3402 100644 --- a/tests/test_image_multiview.py +++ b/tests/test_image_multiview.py @@ -81,6 +81,16 @@ def test_zero_minimum_disparity(self, pair, drange): nt.assert_array_equal(disparity.array[10:30, 30:80], DISPARITY) nt.assert_allclose(similarity.array[10:30, 30:80], 1.0, atol=1e-5) + def test_integer_range_means_zero_to_n(self, pair): + left, right = pair + left = Image(left.array.astype(np.float32)) + right = Image(right.array.astype(np.float32)) + a, sa, dsia = left.stereo_simple(right, hw=2, drange=12) + b, sb, dsib = left.stereo_simple(right, hw=2, drange=[0, 12]) + nt.assert_array_equal(a.array, b.array) + nt.assert_array_equal(dsia, dsib) + nt.assert_array_equal(a.array[10:30, 30:80], DISPARITY) + def test_zero_minimum_agrees_with_a_larger_one(self, pair): left, right = pair left = Image(left.array.astype(np.float32))