// 二维边缘几何证据 · L0/L1 实现:基础图像算子(零外部依赖,确定性)。 // // 对应规范:docs/edge_evidence.md §3.1(预处理)/ §3.5 ④(强度用分位数)/ §3.7(强台阶)。 // 设计取舍: // * 缩放/滤波都用"盒式平均 / 小核卷积"这类确定性算子,避免不同平台浮点归约差异; // * 分位数(percentile)是全部阈值的来源:固定灰度阈值在逆光 / 雪 / 夜 / 红外下必然失效。 #include "portcv/edge_image.hpp" #include #include #include namespace portcv { namespace { inline uint8_t clamp_u8(double v) { if (v <= 0.0) { return 0; } if (v >= 255.0) { return 255; } return static_cast(v + 0.5); } inline std::size_t pixel_index(const GrayImage& img, int x, int y) { return static_cast(y) * static_cast(img.width) + static_cast(x); } /// 边界**复制**采样:邻域算子(中值 / 高斯 / Sobel)在画幅最外一圈也必须有确定语义。 /// 历史缺陷:把越界像素当 0 灰度参与运算,会在画面最外一圈凭空造出"暗边框" /// → 整圈变成强边缘:既污染强弱边分位数门(§3.5 ④),又会给 F1/G1 之类的 /// "贴边结构线"判据送进假结构。真实相机边缘不是黑的,复制延拓才是中性选择。 inline uint8_t at_clamped(const GrayImage& img, int x, int y) { if (x < 0) { x = 0; } if (y < 0) { y = 0; } if (x >= img.width) { x = img.width - 1; } if (y >= img.height) { y = img.height - 1; } return img.at(x, y); } /// 9 元素内联排序取中位(3×3 中值专用,比 nth_element 更省)。 inline uint8_t median9(uint8_t v[9]) { for (int i = 1; i < 9; ++i) { const uint8_t key = v[i]; int j = i - 1; while (j >= 0 && v[j] > key) { v[j + 1] = v[j]; --j; } v[j + 1] = key; } return v[4]; } /// xorshift32:自实现 PRNG(不依赖 的实现细节,跨平台/跨编译器结果一致)。 struct XorShift32 { uint32_t s; explicit XorShift32(uint32_t seed) : s(seed ? seed : 0x9E3779B9u) {} uint32_t next() { s ^= s << 13; s ^= s >> 17; s ^= s << 5; return s; } /// [-1, 1] double uniform_signed() { return static_cast(next() % 20001) / 10000.0 - 1.0; } }; } // namespace // ---------------------------------------------------------------- 采样 / 混合 double GrayImage::sample_bilinear(double x, double y) const { if (empty()) { return 0.0; } if (x < 0.0) { x = 0.0; } if (y < 0.0) { y = 0.0; } if (x > width - 1) { x = width - 1; } if (y > height - 1) { y = height - 1; } const int x0 = static_cast(x); const int y0 = static_cast(y); const int x1 = x0 + 1 < width ? x0 + 1 : x0; const int y1 = y0 + 1 < height ? y0 + 1 : y0; const double fx = x - x0; const double fy = y - y0; const double top = at(x0, y0) * (1.0 - fx) + at(x1, y0) * fx; const double bot = at(x0, y1) * (1.0 - fx) + at(x1, y1) * fx; return top * (1.0 - fy) + bot * fy; } void GrayImage::blend(int x, int y, double value, double alpha) { if (x < 0 || y < 0 || x >= width || y >= height) { return; } if (alpha <= 0.0) { return; } if (alpha > 1.0) { alpha = 1.0; } uint8_t& p = data[pixel_index(*this, x, y)]; p = clamp_u8(static_cast(p) * (1.0 - alpha) + value * alpha); } // ---------------------------------------------------------------- 统计 uint8_t percentile_u8(const GrayImage& img, double pct) { if (img.empty()) { return 0; } if (pct < 0.0) { pct = 0.0; } if (pct > 1.0) { pct = 1.0; } std::size_t hist[256] = {0}; for (uint8_t v : img.data) { ++hist[v]; } const std::size_t want = static_cast( pct * static_cast(img.data.size() - 1) + 0.5); std::size_t acc = 0; for (int v = 0; v < 256; ++v) { acc += hist[v]; if (acc > want) { return static_cast(v); } } return 255; } float percentile(const std::vector& values, double pct) { if (values.empty()) { return 0.0f; } if (pct < 0.0) { pct = 0.0; } if (pct > 1.0) { pct = 1.0; } std::vector copy(values); const std::size_t k = static_cast( pct * static_cast(copy.size() - 1) + 0.5); std::nth_element(copy.begin(), copy.begin() + static_cast(k), copy.end()); return copy[k]; } // ---------------------------------------------------------------- 缩放(§3.1 降采样) GrayImage resize_long_side(const GrayImage& src, int target_long, double* scale) { GrayImage out; if (src.empty()) { if (scale) { *scale = 1.0; } return out; } const int long_side = src.width > src.height ? src.width : src.height; if (target_long <= 0 || long_side <= target_long) { if (scale) { *scale = 1.0; } return src; // 只降不升:小图保持原样,避免"放大造边缘" } const double s = static_cast(target_long) / static_cast(long_side); const int dw = std::max(1, static_cast(static_cast(src.width) * s + 0.5)); const int dh = std::max(1, static_cast(static_cast(src.height) * s + 0.5)); if (scale) { *scale = static_cast(dw) / static_cast(src.width); } out.reset(dw, dh, 0); for (int y = 0; y < dh; ++y) { const int sy0 = static_cast(static_cast(y) * src.height / dh); int sy1 = static_cast(static_cast(y + 1) * src.height / dh); if (sy1 <= sy0) { sy1 = sy0 + 1; } for (int x = 0; x < dw; ++x) { const int sx0 = static_cast(static_cast(x) * src.width / dw); int sx1 = static_cast(static_cast(x + 1) * src.width / dw); if (sx1 <= sx0) { sx1 = sx0 + 1; } double sum = 0.0; int n = 0; for (int sy = sy0; sy < sy1 && sy < src.height; ++sy) { for (int sx = sx0; sx < sx1 && sx < src.width; ++sx) { sum += src.at(sx, sy); ++n; } } out.set(x, y, clamp_u8(n > 0 ? sum / n : 0.0)); } } return out; } GrayImage box_downscale(const GrayImage& src, int dst_w, int dst_h) { GrayImage out; if (src.empty() || dst_w <= 0 || dst_h <= 0) { return out; } if (dst_w >= src.width || dst_h >= src.height) { return src; } out.reset(dst_w, dst_h, 0); for (int y = 0; y < dst_h; ++y) { const int sy0 = static_cast(static_cast(y) * src.height / dst_h); int sy1 = static_cast(static_cast(y + 1) * src.height / dst_h); if (sy1 <= sy0) { sy1 = sy0 + 1; } for (int x = 0; x < dst_w; ++x) { const int sx0 = static_cast(static_cast(x) * src.width / dst_w); int sx1 = static_cast(static_cast(x + 1) * src.width / dst_w); if (sx1 <= sx0) { sx1 = sx0 + 1; } double sum = 0.0; int n = 0; for (int sy = sy0; sy < sy1 && sy < src.height; ++sy) { for (int sx = sx0; sx < sx1 && sx < src.width; ++sx) { sum += src.at(sx, sy); ++n; } } out.set(x, y, clamp_u8(n > 0 ? sum / n : 0.0)); } } return out; } // ---------------------------------------------------------------- 去噪(§3.1) void median3x3_inplace(GrayImage* img) { if (img == nullptr || img->empty()) { return; } const GrayImage src = *img; for (int y = 0; y < src.height; ++y) { for (int x = 0; x < src.width; ++x) { uint8_t w[9]; int i = 0; for (int dy = -1; dy <= 1; ++dy) { for (int dx = -1; dx <= 1; ++dx) { w[i++] = at_clamped(src, x + dx, y + dy); } } img->set(x, y, median9(w)); } } } void gaussian3x3_inplace(GrayImage* img) { if (img == nullptr || img->empty()) { return; } const GrayImage src = *img; // 核 = [1 2 1] ⊗ [1 2 1] / 16(边界按"复制"处理) for (int y = 0; y < src.height; ++y) { for (int x = 0; x < src.width; ++x) { double acc = 0.0; for (int dy = -1; dy <= 1; ++dy) { const int wy = (dy == 0) ? 2 : 1; for (int dx = -1; dx <= 1; ++dx) { const int wx = (dx == 0) ? 2 : 1; acc += static_cast(wx * wy) * static_cast(at_clamped(src, x + dx, y + dy)); } } img->set(x, y, clamp_u8(acc / 16.0)); } } } void median_kernel_inplace(GrayImage* img, int radius) { if (img == nullptr || img->empty()) { return; } if (radius <= 0) { median3x3_inplace(img); return; } const GrayImage src = *img; std::vector window; window.reserve(static_cast((2 * radius + 1) * (2 * radius + 1))); for (int y = 0; y < src.height; ++y) { for (int x = 0; x < src.width; ++x) { window.clear(); for (int dy = -radius; dy <= radius; ++dy) { for (int dx = -radius; dx <= radius; ++dx) { window.push_back(at_clamped(src, x + dx, y + dy)); } } const std::size_t mid = window.size() / 2; std::nth_element(window.begin(), window.begin() + static_cast(mid), window.end()); img->set(x, y, window[mid]); } } } void box_blur_inplace(GrayImage* img, int radius) { if (img == nullptr || img->empty() || radius <= 0) { return; } const int w = img->width; const int h = img->height; const double inv = 1.0 / static_cast(2 * radius + 1); // 两趟(横向 + 纵向)各算一次"窗口和",所以归一化因子是 1/(2r+1)^2。 // 若只归一化一趟,结果会被放大 (2r+1) 倍并直接饱和(曾经的缺陷:r=4 时整行被夹到 255, // 模糊反而让 Sobel 幅值变大 → 运动模糊门控与降质自检全部失真)。 const double inv2 = inv * inv; std::vector tmp(static_cast(w) * h, 0); auto clamp_x = [w](int x) { return x < 0 ? 0 : (x >= w ? w - 1 : x); }; auto clamp_y = [h](int y) { return y < 0 ? 0 : (y >= h ? h - 1 : y); }; for (int y = 0; y < h; ++y) { // 横向:滑动窗口 int acc = 0; for (int k = -radius; k <= radius; ++k) { acc += img->at(clamp_x(k), y); } for (int x = 0; x < w; ++x) { tmp[static_cast(y) * w + x] = acc; acc -= img->at(clamp_x(x - radius), y); acc += img->at(clamp_x(x + radius + 1), y); } } for (int x = 0; x < w; ++x) { // 纵向 int acc = 0; for (int k = -radius; k <= radius; ++k) { acc += tmp[static_cast(clamp_y(k)) * w + x]; } for (int y = 0; y < h; ++y) { img->set(x, y, clamp_u8(static_cast(acc) * inv2)); acc -= tmp[static_cast(clamp_y(y - radius)) * w + x]; acc += tmp[static_cast(clamp_y(y + radius + 1)) * w + x]; } } } // ---------------------------------------------------------------- 光照归一(§3.1) void clahe_lite_inplace(GrayImage* img, int tiles, double clip) { if (img == nullptr || img->empty()) { return; } if (tiles < 1) { tiles = 1; } const int w = img->width; const int h = img->height; const int btw = (w + tiles - 1) / tiles; const int bth = (h + tiles - 1) / tiles; // ① 每块做"限幅直方图均衡",得到 256 项 LUT std::vector> luts(static_cast(tiles * tiles), std::vector(256)); for (int ty = 0; ty < tiles; ++ty) { for (int tx = 0; tx < tiles; ++tx) { const int x0 = tx * btw; const int y0 = ty * bth; const int x1 = std::min(w, x0 + btw); const int y1 = std::min(h, y0 + bth); std::vector& lut = luts[static_cast(ty) * tiles + tx]; if (x1 <= x0 || y1 <= y0) { // 空块 → 恒等映射 for (int v = 0; v < 256; ++v) { lut[static_cast(v)] = static_cast(v); } continue; } int hist[256] = {0}; int n = 0; for (int y = y0; y < y1; ++y) { for (int x = x0; x < x1; ++x) { ++hist[img->at(x, y)]; ++n; } } const int limit = std::max(1, static_cast(clip * static_cast(n) / 256.0)); int excess = 0; for (int v = 0; v < 256; ++v) { if (hist[v] > limit) { excess += hist[v] - limit; hist[v] = limit; } } const int share = excess / 256; const int rem = excess % 256; for (int v = 0; v < 256; ++v) { hist[v] += share; } for (int v = 0; v < rem; ++v) { hist[v] += 1; } int acc = 0; for (int v = 0; v < 256; ++v) { acc += hist[v]; const double scaled = 255.0 * static_cast(acc) / static_cast(n); lut[static_cast(v)] = static_cast(scaled < 0.0 ? 0.0 : (scaled > 255.0 ? 255.0 : scaled)); } } } // ② 逐像素在相邻 4 块 LUT 之间双线性插值(块中心对齐),避免块边界出现硬接缝 const GrayImage src = *img; auto lut_at = [&luts, tiles](int tx, int ty, int v) -> double { if (tx < 0) { tx = 0; } if (ty < 0) { ty = 0; } if (tx > tiles - 1) { tx = tiles - 1; } if (ty > tiles - 1) { ty = tiles - 1; } return static_cast(luts[static_cast(ty) * tiles + tx] [static_cast(v)]); }; for (int y = 0; y < h; ++y) { const double fy = (static_cast(y) - (bth - 1) * 0.5) / bth; const int ty0 = static_cast(std::floor(fy)); const double ay = fy - ty0; for (int x = 0; x < w; ++x) { const double fx = (static_cast(x) - (btw - 1) * 0.5) / btw; const int tx0 = static_cast(std::floor(fx)); const double ax = fx - tx0; const int v = src.at(x, y); const double v00 = lut_at(tx0, ty0, v); const double v10 = lut_at(tx0 + 1, ty0, v); const double v01 = lut_at(tx0, ty0 + 1, v); const double v11 = lut_at(tx0 + 1, ty0 + 1, v); const double a = v00 * (1.0 - ax) + v10 * ax; const double b = v01 * (1.0 - ax) + v11 * ax; img->set(x, y, clamp_u8(a * (1.0 - ay) + b * ay)); } } } // ---------------------------------------------------------------- 降质(自检 / 合成用) void apply_exposure_contrast_inplace(GrayImage* img, double exposure, double contrast) { if (img == nullptr || img->empty()) { return; } for (uint8_t& p : img->data) { const double v = (static_cast(p) - 128.0) * contrast + 128.0 * exposure; p = clamp_u8(v); } } void add_noise_inplace(GrayImage* img, double sigma, uint32_t seed) { if (img == nullptr || img->empty() || sigma <= 0.0) { return; } XorShift32 rng(seed); for (uint8_t& p : img->data) { p = clamp_u8(static_cast(p) + rng.uniform_signed() * sigma); } } // ---------------------------------------------------------------- 梯度(§3.1) double SobelResult::mean_mag() const { if (mag.empty()) { return 0.0; } double sum = 0.0; for (float v : mag) { sum += static_cast(v); } return sum / static_cast(mag.size()); } SobelResult sobel(const GrayImage& img) { SobelResult out; if (img.empty()) { return out; } out.width = img.width; out.height = img.height; const std::size_t n = static_cast(img.width) * static_cast(img.height); out.gx.assign(n, 0.0f); out.gy.assign(n, 0.0f); out.mag.assign(n, 0.0f); for (int y = 0; y < img.height; ++y) { for (int x = 0; x < img.width; ++x) { // 边界复制(与头文件声明的语义一致):越界当 0 会让画面最外一圈的 // 幅值虚高(实测 170 阶跃上 max_mag 890 → 复制延拓后为 680), // 进而把整圈边框当成"强边缘"混进线段集合。 const double tl = at_clamped(img, x - 1, y - 1); const double tc = at_clamped(img, x, y - 1); const double tr = at_clamped(img, x + 1, y - 1); const double ml = at_clamped(img, x - 1, y); const double mr = at_clamped(img, x + 1, y); const double bl = at_clamped(img, x - 1, y + 1); const double bc = at_clamped(img, x, y + 1); const double br = at_clamped(img, x + 1, y + 1); const double gx = (tr + 2.0 * mr + br) - (tl + 2.0 * ml + bl); const double gy = (bl + 2.0 * bc + br) - (tl + 2.0 * tc + tr); const std::size_t i = static_cast(y) * static_cast(img.width) + static_cast(x); out.gx[i] = static_cast(gx); out.gy[i] = static_cast(gy); out.mag[i] = static_cast(std::sqrt(gx * gx + gy * gy)); } } return out; } // ---------------------------------------------------------------- 绘制(合成器用) void draw_line_aa(GrayImage* img, double x0, double y0, double x1, double y1, double value, double thickness) { if (img == nullptr || img->empty()) { return; } const double half = thickness * 0.5; const int ix0 = static_cast(std::floor(std::min(x0, x1) - half - 1.0)); const int ix1 = static_cast(std::ceil(std::max(x0, x1) + half + 1.0)); const int iy0 = static_cast(std::floor(std::min(y0, y1) - half - 1.0)); const int iy1 = static_cast(std::ceil(std::max(y0, y1) + half + 1.0)); const double dx = x1 - x0; const double dy = y1 - y0; const double len2 = dx * dx + dy * dy; for (int y = iy0; y <= iy1; ++y) { for (int x = ix0; x <= ix1; ++x) { const double px = x + 0.5; const double py = y + 0.5; double t = 0.0; if (len2 > 1e-9) { t = ((px - x0) * dx + (py - y0) * dy) / len2; if (t < 0.0) { t = 0.0; } if (t > 1.0) { t = 1.0; } } const double cx = x0 + t * dx; const double cy = y0 + t * dy; const double dist = std::sqrt((px - cx) * (px - cx) + (py - cy) * (py - cy)); const double cov = 0.5 + half - dist; // 覆盖度(像素中心到线的距离) if (cov > 0.0) { img->blend(x, y, value, cov > 1.0 ? 1.0 : cov); } } } } void fill_polygon_aa(GrayImage* img, const std::vector>& poly, double value) { if (img == nullptr || img->empty() || poly.size() < 3) { return; } double min_x = poly[0].first; double max_x = poly[0].first; double min_y = poly[0].second; double max_y = poly[0].second; for (const auto& p : poly) { min_x = std::min(min_x, p.first); max_x = std::max(max_x, p.first); min_y = std::min(min_y, p.second); max_y = std::max(max_y, p.second); } const int bx0 = std::max(0, static_cast(std::floor(min_x - 1.0))); const int bx1 = std::min(img->width - 1, static_cast(std::ceil(max_x + 1.0))); const int by0 = std::max(0, static_cast(std::floor(min_y - 1.0))); const int by1 = std::min(img->height - 1, static_cast(std::ceil(max_y + 1.0))); if (bx1 < bx0 || by1 < by0) { return; } // 每条扫描线取 4 条子扫描线(1/4 像素)→ 每个像素得到 0..1 的覆盖度, // 这是"抗锯齿多边形填充"的简化实现:既省算力,又让灰度直方图/分位数稳定。 const int sub = 4; std::vector cover(static_cast(bx1 - bx0 + 1), 0.0); std::vector xs; for (int py = by0; py <= by1; ++py) { std::fill(cover.begin(), cover.end(), 0.0); bool any = false; for (int k = 0; k < sub; ++k) { const double y = static_cast(py) + 0.125 + 0.25 * static_cast(k); xs.clear(); for (std::size_t i = 0; i < poly.size(); ++i) { const std::pair& a = poly[i]; const std::pair& b = poly[(i + 1) % poly.size()]; if ((a.second <= y && b.second > y) || (b.second <= y && a.second > y)) { const double t = (y - a.second) / (b.second - a.second); xs.push_back(a.first + t * (b.first - a.first)); } } if (xs.size() < 2) { continue; } std::sort(xs.begin(), xs.end()); for (std::size_t i = 0; i + 1 < xs.size(); i += 2) { const double xa = xs[i]; const double xb = xs[i + 1]; const int ia = std::max(bx0, static_cast(std::ceil(xa - 0.5))); const int ib = std::min(bx1, static_cast(std::floor(xb - 0.5))); for (int ix = ia; ix <= ib; ++ix) { const double l = std::max(xa, static_cast(ix) - 0.5); const double r = std::min(xb, static_cast(ix) + 0.5); if (r > l) { cover[static_cast(ix - bx0)] += (r - l); any = true; } } } } if (!any) { continue; } for (int ix = bx0; ix <= bx1; ++ix) { const double c = cover[static_cast(ix - bx0)] / static_cast(sub); if (c > 0.0) { img->blend(ix, py, value, c); } } } } void add_gradient_inplace(GrayImage* img, double top_value, double bottom_value) { if (img == nullptr || img->empty()) { return; } for (int y = 0; y < img->height; ++y) { const double t = img->height > 1 ? static_cast(y) / static_cast(img->height - 1) : 0.0; const double delta = top_value * (1.0 - t) + bottom_value * t; for (int x = 0; x < img->width; ++x) { img->set(x, y, clamp_u8(static_cast(img->at(x, y)) + delta)); } } } } // namespace portcv