Skip to content

Add auto-thresholded template matching (Otsu on score map)#376

Merged
JE-Chen merged 2 commits into
devfrom
feat/match-autothresh-batch
Jun 23, 2026
Merged

Add auto-thresholded template matching (Otsu on score map)#376
JE-Chen merged 2 commits into
devfrom
feat/match-autothresh-batch

Conversation

@JE-Chen

@JE-Chen JE-Chen commented Jun 23, 2026

Copy link
Copy Markdown
Member

摘要

新增 match_auto / auto_threshold — 從相關性分數圖自動推導比對門檻,免除手調 min_score。每次 match_template_all 都迫使呼叫者猜 min_score(太低淹沒 NMS、太高漏掉縮放/換膚目標,且因素材與畫面而異)。

本功能對相關性分數直方圖套用 Otsu(而非像素強度,後者是 preprocess.binarize 的做法),找出「背景相關」與「真正匹配」之間的谷,並回傳該門檻加上 separability 分離度,讓呼叫端在直方圖為單峰(無明確匹配)時知道不可信任該門檻。match_auto 透過 connected_boxes 對每個過門檻連通區只回傳單一峰,避免原始像素掃描 + NMS 在寬相關峰上留下的重複命中,並以 floor 夾住門檻。

重用新增的 visual_match._score_map(公開比對器丟棄的完整 matchTemplate 曲面);haystack 可注入;Qt-free。

五層

  • 核心:utils/match_autothresh/auto_thresholdmatch_auto
  • Facade:由 je_auto_control 匯出 + __all__
  • Executor:AC_match_auto({count, matches})/ AC_auto_threshold({found, info})。
  • MCP:ac_match_auto / ac_auto_threshold(read-only)。
  • Script Builder:Match Template (auto-threshold) / Auto Threshold (Otsu on scores)(Image)。
  • 文件:v164 EN + Zh + toctree。更新日誌:root EN + zh-TW + zh-CN。

測試

test/unit_test/headless/test_match_autothresh_batch.py — auto_threshold 指標、雙峰可分離度 > 0.3、match_auto 找到兩個目標、blank 用 floor 擋住雜訊、blank separability < 0.3、wiring + facade。6 passed。ruff / bandit / radon / float-scan / Qt-free 全乾淨。

Every match_template_all call forces a hand-tuned min_score: too low floods
NMS, too high drops re-themed targets, and the right value differs per asset.
Run Otsu on the correlation score histogram to find the valley between
background correlation and real matches, returning that cut-off plus a
separability score so a unimodal (no-match) surface is flagged. match_auto
returns one peak per above-threshold region via connected_boxes.
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 28 complexity · 1 duplication

Metric Results
Complexity 28
Duplication 1

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

ccoeff_normed scores span [-1, 1], so the Otsu cut-off can legitimately be
negative on some OpenCV builds (match_auto clamps it with floor anyway). Assert
the threshold is below a perfect match and use a relative separability check
(bimodal > flat) instead of absolute bounds.
@JE-Chen JE-Chen merged commit c97a23b into dev Jun 23, 2026
16 checks passed
@JE-Chen JE-Chen deleted the feat/match-autothresh-batch branch June 23, 2026 19:27
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant