Skip to content

perf: avoid masked matchTemplate when no mask is needed#1317

Open
weypro wants to merge 1 commit into
MaaXYZ:mainfrom
weypro:dev/matchtemplatenew
Open

perf: avoid masked matchTemplate when no mask is needed#1317
weypro wants to merge 1 commit into
MaaXYZ:mainfrom
weypro:dev/matchtemplatenew

Conversation

@weypro
Copy link
Copy Markdown

@weypro weypro commented May 8, 2026

由 Sourcery 提供的总结

通过在掩码实际不会起作用时跳过带 mask 的 matchTemplate 调用来优化模板匹配,并基于现有测试数据集为 TemplateMatcher 引入专门的性能与正确性测试可执行程序。

新特性:

  • 添加可选的性能测试目标和 TemplateMatcherPerformance 可执行文件,用于对模板匹配行为以及 OpenCV 使用情况进行基准测试。
  • 添加 TemplateMatcherCorrectness 可执行文件,用于将 TemplateMatcher 的结果与原生 OpenCV matchTemplate 的预期结果进行对比验证。

增强:

  • 当启用了绿色掩码但实际生成的是一个“完全激活”的掩码时,不再构造并传递 mask 给 cv::matchTemplate,而是回退到无 mask 的重载版本。

构建:

  • 引入 BUILD_PERFORMANCE_TESTING 选项,将 templatematcher 测试和共享的 TestingDataSet 子目录接入构建系统,并在启用流水线测试时强制启用 WITH_DBG_CONTROLLER

测试:

  • 为以 TemplateMatcher 为重点的测试可执行文件添加可复用的 CMake 配置和共享源码。
  • 添加基于数据集的性能与正确性探针,在真实负载和各种掩码配置下对 TemplateMatcher 进行验证与压测。
Original summary in English

Summary by Sourcery

Optimize template matching by skipping masked matchTemplate calls when the mask would be effectively unused, and introduce dedicated performance and correctness test binaries for TemplateMatcher using the existing test dataset.

New Features:

  • Add optional performance testing target and TemplateMatcherPerformance executable to benchmark template matching behavior and OpenCV usage.
  • Add TemplateMatcherCorrectness executable to validate TemplateMatcher results against raw OpenCV matchTemplate expectations.

Enhancements:

  • Avoid constructing and passing a mask to cv::matchTemplate when green masking is enabled but produces a fully active mask, falling back to the unmasked overload instead.

Build:

  • Introduce BUILD_PERFORMANCE_TESTING option, wire templatematcher tests and shared TestingDataSet subdirectory into the build, and enforce WITH_DBG_CONTROLLER when pipeline testing is enabled.

Tests:

  • Add reusable CMake configuration and shared sources for TemplateMatcher-focused test executables.
  • Add dataset-based performance and correctness probes to exercise TemplateMatcher under realistic workloads and mask configurations.

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你好——我在这里给出了一些总体反馈:

  • TemplateMatcherPerformance.cpp 和 TemplateMatcherCorrectness.cpp 都重新实现了若干辅助函数(例如 default_testset_dir、has_pipeline_smoking_dataset、list_png_files、create_green_mask_like_framework、make_real_data_green_mask_template);建议将这些函数抽取到一个共享的测试工具源码中,以避免重复实现,并保持行为一致。
  • TemplateMatcher 测试可执行文件在 template_matcher_common_src 中直接列出了 TemplateMatcher.cpp、VisionBase.cpp 和 OptionMgr.cpp,而不是链接到已有的 MaaFramework 目标;将它们改为链接到该库目标可以降低 ODR/编译选项分歧的风险,并让测试自动与框架的构建设置保持同步。
给 AI Agent 的提示
Please address the comments from this code review:

## Overall Comments
- TemplateMatcherPerformance.cpp 和 TemplateMatcherCorrectness.cpp 都重新实现了若干辅助函数(例如 default_testset_dir、has_pipeline_smoking_dataset、list_png_files、create_green_mask_like_framework、make_real_data_green_mask_template);建议将这些函数抽取到一个共享的测试工具源码中,以避免重复实现,并保持行为一致。
- TemplateMatcher 测试可执行文件在 template_matcher_common_src 中直接列出了 TemplateMatcher.cpp、VisionBase.cpp 和 OptionMgr.cpp,而不是链接到已有的 MaaFramework 目标;将它们改为链接到该库目标可以降低 ODR/编译选项分歧的风险,并让测试自动与框架的构建设置保持同步。

Sourcery 对开源项目是免费的——如果你喜欢我们的评审,请考虑分享 ✨
帮我变得更有用!请在每条评论上点击 👍 或 👎,我会根据你的反馈改进后续评审。
Original comment in English

Hey - I've left some high level feedback:

  • Both TemplateMatcherPerformance.cpp and TemplateMatcherCorrectness.cpp reimplement several helpers (e.g., default_testset_dir, has_pipeline_smoking_dataset, list_png_files, create_green_mask_like_framework, make_real_data_green_mask_template); consider factoring these into a shared test utility source to avoid duplication and keep behavior consistent.
  • The TemplateMatcher test executables list TemplateMatcher.cpp, VisionBase.cpp, and OptionMgr.cpp directly in template_matcher_common_src instead of linking against the existing MaaFramework target; wiring them to the library target would reduce the risk of ODR/flag divergence and keep the tests automatically in sync with framework build settings.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Both TemplateMatcherPerformance.cpp and TemplateMatcherCorrectness.cpp reimplement several helpers (e.g., default_testset_dir, has_pipeline_smoking_dataset, list_png_files, create_green_mask_like_framework, make_real_data_green_mask_template); consider factoring these into a shared test utility source to avoid duplication and keep behavior consistent.
- The TemplateMatcher test executables list TemplateMatcher.cpp, VisionBase.cpp, and OptionMgr.cpp directly in template_matcher_common_src instead of linking against the existing MaaFramework target; wiring them to the library target would reduce the risk of ODR/flag divergence and keep the tests automatically in sync with framework build settings.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@MistEO
Copy link
Copy Markdown
Member

MistEO commented May 10, 2026

🤖 感谢!核心 perf 修复(绕过 all-ones mask 走 unmasked overload)没问题,收益也实在。不过这 PR 其实塞了三样东西,能拆一下吗?

  1. perf 修复单开一个 PR,就 TemplateMatcher.cpp 那两行改动,这个我们可以很快合。
  2. 性能/正确性测试另开 PR,顺便处理几个问题:
    • template_matcher_common_src.cpp 当源码直接列进 test target 不太行,ODR 风险 + 编译选项发散(那个 -UMAA_DEBUG 就是症状),建议抽 OBJECT library 或者直接 link MaaFramework 目标
    • 两个 test cpp 里 default_testset_dir / list_png_files / make_real_data_green_mask_template 重复了,抽共享 header
    • Performance.cpp 里很多 probe 是调试产物,留最有代表性的一两个就行,这块主仓库维护成本高
  3. BUILD_PIPELINE_TESTING 强依赖 WITH_DBG_CONTROLLER 跟前两个没关系,单开或者撤掉

@weypro weypro force-pushed the dev/matchtemplatenew branch from 72ef426 to 655ed2f Compare May 10, 2026 17:01
Copy link
Copy Markdown
Member

@MistEO MistEO left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 单文件这版干净,全 1 mask bypass 没毛病,LGTM

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.

2 participants