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