diff --git a/src/stitching_scanner.py b/src/stitching_scanner.py index 2aaf745..865ce02 100644 --- a/src/stitching_scanner.py +++ b/src/stitching_scanner.py @@ -165,15 +165,17 @@ class StitchingScanner: h_base, w_base = base.shape[:2] h_strip, w_strip = strip.shape[:2] - # Clamp y_offset - y_offset = max(0, min(y_offset, h_base - h_strip)) - blend_w = min(blend_width, w_strip, w_base) + # === DEBUG MAGIC NUMBERS === + DEBUG_SHIFT_RIGHT = -15 # Positive = shift strip right + DEBUG_SHIFT_UP = 5 # Positive = shift strip up + # =========================== - - DEBUG_SHIFT_RIGHT = -12 # Positive = shift strip right - DEBUG_SHIFT_UP = -25 # Positive = shift strip up + blend_w = min(blend_width, w_strip, w_base) if append_right: + # Clamp y_offset for append_right (no debug shifts here) + y_offset = max(0, min(y_offset, h_base - h_strip)) + # Expand mosaic to the right result_width = w_base + w_strip - blend_w result = np.zeros((h_base, result_width, 3), dtype=np.uint8) @@ -207,19 +209,40 @@ class StitchingScanner: if x_offset is None: x_offset = 0 - + # Apply debug shifts x_offset = x_offset + DEBUG_SHIFT_RIGHT - y_offset = y_offset - DEBUG_SHIFT_UP - # Clamp x_offset to valid range - x_offset = 0 - min(x_offset, w_base - blend_w) - - # y_offset = max(0, min(y_offset, h_base - h_strip)) - + # Clamp x_offset to valid range + x_offset = max(0, min(x_offset, w_base - blend_w)) + # x_offset = min(x_offset, w_base) + + # Handle strip cropping if y_offset is negative (strip protrudes above frame) + strip_y_start = 0 # How much to crop from top of strip + if y_offset < 0: + strip_y_start = -y_offset # Crop this many rows from top of strip + y_offset = 0 + self.log(f" Cropping {strip_y_start}px from top of strip (DEBUG_SHIFT_UP={DEBUG_SHIFT_UP})") + + # Handle strip cropping if it protrudes below frame + strip_y_end = h_strip + if y_offset + (h_strip - strip_y_start) > h_base: + strip_y_end = h_strip - ((y_offset + (h_strip - strip_y_start)) - h_base) + self.log(f" Cropping bottom of strip, new strip_y_end={strip_y_end}") + + # Get the cropped strip + if strip_y_start >= strip_y_end: + self.log(f" WARNING: Strip fully cropped, nothing to place") + return base.copy() + + cropped_strip = strip[strip_y_start:strip_y_end, :] + h_cropped = cropped_strip.shape[0] + self.log(f"=== _blend_horizontal_at_y (append_left) ===") self.log(f" base: {w_base}x{h_base}, strip: {w_strip}x{h_strip}") self.log(f" x_offset: {x_offset}, y_offset: {y_offset}, blend_w: {blend_w}") + self.log(f" DEBUG: shift_right={DEBUG_SHIFT_RIGHT}, shift_up={DEBUG_SHIFT_UP}") + self.log(f" Strip crop: rows [{strip_y_start}:{strip_y_end}] -> height {h_cropped}") # Result is same size as base (no expansion when going left) result = base.copy() @@ -229,14 +252,14 @@ class StitchingScanner: strip_x_end = min(x_offset + w_strip, w_base) strip_cols_to_copy = strip_x_end - strip_x_start - self.log(f" Placing strip at X={strip_x_start}:{strip_x_end}, Y={y_offset}:{y_offset + h_strip}") + self.log(f" Placing strip at X={strip_x_start}:{strip_x_end}, Y={y_offset}:{y_offset + h_cropped}") self.log(f" Strip cols to copy: {strip_cols_to_copy}") # Step 1: Copy strip content (non-blend portion) at correct position # For LEFT scanning, blend is on the RIGHT side of the strip non_blend_end = strip_cols_to_copy - blend_w if non_blend_end > 0: - result[y_offset:y_offset + h_strip, strip_x_start:strip_x_start + non_blend_end] = strip[:, :non_blend_end] + result[y_offset:y_offset + h_cropped, strip_x_start:strip_x_start + non_blend_end] = cropped_strip[:, :non_blend_end] self.log(f" Step 1: Placed non-blend at X={strip_x_start}:{strip_x_start + non_blend_end}") # Step 2: Create blend on the RIGHT edge of strip (blending with existing content) @@ -245,11 +268,11 @@ class StitchingScanner: if blend_w > 0 and blend_x_start >= strip_x_start: alpha = np.linspace(1, 0, blend_w, dtype=np.float32)[np.newaxis, :, np.newaxis] - strip_overlap = strip[:, -blend_w:].astype(np.float32) - base_overlap = base[y_offset:y_offset + h_strip, blend_x_start:blend_x_end].astype(np.float32) + strip_overlap = cropped_strip[:, -blend_w:].astype(np.float32) + base_overlap = base[y_offset:y_offset + h_cropped, blend_x_start:blend_x_end].astype(np.float32) blended = (strip_overlap * alpha + base_overlap * (1 - alpha)).astype(np.uint8) - result[y_offset:y_offset + h_strip, blend_x_start:blend_x_end] = blended + result[y_offset:y_offset + h_cropped, blend_x_start:blend_x_end] = blended self.log(f" Step 2: Blend zone at X={blend_x_start}:{blend_x_end}") self.log(f" Final: Strip placed at X={strip_x_start}, Y={y_offset}, mosaic size unchanged: {w_base}x{h_base}")