Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(tiles): start the seam-blend seam at the lowest-energy column (#9602)

Mohammad Hijjawi3eb38cfb1b1c
diff --git a/invokeai/backend/tiles/utils.py b/invokeai/backend/tiles/utils.py
index dc6d914170..2a1519b1dd 100644
--- a/invokeai/backend/tiles/utils.py
+++ b/invokeai/backend/tiles/utils.py
@@ -108,7 +108,7 @@ def seam_blend(ia1: np.ndarray, ia2: np.ndarray, blend_amount: int, x_seam: bool
# create an array max_y long
lowest_energy_line = np.empty([max_y], dtype="uint16")
- lowest_energy_line[max_y - 1] = np.argmin(res[max_y - 1, min_x : max_x - 1])
+ lowest_energy_line[max_y - 1] = np.argmin(res[max_y - 1, min_x:max_x]) + min_x
# Calc the path of the seam
# could offer options for larger search than just 1 pixel by adjusting lpos and rpos
diff --git a/tests/backend/tiles/test_utils.py b/tests/backend/tiles/test_utils.py
index bbef233ca5..08165cd2a1 100644
--- a/tests/backend/tiles/test_utils.py
+++ b/tests/backend/tiles/test_utils.py
@@ -1,7 +1,8 @@
+import cv2
import numpy as np
import pytest
-from invokeai.backend.tiles.utils import TBLR, paste
+from invokeai.backend.tiles.utils import TBLR, paste, seam_blend
def test_paste_no_mask_success():
@@ -99,3 +100,57 @@ def test_paste_mask_does_not_match_src_image():
with pytest.raises(ValueError):
paste(dst_image=dst_image, src_image=src_image, box=box, mask=mask)
+
+
[email protected]("x_seam", [True, False])
+def test_seam_blend_places_seam_in_low_energy_region(x_seam: bool):
+ """Test that the seam follows the region where the two images agree, rather than a column offset from it."""
+ blend_amount = 8
+ rng = np.random.default_rng(0)
+ ia1 = np.zeros((8, 64, 3))
+ # ia2 is noise everywhere except for a band where it matches ia1, so the lowest-energy seam lies in that band.
+ ia2 = rng.uniform(0, 255, (8, 64, 1)).repeat(3, axis=2)
+ ia2[:, 36:52] = 0.0
+ if x_seam:
+ ia1 = ia1.transpose(1, 0, 2)
+ ia2 = ia2.transpose(1, 0, 2)
+
+ blended = seam_blend(ia1, ia2, blend_amount, x_seam=x_seam)
+
+ if x_seam:
+ blended = blended.transpose(1, 0, 2)
+ ia1 = ia1.transpose(1, 0, 2)
+ ia2 = ia2.transpose(1, 0, 2)
+
+ # Outside the band (plus half the blur on either side), each image must be passed through untouched.
+ half_blend = blend_amount // 2
+ np.testing.assert_array_equal(blended[:, : 36 - half_blend], ia1[:, : 36 - half_blend])
+ np.testing.assert_array_equal(blended[:, 52 + half_blend :], ia2[:, 52 + half_blend :])
+
+
[email protected]("x_seam", [True, False])
+def test_seam_blend_can_start_seam_at_last_column_before_gutter(x_seam: bool):
+ """Test that the last column outside the gutter is searched when it holds the unique lowest-energy seam."""
+ blend_amount = 8
+ last_column = 64 - blend_amount // 2 - 1
+ rng = np.random.default_rng(0)
+ ia1 = np.zeros((8, 64, 3))
+ # The images match only around the last column, so it is the only column with zero energy.
+ ia2 = rng.uniform(0, 255, (8, 64, 1)).repeat(3, axis=2)
+ ia2[:, last_column - 1 : last_column + 2] = 0.0
+
+ expected_mask = np.zeros((8, 64))
+ expected_mask[:, :last_column] = 1.0
+ expected_mask = cv2.blur(expected_mask, (blend_amount, blend_amount))[..., np.newaxis]
+ expected = ia1 * expected_mask + ia2 * (1.0 - expected_mask)
+
+ if x_seam:
+ ia1 = ia1.transpose(1, 0, 2)
+ ia2 = ia2.transpose(1, 0, 2)
+
+ blended = seam_blend(ia1, ia2, blend_amount, x_seam=x_seam)
+
+ if x_seam:
+ blended = blended.transpose(1, 0, 2)
+
+ np.testing.assert_allclose(blended, expected)