diff --git a/src/machinevisiontoolbox/ImageMultiview.py b/src/machinevisiontoolbox/ImageMultiview.py index 93b20c3f..1698f65e 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. @@ -65,11 +65,28 @@ 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. .. 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). 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` """ @@ -91,15 +108,20 @@ 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) # 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) @@ -125,9 +147,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) @@ -154,17 +174,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) - - # wherever maxima is nan set disparity to nan, similarity will be - # done for border regions - disparity = np.where(np.isnan(maxima), np.nan, disparity) + maxima = np.max(filled, axis=2) - disparity[:, : drange[0]] = np.nan + # 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) return self.__class__(disparity, dtype=np.float32), self.__class__(maxima), dsi diff --git a/tests/test_image_multiview.py b/tests/test_image_multiview.py index 3ede3402..2a81952c 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,15 +52,68 @@ 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() - - @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", - ) + # 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 disparity, similarity, dsi = result