edge_v2_gates.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // P0-0 安全门 · 图像侧实现(零外部依赖)。判据逐条对应
  2. // `portcv/logic/evidence_v2.py` 的 `detect_hoist_high()` / `detect_spreader_contact()`。
  3. #include "portcv/edge_v2_gates.hpp"
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <vector>
  7. #include "portcv/edge_lines.hpp"
  8. namespace portcv {
  9. namespace {
  10. // 局部裁剪(越界自动钳制;空框返回空图)。
  11. GrayImage crop_rows(const GrayImage& src, int y0, int y1) {
  12. const int ya = std::max(0, std::min(y0, src.height));
  13. const int yb = std::max(ya, std::min(y1, src.height));
  14. if (src.width <= 0 || yb <= ya) { return GrayImage(); }
  15. GrayImage out(src.width, yb - ya);
  16. const std::size_t row = static_cast<std::size_t>(src.width);
  17. for (int y = ya; y < yb; ++y) {
  18. const auto from = src.data.begin() + static_cast<std::ptrdiff_t>(row * static_cast<std::size_t>(y));
  19. const auto to = from + static_cast<std::ptrdiff_t>(row);
  20. std::copy(from, to,
  21. out.data.begin() + static_cast<std::ptrdiff_t>(row * static_cast<std::size_t>(y - ya)));
  22. }
  23. return out;
  24. }
  25. // ROI 内一个矩形块的灰度均值与"暗像素占比"(一次遍历取两个量)。
  26. void mean_and_dark(const GrayImage& img, int x0, int x1, int y0, int y1, int dark_max,
  27. double* mean_out, double* dark_ratio_out) {
  28. const int xa = std::max(0, std::min(x0, img.width));
  29. const int xb = std::max(xa, std::min(x1, img.width));
  30. const int ya = std::max(0, std::min(y0, img.height));
  31. const int yb = std::max(ya, std::min(y1, img.height));
  32. if (mean_out != nullptr) { *mean_out = 0.0; }
  33. if (dark_ratio_out != nullptr) { *dark_ratio_out = 0.0; }
  34. if (xb <= xa || yb <= ya) { return; }
  35. long long sum = 0;
  36. long long dark = 0;
  37. for (int y = ya; y < yb; ++y) {
  38. const std::size_t base = static_cast<std::size_t>(y) * static_cast<std::size_t>(img.width);
  39. for (int x = xa; x < xb; ++x) {
  40. const int v = static_cast<int>(img.data[base + static_cast<std::size_t>(x)]);
  41. sum += v;
  42. if (v < dark_max) { ++dark; }
  43. }
  44. }
  45. const long long n = static_cast<long long>(xb - xa) * static_cast<long long>(yb - ya);
  46. if (mean_out != nullptr) { *mean_out = static_cast<double>(sum) / static_cast<double>(n); }
  47. if (dark_ratio_out != nullptr) {
  48. *dark_ratio_out = static_cast<double>(dark) / static_cast<double>(n);
  49. }
  50. }
  51. } // namespace
  52. HoistCues detect_hoist_high(const GrayImage& gray, double box_top_y,
  53. const HoistGateParams& p) {
  54. HoistCues out;
  55. if (gray.empty()) { return out; }
  56. const int h = gray.height;
  57. const int w = gray.width;
  58. // 只检查"箱体上沿以上",且不打到画面底部 45% 以下(那里是车体/地面)。
  59. const int limit = static_cast<int>(
  60. std::min(std::max(8.0, box_top_y - 2.0), 0.45 * static_cast<double>(h)));
  61. if (limit < std::max(12, static_cast<int>(0.06 * h)) || w < 32) { return out; }
  62. // 宽度 > 480 先降采样(盒式 ≈ INTER_AREA):判据是"长横线 + 带均值",
  63. // 这个尺度足够;480 宽 ROI 的代价 ≈ 原图 1/7,可以逐帧跑。
  64. GrayImage work;
  65. double scale_w = 1.0;
  66. if (w > 480) {
  67. const int dst_w = 480;
  68. const int dst_h =
  69. std::max(1, static_cast<int>(std::lround(h * (dst_w / static_cast<double>(w)))));
  70. work = box_downscale(gray, dst_w, dst_h);
  71. scale_w = work.width / static_cast<double>(w);
  72. } else {
  73. work = gray;
  74. }
  75. if (work.empty()) { return out; }
  76. const int limit_w =
  77. std::min(work.height, std::max(1, static_cast<int>(std::lround(limit * scale_w))));
  78. if (limit_w < 12 || work.width < 32) { return out; }
  79. out.limit_px = limit;
  80. GrayImage roi = crop_rows(work, 0, limit_w);
  81. if (roi.height < 12 || roi.width < 32) { return out; }
  82. gaussian3x3_inplace(&roi);
  83. const SobelResult grad = sobel(roi);
  84. const std::vector<uint8_t> edges = canny_lite(roi, grad, 0.90, 0.97);
  85. LineParams lp;
  86. lp.min_points = 12;
  87. lp.grad_pct = 0.90;
  88. const std::vector<Segment> segs = extract_segments(roi, grad, edges, lp);
  89. const int band_h = std::max(3, static_cast<int>(p.band_frac * roi.height));
  90. const int x0 = static_cast<int>(0.10 * roi.width);
  91. const int x1 = std::max(x0 + 8, static_cast<int>(0.90 * roi.width));
  92. const double min_len = p.line_ratio * roi.width * 0.8;
  93. for (const Segment& s : segs) {
  94. const double dx = std::fabs(s.x2 - s.x1);
  95. const double dy = std::fabs(s.y2 - s.y1);
  96. if (dx < min_len) { continue; }
  97. if (dy <= std::max(3.0, 0.04 * dx)) {
  98. // 近水平线 = 吊具横梁 / 臂架下缘:线上方暗带、线下方更亮才算。
  99. const int y = static_cast<int>(std::lround(s.cy()));
  100. if (y - band_h < 0 || y + 2 >= roi.height) { continue; }
  101. double band_mean = 0.0;
  102. double band_dark = 0.0;
  103. mean_and_dark(roi, x0, x1, y - band_h, y, p.dark_max, &band_mean, &band_dark);
  104. double below_mean = 0.0;
  105. mean_and_dark(roi, x0, x1, y + 2, std::min(roi.height, y + 2 + 2 * band_h),
  106. p.dark_max, &below_mean, nullptr);
  107. if (band_dark >= p.dark_ratio && below_mean >= band_mean + p.bright_delta) {
  108. ++out.horizontal_lines;
  109. }
  110. } else if (dy >= std::max(6.0, 4.0 * dx) &&
  111. dy >= 0.12 * static_cast<double>(roi.height)) {
  112. // 近竖直线 = 钢缆 / 油管:只作辅助诊断,按沿线 12 点均值判暗。
  113. double sum = 0.0;
  114. int n = 0;
  115. for (int i = 0; i < 12; ++i) {
  116. const double t = static_cast<double>(i) / 11.0;
  117. const int xs = static_cast<int>(std::lround(s.x1 + (s.x2 - s.x1) * t));
  118. const int ys = static_cast<int>(std::lround(s.y1 + (s.y2 - s.y1) * t));
  119. if (xs < 0 || xs >= roi.width || ys < 0 || ys >= roi.height) { continue; }
  120. sum += static_cast<double>(roi.at(xs, ys));
  121. ++n;
  122. }
  123. if (n > 0 && sum / n <= static_cast<double>(p.dark_max) + 25.0) { ++out.cables; }
  124. }
  125. }
  126. out.hoist_high = out.horizontal_lines >= 1;
  127. return out;
  128. }
  129. SpreaderCues detect_spreader_contact(const GrayImage& gray, int x0, int y0, int x1, int y1,
  130. const SpreaderGateParams& p) {
  131. SpreaderCues out;
  132. if (gray.empty()) { return out; }
  133. const int h = gray.height;
  134. const int w = gray.width;
  135. const int xa = std::max(0, std::min(x0, x1));
  136. const int xb = std::min(w, std::max(x0, x1));
  137. const int ya = std::max(0, std::min(y0, y1));
  138. const int yb = std::min(h, std::max(y0, y1));
  139. const int box_h = yb - ya;
  140. const int box_w = xb - xa;
  141. if (box_h < 20 || box_w < 20) { return out; }
  142. // 箱顶以上 30% 箱高的窄条:吊具未松开时这里必有一条横贯箱宽的暗梁。
  143. const int probe = std::max(4, static_cast<int>(0.30 * box_h));
  144. const int ry0 = std::max(0, ya - probe);
  145. const int rows = ya - ry0;
  146. if (rows < 3) { return out; }
  147. const int min_run = std::max(3, static_cast<int>(0.02 * box_h));
  148. int best_start = -1;
  149. int best_end = -1;
  150. int run_start = -1;
  151. // 优先离箱顶最近的一条暗梁(吊具夹着箱时它紧贴箱顶);同样近时取更厚的一条。
  152. const auto close_run = [&](int end_row) {
  153. if (run_start < 0) { return; }
  154. if (end_row - run_start + 1 >= min_run) {
  155. const bool better =
  156. best_start < 0 || (rows - 1 - end_row) < (rows - 1 - best_end) ||
  157. ((rows - 1 - end_row) == (rows - 1 - best_end) &&
  158. (end_row - run_start) > (best_end - best_start));
  159. if (better) {
  160. best_start = run_start;
  161. best_end = end_row;
  162. }
  163. }
  164. run_start = -1;
  165. };
  166. for (int row = 0; row < rows; ++row) {
  167. double dark = 0.0;
  168. mean_and_dark(gray, xa, xb, ry0 + row, ry0 + row + 1, p.dark_max, nullptr, &dark);
  169. if (dark >= p.dark_ratio) {
  170. if (run_start < 0) { run_start = row; }
  171. } else {
  172. close_run(row - 1);
  173. }
  174. }
  175. close_run(rows - 1);
  176. if (best_start < 0) { return out; }
  177. if ((rows - 1 - best_end) > std::max(2, static_cast<int>(0.08 * box_h))) { return out; }
  178. double band_mean = 0.0;
  179. mean_and_dark(gray, xa, xb, ry0 + best_start, ry0 + best_end + 1, p.dark_max, &band_mean,
  180. nullptr);
  181. double below_mean = 0.0;
  182. mean_and_dark(gray, xa, xb, ya, std::min(h, ya + std::max(6, static_cast<int>(0.06 * box_h))),
  183. p.dark_max, &below_mean, nullptr);
  184. if (below_mean < band_mean + 10.0) { return out; }
  185. out.contact = true;
  186. out.dark_run_px = best_end - best_start + 1;
  187. out.band_delta = std::round((below_mean - band_mean) * 100.0) / 100.0;
  188. return out;
  189. }
  190. } // namespace portcv