From 5e01e086f278e8d5f78d19942073edba6205d4aa Mon Sep 17 00:00:00 2001 From: MixBadGun <1059129006@qq.com> Date: Sat, 13 Dec 2025 20:22:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=A2=E7=8A=B6=E6=8F=8F=E8=BE=B9=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- konabot/plugins/fx_process/fx_handle.py | 4 ++++ konabot/plugins/fx_process/math_helper.py | 23 +++++------------------ 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/konabot/plugins/fx_process/fx_handle.py b/konabot/plugins/fx_process/fx_handle.py index 577e535..396e73b 100644 --- a/konabot/plugins/fx_process/fx_handle.py +++ b/konabot/plugins/fx_process/fx_handle.py @@ -1191,6 +1191,10 @@ class ImageFilterImplement: # 创建描边图像 stroke_img = np.zeros_like(img) + + # 如果没有轮廓,直接返回原图 + if not expanded_contours[0].any(): + return image cv2.fillPoly(stroke_img, expanded_contours, ColorHandle.parse_color(stroke_color) + (255,)) # 轮廓图像转为PIL格式 diff --git a/konabot/plugins/fx_process/math_helper.py b/konabot/plugins/fx_process/math_helper.py index ca9521c..2669a5c 100644 --- a/konabot/plugins/fx_process/math_helper.py +++ b/konabot/plugins/fx_process/math_helper.py @@ -9,37 +9,24 @@ def fix_with_shapely(contours: list) -> np.ndarray: """ 使用Shapely库处理复杂自相交 """ - fixed_results = [] + fixed_polygons = [] for contour in contours: # 转换输入为正确的格式 contour_array = contour.reshape(-1, 2) # 转换为Shapely多边形 polygon = Polygon(contour_array) - - # 修复自相交 if not polygon.is_valid: - polygon = polygon.buffer(0) # 修复无效多边形 - - # 提取修复后的轮廓点 - if polygon.geom_type == 'Polygon': - fixed_points = np.array(polygon.exterior.coords, dtype=np.int32) - elif polygon.geom_type == 'MultiPolygon': - # 处理多个多边形 - largest = max(polygon.geoms, key=lambda p: p.area) - fixed_points = np.array(largest.exterior.coords, dtype=np.int32) - - fixed_results.append(fixed_points.reshape(-1, 1, 2)) + polygon = polygon.buffer(0) + fixed_polygons.append(polygon) # 接下来把所有轮廓合并为一个 - if len(fixed_results) > 1: - merged_polygon = unary_union([Polygon(cnt.reshape(-1, 2)) for cnt in fixed_results]) + if len(fixed_polygons) >= 1: + merged_polygon = unary_union(fixed_polygons) if merged_polygon.geom_type == 'Polygon': merged_points = np.array(merged_polygon.exterior.coords, dtype=np.int32) elif merged_polygon.geom_type == 'MultiPolygon': largest = max(merged_polygon.geoms, key=lambda p: p.area) merged_points = np.array(largest.exterior.coords, dtype=np.int32) return [merged_points.reshape(-1, 1, 2)] - elif len(fixed_results) == 1: - return [fixed_results[0]] else: logger.warning("No valid contours found after fixing with Shapely.") return [np.array([], dtype=np.int32).reshape(0, 1, 2)]