image.process\enhance\dehaze.mbt

87% · 141 / 162 points · 513 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/// # 去雾 dehaze
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 图像去雾(v11.6.0)
  7. 7// 暗通道先验算法 + 大气光估计 + 透射率优化 + 去雾复原
  8. 8
  9. 9///|
  10. 10/// 去雾配置
  11. 11pub(all) struct DehazeConfig {
  12. 12 patch_size : Int32 // 暗通道 patch 大小
  13. 13 omega : Double // 透射率保留系数(0-1,越大去雾越强)
  14. 14 t_min : Double // 最小透射率
  15. 15 guided_radius : Int32 // 引导滤波半径
  16. 16 guided_eps : Double // 引导滤波正则化参数
  17. 17 max_atmospheric : Int32 // 大气光候选像素比例(前 0.1%)
  18. 18}
  19. 19
  20. 20///|
  21. 21/// 创建默认去雾配置
  22. 22pub fn dehaze_default_config() -> DehazeConfig {
  23. 23● DehazeConfig::{
  24. 24 patch_size: 15,
  25. 25 omega: 0.95,
  26. 26 t_min: 0.1,
  27. 27 guided_radius: 20,
  28. 28 guided_eps: 0.0001,
  29. 29 max_atmospheric: 1000,
  30. 30 }
  31. 31}
  32. 32
  33. 33///|
  34. 34/// 计算暗通道
  35. 35/// 暗通道:在局部 patch 内,取每个像素 RGB 三通道的最小值,再取 patch 内最小值
  36. 36pub fn dark_channel(
  37. 37 img : @types.Image,
  38. 38 patch_size~ : Int32 = 15,
  39. 39) -> @types.Image raise @types.LoadError {
  40. 40 // 输入验证
  41. 41● if img.width <= 0 || img.height <= 0 {
  42. 42● raise @types.LoadError::DecodeFailed(
  43. 43 "dark_channel: 图像尺寸必须大于0",
  44. 44 )
  45. 45 }
  46. 46 if img.channels < 1 || img.channels > 4 {
  47. 47● raise @types.LoadError::DecodeFailed(
  48. 48 "dark_channel: 通道数必须在1-4之间",
  49. 49 )
  50. 50 }
  51. 51 let expected_size = img.width * img.height * img.channels
  52. 52● if img.data.length() != expected_size {
  53. 53● raise @types.LoadError::DecodeFailed(
  54. 54 "dark_channel: 数据大小不匹配,期望 " +
  55. 55● expected_size.to_string() +
  56. 56 ",实际 " +
  57. 57● img.data.length().to_string(),
  58. 58 )
  59. 59 }
  60. 60 if patch_size <= 0 || patch_size % 2 == 0 {
  61. 61● raise @types.LoadError::DecodeFailed(
  62. 62 "dark_channel: patch_size必须是正奇数",
  63. 63 )
  64. 64 }
  65. 65
  66. 66 let ps = patch_size
  67. 67 let w = img.width
  68. 68 let h = img.height
  69. 69 let channels = img.channels
  70. 70 let data = img.data
  71. 71 // 1. 计算每个像素的最小通道值
  72. 72 let min_channel : Array[Byte] = []
  73. 73 for i in 0..<(w * h) {
  74. 74● let idx = i * channels
  75. 75● let r = data[idx].to_int()
  76. 76● let g = if channels >= 2 { data[idx + 1].to_int() } else { r }
  77. 77● let b = if channels >= 3 { data[idx + 2].to_int() } else { r }
  78. 78● let min_val = r.min(g).min(b)
  79. 79● min_channel.push(min_val.to_byte())
  80. 80 }
  81. 81 // 2. 计算局部最小值(最小值滤波)
  82. 82● let out_data : Array[Byte] = Array::make(w * h, b'\x00')
  83. 83 let half = ps / 2
  84. 84 for y in 0..<h {
  85. 85● for x in 0..<w {
  86. 86● let mut min_val = 255
  87. 87 for dy in -half..<(half + 1) {
  88. 88● for dx in -half..<(half + 1) {
  89. 89● let nx = x + dx
  90. 90 let ny = y + dy
  91. 91 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  92. 92● let val = min_channel[ny * w + nx].to_int()
  93. 93 if val < min_val {
  94. 94● min_val = val
  95. 95 }
  96. 96 }
  97. 97 }
  98. 98 }
  99. 99● out_data[y * w + x] = min_val.to_byte()
  100. 100 }
  101. 101 }
  102. 102 @types.Image::{
  103. 103 width: w,
  104. 104 height: h,
  105. 105 channels: 1,
  106. 106● data: Bytes::from_array(out_data),
  107. 107 }
  108. 108}
  109. 109
  110. 110///|
  111. 111/// 估计大气光
  112. 112/// 取暗通道中最亮的 0.1% 像素,在原图中取这些像素的最大值作为大气光
  113. 113pub fn estimate_atmospheric_light(
  114. 114 img : @types.Image,
  115. 115 dark : @types.Image,
  116. 116 top_percent~ : Int32 = 1000,
  117. 117) -> Array[Int32] raise @types.LoadError {
  118. 118 // 输入验证
  119. 119● if img.width <= 0 || img.height <= 0 {
  120. 120○ raise @types.LoadError::DecodeFailed(
  121. 121 "estimate_atmospheric_light: 图像尺寸必须大于0",
  122. 122 )
  123. 123 }
  124. 124 if img.channels < 1 || img.channels > 4 {
  125. 125● raise @types.LoadError::DecodeFailed(
  126. 126 "estimate_atmospheric_light: 通道数必须在1-4之间",
  127. 127 )
  128. 128 }
  129. 129 if dark.width != img.width || dark.height != img.height {
  130. 130○ raise @types.LoadError::DecodeFailed(
  131. 131 "estimate_atmospheric_light: 暗通道图像尺寸必须与原图一致",
  132. 132 )
  133. 133 }
  134. 134 if dark.channels != 1 {
  135. 135● raise @types.LoadError::DecodeFailed(
  136. 136 "estimate_atmospheric_light: 暗通道图像必须是单通道",
  137. 137 )
  138. 138 }
  139. 139 if top_percent <= 0 {
  140. 140○ raise @types.LoadError::DecodeFailed(
  141. 141 "estimate_atmospheric_light: top_percent必须大于0",
  142. 142 )
  143. 143 }
  144. 144
  145. 145 let top = top_percent
  146. 146 let w = img.width
  147. 147 let h = img.height
  148. 148 let channels = img.channels
  149. 149 let img_data = img.data
  150. 150 let dark_data = dark.data
  151. 151 // 收集所有像素的暗通道值和坐标
  152. 152 let pixels : Array[(Int32, Int32)] = []
  153. 153 for y in 0..<h {
  154. 154● for x in 0..<w {
  155. 155● pixels.push((dark_data[y * w + x].to_int(), y * w + x))
  156. 156 }
  157. 157 }
  158. 158 // 按暗通道值降序排序(选择排序,取前 top 个)
  159. 159● let count = if top < pixels.length() { top } else { pixels.length() }
  160. 160 let selected_indices : Array[Int32] = []
  161. 161● let used : Array[Bool] = Array::make(pixels.length(), false)
  162. 162 for _ in 0..<count {
  163. 163● let mut max_val = -1
  164. 164 let mut max_idx = 0
  165. 165● for i in 0..<pixels.length() {
  166. 166● if !used[i] && pixels[i].0 > max_val {
  167. 167● max_val = pixels[i].0
  168. 168 max_idx = i
  169. 169 }
  170. 170 }
  171. 171 used[max_idx] = true
  172. 172● selected_indices.push(pixels[max_idx].1)
  173. 173 }
  174. 174 // 在这些像素中取每个通道的最大值
  175. 175 let atmospheric : Array[Int32] = []
  176. 176 for c in 0..<channels {
  177. 177● let mut max_val = 0
  178. 178 for idx in selected_indices {
  179. 179● let val = img_data[idx * channels + c].to_int()
  180. 180 if val > max_val {
  181. 181● max_val = val
  182. 182 }
  183. 183 }
  184. 184● atmospheric.push(max_val)
  185. 185 }
  186. 186 atmospheric
  187. 187}
  188. 188
  189. 189///|
  190. 190/// 计算透射率
  191. 191/// 透射率 t(x) = 1 - omega * dark_channel(I/A)
  192. 192pub fn estimate_transmission(
  193. 193 img : @types.Image,
  194. 194 atmospheric : Array[Int32],
  195. 195 omega~ : Double = 0.95,
  196. 196 patch_size~ : Int32 = 15,
  197. 197) -> @types.Image raise @types.LoadError {
  198. 198 // 输入验证
  199. 199● if img.width <= 0 || img.height <= 0 {
  200. 200○ raise @types.LoadError::DecodeFailed(
  201. 201 "estimate_transmission: 图像尺寸必须大于0",
  202. 202 )
  203. 203 }
  204. 204 if img.channels < 1 || img.channels > 4 {
  205. 205○ raise @types.LoadError::DecodeFailed(
  206. 206 "estimate_transmission: 通道数必须在1-4之间",
  207. 207 )
  208. 208 }
  209. 209● if atmospheric.length() == 0 {
  210. 210● raise @types.LoadError::DecodeFailed(
  211. 211 "estimate_transmission: atmospheric数组不能为空",
  212. 212 )
  213. 213 }
  214. 214 if omega < 0.0 || omega > 1.0 {
  215. 215● raise @types.LoadError::DecodeFailed(
  216. 216 "estimate_transmission: omega必须在0-1之间",
  217. 217 )
  218. 218 }
  219. 219 if patch_size <= 0 || patch_size % 2 == 0 {
  220. 220○ raise @types.LoadError::DecodeFailed(
  221. 221 "estimate_transmission: patch_size必须是正奇数",
  222. 222 )
  223. 223 }
  224. 224
  225. 225 let om = omega
  226. 226 let ps = patch_size
  227. 227 let w = img.width
  228. 228 let h = img.height
  229. 229 let channels = img.channels
  230. 230 let data = img.data
  231. 231 // 1. 归一化图像(除以大气光)
  232. 232 let normalized : Array[Byte] = []
  233. 233 for i in 0..<(w * h) {
  234. 234● for c in 0..<channels {
  235. 235● let idx = i * channels + c
  236. 236● let val = data[idx].to_double()
  237. 237● let a = if c < atmospheric.length() {
  238. 238● atmospheric[c].to_double()
  239. 239 } else {
  240. 240○ 255.0
  241. 241 }
  242. 242 let norm = if a > 0.0 {
  243. 243● (val / a * 255.0).min(255.0).max(0.0)
  244. 244 } else {
  245. 245○ 0.0
  246. 246 }
  247. 247● normalized.push(norm.to_int().to_byte())
  248. 248 }
  249. 249 }
  250. 250 let norm_img = @types.Image::{
  251. 251 width: w,
  252. 252 height: h,
  253. 253 channels,
  254. 254● data: Bytes::from_array(normalized),
  255. 255 }
  256. 256 // 2. 计算归一化图像的暗通道
  257. 257● let dark = dark_channel(norm_img, patch_size=ps)
  258. 258 // 3. 计算透射率
  259. 259 let dark_data = dark.data
  260. 260● let out_data : Array[Byte] = Array::make(w * h, b'\x00')
  261. 261 for i in 0..<(w * h) {
  262. 262● let t = 1.0 - om * dark_data[i].to_double() / 255.0
  263. 263● let t_clamped = t.max(0.0).min(1.0)
  264. 264● out_data[i] = (t_clamped * 255.0).to_int().to_byte()
  265. 265 }
  266. 266 @types.Image::{
  267. 267 width: w,
  268. 268 height: h,
  269. 269 channels: 1,
  270. 270● data: Bytes::from_array(out_data),
  271. 271 }
  272. 272}
  273. 273
  274. 274///|
  275. 275/// 引导滤波(用于透射率优化)
  276. 276/// 简化版:使用均值滤波近似引导滤波
  277. 277pub fn guided_filter_image(
  278. 278 input : @types.Image,
  279. 279 guide : @types.Image,
  280. 280 radius~ : Int32 = 20,
  281. 281 eps~ : Double = 0.0001,
  282. 282) -> @types.Image raise @types.LoadError {
  283. 283 // 输入验证
  284. 284● if input.width <= 0 || input.height <= 0 {
  285. 285○ raise @types.LoadError::DecodeFailed(
  286. 286 "guided_filter_image: 输入图像尺寸必须大于0",
  287. 287 )
  288. 288 }
  289. 289 if guide.width <= 0 || guide.height <= 0 {
  290. 290○ raise @types.LoadError::DecodeFailed(
  291. 291 "guided_filter_image: 引导图像尺寸必须大于0",
  292. 292 )
  293. 293 }
  294. 294 if input.width != guide.width || input.height != guide.height {
  295. 295● raise @types.LoadError::DecodeFailed(
  296. 296 "guided_filter_image: 输入图像和引导图像尺寸必须一致",
  297. 297 )
  298. 298 }
  299. 299 if radius <= 0 {
  300. 300● raise @types.LoadError::DecodeFailed(
  301. 301 "guided_filter_image: radius必须大于0",
  302. 302 )
  303. 303 }
  304. 304 if eps < 0.0 {
  305. 305○ raise @types.LoadError::DecodeFailed(
  306. 306 "guided_filter_image: eps必须大于等于0",
  307. 307 )
  308. 308 }
  309. 309
  310. 310 let r = radius
  311. 311 let e = eps
  312. 312 let w = input.width
  313. 313 let h = input.height
  314. 314 let input_data = input.data
  315. 315 let guide_data = guide.data
  316. 316 // 简化版引导滤波:使用均值滤波
  317. 317 // 完整引导滤波需要计算均值、方差、协方差等
  318. 318 // 这里用双边滤波近似
  319. 319● let out_data : Array[Byte] = Array::make(w * h, b'\x00')
  320. 320 let half = r / 2
  321. 321 for y in 0..<h {
  322. 322● for x in 0..<w {
  323. 323● let mut sum = 0.0
  324. 324 let mut weight_sum = 0.0
  325. 325● let center_guide = guide_data[y * w + x].to_double()
  326. 326 for dy in -half..<(half + 1) {
  327. 327● for dx in -half..<(half + 1) {
  328. 328● let nx = x + dx
  329. 329 let ny = y + dy
  330. 330 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  331. 331● let spatial_dist = (dx * dx + dy * dy).to_double().sqrt()
  332. 332● let guide_diff = (guide_data[ny * w + nx].to_double() - center_guide).abs()
  333. 333 // 双边权重:空间距离 + 引导图差异
  334. 334● let spatial_weight = (1.0 - spatial_dist / half.to_double()).max(
  335. 335 0.0,
  336. 336 )
  337. 337● let range_weight = (1.0 - guide_diff / 255.0).max(0.0)
  338. 338 let weight = spatial_weight * range_weight + e
  339. 339● sum += input_data[ny * w + nx].to_double() * weight
  340. 340 weight_sum += weight
  341. 341 }
  342. 342 }
  343. 343 }
  344. 344 let result = if weight_sum > 0.0 {
  345. 345● sum / weight_sum
  346. 346 } else {
  347. 347○ input_data[y * w + x].to_double()
  348. 348 }
  349. 349● out_data[y * w + x] = result.min(255.0).max(0.0).to_int().to_byte()
  350. 350 }
  351. 351 }
  352. 352 @types.Image::{
  353. 353 width: w,
  354. 354 height: h,
  355. 355 channels: 1,
  356. 356● data: Bytes::from_array(out_data),
  357. 357 }
  358. 358}
  359. 359
  360. 360///|
  361. 361/// 去雾复原
  362. 362/// 去雾复原 J(x) = (I(x) - A) / max(t(x), t0) + A
  363. 363pub fn dehaze_recover(
  364. 364 img : @types.Image,
  365. 365 transmission : @types.Image,
  366. 366 atmospheric : Array[Int32],
  367. 367 t_min~ : Double = 0.1,
  368. 368) -> @types.Image raise @types.LoadError {
  369. 369 // 输入验证
  370. 370● if img.width <= 0 || img.height <= 0 {
  371. 371○ raise @types.LoadError::DecodeFailed(
  372. 372 "dehaze_recover: 图像尺寸必须大于0",
  373. 373 )
  374. 374 }
  375. 375 if img.channels < 1 || img.channels > 4 {
  376. 376○ raise @types.LoadError::DecodeFailed(
  377. 377 "dehaze_recover: 通道数必须在1-4之间",
  378. 378 )
  379. 379 }
  380. 380 if transmission.width != img.width || transmission.height != img.height {
  381. 381● raise @types.LoadError::DecodeFailed(
  382. 382 "dehaze_recover: 透射率图像尺寸必须与原图一致",
  383. 383 )
  384. 384 }
  385. 385 if transmission.channels != 1 {
  386. 386● raise @types.LoadError::DecodeFailed(
  387. 387 "dehaze_recover: 透射率图像必须是单通道",
  388. 388 )
  389. 389 }
  390. 390● if atmospheric.length() == 0 {
  391. 391● raise @types.LoadError::DecodeFailed(
  392. 392 "dehaze_recover: atmospheric数组不能为空",
  393. 393 )
  394. 394 }
  395. 395 if t_min < 0.0 || t_min > 1.0 {
  396. 396○ raise @types.LoadError::DecodeFailed(
  397. 397 "dehaze_recover: t_min必须在0-1之间",
  398. 398 )
  399. 399 }
  400. 400
  401. 401 let t0 = t_min
  402. 402 let w = img.width
  403. 403 let h = img.height
  404. 404 let channels = img.channels
  405. 405 let img_data = img.data
  406. 406 let trans_data = transmission.data
  407. 407● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  408. 408 for i in 0..<(w * h) {
  409. 409● let t = trans_data[i].to_double() / 255.0
  410. 410● let t_clamped = t.max(t0)
  411. 411 for c in 0..<channels {
  412. 412● let idx = i * channels + c
  413. 413● let val = img_data[idx].to_double()
  414. 414● let a = if c < atmospheric.length() {
  415. 415● atmospheric[c].to_double()
  416. 416 } else {
  417. 417○ 255.0
  418. 418 }
  419. 419 let recovered = (val - a) / t_clamped + a
  420. 420● out_data[idx] = recovered.min(255.0).max(0.0).to_int().to_byte()
  421. 421 }
  422. 422 }
  423. 423 @types.Image::{
  424. 424 width: w,
  425. 425 height: h,
  426. 426 channels,
  427. 427● data: Bytes::from_array(out_data),
  428. 428 }
  429. 429}
  430. 430
  431. 431///|
  432. 432/// 完整去雾流水线
  433. 433pub fn dehaze_advanced(
  434. 434 img : @types.Image,
  435. 435 config? : DehazeConfig,
  436. 436) -> @types.Image raise @types.LoadError {
  437. 437 // 输入验证
  438. 438● if img.width <= 0 || img.height <= 0 {
  439. 439● raise @types.LoadError::DecodeFailed(
  440. 440 "dehaze_advanced: 图像尺寸必须大于0",
  441. 441 )
  442. 442 }
  443. 443 if img.channels < 1 || img.channels > 4 {
  444. 444● raise @types.LoadError::DecodeFailed(
  445. 445 "dehaze_advanced: 通道数必须在1-4之间",
  446. 446 )
  447. 447 }
  448. 448 let expected_size = img.width * img.height * img.channels
  449. 449● if img.data.length() != expected_size {
  450. 450● raise @types.LoadError::DecodeFailed(
  451. 451 "dehaze_advanced: 数据大小不匹配,期望 " +
  452. 452● expected_size.to_string() +
  453. 453 ",实际 " +
  454. 454● img.data.length().to_string(),
  455. 455 )
  456. 456 }
  457. 457
  458. 458● let cfg = match config {
  459. 459● Some(c) => c
  460. 460● None => dehaze_default_config()
  461. 461 }
  462. 462
  463. 463 // 参数验证
  464. 464 if cfg.patch_size <= 0 || cfg.patch_size % 2 == 0 {
  465. 465● raise @types.LoadError::DecodeFailed(
  466. 466 "dehaze_advanced: patch_size必须是正奇数",
  467. 467 )
  468. 468 }
  469. 469 if cfg.omega < 0.0 || cfg.omega > 1.0 {
  470. 470○ raise @types.LoadError::DecodeFailed(
  471. 471 "dehaze_advanced: omega必须在0-1之间",
  472. 472 )
  473. 473 }
  474. 474 if cfg.t_min < 0.0 || cfg.t_min > 1.0 {
  475. 475○ raise @types.LoadError::DecodeFailed(
  476. 476 "dehaze_advanced: t_min必须在0-1之间",
  477. 477 )
  478. 478 }
  479. 479 if cfg.guided_radius <= 0 {
  480. 480○ raise @types.LoadError::DecodeFailed(
  481. 481 "dehaze_advanced: guided_radius必须大于0",
  482. 482 )
  483. 483 }
  484. 484 if cfg.max_atmospheric <= 0 {
  485. 485○ raise @types.LoadError::DecodeFailed(
  486. 486 "dehaze_advanced: max_atmospheric必须大于0",
  487. 487 )
  488. 488 }
  489. 489 // 1. 计算暗通道
  490. 490● let dark = dark_channel(img, patch_size=cfg.patch_size)
  491. 491 // 2. 估计大气光
  492. 492● let atmospheric = estimate_atmospheric_light(
  493. 493 img,
  494. 494 dark,
  495. 495 top_percent=cfg.max_atmospheric,
  496. 496 )
  497. 497 // 3. 估计透射率
  498. 498● let transmission_raw = estimate_transmission(
  499. 499 img,
  500. 500 atmospheric,
  501. 501 omega=cfg.omega,
  502. 502 patch_size=cfg.patch_size,
  503. 503 )
  504. 504 // 4. 引导滤波优化透射率
  505. 505● let transmission = guided_filter_image(
  506. 506 transmission_raw,
  507. 507 img,
  508. 508 radius=cfg.guided_radius,
  509. 509 eps=cfg.guided_eps,
  510. 510 )
  511. 511 // 5. 去雾复原
  512. 512● dehaze_recover(img, transmission, atmospheric, t_min=cfg.t_min)
  513. 513}