Skip to content

Feat/outpost filter#50

Closed
heyeuu wants to merge 6 commits into
mainfrom
feat/outpost-filter
Closed

Feat/outpost filter#50
heyeuu wants to merge 6 commits into
mainfrom
feat/outpost-filter

Conversation

@heyeuu
Copy link
Copy Markdown
Member

@heyeuu heyeuu commented May 18, 2026

功能: 哨兵绿灯检测与装甲过滤

概述

本 PR 引入基于绿灯检测的装甲过滤机制(对 OUTPOST 与 BASE 两类均生效),在识别流程中定位绿灯并按 Y 坐标阈值剔除与绿灯重叠或位于绿灯上方的装甲,配套新增检测/定位模块与可视化工具,调整若干配置参数与运行时展示逻辑。

核心改动

1. 标识符模块重构

  • 修改返回类型:Identifier::sync_identify(const Image&)std::optional<std::vector<Armor2D>> 改为 std::optional<Identifier::Result>
  • 新增 Identifier::Result:包含 Armor2Ds armors 以及可选的 outpost_green_lightbase_green_light(均为 std::optional<cv::Rect2i>)。
  • 识别流程更改:先通过现有装甲检测获取全部装甲,再按 DeviceId 分组(OUTPOST / BASE),分别调用 GreenLightLocator 定位绿灯;若某类别有绿灯结果则计算阈值 threshold_y = green_light.y + green_light.height,并在最终返回前剔除 center.y < threshold_y 的对应类别装甲。

2. 绿灯检测模块(新增)

  • 新增类 GreenLightDetection(实现与初始化),提供接口 initialize(const YAML::Node&)sync_detect(const Image&, const cv::Rect2i&) -> std::optional<cv::Rect2i>
  • 检测逻辑:在 ROI 上提取绿色通道 → 二值化(green_threshold)→ 闭运算 → 轮廓筛选(min_area、min_circularity、max_aspect_ratio)→ 返回面积最大的候选矩形。
  • 配置校验:对阈值与形状约束做范围校验,不满足返回错误。

3. 绿灯定位器(新增)

  • 新增类 GreenLightLocator,封装对 GreenLightDetection 的使用并实现自适应 ROI 计算。
  • ROI 生成:根据输入装甲集合的四点计算外接矩形并按固定像素扩张形成检测 ROI;在 ROI 有效且图像可用时调用 GreenLightDetection::sync_detect 获取绿灯矩形并返回 Result { std::optional<cv::Rect2i> green_light }
  • 在 Identifier 初始化阶段会从 YAML 的 green_light_filter 节点初始化该定位器。

4. 可视化与文本绘制(新增)

  • 新增 rmcs::util::draw_green_light(Image&, const cv::Rect2i&) 在图像上绘制绿灯矩形与标签(noexcept)。
  • 新增 rmcs::util::draw_text(Image&, const std::string&),实现带阴影/描边的文本渲染(用于显示攻击状态)。
  • 运行时集成:在 src/runtime.cpp 当使用涂装图像且识别结果有效时,会绘制 result->armors 并按 outpost_green_light / base_green_light 的可用性叠加绿灯标识;调用 tracker 之前将使用 result->armors;在火控求解后若启用可视化会绘制 "ATTACK""IDLE" 文本。

5. 配置与参数调整

  • capturer.fixed_framerate: true → false
  • 新增 green_light_filter 配置块(包含 green_threshold、min_area、min_circularity、max_aspect_ratio 等)
  • fire_control.initial_bullet_speed: 21.4 → 23.2
  • fire_control.aim_point_chooser 中关于 OUTPOST 的角度参数:
    • outpost_coming_angle: 70.0 → 60.0
    • outpost_leaving_angle: 30.0 → 25.0

(注:raw diff 中为 green_light_filter,而非此前摘要中的 outpost_green_light_filter

6. 其他小改动

  • ArmorFilter::Impl::filter 额外排除 DeviceId::UNKNOWN(之前仅排除 INFANTRY_5)。
  • 新增/修改的头文件与依赖:identifier.hpp 引入 与 cv::Rect2i,新增 green_light_detection.hpp / green_light_locator.hpp / utility image 文本/绿灯头文件与实现等。
  • 若绿灯在 OUTPOST 与 BASE 都不存在,则保留全部检测装甲(不做过滤)。

代码审查复杂度

  • 高:Identifier 接口与行为变更(identifier.cpp / identifier.hpp)
  • 中等:新增 green_light_detection / green_light_locator 实现、运行时集成、可视化工具、配置调整
  • 低:ArmorFilter 小改动、绘制工具头文件

注意事项

  • PR 评论指出“AI 率较高,建议赛后逐步合入”(来自 creeper5820)。
  • 需关注接口返回类型变更对调用方的影响(尤其是 sync_identify 的签名变化与 Result 结构)。

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 18, 2026

Caution

Review failed

Pull request was closed or merged during review

Walkthrough

该 PR 新增绿灯检测与定位模块,更新 Identifier 返回契约以按 OUTPOST/BASE 绿灯底边过滤装甲,补充绘制与文本工具,在 runtime 可视化绿灯与 ATTACK/IDLE 状态,并调整配置与追踪过滤规则。

Changes

绿灯装甲识别与过滤

Layer / File(s) Summary
绿灯检测核心实现
src/module/identifier/green_light_detection.hpp, src/module/identifier/green_light_detection.cpp
新增 GreenLightDetection,从 YAML 加载阈值并校验;在 ROI 内提取绿色通道、阈值化、形态学处理、轮廓筛选并返回面积最大的矩形。
绿灯定位与 ROI 计算
src/module/identifier/green_light_locator.hpp, src/module/identifier/green_light_locator.cpp
新增 GreenLightLocator:基于装甲四点并集扩张为检测 ROI,裁剪至图像并委托 GreenLightDetection 执行检测,返回可选 green_light。
识别器契约与集成
src/kernel/identifier.hpp, src/kernel/identifier.cpp
Identifier 中新增公有 Result(含 armorsoutpost_green_lightbase_green_light),更新 sync_identify 返回类型;初始化 GreenLightLocator 并在识别流程中按对应绿灯底边阈值过滤 OUTPOST/BASE 装甲。
绘制工具与可视化
src/utility/image/green_light.hpp, src/utility/image/green_light.cpp, src/utility/image/text.hpp, src/utility/image/text.cpp
新增 draw_green_light 在矩形处绘制绿色边框与标签;新增 draw_text 在图像上绘制带描边的文本。
运行时集成与追踪器扩展
src/runtime.cpp, src/module/tracker/armor_filter.cpp
runtime 在 use_painted_image 分支渲染 result->armors 并叠加 outpost/base 绿灯,新增 ATTACK/IDLE 文本渲染;将跟踪器过滤输入改为 result->armorsArmorFilter 新增排除 UNKNOWN 装甲。
配置参数调整
config/config.yaml
capturer.fixed_framerate 设为 false,新增 green_light_filter 配置块(green_thresholdmin_areamin_circularitymax_aspect_ratio 等),将 fire_control.initial_bullet_speed 调整为 23.2,并将 OUTPOST 的 coming/leaving 角度更新为 60.0/25.0

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested reviewers

  • creeper5820

Poem

兔兔眼里绿灯亮,🌿
画框跳跃显形堂;
检测定位链成线,
识别过滤不迷茫,
小兔鼓掌庆新装。 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR标题'Feat/outpost filter'与本次改动的主要目的相关——新增绿灯检测与定位功能,用于过滤OUTPOST/BASE装甲,但标题表述过于简洁且未充分反映完整改动范围。
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/outpost-filter

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@heyeuu heyeuu added the enhancement New feature or request label May 18, 2026
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (1)
config/config.yaml (1)

54-59: ⚡ Quick win

建议为绿灯过滤增加显式开关位。

outpost_green_light_filter 目前只有阈值参数,建议增加 enabled 布尔开关,方便赛场快速回滚/AB 对比,避免只能通过改阈值“间接关闭”功能。

建议改动
   outpost_green_light_filter:
+    enabled: true
     green_threshold: 120
     min_area: 20
     min_circularity: 0.6
     max_aspect_ratio: 1.5
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@config/config.yaml` around lines 54 - 59, Add an explicit boolean switch
"enabled" to the outpost_green_light_filter config (e.g.,
outpost_green_light_filter.enabled: true) so the feature can be toggled without
changing thresholds (green_threshold, min_area, min_circularity,
max_aspect_ratio); then update all code paths that apply the green-light filter
to first check this flag (where the filter is referenced/used) and skip
filtering when enabled is false. Ensure the default is true to preserve current
behavior and include validation/typing in the config loader so missing values
fall back to the default.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/module/identifier/green_light_armor_filter.cpp`:
- Around line 22-31: The early returns in filter (function filter) skip updating
the detector state, so green_light() can yield stale rectangles across frames;
call a clear method on the detector at start of filter (add and use
GreenLightDetection::clear() on green_light_detection) before any early-return
paths (i.e., before checking armors.empty() and before compute_detect_roi early
return), and also ensure clear() is invoked when
green_light_detection.sync_detect(...) returns no value so the detector state is
reset consistently instead of preserving last-frame results.

In `@src/runtime.cpp`:
- Line 215: 当前在绘制文本的调用 util::draw_text(*image, command.should_shoot ? "ATTACK" :
"IDLE"); 未受 use_painted_image 开关约束导致即使关闭叠加仍修改图像并增加开销;请将该调用包裹在与其它叠加绘制相同的条件判断中(例如
if (use_painted_image) { util::draw_text(...); }),保持对 command.should_shoot
的现有用法并确保与其它 overlay 绘制代码使用相同的开关变量和位置以避免重复绘制/性能回退。

In `@src/utility/image/green_light.cpp`:
- Around line 11-17: The functions draw_green_light and draw_text are marked
noexcept but call OpenCV functions (cv::rectangle, cv::putText, etc.) that can
throw cv::Exception; remove the noexcept specifier from the draw_green_light
definition in green_light.cpp and from the draw_text definition in text.cpp so
OpenCV exceptions can propagate normally (or alternatively explicitly mark as
noexcept(false) if you prefer), ensuring no noexcept remains on these function
signatures.

---

Nitpick comments:
In `@config/config.yaml`:
- Around line 54-59: Add an explicit boolean switch "enabled" to the
outpost_green_light_filter config (e.g., outpost_green_light_filter.enabled:
true) so the feature can be toggled without changing thresholds
(green_threshold, min_area, min_circularity, max_aspect_ratio); then update all
code paths that apply the green-light filter to first check this flag (where the
filter is referenced/used) and skip filtering when enabled is false. Ensure the
default is true to preserve current behavior and include validation/typing in
the config loader so missing values fall back to the default.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 92fe4d0c-6462-4a5c-9704-ae6492294abe

📥 Commits

Reviewing files that changed from the base of the PR and between d835419 and eb94910.

📒 Files selected for processing (13)
  • config/config.yaml
  • src/kernel/identifier.cpp
  • src/kernel/identifier.hpp
  • src/module/identifier/green_light_armor_filter.cpp
  • src/module/identifier/green_light_armor_filter.hpp
  • src/module/identifier/green_light_detection.cpp
  • src/module/identifier/green_light_detection.hpp
  • src/module/tracker/armor_filter.cpp
  • src/runtime.cpp
  • src/utility/image/green_light.cpp
  • src/utility/image/green_light.hpp
  • src/utility/image/text.cpp
  • src/utility/image/text.hpp

Comment thread src/module/identifier/green_light_armor_filter.cpp Outdated
Comment thread src/runtime.cpp Outdated
Comment on lines +11 to +17
auto draw_green_light(Image& canvas, const cv::Rect2i& rect) noexcept -> void {
auto& opencv_mat = canvas.details().mat;

cv::rectangle(opencv_mat, rect, cv::Scalar { 0, 255, 0 }, 2, cv::LINE_AA);
cv::putText(opencv_mat, "green_light",
cv::Point2i { rect.x, std::max(rect.y - 6, 0) }, cv::FONT_HERSHEY_SIMPLEX, 0.6,
cv::Scalar { 0, 255, 0 }, 1, cv::LINE_AA);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# 只读验证:定位 noexcept 绘制函数与 OpenCV 调用并列出现的位置
rg -nP 'auto\s+draw_(green_light|text)\s*\([^)]*\)\s*noexcept' src/utility/image
rg -nP 'cv::(rectangle|putText|getTextSize)\s*\(' src/utility/image

Repository: Alliance-Algorithm/rmcs_auto_aim_v2

Length of output: 1535


🏁 Script executed:

# 读取 green_light.cpp 和 green_light.hpp 的完整实现
cat -n src/utility/image/green_light.cpp

Repository: Alliance-Algorithm/rmcs_auto_aim_v2

Length of output: 761


🏁 Script executed:

# 读取 green_light.hpp 的完整实现
cat -n src/utility/image/green_light.hpp

Repository: Alliance-Algorithm/rmcs_auto_aim_v2

Length of output: 335


🏁 Script executed:

# 读取 text.cpp 以对比实现模式
cat -n src/utility/image/text.cpp

Repository: Alliance-Algorithm/rmcs_auto_aim_v2

Length of output: 1357


🌐 Web query:

OpenCV cv::rectangle cv::putText throw exception noexcept guarantee

💡 Result:

OpenCV drawing functions such as cv::rectangle and cv::putText do not provide a noexcept guarantee [1][2]. In fact, they are designed to throw exceptions when they encounter errors, such as invalid arguments, incorrect image types, or memory issues [3][4][2]. OpenCV uses the cv::Exception class (which inherits from std::exception) to signal critical errors [2]. These exceptions are typically triggered by internal assertions (e.g., CV_Assert) that validate input parameters, such as image dimensions, data types, or coordinate ranges [3][4][2]. If these conditions are not met, the library throws an exception rather than failing silently or returning an error code [2]. Because these functions can throw, they are not noexcept-compliant. If you are calling them in a context where exceptions must not propagate (such as a destructor or a noexcept function), you must wrap the calls in a try-catch block to handle potential cv::Exception instances [2]. Additionally, while OpenCV aims for efficiency, certain extreme inputs (such as extremely large rectangles) may lead to undefined behavior or crashes if they trigger integer overflows before an assertion can catch them [5]. In some cases, particularly when dealing with external dependencies like FreeType for text rendering, bugs in the underlying libraries can also lead to crashes or segmentation faults rather than standard C++ exceptions [6].

Citations:


🏁 Script executed:

# 验证 text.hpp 声明
cat -n src/utility/image/text.hpp

Repository: Alliance-Algorithm/rmcs_auto_aim_v2

Length of output: 318


🏁 Script executed:

# 检查是否还有其他绘制函数有相同问题
rg -n 'noexcept.*->.*void' src/utility/image/*.cpp | grep -E 'draw_|text'

Repository: Alliance-Algorithm/rmcs_auto_aim_v2

Length of output: 300


移除 noexcept,允许 OpenCV 异常正常传播而非导致进程终止。

第 11 行函数声明为 noexcept,但第 14-17 行的 cv::rectanglecv::putText 调用会在参数验证失败时抛出 cv::Exception。异常逸出 noexcept 函数将触发 std::terminate,导致进程强制终止且不可恢复。

建议修复
--- a/src/utility/image/green_light.hpp
+++ b/src/utility/image/green_light.hpp
@@ -6,6 +6,6 @@
 
 namespace rmcs::util {
 
-auto draw_green_light(Image&, const cv::Rect2i&) noexcept -> void;
+auto draw_green_light(Image&, const cv::Rect2i&) -> void;
--- a/src/utility/image/green_light.cpp
+++ b/src/utility/image/green_light.cpp
@@ -8,7 +8,7 @@
 
 namespace rmcs::util {
 
-auto draw_green_light(Image& canvas, const cv::Rect2i& rect) noexcept -> void {
+auto draw_green_light(Image& canvas, const cv::Rect2i& rect) -> void {
     auto& opencv_mat = canvas.details().mat;

同样的问题也存在于 src/utility/image/text.cppdraw_text 函数。

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
auto draw_green_light(Image& canvas, const cv::Rect2i& rect) noexcept -> void {
auto& opencv_mat = canvas.details().mat;
cv::rectangle(opencv_mat, rect, cv::Scalar { 0, 255, 0 }, 2, cv::LINE_AA);
cv::putText(opencv_mat, "green_light",
cv::Point2i { rect.x, std::max(rect.y - 6, 0) }, cv::FONT_HERSHEY_SIMPLEX, 0.6,
cv::Scalar { 0, 255, 0 }, 1, cv::LINE_AA);
auto draw_green_light(Image& canvas, const cv::Rect2i& rect) -> void {
auto& opencv_mat = canvas.details().mat;
cv::rectangle(opencv_mat, rect, cv::Scalar { 0, 255, 0 }, 2, cv::LINE_AA);
cv::putText(opencv_mat, "green_light",
cv::Point2i { rect.x, std::max(rect.y - 6, 0) }, cv::FONT_HERSHEY_SIMPLEX, 0.6,
cv::Scalar { 0, 255, 0 }, 1, cv::LINE_AA);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/utility/image/green_light.cpp` around lines 11 - 17, The functions
draw_green_light and draw_text are marked noexcept but call OpenCV functions
(cv::rectangle, cv::putText, etc.) that can throw cv::Exception; remove the
noexcept specifier from the draw_green_light definition in green_light.cpp and
from the draw_text definition in text.cpp so OpenCV exceptions can propagate
normally (or alternatively explicitly mark as noexcept(false) if you prefer),
ensuring no noexcept remains on these function signatures.

@creeper5820
Copy link
Copy Markdown
Collaborator

现在这版 AI 率有点高,我们还是等赛后慢慢合进去吧

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/kernel/identifier.cpp`:
- Around line 43-50: When a green light is present the code currently applies
the y-position threshold to all entries in detected_armors and may drop
non-OUTPOST targets; change the loop in the block that uses
locator_result.green_light and threshold_y so that you only apply the vertical
filter to armors whose device id equals DeviceId::OUTPOST (e.g., check
armor.device_id == DeviceId::OUTPOST before comparing armor.center.y to
threshold_y) and for all other armors push them into filtered unchanged; keep
the existing behavior when green_light is not present.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2e074ed1-8b90-47d1-91e1-efdfffe5de46

📥 Commits

Reviewing files that changed from the base of the PR and between eaf4a73 and e1f197b.

📒 Files selected for processing (4)
  • config/config.yaml
  • src/kernel/identifier.cpp
  • src/module/identifier/green_light_locator.cpp
  • src/module/identifier/green_light_locator.hpp
✅ Files skipped from review due to trivial changes (1)
  • config/config.yaml

Comment thread src/kernel/identifier.cpp Outdated
@heyeuu heyeuu closed this May 20, 2026
@github-project-automation github-project-automation Bot moved this from Todo to Done in RMCS Auto Aim V2 May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants