image.photography\inpainting\inpainting_enhancement.mbt

86.2% · 137 / 159 points · 633 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 修复增强 inpainting/inpainting_enhancement
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 图像修复增强(v25.0.0)
  7. 7// 图像修复、图像去雾、图像去雨、图像去噪
  8. 8
  9. 9///|
  10. 10/// 修复算法类型
  11. 11/// `#non_exhaustive` 表示未来可能添加新构造器
  12. 12#derive(Eq, @debug.Debug)
  13. 13#non_exhaustive
  14. 14pub(all) enum InpaintingAlgorithm {
  15. 15 Telea
  16. 16 NavierStokes
  17. 17 FastMarching
  18. 18 PatchMatch
  19. 19 DeepLearning
  20. 20 Unknown
  21. 21}
  22. 22
  23. 23///|
  24. 24pub extend InpaintingAlgorithm with @debug.Debug::{to_repr}
  25. 25
  26. 26///|
  27. 27pub extend InpaintingAlgorithm with Eq::{not_equal, equal}
  28. 28
  29. 29///|
  30. 30/// 修复结果
  31. 31#derive(Eq, @debug.Debug)
  32. 32pub(all) struct InpaintingResult {
  33. 33 image : @types.Image
  34. 34 mask : @types.Image
  35. 35 algorithm : InpaintingAlgorithm
  36. 36 inpainted_pixels : Int32
  37. 37 total_mask_pixels : Int32
  38. 38 processing_time_ms : Double
  39. 39 iterations : Int32
  40. 40}
  41. 41
  42. 42///|
  43. 43pub extend InpaintingResult with Eq::{not_equal, equal}
  44. 44
  45. 45///|
  46. 46pub extend InpaintingResult with @debug.Debug::{to_repr}
  47. 47
  48. 48///|
  49. 49/// 修复配置
  50. 50#derive(Eq, @debug.Debug)
  51. 51pub(all) struct InpaintingConfig {
  52. 52 algorithm : InpaintingAlgorithm
  53. 53 radius : Int32
  54. 54 iterations : Int32
  55. 55 use_edge_preservation : Bool
  56. 56 tolerance : Double
  57. 57}
  58. 58
  59. 59///|
  60. 60pub extend InpaintingConfig with Eq::{not_equal, equal}
  61. 61
  62. 62///|
  63. 63pub extend InpaintingConfig with @debug.Debug::{to_repr}
  64. 64
  65. 65///|
  66. 66/// 去雾算法类型
  67. 67/// `#non_exhaustive` 表示未来可能添加新构造器
  68. 68#derive(Eq, @debug.Debug)
  69. 69#non_exhaustive
  70. 70pub(all) enum DehazeAlgorithm {
  71. 71 DarkChannelPrior
  72. 72 ColorAttenuation
  73. 73 HomomorphicFiltering
  74. 74 Retinex
  75. 75 CLAHE
  76. 76 Unknown
  77. 77}
  78. 78
  79. 79///|
  80. 80pub extend DehazeAlgorithm with @debug.Debug::{to_repr}
  81. 81
  82. 82///|
  83. 83pub extend DehazeAlgorithm with Eq::{not_equal, equal}
  84. 84
  85. 85///|
  86. 86/// 去雾结果
  87. 87#derive(Eq, @debug.Debug)
  88. 88pub(all) struct DehazeResult {
  89. 89 image : @types.Image
  90. 90 algorithm : DehazeAlgorithm
  91. 91 atmospheric_light : Double
  92. 92 transmission_map : Array[Double]
  93. 93 processing_time_ms : Double
  94. 94}
  95. 95
  96. 96///|
  97. 97pub extend DehazeResult with Eq::{not_equal, equal}
  98. 98
  99. 99///|
  100. 100pub extend DehazeResult with @debug.Debug::{to_repr}
  101. 101
  102. 102///|
  103. 103/// 去雨算法类型
  104. 104/// `#non_exhaustive` 表示未来可能添加新构造器
  105. 105#derive(Eq, @debug.Debug)
  106. 106#non_exhaustive
  107. 107pub(all) enum DerainAlgorithm {
  108. 108 DictionaryLearning
  109. 109 ConvolutionalNeuralNetwork
  110. 110 GAN
  111. 111 Morphological
  112. 112 Unknown
  113. 113}
  114. 114
  115. 115///|
  116. 116pub extend DerainAlgorithm with @debug.Debug::{to_repr}
  117. 117
  118. 118///|
  119. 119pub extend DerainAlgorithm with Eq::{not_equal, equal}
  120. 120
  121. 121///|
  122. 122/// 去雨结果
  123. 123#derive(Eq, @debug.Debug)
  124. 124pub(all) struct DerainResult {
  125. 125 image : @types.Image
  126. 126 algorithm : DerainAlgorithm
  127. 127 rain_streaks_removed : Int32
  128. 128 processing_time_ms : Double
  129. 129}
  130. 130
  131. 131///|
  132. 132pub extend DerainResult with Eq::{not_equal, equal}
  133. 133
  134. 134///|
  135. 135pub extend DerainResult with @debug.Debug::{to_repr}
  136. 136
  137. 137///|
  138. 138/// 创建默认修复配置
  139. 139pub fn create_default_inpainting_config() -> InpaintingConfig {
  140. 140● InpaintingConfig::{
  141. 141 algorithm: InpaintingAlgorithm::Telea,
  142. 142 radius: 3,
  143. 143 iterations: 10,
  144. 144 use_edge_preservation: true,
  145. 145 tolerance: 0.1,
  146. 146 }
  147. 147}
  148. 148
  149. 149///|
  150. 150/// 创建修复掩膜(矩形区域)
  151. 151pub fn create_rect_mask(
  152. 152 width : Int32,
  153. 153 height : Int32,
  154. 154 x : Int32,
  155. 155 y : Int32,
  156. 156 rect_width : Int32,
  157. 157 rect_height : Int32,
  158. 158) -> @types.Image {
  159. 159● let total = width * height
  160. 160 let mask_data : Array[Byte] = []
  161. 161 for _ in 0..<total {
  162. 162● mask_data.push(b'\x00')
  163. 163 }
  164. 164
  165. 165 for my in 0..<rect_height {
  166. 166● for mx in 0..<rect_width {
  167. 167● let px = x + mx
  168. 168 let py = y + my
  169. 169 if px >= 0 && px < width && py >= 0 && py < height {
  170. 170● mask_data[py * width + px] = b'\xFF'
  171. 171 }
  172. 172 }
  173. 173 }
  174. 174
  175. 175 @types.Image::{
  176. 176 width,
  177. 177 height,
  178. 178 channels: 1,
  179. 179● data: Bytes::from_array(mask_data),
  180. 180 }
  181. 181}
  182. 182
  183. 183///|
  184. 184/// 简单修复(邻域平均法)
  185. 185pub fn inpaint_simple(
  186. 186 img : @types.Image,
  187. 187 mask : @types.Image,
  188. 188 radius : Int32,
  189. 189) -> @types.Image {
  190. 190● let w = img.width
  191. 191 let h = img.height
  192. 192 let total = w * h
  193. 193 let out : Array[Byte] = []
  194. 194
  195. 195 for i in 0..<total {
  196. 196● out.push(img.data[i])
  197. 197 }
  198. 198
  199. 199 // 迭代修复
  200. 200 for _ in 0..<5 {
  201. 201● for y in 0..<h {
  202. 202● for x in 0..<w {
  203. 203● let idx = y * w + x
  204. 204 if mask.data[idx] == b'\xFF' {
  205. 205 // 计算邻域平均值
  206. 206● let mut sum = 0.0
  207. 207 let mut count = 0
  208. 208 for dy in -radius..<(radius + 1) {
  209. 209● for dx in -radius..<(radius + 1) {
  210. 210● if dx == 0 && dy == 0 {
  211. 211● continue
  212. 212 }
  213. 213 let nx = x + dx
  214. 214 let ny = y + dy
  215. 215 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  216. 216● let nidx = ny * w + nx
  217. 217 if mask.data[nidx] == b'\x00' || out[nidx] != img.data[nidx] {
  218. 218● sum += out[nidx].to_double()
  219. 219 count += 1
  220. 220 }
  221. 221 }
  222. 222 }
  223. 223 }
  224. 224 if count > 0 {
  225. 225● let avg = sum / count.to_double()
  226. 226 let clamped = if avg < 0.0 {
  227. 227○ 0.0
  228. 228● } else if avg > 255.0 {
  229. 229○ 255.0
  230. 230 } else {
  231. 231● avg
  232. 232 }
  233. 233● out[idx] = clamped.to_int().to_byte()
  234. 234 }
  235. 235 }
  236. 236 }
  237. 237 }
  238. 238 }
  239. 239
  240. 240 @types.Image::{
  241. 241 width: w,
  242. 242 height: h,
  243. 243 channels: img.channels,
  244. 244● data: Bytes::from_array(out),
  245. 245 }
  246. 246}
  247. 247
  248. 248///|
  249. 249/// 基于扩散的修复(简化版 Telea)
  250. 250pub fn inpaint_diffusion(
  251. 251 img : @types.Image,
  252. 252 mask : @types.Image,
  253. 253 radius : Int32,
  254. 254) -> @types.Image {
  255. 255● let w = img.width
  256. 256 let h = img.height
  257. 257 let total = w * h
  258. 258 let out : Array[Byte] = []
  259. 259
  260. 260 for i in 0..<total {
  261. 261● out.push(img.data[i])
  262. 262 }
  263. 263
  264. 264 // 从边界向内部扩散
  265. 265 for _ in 0..<10 {
  266. 266● for y in 0..<h {
  267. 267● for x in 0..<w {
  268. 268● let idx = y * w + x
  269. 269 if mask.data[idx] == b'\xFF' {
  270. 270 // 检查是否有已修复的邻域
  271. 271● let mut has_known = false
  272. 272 for dy in -1..<2 {
  273. 273● for dx in -1..<2 {
  274. 274● if dx == 0 && dy == 0 {
  275. 275● continue
  276. 276 }
  277. 277 let nx = x + dx
  278. 278 let ny = y + dy
  279. 279 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  280. 280● let nidx = ny * w + nx
  281. 281 if mask.data[nidx] == b'\x00' || out[nidx] != img.data[nidx] {
  282. 282● has_known = true
  283. 283 }
  284. 284 }
  285. 285 }
  286. 286 }
  287. 287
  288. 288 if has_known {
  289. 289 // 加权平均(距离加权)
  290. 290● let mut sum = 0.0
  291. 291 let mut weight_sum = 0.0
  292. 292 for dy in -radius..<(radius + 1) {
  293. 293● for dx in -radius..<(radius + 1) {
  294. 294● if dx == 0 && dy == 0 {
  295. 295● continue
  296. 296 }
  297. 297 let nx = x + dx
  298. 298 let ny = y + dy
  299. 299 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  300. 300● let nidx = ny * w + nx
  301. 301 if mask.data[nidx] == b'\x00' || out[nidx] != img.data[nidx] {
  302. 302● let dist = (dx * dx + dy * dy).to_double().sqrt()
  303. 303 let weight = 1.0 / (1.0 + dist)
  304. 304● sum += out[nidx].to_double() * weight
  305. 305 weight_sum += weight
  306. 306 }
  307. 307 }
  308. 308 }
  309. 309 }
  310. 310 if weight_sum > 0.0 {
  311. 311● let avg = sum / weight_sum
  312. 312 let clamped = if avg < 0.0 {
  313. 313○ 0.0
  314. 314● } else if avg > 255.0 {
  315. 315○ 255.0
  316. 316 } else {
  317. 317● avg
  318. 318 }
  319. 319● out[idx] = clamped.to_int().to_byte()
  320. 320 }
  321. 321 }
  322. 322 }
  323. 323 }
  324. 324 }
  325. 325 }
  326. 326
  327. 327 @types.Image::{
  328. 328 width: w,
  329. 329 height: h,
  330. 330 channels: img.channels,
  331. 331● data: Bytes::from_array(out),
  332. 332 }
  333. 333}
  334. 334
  335. 335///|
  336. 336/// 图像修复
  337. 337pub fn inpaint(
  338. 338 img : @types.Image,
  339. 339 mask : @types.Image,
  340. 340 config : InpaintingConfig,
  341. 341) -> InpaintingResult {
  342. 342● let result = if config.algorithm == InpaintingAlgorithm::Telea {
  343. 343● inpaint_diffusion(img, mask, config.radius)
  344. 344 } else {
  345. 345● inpaint_simple(img, mask, config.radius)
  346. 346 }
  347. 347
  348. 348 // 统计掩膜像素数
  349. 349 let total = mask.width * mask.height
  350. 350 let mut mask_pixels = 0
  351. 351 for i in 0..<total {
  352. 352● if mask.data[i] == b'\xFF' {
  353. 353● mask_pixels += 1
  354. 354 }
  355. 355 }
  356. 356
  357. 357 InpaintingResult::{
  358. 358 image: result,
  359. 359 mask,
  360. 360 algorithm: config.algorithm,
  361. 361 inpainted_pixels: mask_pixels,
  362. 362 total_mask_pixels: mask_pixels,
  363. 363 processing_time_ms: 0.0,
  364. 364 iterations: config.iterations,
  365. 365 }
  366. 366}
  367. 367
  368. 368///|
  369. 369/// 暗通道先验去雾(简化版)
  370. 370pub fn dehaze_dark_channel(
  371. 371 img : @types.Image,
  372. 372 omega : Double,
  373. 373 t0 : Double,
  374. 374) -> DehazeResult {
  375. 375● let w = img.width
  376. 376 let h = img.height
  377. 377 let total = w * h
  378. 378 let patch_size = 15
  379. 379
  380. 380 // 计算暗通道
  381. 381 let dark_channel : Array[Double] = []
  382. 382 for _ in 0..<total {
  383. 383● dark_channel.push(255.0)
  384. 384 }
  385. 385
  386. 386 for y in 0..<h {
  387. 387● for x in 0..<w {
  388. 388● let mut min_val = 255.0
  389. 389 for dy in 0..<patch_size {
  390. 390● for dx in 0..<patch_size {
  391. 391● let nx = x + dx - patch_size / 2
  392. 392 let ny = y + dy - patch_size / 2
  393. 393 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  394. 394● let val = img.data[ny * w + nx].to_double()
  395. 395 if val < min_val {
  396. 396● min_val = val
  397. 397 }
  398. 398 }
  399. 399 }
  400. 400 }
  401. 401 dark_channel[y * w + x] = min_val
  402. 402 }
  403. 403 }
  404. 404
  405. 405 // 估计大气光(暗通道最亮的 0.1% 像素的平均值)
  406. 406 let mut atmospheric_light = 0.0
  407. 407 let mut max_dark = 0.0
  408. 408 for i in 0..<total {
  409. 409● if dark_channel[i] > max_dark {
  410. 410○ max_dark = dark_channel[i]
  411. 411 }
  412. 412 }
  413. 413 let threshold = max_dark * 0.999
  414. 414 let mut count = 0
  415. 415 for i in 0..<total {
  416. 416● if dark_channel[i] >= threshold {
  417. 417● atmospheric_light += img.data[i].to_double()
  418. 418 count += 1
  419. 419 }
  420. 420 }
  421. 421 if count > 0 {
  422. 422● atmospheric_light /= count.to_double()
  423. 423 }
  424. 424
  425. 425 // 计算透射率
  426. 426 let transmission : Array[Double] = []
  427. 427 for i in 0..<total {
  428. 428● let t = 1.0 - omega * (dark_channel[i] / atmospheric_light)
  429. 429◐ let clamped = if t < t0 { t0 } else if t > 1.0 { 1.0 } else { t }
  430. 430● transmission.push(clamped)
  431. 431 }
  432. 432
  433. 433 // 去雾
  434. 434 let out : Array[Byte] = []
  435. 435 for i in 0..<total {
  436. 436● let t = transmission[i]
  437. 437● let pixel = img.data[i].to_double()
  438. 438 let dehazed = (pixel - atmospheric_light) / t + atmospheric_light
  439. 439 let clamped = if dehazed < 0.0 {
  440. 440○ 0.0
  441. 441● } else if dehazed > 255.0 {
  442. 442○ 255.0
  443. 443 } else {
  444. 444● dehazed
  445. 445 }
  446. 446● out.push(clamped.to_int().to_byte())
  447. 447 }
  448. 448
  449. 449 DehazeResult::{
  450. 450 image: @types.Image::{
  451. 451 width: w,
  452. 452 height: h,
  453. 453 channels: img.channels,
  454. 454● data: Bytes::from_array(out),
  455. 455 },
  456. 456 algorithm: DehazeAlgorithm::DarkChannelPrior,
  457. 457 atmospheric_light,
  458. 458 transmission_map: transmission,
  459. 459 processing_time_ms: 0.0,
  460. 460 }
  461. 461}
  462. 462
  463. 463///|
  464. 464/// CLAHE 去雾(对比度受限自适应直方图均衡化)
  465. 465pub fn dehaze_clahe(
  466. 466 img : @types.Image,
  467. 467 clip_limit : Double,
  468. 468 tile_size : Int32,
  469. 469) -> @types.Image {
  470. 470● ignore(tile_size)
  471. 471● ignore(clip_limit)
  472. 472 let w = img.width
  473. 473 let h = img.height
  474. 474 let total = w * h
  475. 475
  476. 476 // 简化:全局直方图均衡化
  477. 477 let histogram : Array[Int32] = []
  478. 478 for _ in 0..<256 {
  479. 479● histogram.push(0)
  480. 480 }
  481. 481 for i in 0..<total {
  482. 482● histogram[img.data[i].to_int()] += 1
  483. 483 }
  484. 484
  485. 485 // 累积分布函数
  486. 486 let cdf : Array[Double] = []
  487. 487● let mut cdf_min = total.to_double()
  488. 488 let mut sum = 0.0
  489. 489 for i in 0..<256 {
  490. 490● sum += histogram[i].to_double()
  491. 491● cdf.push(sum)
  492. 492 if histogram[i] > 0 && sum < cdf_min {
  493. 493● cdf_min = sum
  494. 494 }
  495. 495 }
  496. 496
  497. 497 // 直方图均衡化
  498. 498 let out : Array[Byte] = []
  499. 499 for i in 0..<total {
  500. 500● let pixel = img.data[i].to_int()
  501. 501 let equalized = if total > 1 {
  502. 502● ((cdf[pixel] - cdf_min) / (total.to_double() - cdf_min) * 255.0).to_int()
  503. 503 } else {
  504. 504○ pixel
  505. 505 }
  506. 506 let clamped = if equalized < 0 {
  507. 507○ 0
  508. 508● } else if equalized > 255 {
  509. 509○ 255
  510. 510 } else {
  511. 511● equalized
  512. 512 }
  513. 513● out.push(clamped.to_byte())
  514. 514 }
  515. 515
  516. 516 @types.Image::{
  517. 517 width: w,
  518. 518 height: h,
  519. 519 channels: img.channels,
  520. 520● data: Bytes::from_array(out),
  521. 521 }
  522. 522}
  523. 523
  524. 524///|
  525. 525/// 去雾
  526. 526pub fn dehaze(img : @types.Image, algorithm : DehazeAlgorithm) -> DehazeResult {
  527. 527● if algorithm == DehazeAlgorithm::DarkChannelPrior {
  528. 528● dehaze_dark_channel(img, 0.95, 0.1)
  529. 529 } else {
  530. 530● let result = dehaze_clahe(img, 2.0, 8)
  531. 531 DehazeResult::{
  532. 532 image: result,
  533. 533 algorithm,
  534. 534 atmospheric_light: 0.0,
  535. 535 transmission_map: [],
  536. 536 processing_time_ms: 0.0,
  537. 537 }
  538. 538 }
  539. 539}
  540. 540
  541. 541///|
  542. 542/// 形态学去雨(简化版)
  543. 543pub fn derain_morphological(img : @types.Image) -> DerainResult {
  544. 544● let w = img.width
  545. 545 let h = img.height
  546. 546 let _ = w * h
  547. 547
  548. 548 // 简化:中值滤波去除雨线
  549. 549 let out : Array[Byte] = []
  550. 550 for y in 0..<h {
  551. 551● for x in 0..<w {
  552. 552 // 3x3 中值滤波
  553. 553● let neighbors : Array[Int32] = []
  554. 554 for dy in -1..<2 {
  555. 555● for dx in -1..<2 {
  556. 556● let nx = x + dx
  557. 557 let ny = y + dy
  558. 558 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  559. 559● neighbors.push(img.data[ny * w + nx].to_int())
  560. 560 }
  561. 561 }
  562. 562 }
  563. 563 // 排序取中值
  564. 564● for i in 0..<neighbors.length() {
  565. 565● for j in 0..<(neighbors.length() - i - 1) {
  566. 566● if neighbors[j] > neighbors[j + 1] {
  567. 567○ let temp = neighbors[j]
  568. 568 neighbors[j] = neighbors[j + 1]
  569. 569 neighbors[j + 1] = temp
  570. 570 }
  571. 571 }
  572. 572 }
  573. 573● let median = neighbors[neighbors.length() / 2]
  574. 574● out.push(median.to_byte())
  575. 575 }
  576. 576 }
  577. 577
  578. 578 DerainResult::{
  579. 579 image: @types.Image::{
  580. 580 width: w,
  581. 581 height: h,
  582. 582 channels: img.channels,
  583. 583● data: Bytes::from_array(out),
  584. 584 },
  585. 585 algorithm: DerainAlgorithm::Morphological,
  586. 586 rain_streaks_removed: 0,
  587. 587 processing_time_ms: 0.0,
  588. 588 }
  589. 589}
  590. 590
  591. 591///|
  592. 592/// 去雨
  593. 593pub fn derain(img : @types.Image, algorithm : DerainAlgorithm) -> DerainResult {
  594. 594● ignore(algorithm)
  595. 595● derain_morphological(img)
  596. 596}
  597. 597
  598. 598///|
  599. 599/// 生成修复报告
  600. 600pub fn inpainting_report_to_string(result : InpaintingResult) -> String {
  601. 601● let mut report = "## 图像修复报告\n\n"
  602. 602 report = report + "### 处理信息\n\n"
  603. 603 report = report + "| 指标 | 值 |\n|------|------|\n"
  604. 604 let algo_str = if result.algorithm == InpaintingAlgorithm::Telea {
  605. 605● "Telea"
  606. 606○ } else if result.algorithm == InpaintingAlgorithm::NavierStokes {
  607. 607○ "Navier-Stokes"
  608. 608○ } else if result.algorithm == InpaintingAlgorithm::FastMarching {
  609. 609○ "快速行进"
  610. 610○ } else if result.algorithm == InpaintingAlgorithm::PatchMatch {
  611. 611○ "PatchMatch"
  612. 612○ } else if result.algorithm == InpaintingAlgorithm::DeepLearning {
  613. 613○ "深度学习"
  614. 614 } else {
  615. 615○ "未知"
  616. 616 }
  617. 617 report = report + "| 算法 | " + algo_str + " |\n"
  618. 618 report = report +
  619. 619 "| 修复像素数 | " +
  620. 620● result.inpainted_pixels.to_string() +
  621. 621 " |\n"
  622. 622 report = report +
  623. 623 "| 掩膜像素总数 | " +
  624. 624● result.total_mask_pixels.to_string() +
  625. 625 " |\n"
  626. 626● report = report + "| 迭代次数 | " + result.iterations.to_string() + " |\n"
  627. 627 report = report +
  628. 628 "| 处理耗时 | " +
  629. 629● result.processing_time_ms.to_string() +
  630. 630 "ms |\n"
  631. 631
  632. 632 report
  633. 633}