#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ x图标模板匹配阈值优化测试脚本 测试不同阈值参数对模板匹配结果的影响 """ import sys import os import cv2 import numpy as np from pathlib import Path import glob from collections import defaultdict # 设置UTF-8编码 if sys.platform == 'win32': sys.stdout.reconfigure(encoding='utf-8') # 测试图片路径(自动扫描debug_captures文件夹) DEBUG_CAPTURES_DIR = r"pic/debug_captures" TEMPLATE_PATH = r"assets/x.png" # ROI坐标(基于1080x2400分辨率) ROI_X1, ROI_Y1 = 972, 147 ROI_X2, ROI_Y2 = 1047, 216 def load_image(image_path): """加载图片(支持中文路径)""" if not os.path.exists(image_path): raise FileNotFoundError(f"图片不存在: {image_path}") # 使用numpy读取,支持中文路径 img_array = np.fromfile(image_path, dtype=np.uint8) img = cv2.imdecode(img_array, cv2.IMREAD_GRAYSCALE) if img is None: raise ValueError(f"无法读取图片: {image_path}") return img def crop_roi(img, x1, y1, x2, y2): """裁剪ROI区域""" h, w = img.shape[:2] # 如果图片尺寸不是1080x2400,需要缩放坐标 if w != 1080 or h != 2400: scale_x = w / 1080.0 scale_y = h / 2400.0 x1 = int(x1 * scale_x) y1 = int(y1 * scale_y) x2 = int(x2 * scale_x) y2 = int(y2 * scale_y) roi = img[y1:y2, x1:x2] return roi def test_fixed_threshold_binary(roi_img, template, threshold_values): """测试固定阈值二值化的不同阈值""" results = [] for thresh in threshold_values: try: _, roi_bin = cv2.threshold(roi_img, thresh, 255, cv2.THRESH_BINARY) _, tpl_bin = cv2.threshold(template, thresh, 255, cv2.THRESH_BINARY) # 确保模板尺寸匹配 th, tw = tpl_bin.shape[:2] rh, rw = roi_bin.shape[:2] if th > rh or tw > rw: scale = min(rh / max(th, 1), rw / max(tw, 1)) tpl_bin = cv2.resize(tpl_bin, (max(1, int(tw * scale)), max(1, int(th * scale)))) res = cv2.matchTemplate(roi_bin, tpl_bin, cv2.TM_CCOEFF_NORMED) _min_val, max_val, _min_loc, _max_loc = cv2.minMaxLoc(res) results.append(("固定阈值二值化", thresh, float(max_val or 0.0))) except Exception as e: results.append(("固定阈值二值化", thresh, 0.0)) return results def test_adaptive_threshold(roi_img, template, block_sizes, c_values): """测试自适应二值化的不同参数""" results = [] for block_size in block_sizes: for c in c_values: try: roi_bin = cv2.adaptiveThreshold( roi_img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, block_size, c ) tpl_bin = cv2.adaptiveThreshold( template, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, block_size, c ) # 确保模板尺寸匹配 th, tw = tpl_bin.shape[:2] rh, rw = roi_bin.shape[:2] if th > rh or tw > rw: scale = min(rh / max(th, 1), rw / max(tw, 1)) tpl_bin = cv2.resize(tpl_bin, (max(1, int(tw * scale)), max(1, int(th * scale)))) res = cv2.matchTemplate(roi_bin, tpl_bin, cv2.TM_CCOEFF_NORMED) _min_val, max_val, _min_loc, _max_loc = cv2.minMaxLoc(res) results.append(("自适应二值化", f"blockSize={block_size}, C={c}", float(max_val or 0.0))) except Exception as e: results.append(("自适应二值化", f"blockSize={block_size}, C={c}", 0.0)) return results def test_canny_edges(roi_img, template, low_thresholds, high_thresholds): """测试Canny边缘检测的不同阈值组合""" results = [] for low in low_thresholds: for high in high_thresholds: if high <= low: continue try: roi_edges = cv2.Canny(roi_img, low, high) tpl_edges = cv2.Canny(template, low, high) # 确保模板尺寸匹配 th, tw = tpl_edges.shape[:2] rh, rw = roi_edges.shape[:2] if th > rh or tw > rw: scale = min(rh / max(th, 1), rw / max(tw, 1)) tpl_edges = cv2.resize(tpl_edges, (max(1, int(tw * scale)), max(1, int(th * scale)))) res = cv2.matchTemplate(roi_edges, tpl_edges, cv2.TM_CCOEFF_NORMED) _min_val, max_val, _min_loc, _max_loc = cv2.minMaxLoc(res) results.append(("Canny边缘检测", f"low={low}, high={high}", float(max_val or 0.0))) except Exception as e: results.append(("Canny边缘检测", f"low={low}, high={high}", 0.0)) return results def test_template_methods(roi_img, template, method_name="固定阈值二值化", thresh=180): """测试不同的模板匹配方法""" # 预处理 _, roi_bin = cv2.threshold(roi_img, thresh, 255, cv2.THRESH_BINARY) _, tpl_bin = cv2.threshold(template, thresh, 255, cv2.THRESH_BINARY) # 确保模板尺寸匹配 th, tw = tpl_bin.shape[:2] rh, rw = roi_bin.shape[:2] if th > rh or tw > rw: scale = min(rh / max(th, 1), rw / max(tw, 1)) tpl_bin = cv2.resize(tpl_bin, (max(1, int(tw * scale)), max(1, int(th * scale)))) methods = [ ("TM_CCOEFF_NORMED", cv2.TM_CCOEFF_NORMED), ("TM_CCORR_NORMED", cv2.TM_CCORR_NORMED), ("TM_SQDIFF_NORMED", cv2.TM_SQDIFF_NORMED), ] results = [] for name, method in methods: try: res = cv2.matchTemplate(roi_bin, tpl_bin, method) if method == cv2.TM_SQDIFF_NORMED: _min_val, _max_val, min_loc, _max_loc = cv2.minMaxLoc(res) score = 1.0 - float(_min_val or 0.0) # SQDIFF越小越好,转换为越大越好 else: _min_val, max_val, _min_loc, _max_loc = cv2.minMaxLoc(res) score = float(max_val or 0.0) results.append((name, score)) except Exception as e: results.append((name, 0.0)) return results def test_advanced_preprocessing(roi_img, template): """测试高级预处理方法""" results = [] # 1. 反转图像 + 二值化 try: roi_inv = 255 - roi_img tpl_inv = 255 - template _, roi_bin_inv = cv2.threshold(roi_inv, 180, 255, cv2.THRESH_BINARY) _, tpl_bin_inv = cv2.threshold(tpl_inv, 180, 255, cv2.THRESH_BINARY) th, tw = tpl_bin_inv.shape[:2] rh, rw = roi_bin_inv.shape[:2] if th > rh or tw > rw: scale = min(rh / max(th, 1), rw / max(tw, 1)) tpl_bin_inv = cv2.resize(tpl_bin_inv, (max(1, int(tw * scale)), max(1, int(th * scale)))) res = cv2.matchTemplate(roi_bin_inv, tpl_bin_inv, cv2.TM_CCOEFF_NORMED) _min_val, max_val, _min_loc, _max_loc = cv2.minMaxLoc(res) results.append(("反转+二值化", "thresh=180", float(max_val or 0.0))) except Exception: results.append(("反转+二值化", "thresh=180", 0.0)) # 2. 直方图均衡化 try: roi_eq = cv2.equalizeHist(roi_img) tpl_eq = cv2.equalizeHist(template) th, tw = tpl_eq.shape[:2] rh, rw = roi_eq.shape[:2] if th > rh or tw > rw: scale = min(rh / max(th, 1), rw / max(tw, 1)) tpl_eq = cv2.resize(tpl_eq, (max(1, int(tw * scale)), max(1, int(th * scale)))) res = cv2.matchTemplate(roi_eq, tpl_eq, cv2.TM_CCOEFF_NORMED) _min_val, max_val, _min_loc, _max_loc = cv2.minMaxLoc(res) results.append(("直方图均衡化", "", float(max_val or 0.0))) except Exception: results.append(("直方图均衡化", "", 0.0)) # 3. 对比度增强(CLAHE) try: clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) roi_clahe = clahe.apply(roi_img) tpl_clahe = clahe.apply(template) th, tw = tpl_clahe.shape[:2] rh, rw = roi_clahe.shape[:2] if th > rh or tw > rw: scale = min(rh / max(th, 1), rw / max(tw, 1)) tpl_clahe = cv2.resize(tpl_clahe, (max(1, int(tw * scale)), max(1, int(th * scale)))) res = cv2.matchTemplate(roi_clahe, tpl_clahe, cv2.TM_CCOEFF_NORMED) _min_val, max_val, _min_loc, _max_loc = cv2.minMaxLoc(res) results.append(("CLAHE对比度增强", "clipLimit=2.0", float(max_val or 0.0))) except Exception: results.append(("CLAHE对比度增强", "clipLimit=2.0", 0.0)) # 4. 高斯模糊 + 边缘检测 try: roi_blur = cv2.GaussianBlur(roi_img, (3, 3), 0) tpl_blur = cv2.GaussianBlur(template, (3, 3), 0) roi_edges = cv2.Canny(roi_blur, 50, 150) tpl_edges = cv2.Canny(tpl_blur, 50, 150) th, tw = tpl_edges.shape[:2] rh, rw = roi_edges.shape[:2] if th > rh or tw > rw: scale = min(rh / max(th, 1), rw / max(tw, 1)) tpl_edges = cv2.resize(tpl_edges, (max(1, int(tw * scale)), max(1, int(th * scale)))) res = cv2.matchTemplate(roi_edges, tpl_edges, cv2.TM_CCOEFF_NORMED) _min_val, max_val, _min_loc, _max_loc = cv2.minMaxLoc(res) results.append(("高斯模糊+边缘检测", "blur(3,3), canny(50,150)", float(max_val or 0.0))) except Exception: results.append(("高斯模糊+边缘检测", "blur(3,3), canny(50,150)", 0.0)) # 5. 形态学操作(开运算) try: _, roi_bin = cv2.threshold(roi_img, 180, 255, cv2.THRESH_BINARY) _, tpl_bin = cv2.threshold(template, 180, 255, cv2.THRESH_BINARY) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) roi_morph = cv2.morphologyEx(roi_bin, cv2.MORPH_OPEN, kernel) tpl_morph = cv2.morphologyEx(tpl_bin, cv2.MORPH_OPEN, kernel) th, tw = tpl_morph.shape[:2] rh, rw = roi_morph.shape[:2] if th > rh or tw > rw: scale = min(rh / max(th, 1), rw / max(tw, 1)) tpl_morph = cv2.resize(tpl_morph, (max(1, int(tw * scale)), max(1, int(th * scale)))) res = cv2.matchTemplate(roi_morph, tpl_morph, cv2.TM_CCOEFF_NORMED) _min_val, max_val, _min_loc, _max_loc = cv2.minMaxLoc(res) results.append(("形态学开运算", "kernel(3,3)", float(max_val or 0.0))) except Exception: results.append(("形态学开运算", "kernel(3,3)", 0.0)) # 6. 形态学操作(闭运算) try: _, roi_bin = cv2.threshold(roi_img, 180, 255, cv2.THRESH_BINARY) _, tpl_bin = cv2.threshold(template, 180, 255, cv2.THRESH_BINARY) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) roi_morph = cv2.morphologyEx(roi_bin, cv2.MORPH_CLOSE, kernel) tpl_morph = cv2.morphologyEx(tpl_bin, cv2.MORPH_CLOSE, kernel) th, tw = tpl_morph.shape[:2] rh, rw = roi_morph.shape[:2] if th > rh or tw > rw: scale = min(rh / max(th, 1), rw / max(tw, 1)) tpl_morph = cv2.resize(tpl_morph, (max(1, int(tw * scale)), max(1, int(th * scale)))) res = cv2.matchTemplate(roi_morph, tpl_morph, cv2.TM_CCOEFF_NORMED) _min_val, max_val, _min_loc, _max_loc = cv2.minMaxLoc(res) results.append(("形态学闭运算", "kernel(3,3)", float(max_val or 0.0))) except Exception: results.append(("形态学闭运算", "kernel(3,3)", 0.0)) # 7. 原始图像(作为基准) try: th, tw = template.shape[:2] rh, rw = roi_img.shape[:2] if th > rh or tw > rw: scale = min(rh / max(th, 1), rw / max(tw, 1)) template_scaled = cv2.resize(template, (max(1, int(tw * scale)), max(1, int(th * scale)))) else: template_scaled = template res = cv2.matchTemplate(roi_img, template_scaled, cv2.TM_CCOEFF_NORMED) _min_val, max_val, _min_loc, _max_loc = cv2.minMaxLoc(res) results.append(("原始图像", "", float(max_val or 0.0))) except Exception: results.append(("原始图像", "", 0.0)) return results def find_test_images(): """查找所有直播间图标测试图片""" pattern = os.path.join(DEBUG_CAPTURES_DIR, "**", "*直播间图标*.png") images = glob.glob(pattern, recursive=True) return images def test_single_image(image_path, template): """测试单张图片的所有方法,返回结果字典""" results = {} try: # 加载图片 test_img = load_image(image_path) # 裁剪ROI roi_img = crop_roi(test_img, ROI_X1, ROI_Y1, ROI_X2, ROI_Y2) # 确保模板尺寸匹配 th, tw = template.shape[:2] rh, rw = roi_img.shape[:2] template_scaled = template.copy() if th > rh or tw > rw: scale = min(rh / max(th, 1), rw / max(tw, 1)) template_scaled = cv2.resize(template_scaled, (max(1, int(tw * scale)), max(1, int(th * scale)))) # 1. 测试固定阈值二值化 threshold_values = list(range(100, 250, 10)) for thresh in threshold_values: try: _, roi_bin = cv2.threshold(roi_img, thresh, 255, cv2.THRESH_BINARY) _, tpl_bin = cv2.threshold(template_scaled, thresh, 255, cv2.THRESH_BINARY) th_t, tw_t = tpl_bin.shape[:2] rh_t, rw_t = roi_bin.shape[:2] if th_t > rh_t or tw_t > rw_t: scale = min(rh_t / max(th_t, 1), rw_t / max(tw_t, 1)) tpl_bin = cv2.resize(tpl_bin, (max(1, int(tw_t * scale)), max(1, int(th_t * scale)))) res = cv2.matchTemplate(roi_bin, tpl_bin, cv2.TM_CCOEFF_NORMED) _min_val, max_val, _min_loc, _max_loc = cv2.minMaxLoc(res) key = f"固定阈值二值化_{thresh}" results[key] = float(max_val or 0.0) except: results[f"固定阈值二值化_{thresh}"] = 0.0 # 2. 测试自适应二值化 block_sizes = [3, 5, 7, 9, 11, 15] c_values = [0, 1, 2, 3, 5] for block_size in block_sizes: for c in c_values: try: roi_bin = cv2.adaptiveThreshold( roi_img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, block_size, c ) tpl_bin = cv2.adaptiveThreshold( template_scaled, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, block_size, c ) th_t, tw_t = tpl_bin.shape[:2] rh_t, rw_t = roi_bin.shape[:2] if th_t > rh_t or tw_t > rw_t: scale = min(rh_t / max(th_t, 1), rw_t / max(tw_t, 1)) tpl_bin = cv2.resize(tpl_bin, (max(1, int(tw_t * scale)), max(1, int(th_t * scale)))) res = cv2.matchTemplate(roi_bin, tpl_bin, cv2.TM_CCOEFF_NORMED) _min_val, max_val, _min_loc, _max_loc = cv2.minMaxLoc(res) key = f"自适应二值化_bs{block_size}_c{c}" results[key] = float(max_val or 0.0) except: results[f"自适应二值化_bs{block_size}_c{c}"] = 0.0 # 3. 测试Canny边缘检测 low_thresholds = [20, 30, 40, 50, 60] high_thresholds = [60, 80, 100, 120, 150, 180] for low in low_thresholds: for high in high_thresholds: if high <= low: continue try: roi_edges = cv2.Canny(roi_img, low, high) tpl_edges = cv2.Canny(template_scaled, low, high) th_t, tw_t = tpl_edges.shape[:2] rh_t, rw_t = roi_edges.shape[:2] if th_t > rh_t or tw_t > rw_t: scale = min(rh_t / max(th_t, 1), rw_t / max(tw_t, 1)) tpl_edges = cv2.resize(tpl_edges, (max(1, int(tw_t * scale)), max(1, int(th_t * scale)))) res = cv2.matchTemplate(roi_edges, tpl_edges, cv2.TM_CCOEFF_NORMED) _min_val, max_val, _min_loc, _max_loc = cv2.minMaxLoc(res) key = f"Canny边缘检测_low{low}_high{high}" results[key] = float(max_val or 0.0) except: results[f"Canny边缘检测_low{low}_high{high}"] = 0.0 # 4. 测试高级预处理方法 advanced_results = test_advanced_preprocessing(roi_img, template_scaled) for method, params, score in advanced_results: key = f"{method}_{params}" if params else method results[key] = score # 5. 测试原始图像 try: res = cv2.matchTemplate(roi_img, template_scaled, cv2.TM_CCOEFF_NORMED) _min_val, max_val, _min_loc, _max_loc = cv2.minMaxLoc(res) results["原始图像"] = float(max_val or 0.0) except: results["原始图像"] = 0.0 except Exception as e: print(f" 处理图片失败: {os.path.basename(image_path)} - {e}") return {} return results def main(): print("=" * 80) print("x图标模板匹配阈值优化测试(多图片批量测试)") print("=" * 80) print() # 查找所有测试图片 print(f"扫描测试图片目录: {DEBUG_CAPTURES_DIR}") test_images = find_test_images() print(f"找到 {len(test_images)} 张直播间图标图片") if len(test_images) == 0: print("未找到测试图片!") return # 加载模板 print(f"\n加载模板: {TEMPLATE_PATH}") try: template = load_image(TEMPLATE_PATH) print(f" 模板尺寸: {template.shape[1]}x{template.shape[0]}") except Exception as e: print(f" 错误: {e}") return print("\n" + "=" * 80) print("开始批量测试...") print("=" * 80) # 统计所有方法在所有图片上的分数 method_scores = defaultdict(list) # {method_name: [score1, score2, ...]} # 测试每张图片 for i, image_path in enumerate(test_images, 1): print(f"\n[{i}/{len(test_images)}] 测试: {os.path.basename(image_path)}") results = test_single_image(image_path, template) # 收集分数 for method, score in results.items(): method_scores[method].append(score) # 计算平均分数 print("\n" + "=" * 80) print("【统计结果】所有方法的平均分数") print("=" * 80) avg_scores = [] for method, scores in method_scores.items(): avg_score = sum(scores) / len(scores) if scores else 0.0 max_score = max(scores) if scores else 0.0 min_score = min(scores) if scores else 0.0 avg_scores.append((method, avg_score, max_score, min_score, len(scores))) # 按平均分数排序 avg_scores.sort(key=lambda x: x[1], reverse=True) print(f"{'排名':<6} {'方法':<35} {'平均分':<10} {'最高分':<10} {'最低分':<10} {'图片数':<8} {'状态':<10}") print("-" * 100) for i, (method, avg_score, max_score, min_score, count) in enumerate(avg_scores[:30], 1): # 显示前30名 status = "✓ 通过" if avg_score >= 0.72 else "✗ 未通过" print(f"{i:<6} {method:<35} {avg_score:<10.4f} {max_score:<10.4f} {min_score:<10.4f} {count:<8} {status:<10}") # 推荐配置 print("\n" + "=" * 80) print("【推荐配置】") print("=" * 80) if avg_scores: best_method, best_avg, best_max, best_min, best_count = avg_scores[0] print(f"最佳方法: {best_method}") print(f"平均分数: {best_avg:.4f}") print(f"最高分数: {best_max:.4f}") print(f"最低分数: {best_min:.4f}") print(f"测试图片数: {best_count}") print(f"是否通过阈值(0.72): {'是' if best_avg >= 0.72 else '否'}") # 解析最佳方法参数 if "固定阈值二值化" in best_method: thresh = best_method.split("_")[-1] print(f"\n推荐配置:") print(f" 方法: 固定阈值二值化") print(f" 阈值: {thresh}") print(f" 模板匹配方法: TM_CCOEFF_NORMED") elif "自适应二值化" in best_method: parts = best_method.split("_") bs = parts[1].replace("bs", "") c = parts[2].replace("c", "") print(f"\n推荐配置:") print(f" 方法: 自适应二值化") print(f" blockSize: {bs}") print(f" C: {c}") print(f" 模板匹配方法: TM_CCOEFF_NORMED") elif "Canny边缘检测" in best_method: parts = best_method.split("_") low = parts[1].replace("low", "") high = parts[2].replace("high", "") print(f"\n推荐配置:") print(f" 方法: Canny边缘检测") print(f" low: {low}") print(f" high: {high}") print(f" 模板匹配方法: TM_CCOEFF_NORMED") else: print(f"\n推荐配置:") print(f" 方法: {best_method}") print(f" 模板匹配方法: TM_CCOEFF_NORMED") print("\n测试完成!") if __name__ == "__main__": main() """ x图标模板匹配阈值优化测试脚本 测试不同阈值参数对模板匹配结果的影响 """ import sys import os import cv2 import numpy as np from pathlib import Path import glob from collections import defaultdict # 设置UTF-8编码 if sys.platform == 'win32': sys.stdout.reconfigure(encoding='utf-8') # 测试图片路径(自动扫描debug_captures文件夹) DEBUG_CAPTURES_DIR = r"pic/debug_captures" TEMPLATE_PATH = r"assets/x.png" # ROI坐标(基于1080x2400分辨率) ROI_X1, ROI_Y1 = 972, 147 ROI_X2, ROI_Y2 = 1047, 216