// P1b/P1c 自检:v2 证据分类 + commit 安全门。 // // 运行: // ctest --test-dir build -R portcv_edge_v2_selftest --output-on-failure // ./build/portcv_edge_v2_selftest // // 覆盖: // * A 类(门板立柱 + 不透明墙)→ 1; // * B 类(无立柱 + 车面边梁)→ 2; // * A 的 9–12 px 间隙不提交;B 的 3–5 px 间隙可提交; // * 贴合车面 gap=0 且无 px_per_m 时仍可提交(侧视 X70A); // * allow_state2=false / motion / PLC 到位信号 / reset。 #include #include #include "portcv/edge_v2.hpp" namespace { int g_pass = 0; int g_fail = 0; void check(bool ok, const std::string& what) { if (ok) { ++g_pass; std::printf(" [OK] %s\n", what.c_str()); } else { ++g_fail; std::printf(" [FAIL] %s\n", what.c_str()); } } void check_eq(long long got, long long want, const std::string& what) { if (got == want) { ++g_pass; std::printf(" [OK] %s (=%lld)\n", what.c_str(), got); } else { ++g_fail; std::printf(" [FAIL] %s: got=%lld want=%lld\n", what.c_str(), got, want); } } void fill_rect(portcv::GrayImage* img, int x0, int y0, int x1, int y1, uint8_t value) { for (int y = y0; y < y1; ++y) { for (int x = x0; x < x1; ++x) { img->set(x, y, value); } } } portcv::GrayImage bed_scene(int gap_px = 18) { portcv::GrayImage img(1280, 720, 120); const int box_bottom = 392; fill_rect(&img, 0, 40, 1280, box_bottom, 185); // 箱体竖波纹(真实集装箱有;也让 P0-Q4 顶部纹理门不把箱顶误当天空) for (int x = 60; x < 1240; x += 48) { fill_rect(&img, x, 50, x + 3, box_bottom - 4, 176); } fill_rect(&img, 0, box_bottom, 1280, box_bottom + 12, 55); const int wall_top = box_bottom + 12 + gap_px; fill_rect(&img, 0, box_bottom + 12, 1280, wall_top, 88); fill_rect(&img, 0, wall_top, 1280, wall_top + 4, 215); fill_rect(&img, 0, wall_top + 4, 1280, 720, 125); for (int x = 120; x < 1180; x += 110) { fill_rect(&img, x, wall_top + 20, x + 5, wall_top + 230, 55); } for (int dy = 30; dy < 240; dy += 20) { fill_rect(&img, 60, wall_top + dy, 1220, wall_top + dy + 2, 85); } return img; } portcv::GrayImage flat_scene(int gap_px = 4) { portcv::GrayImage img(1280, 720, 120); fill_rect(&img, 0, 40, 1280, 392, 185); for (int x = 60; x < 1240; x += 48) { fill_rect(&img, x, 50, x + 3, 388, 176); } fill_rect(&img, 0, 392, 1280, 404, 55); const int deck_top = 404 + gap_px; fill_rect(&img, 0, deck_top, 1280, deck_top + 5, 225); fill_rect(&img, 0, deck_top + 5, 1280, 520, 145); fill_rect(&img, 40, deck_top + 24, 1240, deck_top + 29, 70); for (int y = 520; y < 720; ++y) { for (int x = 0; x < 1280; ++x) { img.set(x, y, static_cast(70 + ((x * 37 + y * 17) % 120))); } } return img; } portcv::GrayImage flat_contact_scene() { // 侧视 X70A:箱底与车面直接贴合(gap=0),车面边梁紧贴箱底; // 动态 ROI 从箱底下方开始,因此边梁本身落在 ROI 外。用于防止 // “贴合时跳过相邻亮带 → gap 误取 60+ px → 255”的回归。 // P0-Q3:边梁按**薄边**建(8 px)+ 下方暗带(车底空隙/阴影)—— // 与现场 746×192 侧视帧同形态;厚亮面(地面/下层箱顶)由负样本覆盖。 portcv::GrayImage img(1280, 720, 120); fill_rect(&img, 0, 40, 1280, 392, 185); for (int x = 60; x < 1240; x += 48) { fill_rect(&img, x, 50, x + 3, 388, 176); } fill_rect(&img, 0, 392, 1280, 404, 55); fill_rect(&img, 0, 404, 1280, 412, 225); fill_rect(&img, 0, 412, 1280, 442, 30); // 地面用均匀灰(本用例只验证“贴合车面”与 gap 取值;真实地面纹理另测)。 fill_rect(&img, 0, 442, 1280, 720, 110); return img; } portcv::GrayImage hoist_scene() { // 箱体已落位(与 bed_scene 同结构),但箱顶上方有一条横跨画面的暗带 // (吊具/臂架),暗带下方是更亮的天空 → P0-0 高悬门必须命中。 // 暗带放在 y=8..32:bed_scene 的箱顶估计在 ~103 px(箱底 402.7 − 箱高 299.5), // 高悬门只检查箱顶以上,所以这条暗带落在检查范围内,同时留出 8 px 天空边, // 不改变箱体/车面那几条亮带的边界(否则 box 选择会变,判据就无从比较)。 portcv::GrayImage img = bed_scene(18); fill_rect(&img, 0, 8, 1280, 32, 30); return img; } /// 箱直接坐地:箱底以下是低对比、连续的地面(P0-Q3 负样本)。 portcv::GrayImage ground_scene() { portcv::GrayImage img(1280, 720, 120); fill_rect(&img, 0, 40, 1280, 392, 185); for (int x = 60; x < 1240; x += 48) { fill_rect(&img, x, 50, x + 3, 388, 176); } fill_rect(&img, 0, 392, 1280, 404, 55); for (int y = 404; y < 720; ++y) { for (int x = 0; x < 1280; ++x) { img.set(x, y, static_cast(100 + ((x * 7 + y * 3) % 12))); } } return img; } /// 箱底紧贴一块**厚亮面**(地面/下层箱顶/月台)——旧版 flat_contact 会判 2。 portcv::GrayImage thick_contact_scene() { portcv::GrayImage img = ground_scene(); fill_rect(&img, 0, 404, 1280, 450, 165); return img; } /// 上层箱坐在下层箱上:下层箱顶亮面 + 侧壁瓦楞(无斜撑)——旧版会判 1。 portcv::GrayImage stacked_scene() { portcv::GrayImage img = ground_scene(); fill_rect(&img, 0, 404, 1280, 450, 165); fill_rect(&img, 0, 450, 1280, 720, 140); for (int x = 60; x < 1280; x += 118) { fill_rect(&img, x, 450, x + 6, 700, 95); } return img; } /// 天空电线 + 车体:电线是横向细线,车体有竖向立柱/门板(P0-Q4 回归,512×512)。 portcv::GrayImage sky_wire_scene() { portcv::GrayImage img(512, 512, 200); for (int y : {20, 52, 84, 116}) { fill_rect(&img, 0, y, 512, y + 2, 30); } fill_rect(&img, 0, 165, 512, 405, 95); for (int x = 30; x < 480; x += 18) { fill_rect(&img, x, 175, x + 5, 400, 45); } for (int x = 30; x + 40 < 480; x += 50) { fill_rect(&img, x, 185, x + 40, 380, 110); fill_rect(&img, x, 185, x + 40, 188, 60); fill_rect(&img, x, 377, x + 40, 380, 60); fill_rect(&img, x, 185, x + 3, 380, 60); fill_rect(&img, x + 37, 185, x + 40, 380, 60); } for (int dy = 5; dy < 235; dy += 8) { fill_rect(&img, 20, 165 + dy, 492, 165 + dy + 2, 80); } for (int x = 40; x + 80 < 480; x += 70) { for (int t = 0; t < 80; ++t) { fill_rect(&img, x + t, 190 + t, x + t + 7, 190 + t + 7, 30); } } fill_rect(&img, 0, 405, 512, 512, 140); return img; } portcv::V2CommitParams fast_commit() { portcv::V2CommitParams p; p.stable_frames = 3; p.structure_frames = 3; p.gap_px_hard_max = 8.0; return p; } } // namespace int main() { using portcv::EdgeV2; using portcv::EdgeV2Params; using portcv::V2CommitParams; using portcv::V2CommitTracker; std::printf("P1b/P1c edge_v2 selftest\n"); // ---------------- commit 安全门 ---------------- { V2CommitTracker t(fast_commit()); portcv::V2CommitState s; for (int i = 0; i < 8; ++i) { s = t.update(i * 0.1, 1, 0.104, 12.0, 0.9, true, true); } check(!s.committed, "A-like gap 12px must not commit"); check(s.phase == "T1", "A-like phase T1"); } { V2CommitTracker t(fast_commit()); portcv::V2CommitState s; for (int i = 0; i < 3; ++i) { s = t.update(i * 0.1, 1, 0.035, 4.0, 0.9, true, true); } check(s.committed, "B-like gap 4px commits"); check(s.phase == "T2", "B-like phase T2"); } { V2CommitParams p = fast_commit(); p.allow_state2 = false; V2CommitTracker t(p); portcv::V2CommitState s; for (int i = 0; i < 8; ++i) { s = t.update(i * 0.1, 2, 0.03, 3.5, 0.9, true, true); } check(!s.committed, "state2 can be disabled explicitly"); check(s.reason == "state2_disabled", "state2 disabled reason"); } { V2CommitTracker t(fast_commit()); portcv::V2CommitState s; for (int i = 0; i < 3; ++i) { s = t.update(i * 0.1, 2, 0.03, 3.5, 0.9, true, true); } check(s.committed, "state2 commits by default"); } { // 侧视贴合:flat_contact 给出 gap_px=0,但没有 px_per_m → gap_m 为空。 V2CommitTracker t(fast_commit()); portcv::V2CommitState s; for (int i = 0; i < 3; ++i) { s = t.update(i * 0.1, 2, std::nullopt, 0.0, 0.9, true, true); } check(s.committed, "zero contact gap commits without px_per_m"); } { V2CommitTracker t(fast_commit()); portcv::V2CommitState s; for (int i = 0; i < 8; ++i) { s = t.update(i * 0.1, 1, 0.03, 3.0, 0.9, false, true); } check(!s.committed && s.phase == "T3", "motion is T3"); } { V2CommitTracker t(fast_commit()); portcv::V2CommitState s; for (int i = 0; i < 3; ++i) { s = t.update(i * 0.1, 1, 0.50, 60.0, 1.0, true, true, true, false); } check(s.committed && s.reason == "commit:plc_arrival", "PLC arrival bypasses gap"); } // ---------------- 合成场景分类 ---------------- EdgeV2Params p; p.px_per_m = 115.6; p.require_commit = false; p.allow_state2 = false; // 合成图纹理很少,分位阈值会过高;真机保持默认 0.90/0.97。 p.canny_lo_pct = 0.50; p.canny_hi_pct = 0.90; { EdgeV2 ev(p, fast_commit()); const auto r = ev.analyze(bed_scene(18), 0.0); std::printf(" A: %s diag=%d\n", r.evidence_summary().c_str(), r.diagonal_braces); check(r.state == 1, "A-like synthetic -> state 1"); check(r.door_posts >= 5, "A-like has >=5 door posts"); check(!r.hoist_high, "A-like is not flagged hoist_high"); } { EdgeV2 ev(p, fast_commit()); const auto r = ev.analyze(flat_scene(4), 0.0); std::printf(" B: %s\n", r.evidence_summary().c_str()); check(r.state == 2, "B-like synthetic -> state 2"); check(r.door_posts <= 4, "B-like has <=4 door posts"); check(r.deck_edge, "B-like has deck edge"); check(!r.hoist_high, "B-like is not flagged hoist_high"); } { EdgeV2 ev(p, fast_commit()); const auto r = ev.analyze(flat_contact_scene(), 0.0); std::printf(" B-contact: %s\n", r.evidence_summary().c_str()); check(r.state == 2, "contact flatbed synthetic -> state 2"); check(r.deck_edge, "contact flatbed has deck/contact edge"); check(r.gap_px.has_value() && *r.gap_px <= 2.0, "contact flatbed gap stays at box bottom (<=2px)"); } // ---------------- P0-Q3:地面/堆料/堆叠箱负样本 ---------------- { EdgeV2 ev(p, fast_commit()); const auto r = ev.analyze(ground_scene(), 0.0); std::printf(" ground: %s\n", r.evidence_summary().c_str()); check(r.state != 1 && r.state != 2, "box on ground is never 1/2"); } { EdgeV2 ev(p, fast_commit()); const auto r = ev.analyze(thick_contact_scene(), 0.0); std::printf(" thick-contact: %s\n", r.evidence_summary().c_str()); check(r.state != 1 && r.state != 2, "thick contact plane is not flatbed"); check(!r.deck_edge, "thick contact plane does not set deck_edge"); } { EdgeV2 ev(p, fast_commit()); const auto r = ev.analyze(stacked_scene(), 0.0); std::printf(" stacked: %s\n", r.evidence_summary().c_str()); check(r.state != 1 && r.state != 2, "stacked container is not bed/flatbed"); } // ---------------- P0-Q4:天空电线/纹理顶 ---------------- { portcv::EdgeV2Params tp; const int win[4] = {static_cast(0.24 * 512), static_cast(0.02 * 512), static_cast(0.78 * 512), static_cast(0.95 * 512)}; const double top = portcv::find_texture_top_y(sky_wire_scene(), win, tp); std::printf(" sky-top=%.1f\n", top); check(top > 120.0 && top < 220.0, "sky wires: vehicle top below wires"); portcv::GrayImage plain(512, 512, 200); for (int y : {20, 52, 84}) { fill_rect(&plain, 0, y, 512, y + 2, 30); } check(portcv::find_texture_top_y(plain, win, tp) < 0.0, "plain sky: no vehicle top"); EdgeV2 ev(p, fast_commit()); const auto r = ev.analyze(sky_wire_scene(), 0.0); std::printf(" sky-body: %s\n", r.evidence_summary().c_str()); check(r.state == 255, "empty C70 + sky wires -> 255 (空车弃权,不判 1/2)"); check(r.texture_top_y > 120.0, "sky-body texture top below wires"); } // ---------------- P0-0 图像侧安全门 ---------------- { // 直接测算子(与 Python `test_detect_hoist_high_finds_dark_bar_with_bright_sky_below` // 同一张合成图):上方暗带 40..140,下方天空 140..300 更亮。 portcv::GrayImage img(1280, 720, 185); fill_rect(&img, 0, 40, 1280, 140, 25); const auto cues = portcv::detect_hoist_high(img, 300.0); check(cues.hoist_high, "detect_hoist_high: dark bar + bright sky below -> true"); check(cues.horizontal_lines >= 1, "detect_hoist_high: horizontal_lines >= 1"); check_eq(cues.limit_px, 298, "detect_hoist_high: probe height = box_top - 2"); } { // 均匀场景必须不命中(否则真机所有低对比帧都会被弃权)。 portcv::GrayImage img(1280, 720, 150); const auto cues = portcv::detect_hoist_high(img, 300.0); check(!cues.hoist_high, "detect_hoist_high: uniform scene -> false"); check_eq(cues.horizontal_lines, 0, "detect_hoist_high: uniform horizontal_lines=0"); } { // 箱顶以上区域太小(box_top≈0,箱顶被画幅裁掉)时必须放行 —— 这条是 // A/B 正样本不被误弃权的保证。 portcv::GrayImage img = bed_scene(18); const auto cues = portcv::detect_hoist_high(img, 6.0); check(!cues.hoist_high && cues.limit_px == 0, "detect_hoist_high: tiny ROI -> not checked"); } { // 夹箱:吊具横梁紧贴箱顶(100..140 暗,箱体 140..450)。 portcv::GrayImage img(600, 500, 180); fill_rect(&img, 100, 100, 500, 140, 30); fill_rect(&img, 100, 140, 500, 450, 150); const auto cues = portcv::detect_spreader_contact(img, 100, 140, 500, 450); check(cues.contact, "detect_spreader_contact: dark bar on box top -> true"); check(cues.dark_run_px >= 20, "detect_spreader_contact: dark_run_px >= 20"); } { // 干净的箱顶(上方是同亮度背景)不能命中。 portcv::GrayImage img(600, 500, 180); fill_rect(&img, 100, 140, 500, 450, 150); const auto cues = portcv::detect_spreader_contact(img, 100, 140, 500, 450); check(!cues.contact, "detect_spreader_contact: plain box top -> false"); } { // 端到端:高悬帧连续 6 帧都成立也必须 0 commit(不写窗口、不进投票)。 EdgeV2Params cp = p; cp.require_commit = true; cp.allow_state2 = true; EdgeV2 ev(cp, fast_commit()); portcv::EvidenceV2Result r; for (int i = 0; i < 6; ++i) { r = ev.analyze(hoist_scene(), i * 0.1); } std::printf(" hoist: %s\n", r.evidence_summary().c_str()); check(r.hoist_high, "hoist scene flagged hoist_high"); check(!r.hoist_injected, "hoist flag came from image (not injected)"); check(r.hoist_lines >= 1, "hoist scene has >=1 horizontal hit"); check(!r.committed, "hoist scene never commits"); // 分类本身仍是 1(几何证据没坏),安全门只拦 commit —— 对外的 `edge_state` // 走 `to_observation(require_commit=true)`,所以必须是 255(见下一条)。 check_eq(r.state, 1, "hoist scene still classifies 1 (geometry intact)"); check(r.abstain_reason == "hoist_high", "hoist scene reason=hoist_high"); check_eq(r.stable_frames, 0, "hoist scene does not accumulate stable frames"); const auto obs = r.to_observation(0.6, 0, true); check(obs.state == 255, "hoist scene observation abstains"); check(obs.reason.find("hoist_high") != std::string::npos, "observation reason carries hoist_high"); } { // 注入路径(PLC/联锁直接给 hoist_high):不做图像检测也不消耗窗口。 EdgeV2Params cp = p; cp.require_commit = true; EdgeV2 ev(cp, fast_commit()); portcv::EvidenceV2Result r; for (int i = 0; i < 6; ++i) { r = ev.analyze(bed_scene(18), i * 0.1, false, false, true); } check(r.hoist_high && r.hoist_injected, "injected hoist_high is honoured"); check(r.abstain_reason == "hoist_high", "injected hoist reason=hoist_high"); check(!r.committed, "injected hoist never commits"); } { // 关掉门只做对照:同一帧仍能出 1(说明弃权确实来自门,而不是几何退化)。 EdgeV2Params cp = p; cp.reject_hoist_high = false; cp.reject_spreader_contact = false; EdgeV2 ev(cp, fast_commit()); const auto r = ev.analyze(hoist_scene(), 0.0); check(!r.hoist_high, "gate disabled -> no hoist flag"); check_eq(r.state, 1, "gate disabled -> geometry still says 1"); } { // 远距小箱:箱底以下 ROI 太矮(这里把 1280×720 等比缩到 480×270, // ROI 只剩 ~60 px)时必须弃权,不能判 1 —— B 片段 f30/f31/f36~f38 // 实测就是这种"远距 + 门板立柱只有 20~27 px"的帧被误判 1 并被投票发布。 EdgeV2Params cp = p; cp.px_per_m = 0.0; // 用自标尺(缩放后 115.6 px/m 已不成立) EdgeV2 ev(cp, fast_commit()); const portcv::GrayImage small = portcv::box_downscale(bed_scene(18), 480, 270); const auto r = ev.analyze(small, 0.0); std::printf(" far-A: %s\n", r.evidence_summary().c_str()); const double roi_h = r.below_roi[3] - r.below_roi[1]; check(roi_h < 90.0, "far-A below ROI is shorter than min_bed_roi_px"); check(r.state != 1, "far gondola is never classified 1 (abstains instead)"); } { EdgeV2 ev(p, fast_commit()); const portcv::GrayImage empty; const auto r = ev.analyze(empty, 0.0); check(r.state == 255, "empty frame -> 255"); check(r.abstain_reason == "no_image", "empty reason=no_image"); } { EdgeV2 ev(p, fast_commit()); const auto r = ev.analyze(bed_scene(18), 0.0); const auto obs = r.to_observation(0.0, 0, true); check(obs.state == 255, "require_commit abstains to 255"); } std::printf("----\n%d passed, %d failed\n", g_pass, g_fail); return g_fail == 0 ? 0 : 1; }