diff --git a/version.md b/version.md index 9bf629d..f1616c5 100644 --- a/version.md +++ b/version.md @@ -21,4 +21,5 @@ # 2.15.8 启动不加载预加载yolo # 2.15.9 20cm # 2.15.10 不保存图片 -# 2.15.11 优化内存 \ No newline at end of file +# 2.15.11 优化内存 +# 2.15.12 优化算法 \ No newline at end of file diff --git a/version.py b/version.py index 46de01c..07953c8 100644 --- a/version.py +++ b/version.py @@ -4,6 +4,6 @@ 应用版本号 每次 OTA 更新时,只需要更新这个文件中的版本号 """ -VERSION = '2.15.11' +VERSION = '2.15.12' diff --git a/vision.py b/vision.py index 556803f..cb3f7b8 100644 --- a/vision.py +++ b/vision.py @@ -570,11 +570,13 @@ def detect_circle_v3(frame, laser_point=None, img_cv=None): # -- 3. 红色掩码:在循环外只算一次 mask_red = cv2.bitwise_or( - cv2.inRange(hsv, np.array([0, 50, 40]), np.array([10, 255, 255])), - cv2.inRange(hsv, np.array([170, 50, 40]), np.array([180, 255, 255])), + cv2.inRange(hsv, np.array([0, 30, 20]), np.array([12, 255, 255])), + cv2.inRange(hsv, np.array([168, 30, 20]), np.array([180, 255, 255])), ) kernel_red = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) mask_red = cv2.morphologyEx(mask_red, cv2.MORPH_CLOSE, kernel_red) + # 再加一次膨胀,加厚环状区域避免碎片化 + mask_red = cv2.dilate(mask_red, kernel_red, iterations=1) contours_red, _ = cv2.findContours(mask_red, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 预先把红色轮廓筛选成 (center, radius) 列表,后续直接查表 red_candidates = [] @@ -583,7 +585,7 @@ def detect_circle_v3(frame, laser_point=None, img_cv=None): if ar <= 10: continue pr = cv2.arcLength(cnt_r, True) - if pr <= 0 or (4 * np.pi * ar) / (pr * pr) <= 0.3: + if pr <= 0 or (4 * np.pi * ar) / (pr * pr) <= 0.2: continue if len(cnt_r) >= 5: (xr, yr), (wr, hr), _ = cv2.fitEllipse(cnt_r) @@ -625,7 +627,11 @@ def detect_circle_v3(frame, laser_point=None, img_cv=None): ddx = yellow_center[0] - rc["center"][0] ddy = yellow_center[1] - rc["center"][1] dist_centers = math.hypot(ddx, ddy) - if dist_centers < yellow_radius * 1.5 and rc["radius"] > yellow_radius * 0.7: + max_dist = yellow_radius * 2.0 + min_r = min(rc["radius"], yellow_radius) + max_r = max(rc["radius"], yellow_radius) + size_ratio = min_r / max_r if max_r > 0 else 0 + if dist_centers < max_dist and size_ratio > 0.5: if logger: logger.info(f"[target] -> 找到匹配的红圈: 黄心({yellow_center}), " f"红心({rc['center']}), 距离:{dist_centers:.1f}, " @@ -638,8 +644,17 @@ def detect_circle_v3(frame, laser_point=None, img_cv=None): }) matched = True break - if not matched and logger: - logger.debug("Debug -> 未找到匹配的红色圆圈,可能是误识别") + if not matched: + # 黄圈高置信度兜底:大且圆时跳过红圈验证 + if area > 300 and circularity > 0.85: + valid_targets.append({ + "center": yellow_center, + "radius": yellow_radius, + "ellipse": yellow_ellipse, + "area": area, + }) + elif logger: + logger.debug("Debug -> 未找到匹配的红色圆圈,可能是误识别") logger.debug(f"[detect_circle_v3] step 4 fin {datetime.now()}")