From b7872474dfb5d3383a54dd714ca6355946cf3773 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Mon, 23 Nov 2026 07:12:23 +1000 Subject: [PATCH 1/3] fix: make stereo_simple return outputs the size of its inputs The window stack is 2*hw larger than the image in each direction, and the outputs kept that padding, against the docstring. Crop the stack to the windows centred on the image's pixels, so the disparity, similarity and disparity space images are the size of the inputs. Co-Authored-By: Claude Sonnet 5.5 --- src/machinevisiontoolbox/ImageMultiview.py | 14 ++++++++++---- tests/test_image_multiview.py | 5 ----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/machinevisiontoolbox/ImageMultiview.py b/src/machinevisiontoolbox/ImageMultiview.py index 93b20c3f..de9ddfac 100644 --- a/src/machinevisiontoolbox/ImageMultiview.py +++ b/src/machinevisiontoolbox/ImageMultiview.py @@ -50,7 +50,7 @@ def stereo_simple( the strength of the stereo match. This is a ZNCC measure where 1 is a perfect match, greater than 0.8 is a decent match. - the disparity space image, a 3D array, whose first two dimensions are - the same size as the input imagesm and whose third dimension is the + the same size as the input images and whose third dimension is the number of disparities. Each fibre DSI[v,u,:] is window similarity versus disparity. @@ -70,6 +70,11 @@ def stereo_simple( .. warning:: Not fast. + .. versionchanged:: 2.5.0 + The outputs are the same size as the input images, as documented. + Previously they were larger by ``2*hw`` in each direction, with + the result for pixel (u, v) at (u+hw, v+hw). + :seealso: :meth:`DSI_refine` :meth:`stereo_BM` :meth:`stereo_SGBM` """ @@ -91,7 +96,10 @@ def window_stack(image, hw): constant_values=np.nan, ) ) - return np.dstack(stack) + # each padded plane is 2*hw larger than the image in each direction, + # keep the part whose windows are centred on the image's pixels + h, w_img = image.shape + return np.dstack(stack)[hw : hw + h, hw : hw + w_img, :] if isinstance(drange, int): drange = (0, drange) @@ -125,9 +133,7 @@ def window_stack(image, hw): # - NaN 0 / 0 invalid value encountered in true_divide # - inf x / 0 divide by zero encountered in true_divide with np.errstate(divide="ignore", invalid="ignore"): - for d in np.arange(drange[1] - drange[0]): # lgtm[py/unused-loop-variable] - # compute the ZNCC sumLL = np.sum(left_arr**2, axis=2) sumRR = np.sum(right_arr**2, axis=2) diff --git a/tests/test_image_multiview.py b/tests/test_image_multiview.py index 3ede3402..1fc1e252 100644 --- a/tests/test_image_multiview.py +++ b/tests/test_image_multiview.py @@ -58,11 +58,6 @@ def test_border_is_nan(self, result): disparity, _, _ = result assert np.isnan(disparity.array[:, :4]).all() - @pytest.mark.xfail( - strict=True, - reason="the docstring says the outputs are the same size as the inputs " - "but the window stack makes them larger by 2*hw in each direction", - ) def test_output_is_the_size_of_the_input(self, pair, result): left, _ = pair disparity, similarity, dsi = result From ded89c1cbf63f038706dbc244be8b04f2b65373b Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Mon, 23 Nov 2026 07:25:07 +1000 Subject: [PATCH 2/3] fix: pad stereo_simple windows with NaN for any image type An integer image was padded with NaN, which numpy casts to 0, so windows beyond the image were treated as valid zero padding, and a float image gave a different result. Convert to float32 first. A NaN similarity at one disparity also made the whole pixel NaN, because np.max propagates NaN, so take the best of the valid disparities and give NaN only to a pixel with none. Co-Authored-By: Claude Sonnet 5.5 --- src/machinevisiontoolbox/ImageMultiview.py | 30 +++++++++++----- tests/test_image_multiview.py | 42 ++++++++++++++++++++-- 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/machinevisiontoolbox/ImageMultiview.py b/src/machinevisiontoolbox/ImageMultiview.py index de9ddfac..9e7f633e 100644 --- a/src/machinevisiontoolbox/ImageMultiview.py +++ b/src/machinevisiontoolbox/ImageMultiview.py @@ -73,7 +73,11 @@ def stereo_simple( .. versionchanged:: 2.5.0 The outputs are the same size as the input images, as documented. Previously they were larger by ``2*hw`` in each direction, with - the result for pixel (u, v) at (u+hw, v+hw). + the result for pixel (u, v) at (u+hw, v+hw). The disparity at a + pixel is now the best of its valid disparities, and NaN only if + none are valid, and an integer image is padded with NaN instead + of 0, so windows that extend beyond the image are invalid for + every image type. :seealso: :meth:`DSI_refine` :meth:`stereo_BM` :meth:`stereo_SGBM` """ @@ -106,8 +110,10 @@ def window_stack(image, hw): # left = self.mono().image.astype(np.float32) # right = right.mono().image.astype(np.float32) - left_arr = self.mono()._A - right_arr = right.mono()._A + # work in float32 for any image type, so that the window stack can be + # padded with NaN (it cannot in an integer array, where it becomes 0) + left_arr = self.mono()._A.astype(np.float32) + right_arr = right.mono()._A.astype(np.float32) # convert to window stacks left_arr = window_stack(left_arr, hw) @@ -160,15 +166,21 @@ def window_stack(image, hw): # stack the similarity images at each disparity into the 3D DSI dsi = np.dstack(similarities) - # disparity is the index of the maxima in the disparity direction - disparity = np.argmax(dsi, axis=2).astype(np.float32) + drange[0] + # disparity is the index of the maxima in the disparity direction. A + # NaN similarity means that disparity is not valid for that pixel, for + # example the window would extend beyond the image, so ignore it. A + # pixel with no valid disparity at all is NaN. + invalid = np.isnan(dsi) + filled = np.where(invalid, -np.inf, dsi) + disparity = np.argmax(filled, axis=2).astype(np.float32) + drange[0] # maxima is the maximum similarity in the disparity direction - maxima = np.max(dsi, axis=2) + maxima = np.max(filled, axis=2) - # wherever maxima is nan set disparity to nan, similarity will be - # done for border regions - disparity = np.where(np.isnan(maxima), np.nan, disparity) + # wherever there is no valid disparity, the disparity and similarity are NaN + none_valid = invalid.all(axis=2) + disparity = np.where(none_valid, np.nan, disparity).astype(np.float32) + maxima = np.where(none_valid, np.nan, maxima) disparity[:, : drange[0]] = np.nan diff --git a/tests/test_image_multiview.py b/tests/test_image_multiview.py index 1fc1e252..f8d063f2 100644 --- a/tests/test_image_multiview.py +++ b/tests/test_image_multiview.py @@ -32,8 +32,6 @@ def interior(a, hw, margin): class TestStereoSimple: - # stereo_simple takes float images here: given a uint8 image it pads with - # NaN, which numpy casts to 0 with a RuntimeWarning. @pytest.fixture(scope="class") def result(self, pair): left, right = pair @@ -54,6 +52,46 @@ def test_dsi_peak_is_at_the_true_disparity(self, result): peak = np.argmax(dsi[10:30, 30:80, :], axis=2) + 4 nt.assert_array_equal(peak, DISPARITY) + def test_uint8_and_float_images_agree(self, pair, result): + # an integer image used to be padded with 0 instead of NaN + import warnings + + left, right = pair + with warnings.catch_warnings(): + warnings.simplefilter("error") + disparity, similarity, dsi = left.stereo_simple(right, hw=2, drange=[4, 12]) + nt.assert_array_equal(disparity.array, result[0].array) + nt.assert_allclose(similarity.array, result[1].array, equal_nan=True) + + def test_windows_beyond_the_image_are_invalid(self, result): + disparity, similarity, dsi = result + # the window centred on a border pixel extends beyond the image + for a in (disparity.array, similarity.array): + assert np.isnan(a[:2, :]).all() and np.isnan(a[-2:, :]).all() + assert np.isnan(a[:, :2]).all() and np.isnan(a[:, -2:]).all() + assert np.isnan(dsi[:2]).all() and np.isnan(dsi[-2:]).all() + + def test_pixel_keeps_its_valid_disparities(self): + # at column 4, disparities up to 2 have a window inside the image, and + # larger ones do not, so the pixel has a disparity and is not NaN + rng = np.random.default_rng(3) + left = Image(rng.integers(0, 255, (20, 40), dtype=np.uint8)) + right = Image(np.roll(left.array, -2, axis=1)) + disparity, similarity, dsi = left.stereo_simple(right, hw=2, drange=[0, 8]) + assert np.isnan(dsi[10, 4, 3:]).all() + assert not np.isnan(dsi[10, 4, :3]).any() + assert disparity.array[10, 4] == 2 + assert similarity.array[10, 4] > 0.99 + + def test_pixel_with_no_valid_disparity_is_nan(self): + rng = np.random.default_rng(3) + left = Image(rng.integers(0, 255, (20, 40), dtype=np.uint8)) + right = Image(np.roll(left.array, -2, axis=1)) + disparity, similarity, _ = left.stereo_simple(right, hw=2, drange=[6, 8]) + # column 4: every disparity in [6, 8) reaches beyond the left edge + assert np.isnan(disparity.array[10, 4]) + assert np.isnan(similarity.array[10, 4]) + def test_border_is_nan(self, result): disparity, _, _ = result assert np.isnan(disparity.array[:, :4]).all() From c1e1c82f37699bd20b653259381e0b84a1f4e6fb Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Mon, 23 Nov 2026 07:31:24 +1000 Subject: [PATCH 3/3] docs: document the NaN border of stereo_simple and test it Add a note on the width of the NaN band in the disparity, similarity and DSI images, remove the now redundant assignment of the left columns, and test the band on every side and in each plane of the DSI. Co-Authored-By: Claude Sonnet 5.5 --- src/machinevisiontoolbox/ImageMultiview.py | 10 ++++++++-- tests/test_image_multiview.py | 22 ++++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/machinevisiontoolbox/ImageMultiview.py b/src/machinevisiontoolbox/ImageMultiview.py index 9e7f633e..1698f65e 100644 --- a/src/machinevisiontoolbox/ImageMultiview.py +++ b/src/machinevisiontoolbox/ImageMultiview.py @@ -65,6 +65,14 @@ def stereo_simple( .. note:: The images are assumed to be epipolar aligned. + .. note:: The disparity and similarity images, and each plane of the DSI, + have a band of ``hw`` NaN valued pixels along the top, bottom and right + edges, where the window extends beyond the image. The band along the + left edge is ``hw + drange[0]`` wide for the disparity and similarity + images, and ``hw + drange[0] + d`` wide for the DSI plane of + disparity ``d`` (counting from 0), since the shifted window in the + right image must also lie within the image. + :references: - |RVC3|, Section 14.4. @@ -182,8 +190,6 @@ def window_stack(image, hw): disparity = np.where(none_valid, np.nan, disparity).astype(np.float32) maxima = np.where(none_valid, np.nan, maxima) - disparity[:, : drange[0]] = np.nan - return self.__class__(disparity, dtype=np.float32), self.__class__(maxima), dsi @classmethod diff --git a/tests/test_image_multiview.py b/tests/test_image_multiview.py index f8d063f2..2a81952c 100644 --- a/tests/test_image_multiview.py +++ b/tests/test_image_multiview.py @@ -93,8 +93,26 @@ def test_pixel_with_no_valid_disparity_is_nan(self): assert np.isnan(similarity.array[10, 4]) def test_border_is_nan(self, result): - disparity, _, _ = result - assert np.isnan(disparity.array[:, :4]).all() + # hw = 2 and drange = [4, 12]: the band is hw on the top, bottom and + # right, and hw + drange[0] on the left + disparity, similarity, dsi = result + for a in (disparity.array, similarity.array): + assert np.isnan(a[:2, :]).all() and np.isnan(a[-2:, :]).all() + assert np.isnan(a[:, -2:]).all() + assert np.isnan(a[:, :6]).all() + # the first column outside the band is valid + assert not np.isnan(a[2:-2, 6]).any() + assert not np.isnan(a[2:-2, -3]).any() + + def test_each_plane_of_the_dsi_has_its_own_left_band(self, result): + _, _, dsi = result + for d in range(dsi.shape[2]): + plane = dsi[:, :, d] + # hw + drange[0] + d = 6 + d + assert np.isnan(plane[:, : 6 + d]).all() + assert not np.isnan(plane[2:-2, 6 + d : -2]).any() + assert np.isnan(plane[:, -2:]).all() + assert np.isnan(plane[:2, :]).all() and np.isnan(plane[-2:, :]).all() def test_output_is_the_size_of_the_input(self, pair, result): left, _ = pair