This commit is contained in:
HuangHai
2026-01-14 10:24:02 +08:00
parent abcdcbf92d
commit 2350355862
3 changed files with 68 additions and 5 deletions

View File

@@ -169,19 +169,22 @@ def detect_cards_cv(image_path, top_ratio=0.40, bottom_ratio=0.12):
# 限制检测范围
top_limit = int(h * top_ratio)
bottom_limit = int(h * (1 - bottom_ratio))
logger.info(f"CV检测卡片: h={h}, w={w}, top_limit={top_limit}, bottom_limit={bottom_limit}")
# 使用自适应阈值
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
# 闭运算:连接断开的边缘
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (w // 2, 1))
# 减小水平核宽度,增加一点垂直连接,以适应卡片边缘不连续的情况
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (w // 4, 3))
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
# 寻找轮廓
contours, _ = cv2.findContours(closed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cards = []
min_card_width = int(w * 0.7)
# 降低最小宽度要求,有些卡片边缘可能没完全闭合
min_card_width = int(w * 0.6)
temp_boxes = []
for cnt in contours:
@@ -189,11 +192,18 @@ def detect_cards_cv(image_path, top_ratio=0.40, bottom_ratio=0.12):
# 增加最小高度验证 MIN_CARD_HEIGHT
if (cw > min_card_width and
ch > MIN_CARD_HEIGHT and
ch > MIN_CARD_HEIGHT * 0.8 and # 稍微放宽高度限制
y > top_limit and
y + ch < bottom_limit):
logger.info(f"✅ 找到候选卡片: y={y}, h={ch}, w={cw}")
temp_boxes.append((x, y, cw, ch))
else:
if cw > min_card_width:
logger.debug(f"❌ 排除候选(不符条件): y={y}, h={ch}, w={cw} (limit: {top_limit}-{bottom_limit}, min_h: {MIN_CARD_HEIGHT*0.8})")
if not temp_boxes:
logger.warning(f"⚠️ CV 未能在指定范围 ({top_limit}-{bottom_limit}) 内检测到任何场站卡片")
# 按 Y 轴排序
temp_boxes.sort(key=lambda b: b[1])