From 22098d9f068e080ec7fb95ea9a46fddc576ce69a Mon Sep 17 00:00:00 2001 From: limbicnation Date: Tue, 19 Aug 2025 02:07:21 +0200 Subject: [PATCH] fix: resolve critical runtime errors in edge detection - Fix IndexError in _combine_edge_maps() when edge_maps list is empty - Fix ZeroDivisionError in _enhanced_sobel_edge_detection() for solid color images - Fix NaN issue in _detect_pixel_art_characteristics() when no edges detected - Make AUTO mode truly automatic by using content detection instead of dither_handling --- background_remover.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/background_remover.py b/background_remover.py index 4a78f33..84aef39 100644 --- a/background_remover.py +++ b/background_remover.py @@ -239,7 +239,11 @@ class EnhancedPixelArtProcessor: magnitude = np.sqrt(sobel_x**2 + sobel_y**2) # Normalize - magnitude = np.clip(magnitude / magnitude.max() * 255, 0, 255).astype(np.uint8) + max_magnitude = magnitude.max() + if max_magnitude > 0: + magnitude = np.clip(magnitude / max_magnitude * 255, 0, 255).astype(np.uint8) + else: + magnitude = magnitude.astype(np.uint8) # Adaptive threshold based on edge sensitivity threshold = int(255 * (1.0 - self.edge_sensitivity) * 0.6) @@ -263,7 +267,7 @@ class EnhancedPixelArtProcessor: def _combine_edge_maps(self, edge_maps: List[np.ndarray]) -> np.ndarray: """Combine multiple edge maps using weighted voting.""" if not edge_maps: - return np.zeros_like(edge_maps[0]) + raise ValueError("Cannot combine an empty list of edge maps.") # Weights: Roberts (sharp edges), Sobel (noise resistance), Canny (completeness) weights = [0.4, 0.35, 0.25] @@ -545,7 +549,7 @@ class EnhancedPixelArtProcessor: gradient_magnitude = np.sqrt(grad_x**2 + grad_y**2) # High gradient values suggest sharp, non-antialiased edges - avg_gradient = np.mean(gradient_magnitude[edges > 0]) + avg_gradient = np.mean(gradient_magnitude[edges > 0]) if edge_pixels > 0 else 0 if avg_gradient > 30: # Sharp edges threshold return True