edge_image.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // 二维边缘几何证据 · L0/L1 实现:基础图像算子(零外部依赖,确定性)。
  2. //
  3. // 对应规范:docs/edge_evidence.md §3.1(预处理)/ §3.5 ④(强度用分位数)/ §3.7(强台阶)。
  4. // 设计取舍:
  5. // * 缩放/滤波都用"盒式平均 / 小核卷积"这类确定性算子,避免不同平台浮点归约差异;
  6. // * 分位数(percentile)是全部阈值的来源:固定灰度阈值在逆光 / 雪 / 夜 / 红外下必然失效。
  7. #include "portcv/edge_image.hpp"
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <cstddef>
  11. namespace portcv {
  12. namespace {
  13. inline uint8_t clamp_u8(double v) {
  14. if (v <= 0.0) { return 0; }
  15. if (v >= 255.0) { return 255; }
  16. return static_cast<uint8_t>(v + 0.5);
  17. }
  18. inline std::size_t pixel_index(const GrayImage& img, int x, int y) {
  19. return static_cast<std::size_t>(y) * static_cast<std::size_t>(img.width) +
  20. static_cast<std::size_t>(x);
  21. }
  22. /// 边界**复制**采样:邻域算子(中值 / 高斯 / Sobel)在画幅最外一圈也必须有确定语义。
  23. /// 历史缺陷:把越界像素当 0 灰度参与运算,会在画面最外一圈凭空造出"暗边框"
  24. /// → 整圈变成强边缘:既污染强弱边分位数门(§3.5 ④),又会给 F1/G1 之类的
  25. /// "贴边结构线"判据送进假结构。真实相机边缘不是黑的,复制延拓才是中性选择。
  26. inline uint8_t at_clamped(const GrayImage& img, int x, int y) {
  27. if (x < 0) { x = 0; }
  28. if (y < 0) { y = 0; }
  29. if (x >= img.width) { x = img.width - 1; }
  30. if (y >= img.height) { y = img.height - 1; }
  31. return img.at(x, y);
  32. }
  33. /// 9 元素内联排序取中位(3×3 中值专用,比 nth_element 更省)。
  34. inline uint8_t median9(uint8_t v[9]) {
  35. for (int i = 1; i < 9; ++i) {
  36. const uint8_t key = v[i];
  37. int j = i - 1;
  38. while (j >= 0 && v[j] > key) {
  39. v[j + 1] = v[j];
  40. --j;
  41. }
  42. v[j + 1] = key;
  43. }
  44. return v[4];
  45. }
  46. /// xorshift32:自实现 PRNG(不依赖 <random> 的实现细节,跨平台/跨编译器结果一致)。
  47. struct XorShift32 {
  48. uint32_t s;
  49. explicit XorShift32(uint32_t seed) : s(seed ? seed : 0x9E3779B9u) {}
  50. uint32_t next() {
  51. s ^= s << 13;
  52. s ^= s >> 17;
  53. s ^= s << 5;
  54. return s;
  55. }
  56. /// [-1, 1]
  57. double uniform_signed() {
  58. return static_cast<double>(next() % 20001) / 10000.0 - 1.0;
  59. }
  60. };
  61. } // namespace
  62. // ---------------------------------------------------------------- 采样 / 混合
  63. double GrayImage::sample_bilinear(double x, double y) const {
  64. if (empty()) { return 0.0; }
  65. if (x < 0.0) { x = 0.0; }
  66. if (y < 0.0) { y = 0.0; }
  67. if (x > width - 1) { x = width - 1; }
  68. if (y > height - 1) { y = height - 1; }
  69. const int x0 = static_cast<int>(x);
  70. const int y0 = static_cast<int>(y);
  71. const int x1 = x0 + 1 < width ? x0 + 1 : x0;
  72. const int y1 = y0 + 1 < height ? y0 + 1 : y0;
  73. const double fx = x - x0;
  74. const double fy = y - y0;
  75. const double top = at(x0, y0) * (1.0 - fx) + at(x1, y0) * fx;
  76. const double bot = at(x0, y1) * (1.0 - fx) + at(x1, y1) * fx;
  77. return top * (1.0 - fy) + bot * fy;
  78. }
  79. void GrayImage::blend(int x, int y, double value, double alpha) {
  80. if (x < 0 || y < 0 || x >= width || y >= height) { return; }
  81. if (alpha <= 0.0) { return; }
  82. if (alpha > 1.0) { alpha = 1.0; }
  83. uint8_t& p = data[pixel_index(*this, x, y)];
  84. p = clamp_u8(static_cast<double>(p) * (1.0 - alpha) + value * alpha);
  85. }
  86. // ---------------------------------------------------------------- 统计
  87. uint8_t percentile_u8(const GrayImage& img, double pct) {
  88. if (img.empty()) { return 0; }
  89. if (pct < 0.0) { pct = 0.0; }
  90. if (pct > 1.0) { pct = 1.0; }
  91. std::size_t hist[256] = {0};
  92. for (uint8_t v : img.data) { ++hist[v]; }
  93. const std::size_t want = static_cast<std::size_t>(
  94. pct * static_cast<double>(img.data.size() - 1) + 0.5);
  95. std::size_t acc = 0;
  96. for (int v = 0; v < 256; ++v) {
  97. acc += hist[v];
  98. if (acc > want) { return static_cast<uint8_t>(v); }
  99. }
  100. return 255;
  101. }
  102. float percentile(const std::vector<float>& values, double pct) {
  103. if (values.empty()) { return 0.0f; }
  104. if (pct < 0.0) { pct = 0.0; }
  105. if (pct > 1.0) { pct = 1.0; }
  106. std::vector<float> copy(values);
  107. const std::size_t k = static_cast<std::size_t>(
  108. pct * static_cast<double>(copy.size() - 1) + 0.5);
  109. std::nth_element(copy.begin(), copy.begin() + static_cast<std::ptrdiff_t>(k), copy.end());
  110. return copy[k];
  111. }
  112. // ---------------------------------------------------------------- 缩放(§3.1 降采样)
  113. GrayImage resize_long_side(const GrayImage& src, int target_long, double* scale) {
  114. GrayImage out;
  115. if (src.empty()) {
  116. if (scale) { *scale = 1.0; }
  117. return out;
  118. }
  119. const int long_side = src.width > src.height ? src.width : src.height;
  120. if (target_long <= 0 || long_side <= target_long) {
  121. if (scale) { *scale = 1.0; }
  122. return src; // 只降不升:小图保持原样,避免"放大造边缘"
  123. }
  124. const double s = static_cast<double>(target_long) / static_cast<double>(long_side);
  125. const int dw = std::max(1, static_cast<int>(static_cast<double>(src.width) * s + 0.5));
  126. const int dh = std::max(1, static_cast<int>(static_cast<double>(src.height) * s + 0.5));
  127. if (scale) { *scale = static_cast<double>(dw) / static_cast<double>(src.width); }
  128. out.reset(dw, dh, 0);
  129. for (int y = 0; y < dh; ++y) {
  130. const int sy0 = static_cast<int>(static_cast<double>(y) * src.height / dh);
  131. int sy1 = static_cast<int>(static_cast<double>(y + 1) * src.height / dh);
  132. if (sy1 <= sy0) { sy1 = sy0 + 1; }
  133. for (int x = 0; x < dw; ++x) {
  134. const int sx0 = static_cast<int>(static_cast<double>(x) * src.width / dw);
  135. int sx1 = static_cast<int>(static_cast<double>(x + 1) * src.width / dw);
  136. if (sx1 <= sx0) { sx1 = sx0 + 1; }
  137. double sum = 0.0;
  138. int n = 0;
  139. for (int sy = sy0; sy < sy1 && sy < src.height; ++sy) {
  140. for (int sx = sx0; sx < sx1 && sx < src.width; ++sx) {
  141. sum += src.at(sx, sy);
  142. ++n;
  143. }
  144. }
  145. out.set(x, y, clamp_u8(n > 0 ? sum / n : 0.0));
  146. }
  147. }
  148. return out;
  149. }
  150. GrayImage box_downscale(const GrayImage& src, int dst_w, int dst_h) {
  151. GrayImage out;
  152. if (src.empty() || dst_w <= 0 || dst_h <= 0) { return out; }
  153. if (dst_w >= src.width || dst_h >= src.height) { return src; }
  154. out.reset(dst_w, dst_h, 0);
  155. for (int y = 0; y < dst_h; ++y) {
  156. const int sy0 = static_cast<int>(static_cast<double>(y) * src.height / dst_h);
  157. int sy1 = static_cast<int>(static_cast<double>(y + 1) * src.height / dst_h);
  158. if (sy1 <= sy0) { sy1 = sy0 + 1; }
  159. for (int x = 0; x < dst_w; ++x) {
  160. const int sx0 = static_cast<int>(static_cast<double>(x) * src.width / dst_w);
  161. int sx1 = static_cast<int>(static_cast<double>(x + 1) * src.width / dst_w);
  162. if (sx1 <= sx0) { sx1 = sx0 + 1; }
  163. double sum = 0.0;
  164. int n = 0;
  165. for (int sy = sy0; sy < sy1 && sy < src.height; ++sy) {
  166. for (int sx = sx0; sx < sx1 && sx < src.width; ++sx) {
  167. sum += src.at(sx, sy);
  168. ++n;
  169. }
  170. }
  171. out.set(x, y, clamp_u8(n > 0 ? sum / n : 0.0));
  172. }
  173. }
  174. return out;
  175. }
  176. // ---------------------------------------------------------------- 去噪(§3.1)
  177. void median3x3_inplace(GrayImage* img) {
  178. if (img == nullptr || img->empty()) { return; }
  179. const GrayImage src = *img;
  180. for (int y = 0; y < src.height; ++y) {
  181. for (int x = 0; x < src.width; ++x) {
  182. uint8_t w[9];
  183. int i = 0;
  184. for (int dy = -1; dy <= 1; ++dy) {
  185. for (int dx = -1; dx <= 1; ++dx) { w[i++] = at_clamped(src, x + dx, y + dy); }
  186. }
  187. img->set(x, y, median9(w));
  188. }
  189. }
  190. }
  191. void gaussian3x3_inplace(GrayImage* img) {
  192. if (img == nullptr || img->empty()) { return; }
  193. const GrayImage src = *img;
  194. // 核 = [1 2 1] ⊗ [1 2 1] / 16(边界按"复制"处理)
  195. for (int y = 0; y < src.height; ++y) {
  196. for (int x = 0; x < src.width; ++x) {
  197. double acc = 0.0;
  198. for (int dy = -1; dy <= 1; ++dy) {
  199. const int wy = (dy == 0) ? 2 : 1;
  200. for (int dx = -1; dx <= 1; ++dx) {
  201. const int wx = (dx == 0) ? 2 : 1;
  202. acc += static_cast<double>(wx * wy) *
  203. static_cast<double>(at_clamped(src, x + dx, y + dy));
  204. }
  205. }
  206. img->set(x, y, clamp_u8(acc / 16.0));
  207. }
  208. }
  209. }
  210. void median_kernel_inplace(GrayImage* img, int radius) {
  211. if (img == nullptr || img->empty()) { return; }
  212. if (radius <= 0) {
  213. median3x3_inplace(img);
  214. return;
  215. }
  216. const GrayImage src = *img;
  217. std::vector<uint8_t> window;
  218. window.reserve(static_cast<std::size_t>((2 * radius + 1) * (2 * radius + 1)));
  219. for (int y = 0; y < src.height; ++y) {
  220. for (int x = 0; x < src.width; ++x) {
  221. window.clear();
  222. for (int dy = -radius; dy <= radius; ++dy) {
  223. for (int dx = -radius; dx <= radius; ++dx) {
  224. window.push_back(at_clamped(src, x + dx, y + dy));
  225. }
  226. }
  227. const std::size_t mid = window.size() / 2;
  228. std::nth_element(window.begin(), window.begin() + static_cast<std::ptrdiff_t>(mid),
  229. window.end());
  230. img->set(x, y, window[mid]);
  231. }
  232. }
  233. }
  234. void box_blur_inplace(GrayImage* img, int radius) {
  235. if (img == nullptr || img->empty() || radius <= 0) { return; }
  236. const int w = img->width;
  237. const int h = img->height;
  238. const double inv = 1.0 / static_cast<double>(2 * radius + 1);
  239. // 两趟(横向 + 纵向)各算一次"窗口和",所以归一化因子是 1/(2r+1)^2。
  240. // 若只归一化一趟,结果会被放大 (2r+1) 倍并直接饱和(曾经的缺陷:r=4 时整行被夹到 255,
  241. // 模糊反而让 Sobel 幅值变大 → 运动模糊门控与降质自检全部失真)。
  242. const double inv2 = inv * inv;
  243. std::vector<int> tmp(static_cast<std::size_t>(w) * h, 0);
  244. auto clamp_x = [w](int x) { return x < 0 ? 0 : (x >= w ? w - 1 : x); };
  245. auto clamp_y = [h](int y) { return y < 0 ? 0 : (y >= h ? h - 1 : y); };
  246. for (int y = 0; y < h; ++y) { // 横向:滑动窗口
  247. int acc = 0;
  248. for (int k = -radius; k <= radius; ++k) { acc += img->at(clamp_x(k), y); }
  249. for (int x = 0; x < w; ++x) {
  250. tmp[static_cast<std::size_t>(y) * w + x] = acc;
  251. acc -= img->at(clamp_x(x - radius), y);
  252. acc += img->at(clamp_x(x + radius + 1), y);
  253. }
  254. }
  255. for (int x = 0; x < w; ++x) { // 纵向
  256. int acc = 0;
  257. for (int k = -radius; k <= radius; ++k) {
  258. acc += tmp[static_cast<std::size_t>(clamp_y(k)) * w + x];
  259. }
  260. for (int y = 0; y < h; ++y) {
  261. img->set(x, y, clamp_u8(static_cast<double>(acc) * inv2));
  262. acc -= tmp[static_cast<std::size_t>(clamp_y(y - radius)) * w + x];
  263. acc += tmp[static_cast<std::size_t>(clamp_y(y + radius + 1)) * w + x];
  264. }
  265. }
  266. }
  267. // ---------------------------------------------------------------- 光照归一(§3.1)
  268. void clahe_lite_inplace(GrayImage* img, int tiles, double clip) {
  269. if (img == nullptr || img->empty()) { return; }
  270. if (tiles < 1) { tiles = 1; }
  271. const int w = img->width;
  272. const int h = img->height;
  273. const int btw = (w + tiles - 1) / tiles;
  274. const int bth = (h + tiles - 1) / tiles;
  275. // ① 每块做"限幅直方图均衡",得到 256 项 LUT
  276. std::vector<std::vector<uint8_t>> luts(static_cast<std::size_t>(tiles * tiles),
  277. std::vector<uint8_t>(256));
  278. for (int ty = 0; ty < tiles; ++ty) {
  279. for (int tx = 0; tx < tiles; ++tx) {
  280. const int x0 = tx * btw;
  281. const int y0 = ty * bth;
  282. const int x1 = std::min(w, x0 + btw);
  283. const int y1 = std::min(h, y0 + bth);
  284. std::vector<uint8_t>& lut = luts[static_cast<std::size_t>(ty) * tiles + tx];
  285. if (x1 <= x0 || y1 <= y0) { // 空块 → 恒等映射
  286. for (int v = 0; v < 256; ++v) { lut[static_cast<std::size_t>(v)] = static_cast<uint8_t>(v); }
  287. continue;
  288. }
  289. int hist[256] = {0};
  290. int n = 0;
  291. for (int y = y0; y < y1; ++y) {
  292. for (int x = x0; x < x1; ++x) {
  293. ++hist[img->at(x, y)];
  294. ++n;
  295. }
  296. }
  297. const int limit = std::max(1, static_cast<int>(clip * static_cast<double>(n) / 256.0));
  298. int excess = 0;
  299. for (int v = 0; v < 256; ++v) {
  300. if (hist[v] > limit) {
  301. excess += hist[v] - limit;
  302. hist[v] = limit;
  303. }
  304. }
  305. const int share = excess / 256;
  306. const int rem = excess % 256;
  307. for (int v = 0; v < 256; ++v) { hist[v] += share; }
  308. for (int v = 0; v < rem; ++v) { hist[v] += 1; }
  309. int acc = 0;
  310. for (int v = 0; v < 256; ++v) {
  311. acc += hist[v];
  312. const double scaled = 255.0 * static_cast<double>(acc) / static_cast<double>(n);
  313. lut[static_cast<std::size_t>(v)] =
  314. static_cast<uint8_t>(scaled < 0.0 ? 0.0 : (scaled > 255.0 ? 255.0 : scaled));
  315. }
  316. }
  317. }
  318. // ② 逐像素在相邻 4 块 LUT 之间双线性插值(块中心对齐),避免块边界出现硬接缝
  319. const GrayImage src = *img;
  320. auto lut_at = [&luts, tiles](int tx, int ty, int v) -> double {
  321. if (tx < 0) { tx = 0; }
  322. if (ty < 0) { ty = 0; }
  323. if (tx > tiles - 1) { tx = tiles - 1; }
  324. if (ty > tiles - 1) { ty = tiles - 1; }
  325. return static_cast<double>(luts[static_cast<std::size_t>(ty) * tiles + tx]
  326. [static_cast<std::size_t>(v)]);
  327. };
  328. for (int y = 0; y < h; ++y) {
  329. const double fy = (static_cast<double>(y) - (bth - 1) * 0.5) / bth;
  330. const int ty0 = static_cast<int>(std::floor(fy));
  331. const double ay = fy - ty0;
  332. for (int x = 0; x < w; ++x) {
  333. const double fx = (static_cast<double>(x) - (btw - 1) * 0.5) / btw;
  334. const int tx0 = static_cast<int>(std::floor(fx));
  335. const double ax = fx - tx0;
  336. const int v = src.at(x, y);
  337. const double v00 = lut_at(tx0, ty0, v);
  338. const double v10 = lut_at(tx0 + 1, ty0, v);
  339. const double v01 = lut_at(tx0, ty0 + 1, v);
  340. const double v11 = lut_at(tx0 + 1, ty0 + 1, v);
  341. const double a = v00 * (1.0 - ax) + v10 * ax;
  342. const double b = v01 * (1.0 - ax) + v11 * ax;
  343. img->set(x, y, clamp_u8(a * (1.0 - ay) + b * ay));
  344. }
  345. }
  346. }
  347. // ---------------------------------------------------------------- 降质(自检 / 合成用)
  348. void apply_exposure_contrast_inplace(GrayImage* img, double exposure, double contrast) {
  349. if (img == nullptr || img->empty()) { return; }
  350. for (uint8_t& p : img->data) {
  351. const double v = (static_cast<double>(p) - 128.0) * contrast + 128.0 * exposure;
  352. p = clamp_u8(v);
  353. }
  354. }
  355. void add_noise_inplace(GrayImage* img, double sigma, uint32_t seed) {
  356. if (img == nullptr || img->empty() || sigma <= 0.0) { return; }
  357. XorShift32 rng(seed);
  358. for (uint8_t& p : img->data) {
  359. p = clamp_u8(static_cast<double>(p) + rng.uniform_signed() * sigma);
  360. }
  361. }
  362. // ---------------------------------------------------------------- 梯度(§3.1)
  363. double SobelResult::mean_mag() const {
  364. if (mag.empty()) { return 0.0; }
  365. double sum = 0.0;
  366. for (float v : mag) { sum += static_cast<double>(v); }
  367. return sum / static_cast<double>(mag.size());
  368. }
  369. SobelResult sobel(const GrayImage& img) {
  370. SobelResult out;
  371. if (img.empty()) { return out; }
  372. out.width = img.width;
  373. out.height = img.height;
  374. const std::size_t n = static_cast<std::size_t>(img.width) * static_cast<std::size_t>(img.height);
  375. out.gx.assign(n, 0.0f);
  376. out.gy.assign(n, 0.0f);
  377. out.mag.assign(n, 0.0f);
  378. for (int y = 0; y < img.height; ++y) {
  379. for (int x = 0; x < img.width; ++x) {
  380. // 边界复制(与头文件声明的语义一致):越界当 0 会让画面最外一圈的
  381. // 幅值虚高(实测 170 阶跃上 max_mag 890 → 复制延拓后为 680),
  382. // 进而把整圈边框当成"强边缘"混进线段集合。
  383. const double tl = at_clamped(img, x - 1, y - 1);
  384. const double tc = at_clamped(img, x, y - 1);
  385. const double tr = at_clamped(img, x + 1, y - 1);
  386. const double ml = at_clamped(img, x - 1, y);
  387. const double mr = at_clamped(img, x + 1, y);
  388. const double bl = at_clamped(img, x - 1, y + 1);
  389. const double bc = at_clamped(img, x, y + 1);
  390. const double br = at_clamped(img, x + 1, y + 1);
  391. const double gx = (tr + 2.0 * mr + br) - (tl + 2.0 * ml + bl);
  392. const double gy = (bl + 2.0 * bc + br) - (tl + 2.0 * tc + tr);
  393. const std::size_t i = static_cast<std::size_t>(y) * static_cast<std::size_t>(img.width) +
  394. static_cast<std::size_t>(x);
  395. out.gx[i] = static_cast<float>(gx);
  396. out.gy[i] = static_cast<float>(gy);
  397. out.mag[i] = static_cast<float>(std::sqrt(gx * gx + gy * gy));
  398. }
  399. }
  400. return out;
  401. }
  402. // ---------------------------------------------------------------- 绘制(合成器用)
  403. void draw_line_aa(GrayImage* img, double x0, double y0, double x1, double y1, double value,
  404. double thickness) {
  405. if (img == nullptr || img->empty()) { return; }
  406. const double half = thickness * 0.5;
  407. const int ix0 = static_cast<int>(std::floor(std::min(x0, x1) - half - 1.0));
  408. const int ix1 = static_cast<int>(std::ceil(std::max(x0, x1) + half + 1.0));
  409. const int iy0 = static_cast<int>(std::floor(std::min(y0, y1) - half - 1.0));
  410. const int iy1 = static_cast<int>(std::ceil(std::max(y0, y1) + half + 1.0));
  411. const double dx = x1 - x0;
  412. const double dy = y1 - y0;
  413. const double len2 = dx * dx + dy * dy;
  414. for (int y = iy0; y <= iy1; ++y) {
  415. for (int x = ix0; x <= ix1; ++x) {
  416. const double px = x + 0.5;
  417. const double py = y + 0.5;
  418. double t = 0.0;
  419. if (len2 > 1e-9) {
  420. t = ((px - x0) * dx + (py - y0) * dy) / len2;
  421. if (t < 0.0) { t = 0.0; }
  422. if (t > 1.0) { t = 1.0; }
  423. }
  424. const double cx = x0 + t * dx;
  425. const double cy = y0 + t * dy;
  426. const double dist = std::sqrt((px - cx) * (px - cx) + (py - cy) * (py - cy));
  427. const double cov = 0.5 + half - dist; // 覆盖度(像素中心到线的距离)
  428. if (cov > 0.0) { img->blend(x, y, value, cov > 1.0 ? 1.0 : cov); }
  429. }
  430. }
  431. }
  432. void fill_polygon_aa(GrayImage* img, const std::vector<std::pair<double, double>>& poly,
  433. double value) {
  434. if (img == nullptr || img->empty() || poly.size() < 3) { return; }
  435. double min_x = poly[0].first;
  436. double max_x = poly[0].first;
  437. double min_y = poly[0].second;
  438. double max_y = poly[0].second;
  439. for (const auto& p : poly) {
  440. min_x = std::min(min_x, p.first);
  441. max_x = std::max(max_x, p.first);
  442. min_y = std::min(min_y, p.second);
  443. max_y = std::max(max_y, p.second);
  444. }
  445. const int bx0 = std::max(0, static_cast<int>(std::floor(min_x - 1.0)));
  446. const int bx1 = std::min(img->width - 1, static_cast<int>(std::ceil(max_x + 1.0)));
  447. const int by0 = std::max(0, static_cast<int>(std::floor(min_y - 1.0)));
  448. const int by1 = std::min(img->height - 1, static_cast<int>(std::ceil(max_y + 1.0)));
  449. if (bx1 < bx0 || by1 < by0) { return; }
  450. // 每条扫描线取 4 条子扫描线(1/4 像素)→ 每个像素得到 0..1 的覆盖度,
  451. // 这是"抗锯齿多边形填充"的简化实现:既省算力,又让灰度直方图/分位数稳定。
  452. const int sub = 4;
  453. std::vector<double> cover(static_cast<std::size_t>(bx1 - bx0 + 1), 0.0);
  454. std::vector<double> xs;
  455. for (int py = by0; py <= by1; ++py) {
  456. std::fill(cover.begin(), cover.end(), 0.0);
  457. bool any = false;
  458. for (int k = 0; k < sub; ++k) {
  459. const double y = static_cast<double>(py) + 0.125 + 0.25 * static_cast<double>(k);
  460. xs.clear();
  461. for (std::size_t i = 0; i < poly.size(); ++i) {
  462. const std::pair<double, double>& a = poly[i];
  463. const std::pair<double, double>& b = poly[(i + 1) % poly.size()];
  464. if ((a.second <= y && b.second > y) || (b.second <= y && a.second > y)) {
  465. const double t = (y - a.second) / (b.second - a.second);
  466. xs.push_back(a.first + t * (b.first - a.first));
  467. }
  468. }
  469. if (xs.size() < 2) { continue; }
  470. std::sort(xs.begin(), xs.end());
  471. for (std::size_t i = 0; i + 1 < xs.size(); i += 2) {
  472. const double xa = xs[i];
  473. const double xb = xs[i + 1];
  474. const int ia = std::max(bx0, static_cast<int>(std::ceil(xa - 0.5)));
  475. const int ib = std::min(bx1, static_cast<int>(std::floor(xb - 0.5)));
  476. for (int ix = ia; ix <= ib; ++ix) {
  477. const double l = std::max(xa, static_cast<double>(ix) - 0.5);
  478. const double r = std::min(xb, static_cast<double>(ix) + 0.5);
  479. if (r > l) {
  480. cover[static_cast<std::size_t>(ix - bx0)] += (r - l);
  481. any = true;
  482. }
  483. }
  484. }
  485. }
  486. if (!any) { continue; }
  487. for (int ix = bx0; ix <= bx1; ++ix) {
  488. const double c = cover[static_cast<std::size_t>(ix - bx0)] / static_cast<double>(sub);
  489. if (c > 0.0) { img->blend(ix, py, value, c); }
  490. }
  491. }
  492. }
  493. void add_gradient_inplace(GrayImage* img, double top_value, double bottom_value) {
  494. if (img == nullptr || img->empty()) { return; }
  495. for (int y = 0; y < img->height; ++y) {
  496. const double t = img->height > 1
  497. ? static_cast<double>(y) / static_cast<double>(img->height - 1)
  498. : 0.0;
  499. const double delta = top_value * (1.0 - t) + bottom_value * t;
  500. for (int x = 0; x < img->width; ++x) {
  501. img->set(x, y, clamp_u8(static_cast<double>(img->at(x, y)) + delta));
  502. }
  503. }
  504. }
  505. } // namespace portcv