π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Preserve covered panorama columns after gaps in the mask (#5942)
A Aswanth Raj998f134678c1
diff --git a/changelog.d/5942.fixed.md b/changelog.d/5942.fixed.mdnew file mode 100644index 000000000..66bd8b08a--- /dev/null+++ b/changelog.d/5942.fixed.md@@ -0,0 +1 @@+`ImageStitcher.postprocess` now removes only the empty columns at the right edge of the mask, and keeps every column that has a covered pixel in any sample. It used to cut at the first empty column. A full-height gap in the mask, such as an invalid column in a `stitch_pair` mask or a pair of images that do not overlap, dropped every column after the gap. A first column with no covered pixel made it return the full width, empty columns included. Masks whose covered columns start at the first column and have no gap, such as those `forward` builds for overlapping images, give the same result as before.diff --git a/kornia/contrib/image_stitching.py b/kornia/contrib/image_stitching.pyindex 1303b4e0a..7a3bdf688 100644--- a/kornia/contrib/image_stitching.py+++ b/kornia/contrib/image_stitching.py@@ -154,10 +154,10 @@ class ImageStitcher(nn.Module):"""# NOTE: assumes no batch mode. This method keeps all valid regions after stitching.mask_ = mask.sum((0, 1))- index = int(mask_.bool().any(0).long().argmin().item())+ index = int(mask_.bool().any(0).flip(0).long().argmax().item())if index == 0: # If no redundant spacereturn image- return image[..., :index]+ return image[..., :-index]def on_matcher(self, data: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:"""Run the configured feature matcher on preprocessed inputs.diff --git a/tests/contrib/test_image_stitcher.py b/tests/contrib/test_image_stitcher.pyindex 47a0c7fee..44f9bdcea 100644--- a/tests/contrib/test_image_stitcher.py+++ b/tests/contrib/test_image_stitcher.py@@ -70,6 +70,47 @@ class _ShiftMatcher(LocalFeatureMatcher):class TestImageStitcher(BaseTester):+ @pytest.mark.parametrize(+ "columns, width",+ [+ ([1, 0, 1, 1, 0, 0], 4),+ ([0, 1, 1, 0, 0, 0], 3),+ ([1, 0, 1, 0, 1, 1], 6),+ ([1, 1, 1, 0, 0, 0], 3),+ ([1, 1, 1, 1, 1, 0], 5),+ ([1, 1, 1, 1, 1, 1], 6),+ ([0, 0, 0, 0, 0, 0], 6),+ ],+ )+ def test_postprocess_trims_only_empty_suffix(self, columns, width, device, dtype):+ stitcher = kornia.contrib.ImageStitcher(torch.nn.Identity())+ image = torch.arange(72, device=device, dtype=dtype).reshape(2, 3, 2, 6)+ mask = torch.tensor(columns, device=device, dtype=dtype).reshape(1, 1, 1, 6).expand_as(image)+ image_before, mask_before = image.clone(), mask.clone()++ actual = stitcher.postprocess(image, mask)++ self.assert_close(actual, image[..., :width])+ assert actual.device == image.device+ assert actual.dtype == image.dtype+ self.assert_close(image, image_before)+ self.assert_close(mask, mask_before)++ def test_postprocess_uses_all_mask_pixels(self, device, dtype):+ stitcher = kornia.contrib.ImageStitcher(torch.nn.Identity())+ image = torch.arange(72, device=device, dtype=dtype).reshape(2, 3, 2, 6)+ mask = torch.zeros_like(image, dtype=torch.bool)+ mask[0, 0, 0, 0] = True+ mask[1, 2, 1, 3] = True++ self.assert_close(stitcher.postprocess(image, mask), image[..., :4])++ def test_postprocess_gradcheck(self, device):+ stitcher = kornia.contrib.ImageStitcher(torch.nn.Identity())+ image = torch.rand(1, 1, 2, 6, device=device, dtype=torch.float64, requires_grad=True)+ mask = torch.tensor([1, 0, 1, 1, 0, 0], device=device, dtype=torch.bool).reshape(1, 1, 1, 6)+ self.gradcheck(lambda value: stitcher.postprocess(value, mask), (image,))+@pytest.mark.parametrize("img_dtype", [torch.float32, torch.float16, torch.bfloat16])@pytest.mark.parametrize("shift", [40.0, 40.5, 40.7])def test_subpixel_shift_has_no_dark_seam(self, shift, img_dtype, device):