// P1b/P1c:前视 v2 结构证据 + commit 门(C++ 版,零外部依赖)。 // // 与 Python `portcv/logic/evidence_v2.py` / `commit.py` 同参数、同字段: // * 1(敞车)= 箱底以下门板立柱阵列 + 不透明侧墙; // * 2(平车)= 门板立柱 ≤4 + 车面边梁; // * 0(其他)= 只有连续地面; // * 看不清 → 255;`allow_state2=false` 时 2 只识别、不提交(现场联锁/回退用)。 // // 与 v1 `edge_evidence.*` 的关系: // * v1 冻结、不再调参;本文件是新的 runtime 路径(`--backend edge-v2`); // * 复用 L0/L1/L2 的灰度图、Sobel、Canny、线段提取,不重复造算子; // * ROI 的 y 起点跟随箱底下沿(动态 ROI),不复用固定 y=475..619。 #pragma once #include #include #include #include #include "portcv/edge_image.hpp" #include "portcv/edge_lines.hpp" #include "portcv/edge_v2_gates.hpp" #include "portcv/schema.hpp" namespace portcv { /// 水平亮带/暗带(原图像素坐标)。 struct V2Band { double y0 = 0.0; double y1 = 0.0; double strength = 0.0; double height() const { return y1 > y0 ? y1 - y0 : 0.0; } }; /// `evidence_v2.*` 参数(与 Python `EvidenceV2Params` 同名同义)。 struct EdgeV2Params { std::string view = "front"; ///< front | high | unknown int work_width = 960; double window[4] = {0.24, 0.02, 0.78, 0.95}; int band_min_h = 3; double band_max_h_ratio = 0.15; double canny_lo_pct = 0.90; double canny_hi_pct = 0.97; double bed_search_ratio = 0.40; double box_bottom_max_ratio = 0.78; double below_start_ratio = 0.07; double below_height_ratio = 0.55; int below_min_height_px = 24; int min_door_posts = 5; int max_door_posts = 16; double max_bed_gap_px = 40.0; int flat_max_door_posts = 4; double max_post_spacing_cv = 6.0; double min_post_len_ratio = 0.30; double min_wall_opacity = 0.35; double wall_edge_density_ref = 0.14; double wall_edge_density_span = 0.04; int ground_block_px = 32; double ground_density_lo = 0.02; double ground_density_hi = 0.35; double min_flat_ground_ratio = 0.40; double min_zero_ground_ratio = 0.65; double deck_min_len_ratio = 0.30; double max_flat_gap_px = 40.0; double box_height_m = 2.591; double px_per_m = 0.0; ///< 0 = 用箱体竖棱自标尺 double min_scale_px = 60.0; double motion_diff_max = 12.0; double motion_grad_ratio_min = 0.72; bool require_commit = true; bool allow_state2 = true; ///< 默认允许 2;与 commit.allow_state2 串联(任一 false 即关闭) double structure_hits_min = 0.60; /// 敞车(1)判定要求"箱底以下动态 ROI"至少这么高(**原图像素**)。 /// 远距帧的 ROI 只有 40~70 px 时,门板立柱只有 20~27 px 长,和地面/钢轨 /// 纹理在 C++ 线段通道里无法区分(B 片段 f30/f31/f36~f38 因此误判 1 并被 /// 投票成"已发布 1",真值 2)。不足这个高度 → 不判 1、宁可 255。 /// 与 docs/edge_evidence.md §8.1 #2"远距可能 255"的能力边界一致。 int min_bed_roi_px = 90; /// P0-0 图像侧安全门(默认开,与 Python `evidence_v2.reject_hoist_high` 同键同义): /// 箱顶以上出现"长横线 + 上方暗带 + 下方更亮"或箱顶紧贴暗横梁 → 整帧弃权, /// 且不累计 commit 窗口。关掉它只为做对照实验,现场不允许关。 bool reject_hoist_high = true; HoistGateParams hoist; ///< hoist_dark_max / hoist_band_frac / hoist_line_ratio / ... bool reject_spreader_contact = true; ///< 与 reject_hoist_high 同源(Python 侧恒开) SpreaderGateParams spreader; ///< spreader_dark_max / spreader_dark_ratio }; /// commit 参数(与 Python `CommitParams` 同名同义)。 struct V2CommitParams { double gap_m_max = 0.10; double gap_px_hard_max = 8.0; int stable_frames = 5; int structure_frames = 5; double min_structure_hits = 0.60; double max_gap_rise_m = 0.01; bool allow_state1 = true; bool allow_state2 = true; ///< 默认允许平车 2 提交;置 false 可回退为“只识别、不提交” bool require_motion_ok = true; bool require_view_known = true; bool use_plc_arrival = true; }; struct V2CommitState { bool committed = false; std::string phase = "T1"; std::string reason = "init"; std::optional gap_m; std::optional gap_px; int stable_frames = 0; int structure_frames = 0; std::optional gap_slope_m_per_s; bool plc_arrival = false; bool hoist_high = false; }; class V2CommitTracker { public: explicit V2CommitTracker(const V2CommitParams& params = V2CommitParams()); const V2CommitParams& params() const { return p_; } const V2CommitState& last() const { return last_; } void reset(); V2CommitState update(double ts, int state, std::optional gap_m, std::optional gap_px, double structure_hits, bool motion_ok, bool view_known, bool plc_arrived = false, bool plc_spreader_released = false, bool hoist_high = false); private: struct Sample { double ts = 0.0; int state = 255; std::optional gap_m; std::optional gap_px; bool structure_ok = false; bool motion_ok = true; }; int same_state_run(int state) const; bool gap_ok(const Sample& s, bool arrival) const; std::optional gap_slope(int n) const; V2CommitParams p_; std::deque samples_; double last_ts_ = 0.0; V2CommitState last_; }; /// 单帧 v2 证据与分类结果(原图像素坐标)。 struct EvidenceV2Result { int state = static_cast(SceneState::UNKNOWN); double confidence = 0.0; std::string view = "unknown"; std::optional box_bottom_y; std::optional box_top_y; std::optional box_h_px; std::optional px_per_m; bool scale_ok = false; std::optional bed_top_y; std::optional gap_px; std::optional gap_m; int door_posts = 0; std::optional post_spacing_px; std::optional post_spacing_cv; int rect_doors = 0; int diagonal_braces = 0; int line_segments = 0; ///< 通用通道线段数(诊断) int vertical_runs = 0; ///< 专用竖棱通道线段数(诊断) int generic_posts = 0; ///< 仅由通用通道得到的立柱数(诊断) int run_posts = 0; ///< 仅由竖棱游程通道得到的立柱数(诊断) std::vector post_positions; ///< 聚类后的立柱 x(work 坐标,诊断) int edge_pixels = 0; ///< Canny 边缘像素数(诊断) double wall_opacity = 0.0; bool deck_edge = false; int wheel_count = 0; double ground_ratio = 0.0; std::string phase = "T1"; bool committed = false; std::string commit_reason = "init"; int stable_frames = 0; int structure_frames = 0; bool motion_ok = true; bool hoist_high = false; /// P0-0 图像侧安全门的诊断量(不参与判定;`hoist_high` 为最终结论)。 bool hoist_injected = false; ///< true = 由调用方/PLC 注入,未做图像检测 int hoist_lines = 0; ///< 命中"横线 + 上方暗带 + 下方更亮"的次数 int hoist_cables = 0; ///< 近竖直暗线(钢缆/油管)命中数 int hoist_limit_px = 0; ///< 实际检查到的高度(原图像素,0 = 区域太小未检查) bool spreader_contact = false; ///< 箱顶紧贴吊具横梁(吊具未松开) int spreader_dark_run_px = 0; ///< 该暗梁厚度 std::string abstain_reason; int below_roi[4] = {0, 0, 0, 0}; std::string class_reason; std::string evidence_summary() const; bool publishable(bool require_commit) const; /// 转成既有 TemporalVoter 可消费的 Observation;commit 不成立 → 255。 Observation to_observation(double ts = 0.0, long frame_id = 0, bool require_commit = true) const; }; /// 前视 v2 证据提取器(单帧确定性;线程内使用)。 class EdgeV2 { public: explicit EdgeV2(const EdgeV2Params& params = EdgeV2Params(), const V2CommitParams& commit = V2CommitParams()); const EdgeV2Params& params() const { return p_; } const V2CommitTracker& commit_tracker() const { return tracker_; } const EvidenceV2Result& last() const { return last_; } void reset(); EvidenceV2Result analyze(const GrayImage& gray, double ts = 0.0, bool plc_arrived = false, bool plc_spreader_released = false, bool hoist_high = false); private: bool motion_ok(const GrayImage& work, double* diff, double* ratio); std::optional scale_from_vertical(const GrayImage& work, const V2Band& box, const int window[4], double scale, double* run_px); struct ClassifyResult { int state = static_cast(SceneState::UNKNOWN); double confidence = 0.0; double structure_hits = 0.0; std::string reason = "weak_evidence"; }; ClassifyResult classify(int posts, std::optional spacing_cv, double opacity, double ground, bool deck, int wheels, std::optional gap_px, bool motion_ok, const std::string& view, double below_roi_h_px) const; EdgeV2Params p_; V2CommitTracker tracker_; EvidenceV2Result last_; GrayImage prev_thumb_; double prev_mag_ = 0.0; }; } // namespace portcv