edge_image.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // 二维边缘几何证据 · L0/L1 层:灰度图容器与基础算子(**零外部依赖**)。
  2. //
  3. // 为什么自己写算子(而不是直接上 OpenCV):
  4. // * 交付形态要求"核心零外部依赖"(见 cpp/README.md §1);`-DPORTCV_WITH_OPENCV=ON`
  5. // 只是可选开关(docs/edge_evidence.md §11 第 9 条仍待现场确认),因此证据提取需要的
  6. // 降采样 / 中值 / 高斯 / 分块直方图均衡 / Sobel / 分位数 / 画线与多边形填充,
  7. // 全部在这里以最小实现给出;
  8. // * 所有实现都是**确定性**的(不用随机数、不做浮点归约顺序依赖),
  9. // 这样合成场景回归(docs/edge_evidence.md §9.1 ①)才能进 CI 且结果可复现。
  10. //
  11. // 坐标约定:原点左上角、y 向下增大 —— 与 schema.hpp 的 Box 完全一致。
  12. #pragma once
  13. #include <cstdint>
  14. #include <utility>
  15. #include <vector>
  16. namespace portcv {
  17. /// 8 位灰度图(行主序:`data[y * width + x]`)。
  18. struct GrayImage {
  19. int width = 0;
  20. int height = 0;
  21. std::vector<uint8_t> data;
  22. GrayImage() = default;
  23. GrayImage(int w, int h, uint8_t fill = 0) { reset(w, h, fill); }
  24. /// 重新分配并填充(w/h <= 0 时清空)。
  25. void reset(int w, int h, uint8_t fill = 0) {
  26. width = w > 0 ? w : 0;
  27. height = h > 0 ? h : 0;
  28. data.assign(static_cast<std::size_t>(width) * static_cast<std::size_t>(height), fill);
  29. }
  30. bool empty() const { return width <= 0 || height <= 0 || data.empty(); }
  31. /// 读像素;越界返回 0(算子内部大量使用"带边界的采样",避免到处写 if)。
  32. uint8_t at(int x, int y) const {
  33. if (x < 0 || y < 0 || x >= width || y >= height) { return 0; }
  34. return data[static_cast<std::size_t>(y) * static_cast<std::size_t>(width) +
  35. static_cast<std::size_t>(x)];
  36. }
  37. /// 写像素;越界忽略。
  38. void set(int x, int y, uint8_t v) {
  39. if (x < 0 || y < 0 || x >= width || y >= height) { return; }
  40. data[static_cast<std::size_t>(y) * static_cast<std::size_t>(width) +
  41. static_cast<std::size_t>(x)] = v;
  42. }
  43. /// 双线性采样(越界钳制到边缘);合成器与亚像素统计用。
  44. double sample_bilinear(double x, double y) const;
  45. /// 与已有值按 alpha 混合(alpha ∈ [0,1]),用于合成器的抗锯齿绘制。
  46. void blend(int x, int y, double value, double alpha);
  47. };
  48. /// 直方图分位数(pct ∈ [0,1])。空图返回 0。
  49. /// 说明:证据阈值全部走"分位数"而不是固定灰度(docs/edge_evidence.md §3 开头),
  50. /// 就是为了在逆光 / 雪地 / 夜间红外下不失效。
  51. uint8_t percentile_u8(const GrayImage& img, double pct);
  52. /// 浮点数组分位数(会拷贝并排序;调用点都是小规模统计,量级可控)。
  53. float percentile(const std::vector<float>& values, double pct);
  54. // ---------------------------------------------------------------- 预处理(§3.1)
  55. /// 长边缩放到 target_long(保持纵横比);`scale` 回传实际缩放系数(像素/源像素)。
  56. GrayImage resize_long_side(const GrayImage& src, int target_long, double* scale = nullptr);
  57. /// 盒式降采样(scale < 1 时有意义);用于运动门控的缩略图。
  58. GrayImage box_downscale(const GrayImage& src, int dst_w, int dst_h);
  59. /// 3×3 中值滤波(就地):抑制雨雪颗粒与椒盐噪声。
  60. void median3x3_inplace(GrayImage* img);
  61. /// 3×3 高斯滤波(就地,核 [1 2 1]⊗[1 2 1]/16):轻度平滑但保留直边。
  62. void gaussian3x3_inplace(GrayImage* img);
  63. /// 大核中值(合成器 / 强降质自检用),radius 为半径(窗口 (2r+1)²)。
  64. void median_kernel_inplace(GrayImage* img, int radius);
  65. /// 盒式模糊(半径 r),运动模糊模拟用。
  66. void box_blur_inplace(GrayImage* img, int radius);
  67. /// CLAHE 精简版:分块直方图均衡 + 对比度限幅 + 双线性插值(抗逆光 / 雪地高亮 / 夜间低对比)。
  68. /// tiles 为每边的分块数(默认 8),clip 为限幅系数(相对块内均值,默认 3.0)。
  69. void clahe_lite_inplace(GrayImage* img, int tiles = 8, double clip = 3.0);
  70. /// 亮度/对比度映射:v' = clamp((v - 128) * contrast + 128 * exposure)(合成降质用)。
  71. void apply_exposure_contrast_inplace(GrayImage* img, double exposure, double contrast);
  72. /// 叠加确定性噪声(自实现 xorshift,跨平台一致)。
  73. void add_noise_inplace(GrayImage* img, double sigma, uint32_t seed);
  74. // ---------------------------------------------------------------- 梯度(§3.1)
  75. /// Sobel 梯度(3×3,边界按复制处理)。`mag = sqrt(gx² + gy²)`。
  76. struct SobelResult {
  77. int width = 0;
  78. int height = 0;
  79. std::vector<float> gx;
  80. std::vector<float> gy;
  81. std::vector<float> mag;
  82. float mag_at(int x, int y) const {
  83. if (x < 0 || y < 0 || x >= width || y >= height) { return 0.0f; }
  84. return mag[static_cast<std::size_t>(y) * static_cast<std::size_t>(width) +
  85. static_cast<std::size_t>(x)];
  86. }
  87. /// 平均梯度幅值 —— 运动模糊指标(§6 ③:模糊时梯度幅值整体下降)。
  88. double mean_mag() const;
  89. };
  90. SobelResult sobel(const GrayImage& img);
  91. // ---------------------------------------------------------------- 绘制(合成器用)
  92. /// 画一条抗锯齿的线段(thickness 为像素宽度)。
  93. void draw_line_aa(GrayImage* img, double x0, double y0, double x1, double y1, double value,
  94. double thickness = 1.0);
  95. /// 填充多边形(4×4 超采样抗锯齿;多边形顶点为浮点像素坐标)。
  96. void fill_polygon_aa(GrayImage* img, const std::vector<std::pair<double, double>>& poly,
  97. double value);
  98. /// 在整幅图上叠加低频渐变(模拟天空 / 光照不均)。
  99. void add_gradient_inplace(GrayImage* img, double top_value, double bottom_value);
  100. } // namespace portcv