| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- // P0-0 安全门 · 图像侧实现(零外部依赖)。判据逐条对应
- // `portcv/logic/evidence_v2.py` 的 `detect_hoist_high()` / `detect_spreader_contact()`。
- #include "portcv/edge_v2_gates.hpp"
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include "portcv/edge_lines.hpp"
- namespace portcv {
- namespace {
- // 局部裁剪(越界自动钳制;空框返回空图)。
- GrayImage crop_rows(const GrayImage& src, int y0, int y1) {
- const int ya = std::max(0, std::min(y0, src.height));
- const int yb = std::max(ya, std::min(y1, src.height));
- if (src.width <= 0 || yb <= ya) { return GrayImage(); }
- GrayImage out(src.width, yb - ya);
- const std::size_t row = static_cast<std::size_t>(src.width);
- for (int y = ya; y < yb; ++y) {
- const auto from = src.data.begin() + static_cast<std::ptrdiff_t>(row * static_cast<std::size_t>(y));
- const auto to = from + static_cast<std::ptrdiff_t>(row);
- std::copy(from, to,
- out.data.begin() + static_cast<std::ptrdiff_t>(row * static_cast<std::size_t>(y - ya)));
- }
- return out;
- }
- // ROI 内一个矩形块的灰度均值与"暗像素占比"(一次遍历取两个量)。
- void mean_and_dark(const GrayImage& img, int x0, int x1, int y0, int y1, int dark_max,
- double* mean_out, double* dark_ratio_out) {
- const int xa = std::max(0, std::min(x0, img.width));
- const int xb = std::max(xa, std::min(x1, img.width));
- const int ya = std::max(0, std::min(y0, img.height));
- const int yb = std::max(ya, std::min(y1, img.height));
- if (mean_out != nullptr) { *mean_out = 0.0; }
- if (dark_ratio_out != nullptr) { *dark_ratio_out = 0.0; }
- if (xb <= xa || yb <= ya) { return; }
- long long sum = 0;
- long long dark = 0;
- for (int y = ya; y < yb; ++y) {
- const std::size_t base = static_cast<std::size_t>(y) * static_cast<std::size_t>(img.width);
- for (int x = xa; x < xb; ++x) {
- const int v = static_cast<int>(img.data[base + static_cast<std::size_t>(x)]);
- sum += v;
- if (v < dark_max) { ++dark; }
- }
- }
- const long long n = static_cast<long long>(xb - xa) * static_cast<long long>(yb - ya);
- if (mean_out != nullptr) { *mean_out = static_cast<double>(sum) / static_cast<double>(n); }
- if (dark_ratio_out != nullptr) {
- *dark_ratio_out = static_cast<double>(dark) / static_cast<double>(n);
- }
- }
- } // namespace
- HoistCues detect_hoist_high(const GrayImage& gray, double box_top_y,
- const HoistGateParams& p) {
- HoistCues out;
- if (gray.empty()) { return out; }
- const int h = gray.height;
- const int w = gray.width;
- // 只检查"箱体上沿以上",且不打到画面底部 45% 以下(那里是车体/地面)。
- const int limit = static_cast<int>(
- std::min(std::max(8.0, box_top_y - 2.0), 0.45 * static_cast<double>(h)));
- if (limit < std::max(12, static_cast<int>(0.06 * h)) || w < 32) { return out; }
- // 宽度 > 480 先降采样(盒式 ≈ INTER_AREA):判据是"长横线 + 带均值",
- // 这个尺度足够;480 宽 ROI 的代价 ≈ 原图 1/7,可以逐帧跑。
- GrayImage work;
- double scale_w = 1.0;
- if (w > 480) {
- const int dst_w = 480;
- const int dst_h =
- std::max(1, static_cast<int>(std::lround(h * (dst_w / static_cast<double>(w)))));
- work = box_downscale(gray, dst_w, dst_h);
- scale_w = work.width / static_cast<double>(w);
- } else {
- work = gray;
- }
- if (work.empty()) { return out; }
- const int limit_w =
- std::min(work.height, std::max(1, static_cast<int>(std::lround(limit * scale_w))));
- if (limit_w < 12 || work.width < 32) { return out; }
- out.limit_px = limit;
- GrayImage roi = crop_rows(work, 0, limit_w);
- if (roi.height < 12 || roi.width < 32) { return out; }
- gaussian3x3_inplace(&roi);
- const SobelResult grad = sobel(roi);
- const std::vector<uint8_t> edges = canny_lite(roi, grad, 0.90, 0.97);
- LineParams lp;
- lp.min_points = 12;
- lp.grad_pct = 0.90;
- const std::vector<Segment> segs = extract_segments(roi, grad, edges, lp);
- const int band_h = std::max(3, static_cast<int>(p.band_frac * roi.height));
- const int x0 = static_cast<int>(0.10 * roi.width);
- const int x1 = std::max(x0 + 8, static_cast<int>(0.90 * roi.width));
- const double min_len = p.line_ratio * roi.width * 0.8;
- for (const Segment& s : segs) {
- const double dx = std::fabs(s.x2 - s.x1);
- const double dy = std::fabs(s.y2 - s.y1);
- if (dx < min_len) { continue; }
- if (dy <= std::max(3.0, 0.04 * dx)) {
- // 近水平线 = 吊具横梁 / 臂架下缘:线上方暗带、线下方更亮才算。
- const int y = static_cast<int>(std::lround(s.cy()));
- if (y - band_h < 0 || y + 2 >= roi.height) { continue; }
- double band_mean = 0.0;
- double band_dark = 0.0;
- mean_and_dark(roi, x0, x1, y - band_h, y, p.dark_max, &band_mean, &band_dark);
- double below_mean = 0.0;
- mean_and_dark(roi, x0, x1, y + 2, std::min(roi.height, y + 2 + 2 * band_h),
- p.dark_max, &below_mean, nullptr);
- if (band_dark >= p.dark_ratio && below_mean >= band_mean + p.bright_delta) {
- ++out.horizontal_lines;
- }
- } else if (dy >= std::max(6.0, 4.0 * dx) &&
- dy >= 0.12 * static_cast<double>(roi.height)) {
- // 近竖直线 = 钢缆 / 油管:只作辅助诊断,按沿线 12 点均值判暗。
- double sum = 0.0;
- int n = 0;
- for (int i = 0; i < 12; ++i) {
- const double t = static_cast<double>(i) / 11.0;
- const int xs = static_cast<int>(std::lround(s.x1 + (s.x2 - s.x1) * t));
- const int ys = static_cast<int>(std::lround(s.y1 + (s.y2 - s.y1) * t));
- if (xs < 0 || xs >= roi.width || ys < 0 || ys >= roi.height) { continue; }
- sum += static_cast<double>(roi.at(xs, ys));
- ++n;
- }
- if (n > 0 && sum / n <= static_cast<double>(p.dark_max) + 25.0) { ++out.cables; }
- }
- }
- out.hoist_high = out.horizontal_lines >= 1;
- return out;
- }
- SpreaderCues detect_spreader_contact(const GrayImage& gray, int x0, int y0, int x1, int y1,
- const SpreaderGateParams& p) {
- SpreaderCues out;
- if (gray.empty()) { return out; }
- const int h = gray.height;
- const int w = gray.width;
- const int xa = std::max(0, std::min(x0, x1));
- const int xb = std::min(w, std::max(x0, x1));
- const int ya = std::max(0, std::min(y0, y1));
- const int yb = std::min(h, std::max(y0, y1));
- const int box_h = yb - ya;
- const int box_w = xb - xa;
- if (box_h < 20 || box_w < 20) { return out; }
- // 箱顶以上 30% 箱高的窄条:吊具未松开时这里必有一条横贯箱宽的暗梁。
- const int probe = std::max(4, static_cast<int>(0.30 * box_h));
- const int ry0 = std::max(0, ya - probe);
- const int rows = ya - ry0;
- if (rows < 3) { return out; }
- const int min_run = std::max(3, static_cast<int>(0.02 * box_h));
- int best_start = -1;
- int best_end = -1;
- int run_start = -1;
- // 优先离箱顶最近的一条暗梁(吊具夹着箱时它紧贴箱顶);同样近时取更厚的一条。
- const auto close_run = [&](int end_row) {
- if (run_start < 0) { return; }
- if (end_row - run_start + 1 >= min_run) {
- const bool better =
- best_start < 0 || (rows - 1 - end_row) < (rows - 1 - best_end) ||
- ((rows - 1 - end_row) == (rows - 1 - best_end) &&
- (end_row - run_start) > (best_end - best_start));
- if (better) {
- best_start = run_start;
- best_end = end_row;
- }
- }
- run_start = -1;
- };
- for (int row = 0; row < rows; ++row) {
- double dark = 0.0;
- mean_and_dark(gray, xa, xb, ry0 + row, ry0 + row + 1, p.dark_max, nullptr, &dark);
- if (dark >= p.dark_ratio) {
- if (run_start < 0) { run_start = row; }
- } else {
- close_run(row - 1);
- }
- }
- close_run(rows - 1);
- if (best_start < 0) { return out; }
- if ((rows - 1 - best_end) > std::max(2, static_cast<int>(0.08 * box_h))) { return out; }
- double band_mean = 0.0;
- mean_and_dark(gray, xa, xb, ry0 + best_start, ry0 + best_end + 1, p.dark_max, &band_mean,
- nullptr);
- double below_mean = 0.0;
- mean_and_dark(gray, xa, xb, ya, std::min(h, ya + std::max(6, static_cast<int>(0.06 * box_h))),
- p.dark_max, &below_mean, nullptr);
- if (below_mean < band_mean + 10.0) { return out; }
- out.contact = true;
- out.dark_run_px = best_end - best_start + 1;
- out.band_delta = std::round((below_mean - band_mean) * 100.0) / 100.0;
- return out;
- }
- } // namespace portcv
|