edge_v2.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // P1b/P1c:前视 v2 结构证据 + commit 门(C++ 版,零外部依赖)。
  2. //
  3. // 与 Python `portcv/logic/evidence_v2.py` / `commit.py` 同参数、同字段:
  4. // * 1(敞车)= 箱底以下门板立柱阵列 + 不透明侧墙;
  5. // * 2(平车)= 门板立柱 ≤4 + 车面边梁;
  6. // * 0(其他)= 只有连续地面;
  7. // * 看不清 → 255;`allow_state2=false` 时 2 只识别、不提交(现场联锁/回退用)。
  8. //
  9. // 与 v1 `edge_evidence.*` 的关系:
  10. // * v1 冻结、不再调参;本文件是新的 runtime 路径(`--backend edge-v2`);
  11. // * 复用 L0/L1/L2 的灰度图、Sobel、Canny、线段提取,不重复造算子;
  12. // * ROI 的 y 起点跟随箱底下沿(动态 ROI),不复用固定 y=475..619。
  13. #pragma once
  14. #include <deque>
  15. #include <optional>
  16. #include <string>
  17. #include <vector>
  18. #include "portcv/edge_image.hpp"
  19. #include "portcv/edge_lines.hpp"
  20. #include "portcv/edge_v2_gates.hpp"
  21. #include "portcv/schema.hpp"
  22. namespace portcv {
  23. /// 水平亮带/暗带(原图像素坐标)。
  24. struct V2Band {
  25. double y0 = 0.0;
  26. double y1 = 0.0;
  27. double strength = 0.0;
  28. double height() const { return y1 > y0 ? y1 - y0 : 0.0; }
  29. };
  30. /// `evidence_v2.*` 参数(与 Python `EvidenceV2Params` 同名同义)。
  31. struct EdgeV2Params {
  32. std::string view = "front"; ///< front | high | unknown
  33. int work_width = 960;
  34. double window[4] = {0.24, 0.02, 0.78, 0.95};
  35. int band_min_h = 3;
  36. double band_max_h_ratio = 0.15;
  37. double canny_lo_pct = 0.90;
  38. double canny_hi_pct = 0.97;
  39. double bed_search_ratio = 0.40;
  40. double box_bottom_max_ratio = 0.78;
  41. double below_start_ratio = 0.07;
  42. double below_height_ratio = 0.55;
  43. int below_min_height_px = 24;
  44. int min_door_posts = 5;
  45. int max_door_posts = 16;
  46. double max_bed_gap_px = 40.0;
  47. int flat_max_door_posts = 4;
  48. double max_post_spacing_cv = 6.0;
  49. double min_post_len_ratio = 0.30;
  50. double min_wall_opacity = 0.35;
  51. double wall_edge_density_ref = 0.14;
  52. double wall_edge_density_span = 0.04;
  53. int ground_block_px = 32;
  54. double ground_density_lo = 0.02;
  55. double ground_density_hi = 0.35;
  56. double min_flat_ground_ratio = 0.40;
  57. double min_zero_ground_ratio = 0.65;
  58. double deck_min_len_ratio = 0.30;
  59. double max_flat_gap_px = 40.0;
  60. double box_height_m = 2.591;
  61. double px_per_m = 0.0; ///< 0 = 用箱体竖棱自标尺
  62. double min_scale_px = 60.0;
  63. double motion_diff_max = 12.0;
  64. double motion_grad_ratio_min = 0.72;
  65. bool require_commit = true;
  66. bool allow_state2 = true; ///< 默认允许 2;与 commit.allow_state2 串联(任一 false 即关闭)
  67. double structure_hits_min = 0.60;
  68. /// 敞车(1)判定要求"箱底以下动态 ROI"至少这么高(**原图像素**)。
  69. /// 远距帧的 ROI 只有 40~70 px 时,门板立柱只有 20~27 px 长,和地面/钢轨
  70. /// 纹理在 C++ 线段通道里无法区分(B 片段 f30/f31/f36~f38 因此误判 1 并被
  71. /// 投票成"已发布 1",真值 2)。不足这个高度 → 不判 1、宁可 255。
  72. /// 与 docs/edge_evidence.md §8.1 #2"远距可能 255"的能力边界一致。
  73. int min_bed_roi_px = 90;
  74. /// P0-0 图像侧安全门(默认开,与 Python `evidence_v2.reject_hoist_high` 同键同义):
  75. /// 箱顶以上出现"长横线 + 上方暗带 + 下方更亮"或箱顶紧贴暗横梁 → 整帧弃权,
  76. /// 且不累计 commit 窗口。关掉它只为做对照实验,现场不允许关。
  77. bool reject_hoist_high = true;
  78. HoistGateParams hoist; ///< hoist_dark_max / hoist_band_frac / hoist_line_ratio / ...
  79. bool reject_spreader_contact = true; ///< 与 reject_hoist_high 同源(Python 侧恒开)
  80. SpreaderGateParams spreader; ///< spreader_dark_max / spreader_dark_ratio
  81. };
  82. /// commit 参数(与 Python `CommitParams` 同名同义)。
  83. struct V2CommitParams {
  84. double gap_m_max = 0.10;
  85. double gap_px_hard_max = 8.0;
  86. int stable_frames = 5;
  87. int structure_frames = 5;
  88. double min_structure_hits = 0.60;
  89. double max_gap_rise_m = 0.01;
  90. bool allow_state1 = true;
  91. bool allow_state2 = true; ///< 默认允许平车 2 提交;置 false 可回退为“只识别、不提交”
  92. bool require_motion_ok = true;
  93. bool require_view_known = true;
  94. bool use_plc_arrival = true;
  95. };
  96. struct V2CommitState {
  97. bool committed = false;
  98. std::string phase = "T1";
  99. std::string reason = "init";
  100. std::optional<double> gap_m;
  101. std::optional<double> gap_px;
  102. int stable_frames = 0;
  103. int structure_frames = 0;
  104. std::optional<double> gap_slope_m_per_s;
  105. bool plc_arrival = false;
  106. bool hoist_high = false;
  107. };
  108. class V2CommitTracker {
  109. public:
  110. explicit V2CommitTracker(const V2CommitParams& params = V2CommitParams());
  111. const V2CommitParams& params() const { return p_; }
  112. const V2CommitState& last() const { return last_; }
  113. void reset();
  114. V2CommitState update(double ts, int state, std::optional<double> gap_m,
  115. std::optional<double> gap_px, double structure_hits,
  116. bool motion_ok, bool view_known, bool plc_arrived = false,
  117. bool plc_spreader_released = false, bool hoist_high = false);
  118. private:
  119. struct Sample {
  120. double ts = 0.0;
  121. int state = 255;
  122. std::optional<double> gap_m;
  123. std::optional<double> gap_px;
  124. bool structure_ok = false;
  125. bool motion_ok = true;
  126. };
  127. int same_state_run(int state) const;
  128. bool gap_ok(const Sample& s, bool arrival) const;
  129. std::optional<double> gap_slope(int n) const;
  130. V2CommitParams p_;
  131. std::deque<Sample> samples_;
  132. double last_ts_ = 0.0;
  133. V2CommitState last_;
  134. };
  135. /// 单帧 v2 证据与分类结果(原图像素坐标)。
  136. struct EvidenceV2Result {
  137. int state = static_cast<int>(SceneState::UNKNOWN);
  138. double confidence = 0.0;
  139. std::string view = "unknown";
  140. std::optional<double> box_bottom_y;
  141. std::optional<double> box_top_y;
  142. std::optional<double> box_h_px;
  143. std::optional<double> px_per_m;
  144. bool scale_ok = false;
  145. std::optional<double> bed_top_y;
  146. std::optional<double> gap_px;
  147. std::optional<double> gap_m;
  148. int door_posts = 0;
  149. std::optional<double> post_spacing_px;
  150. std::optional<double> post_spacing_cv;
  151. int rect_doors = 0;
  152. int diagonal_braces = 0;
  153. int line_segments = 0; ///< 通用通道线段数(诊断)
  154. int vertical_runs = 0; ///< 专用竖棱通道线段数(诊断)
  155. int generic_posts = 0; ///< 仅由通用通道得到的立柱数(诊断)
  156. int run_posts = 0; ///< 仅由竖棱游程通道得到的立柱数(诊断)
  157. std::vector<double> post_positions; ///< 聚类后的立柱 x(work 坐标,诊断)
  158. int edge_pixels = 0; ///< Canny 边缘像素数(诊断)
  159. double wall_opacity = 0.0;
  160. bool deck_edge = false;
  161. int wheel_count = 0;
  162. double ground_ratio = 0.0;
  163. std::string phase = "T1";
  164. bool committed = false;
  165. std::string commit_reason = "init";
  166. int stable_frames = 0;
  167. int structure_frames = 0;
  168. bool motion_ok = true;
  169. bool hoist_high = false;
  170. /// P0-0 图像侧安全门的诊断量(不参与判定;`hoist_high` 为最终结论)。
  171. bool hoist_injected = false; ///< true = 由调用方/PLC 注入,未做图像检测
  172. int hoist_lines = 0; ///< 命中"横线 + 上方暗带 + 下方更亮"的次数
  173. int hoist_cables = 0; ///< 近竖直暗线(钢缆/油管)命中数
  174. int hoist_limit_px = 0; ///< 实际检查到的高度(原图像素,0 = 区域太小未检查)
  175. bool spreader_contact = false; ///< 箱顶紧贴吊具横梁(吊具未松开)
  176. int spreader_dark_run_px = 0; ///< 该暗梁厚度
  177. std::string abstain_reason;
  178. int below_roi[4] = {0, 0, 0, 0};
  179. std::string class_reason;
  180. std::string evidence_summary() const;
  181. bool publishable(bool require_commit) const;
  182. /// 转成既有 TemporalVoter 可消费的 Observation;commit 不成立 → 255。
  183. Observation to_observation(double ts = 0.0, long frame_id = 0,
  184. bool require_commit = true) const;
  185. };
  186. /// 前视 v2 证据提取器(单帧确定性;线程内使用)。
  187. class EdgeV2 {
  188. public:
  189. explicit EdgeV2(const EdgeV2Params& params = EdgeV2Params(),
  190. const V2CommitParams& commit = V2CommitParams());
  191. const EdgeV2Params& params() const { return p_; }
  192. const V2CommitTracker& commit_tracker() const { return tracker_; }
  193. const EvidenceV2Result& last() const { return last_; }
  194. void reset();
  195. EvidenceV2Result analyze(const GrayImage& gray, double ts = 0.0,
  196. bool plc_arrived = false,
  197. bool plc_spreader_released = false,
  198. bool hoist_high = false);
  199. private:
  200. bool motion_ok(const GrayImage& work, double* diff, double* ratio);
  201. std::optional<double> scale_from_vertical(const GrayImage& work, const V2Band& box,
  202. const int window[4], double scale,
  203. double* run_px);
  204. struct ClassifyResult {
  205. int state = static_cast<int>(SceneState::UNKNOWN);
  206. double confidence = 0.0;
  207. double structure_hits = 0.0;
  208. std::string reason = "weak_evidence";
  209. };
  210. ClassifyResult classify(int posts, std::optional<double> spacing_cv, double opacity,
  211. double ground, bool deck, int wheels,
  212. std::optional<double> gap_px, bool motion_ok,
  213. const std::string& view, double below_roi_h_px) const;
  214. EdgeV2Params p_;
  215. V2CommitTracker tracker_;
  216. EvidenceV2Result last_;
  217. GrayImage prev_thumb_;
  218. double prev_mag_ = 0.0;
  219. };
  220. } // namespace portcv