'commit'
This commit is contained in:
@@ -66,10 +66,7 @@ def get_image_content_md5(file_path, top_ratio=0.1, bottom_ratio=0.1):
|
||||
# 裁剪中间部分
|
||||
content = img[top:bottom, :]
|
||||
|
||||
# 将图片数据转换为字节流计算 MD5
|
||||
success, encoded_img = cv2.imencode(".jpg", content)
|
||||
if success:
|
||||
return hashlib.md5(encoded_img.tobytes()).hexdigest()
|
||||
# 直接使用原始像素数组计算 MD5,避免压缩损失
|
||||
return hashlib.md5(content.tobytes()).hexdigest()
|
||||
|
||||
def read_image(path):
|
||||
@@ -98,93 +95,117 @@ def save_image(path, img):
|
||||
logger.error(f"Error saving image {path}: {e}")
|
||||
return False
|
||||
|
||||
def find_template(image_path, template_name, threshold=0.8):
|
||||
"""
|
||||
在图片中查找模板位置
|
||||
"""
|
||||
img = read_image(image_path)
|
||||
if img is None:
|
||||
return None
|
||||
|
||||
template_path = os.path.join(os.path.dirname(__file__), "Template", template_name)
|
||||
if not os.path.exists(template_path):
|
||||
logger.warning(f"Template not found: {template_path}")
|
||||
return None
|
||||
|
||||
template = read_image(template_path)
|
||||
if template is None:
|
||||
return None
|
||||
|
||||
res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
|
||||
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
|
||||
|
||||
if max_val >= threshold:
|
||||
t_h, t_w = template.shape[:2]
|
||||
center_x = max_loc[0] + t_w // 2
|
||||
center_y = max_loc[1] + t_h // 2
|
||||
return (center_x, center_y)
|
||||
return None
|
||||
|
||||
def detect_cards_cv(image_path, top_ratio=0.15, bottom_ratio=0.1):
|
||||
"""
|
||||
使用计算机图形学 (OpenCV) 检测列表中的场站卡片
|
||||
方案:以 nav.jpg 为锚点,向上/下探测白色卡片边界
|
||||
方案:使用 Canny 边缘检测 + 轮廓查找,提取圆角矩形卡片
|
||||
"""
|
||||
img = read_image(image_path)
|
||||
if img is None:
|
||||
return []
|
||||
|
||||
h, w = img.shape[:2]
|
||||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
# 1. 模板匹配查找锚点 (nav.jpg)
|
||||
template_path = os.path.join(os.path.dirname(__file__), "Template", "nav.jpg")
|
||||
anchors = []
|
||||
if os.path.exists(template_path):
|
||||
template = read_image(template_path)
|
||||
if template is not None:
|
||||
res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
|
||||
threshold = 0.8
|
||||
loc = np.where(res >= threshold)
|
||||
|
||||
# 去重记录锚点中心
|
||||
t_h, t_w = template.shape[:2]
|
||||
matched_points = []
|
||||
for pt in zip(*loc[::-1]): # (x, y)
|
||||
is_duplicate = False
|
||||
for mx, my in matched_points:
|
||||
if abs(mx - pt[0]) < 50 and abs(my - pt[1]) < 50:
|
||||
is_duplicate = True
|
||||
break
|
||||
if not is_duplicate:
|
||||
matched_points.append(pt)
|
||||
anchors.append((pt[0] + t_w // 2, pt[1] + t_h // 2))
|
||||
|
||||
logger.info(f"通过模板匹配找到 {len(anchors)} 个锚点")
|
||||
|
||||
# 2. 基于锚点向上/下扩张探测边界
|
||||
# 计算每一行的平均亮度,用于探测背景
|
||||
row_means = np.mean(gray[:, int(w*0.2):int(w*0.8)], axis=1)
|
||||
# 1. 预处理:转灰度 -> 高斯模糊去噪
|
||||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
|
||||
|
||||
# 2. Canny 边缘检测
|
||||
# 阈值设置:降低阈值以适应低对比度边缘 (如浅灰背景上的白卡片)
|
||||
# 经过测试,(10, 50) 对于浅灰背景上的白卡片效果最佳
|
||||
edges = cv2.Canny(blurred, 10, 50)
|
||||
|
||||
# 3. 查找轮廓
|
||||
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||||
|
||||
cards = []
|
||||
for ax, ay in anchors:
|
||||
# 向上探测:直到亮度低于 245 (进入灰色背景)
|
||||
y1 = ay
|
||||
while y1 > h * top_ratio:
|
||||
if row_means[int(y1)] < 245:
|
||||
break
|
||||
y1 -= 1
|
||||
min_card_width = int(w * 0.8) # 卡片宽度通常占屏幕 80% 以上
|
||||
min_card_height = 150 # 卡片高度至少 150px
|
||||
top_limit = int(h * top_ratio)
|
||||
bottom_limit = int(h * (1 - bottom_ratio))
|
||||
|
||||
valid_contours = []
|
||||
|
||||
for cnt in contours:
|
||||
x, y, cw, ch = cv2.boundingRect(cnt)
|
||||
|
||||
# 向下探测:直到亮度低于 245
|
||||
y2 = ay
|
||||
while y2 < h * (1 - bottom_ratio):
|
||||
if row_means[int(y2)] < 245:
|
||||
break
|
||||
y2 += 1
|
||||
# 筛选条件:
|
||||
# 1. 宽度足够大 (排除小图标、文字)
|
||||
# 2. 高度适中
|
||||
# 3. 位置在安全区域内 (排除顶部导航和底部 TabBar)
|
||||
if (cw > min_card_width and
|
||||
ch > min_card_height and
|
||||
y > top_limit and
|
||||
y + ch < bottom_limit):
|
||||
|
||||
# 验证卡片高度是否合理
|
||||
if y2 - y1 > 150:
|
||||
# 给 y 坐标增加一点 padding
|
||||
cards.append([int(w * 0.05), int(y1 + 5), int(w * 0.95), int(y2 - 5)])
|
||||
|
||||
# 3. 兜底方案:如果模板匹配没找到,使用之前的投影法
|
||||
if not cards:
|
||||
logger.warning("模板匹配未找到卡片,尝试投影法兜底...")
|
||||
_, binary = cv2.threshold(gray, 250, 255, cv2.THRESH_BINARY)
|
||||
top = int(h * top_ratio)
|
||||
bottom = int(h * (1 - bottom_ratio))
|
||||
roi_binary = binary[:, int(w*0.2):int(w*0.8)]
|
||||
row_white_counts = np.sum(roi_binary == 255, axis=1)
|
||||
threshold_count = (int(w*0.8) - int(w*0.2)) * 0.9
|
||||
is_card_row = row_white_counts > threshold_count
|
||||
|
||||
start_y = None
|
||||
for y in range(top, bottom):
|
||||
if is_card_row[y]:
|
||||
if start_y is None: start_y = y
|
||||
else:
|
||||
if start_y is not None:
|
||||
if y - start_y > 150:
|
||||
cards.append([int(w * 0.05), start_y + 10, int(w * 0.95), y - 10])
|
||||
start_y = None
|
||||
if start_y is not None and bottom - start_y > 150:
|
||||
cards.append([int(w * 0.05), start_y + 10, int(w * 0.95), bottom - 10])
|
||||
|
||||
# 4. 排序
|
||||
# 简单去重:检查是否与已有的卡片高度重叠
|
||||
is_duplicate = False
|
||||
center_y = y + ch // 2
|
||||
for vx, vy, vcw, vch in valid_contours:
|
||||
v_center_y = vy + vch // 2
|
||||
if abs(center_y - v_center_y) < 50: # 中心点接近,视为同一个
|
||||
is_duplicate = True
|
||||
break
|
||||
|
||||
if not is_duplicate:
|
||||
valid_contours.append((x, y, cw, ch))
|
||||
# 增加一点 padding,收缩一下边界,避免点到边缘
|
||||
padding = 5
|
||||
cards.append([
|
||||
max(0, x + padding),
|
||||
max(0, y + padding),
|
||||
min(w, x + cw - padding),
|
||||
min(h, y + ch - padding)
|
||||
])
|
||||
|
||||
# 按 y 坐标排序
|
||||
cards.sort(key=lambda c: c[1])
|
||||
|
||||
# 如果没找到,尝试稍微放宽条件兜底 (比如宽度 > 60%)
|
||||
if not cards:
|
||||
logger.warning("严格轮廓检测未找到卡片,尝试放宽条件...")
|
||||
for cnt in contours:
|
||||
x, y, cw, ch = cv2.boundingRect(cnt)
|
||||
if (cw > int(w * 0.6) and ch > 100 and y > top_limit and y + ch < bottom_limit):
|
||||
# 简单去重
|
||||
center_y = y + ch // 2
|
||||
is_duplicate = False
|
||||
for existing in cards:
|
||||
ec_y = (existing[1] + existing[3]) // 2
|
||||
if abs(center_y - ec_y) < 50:
|
||||
is_duplicate = True
|
||||
break
|
||||
if not is_duplicate:
|
||||
cards.append([x, y, x+cw, y+ch])
|
||||
cards.sort(key=lambda c: c[1])
|
||||
|
||||
logger.info(f"最终检测到 {len(cards)} 个场站卡片区域")
|
||||
return cards
|
||||
|
||||
|
||||
Reference in New Issue
Block a user