image.process\color\dehaze.mbt

92.4% · 121 / 131 points · 330 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// 暗通道先验去雾(纯 MoonBit)
  7. 7
  8. 8///|
  9. 9/// 暗通道先验去雾(Dark Channel Prior Dehaze)。
  10. 10/// 基于 He et al. (2009) 的暗通道先验方法。
  11. 11/// 适用于 RGB/RGBA 图像(channels=3/4)。
  12. 12/// patch_size 为暗通道计算窗口半径(默认 7),omega 为去雾强度(默认 0.95),
  13. 13/// t0 为透射率下限(默认 0.1)。
  14. 14pub fn dehaze(
  15. 15 img : @types.Image,
  16. 16 patch_size~ : Int32 = 7,
  17. 17 omega~ : Float = 0.95F,
  18. 18 t0~ : Float = 0.1F,
  19. 19) -> @types.Image raise @types.LoadError {
  20. 20 // 输入验证
  21. 21● if img.width < 0 || img.height < 0 {
  22. 22○ raise @types.LoadError::DecodeFailed("dehaze: 图像尺寸不能为负")
  23. 23 }
  24. 24 if img.channels != 3 && img.channels != 4 {
  25. 25● raise @types.LoadError::DecodeFailed("dehaze 需要 RGB/RGBA 图像")
  26. 26 }
  27. 27 let expected_size = img.width * img.height * img.channels
  28. 28● if img.data.length() != expected_size {
  29. 29○ raise @types.LoadError::DecodeFailed(
  30. 30 "dehaze: 数据大小不匹配,期望 " +
  31. 31○ expected_size.to_string() +
  32. 32 ",实际 " +
  33. 33○ img.data.length().to_string(),
  34. 34 )
  35. 35 }
  36. 36 if patch_size < 1 || patch_size > 30 {
  37. 37● raise @types.LoadError::DecodeFailed("patch_size 必须在 1..30 之间")
  38. 38 }
  39. 39 if omega <= 0.0F || omega > 1.0F {
  40. 40● raise @types.LoadError::DecodeFailed("omega 必须在 (0, 1] 之间")
  41. 41 }
  42. 42 if t0 <= 0.0F || t0 > 1.0F {
  43. 43● raise @types.LoadError::DecodeFailed("t0 必须在 (0, 1] 之间")
  44. 44 }
  45. 45 let ch = img.channels
  46. 46 let w = img.width
  47. 47 let h = img.height
  48. 48 let n = w * h
  49. 49 if n == 0 {
  50. 50● return img
  51. 51 }
  52. 52 // 1. 计算暗通道
  53. 53● let dark_channel = compute_dark_channel(img, w, h, ch, patch_size)
  54. 54 // 2. 估计大气光 A(取暗通道中最亮的 0.1% 像素对应的原图最亮像素)
  55. 55● let a = estimate_atmospheric_light(img, dark_channel, w, h, ch)
  56. 56● let ar = Float::from_int(a[0])
  57. 57● let ag = Float::from_int(a[1])
  58. 58● let ab = Float::from_int(a[2])
  59. 59 // 3. 计算透射率 t = 1 - omega * dark(I/A)
  60. 60● let transmission : Array[Float] = Array::make(n, 0.0F)
  61. 61 for i in 0..<n {
  62. 62● transmission[i] = 1.0F - omega * dark_channel[i]
  63. 63 if transmission[i] < t0 {
  64. 64● transmission[i] = t0
  65. 65 }
  66. 66 }
  67. 67 // 4. 恢复无雾图像: J = (I - A) / t + A
  68. 68● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  69. 69 for i in 0..<n {
  70. 70● for c in 0..<3 {
  71. 71● let iv = Float::from_int(img.data[i * ch + c].to_int())
  72. 72● let ac = if c == 0 { ar } else if c == 1 { ag } else { ab }
  73. 73 let jv = (iv - ac) / transmission[i] + ac
  74. 74● out[i * ch + c] = jv.to_int().clamp(min=0, max=255).to_byte()
  75. 75 }
  76. 76 if ch == 4 {
  77. 77● out[i * ch + 3] = img.data[i * ch + 3]
  78. 78 }
  79. 79 }
  80. 80 @types.Image::{
  81. 81 width: w,
  82. 82 height: h,
  83. 83 channels: ch,
  84. 84● data: Bytes::from_array(out),
  85. 85 }
  86. 86}
  87. 87
  88. 88///|
  89. 89/// 计算暗通道
  90. 90fn compute_dark_channel(
  91. 91 img : @types.Image,
  92. 92 w : Int32,
  93. 93 h : Int32,
  94. 94 ch : Int32,
  95. 95 patch_size : Int32,
  96. 96) -> Array[Float] {
  97. 97● let n = w * h
  98. 98 // 先计算每个像素的最小通道值
  99. 99● let min_channel : Array[Int32] = Array::make(n, 255)
  100. 100 for i in 0..<n {
  101. 101● let r = img.data[i * ch].to_int()
  102. 102● let g = img.data[i * ch + 1].to_int()
  103. 103● let b = img.data[i * ch + 2].to_int()
  104. 104● min_channel[i] = r.min(g).min(b)
  105. 105 }
  106. 106 // 最小值滤波,分离内部/边界区域
  107. 107● let dark : Array[Float] = Array::make(n, 0.0F)
  108. 108 // 内部区域:x in [patch_size, w-patch_size), y in [patch_size, h-patch_size)
  109. 109 for y in patch_size..<(h - patch_size) {
  110. 110● for x in patch_size..<(w - patch_size) {
  111. 111● let mut min_val = 255
  112. 112 for py in (y - patch_size)..<=(y + patch_size) {
  113. 113● for px in (x - patch_size)..<=(x + patch_size) {
  114. 114● if min_channel[py * w + px] < min_val {
  115. 115● min_val = min_channel[py * w + px]
  116. 116 }
  117. 117 }
  118. 118 }
  119. 119● dark[y * w + x] = Float::from_int(min_val) / 255.0F
  120. 120 }
  121. 121 }
  122. 122 // 边界区域
  123. 123 for y in 0..<h {
  124. 124● let y_inner = y >= patch_size && y < h - patch_size
  125. 125 for x in 0..<w {
  126. 126● if y_inner && x >= patch_size && x < w - patch_size {
  127. 127● continue
  128. 128 }
  129. 129 let mut min_val = 255
  130. 130● let y_start = (y - patch_size).clamp(min=0, max=h - 1)
  131. 131● let y_end = (y + patch_size).clamp(min=0, max=h - 1)
  132. 132● let x_start = (x - patch_size).clamp(min=0, max=w - 1)
  133. 133● let x_end = (x + patch_size).clamp(min=0, max=w - 1)
  134. 134 for py in y_start..<=y_end {
  135. 135● for px in x_start..<=x_end {
  136. 136● if min_channel[py * w + px] < min_val {
  137. 137● min_val = min_channel[py * w + px]
  138. 138 }
  139. 139 }
  140. 140 }
  141. 141● dark[y * w + x] = Float::from_int(min_val) / 255.0F
  142. 142 }
  143. 143 }
  144. 144 dark
  145. 145}
  146. 146
  147. 147///|
  148. 148/// 估计大气光
  149. 149fn estimate_atmospheric_light(
  150. 150 img : @types.Image,
  151. 151 dark_channel : Array[Float],
  152. 152 w : Int32,
  153. 153 h : Int32,
  154. 154 ch : Int32,
  155. 155) -> Array[Int32] {
  156. 156● let n = w * h
  157. 157 // 取暗通道中最亮的 0.1% 像素
  158. 158● let num_pixels = (n / 1000).max(1)
  159. 159 // 收集暗通道值和索引
  160. 160 let pairs : Array[(Float, Int32)] = []
  161. 161 for i in 0..<n {
  162. 162● pairs.push((dark_channel[i], i))
  163. 163 }
  164. 164 // 按暗通道值降序排序
  165. 165● pairs.sort_by(fn(a, b) { b.0.compare(a.0) })
  166. 166 // 在这些像素中找原图最亮的像素
  167. 167 let mut max_r = 0
  168. 168 let mut max_g = 0
  169. 169 let mut max_b = 0
  170. 170 let mut max_sum = 0
  171. 171● for i in 0..<num_pixels.min(pairs.length()) {
  172. 172● let idx = pairs[i].1
  173. 173● let r = img.data[idx * ch].to_int()
  174. 174● let g = img.data[idx * ch + 1].to_int()
  175. 175● let b = img.data[idx * ch + 2].to_int()
  176. 176 let sum = r + g + b
  177. 177 if sum > max_sum {
  178. 178● max_sum = sum
  179. 179 max_r = r
  180. 180 max_g = g
  181. 181 max_b = b
  182. 182 }
  183. 183 }
  184. 184 [max_r, max_g, max_b]
  185. 185}
  186. 186
  187. 187///|
  188. 188/// 引导滤波(Guided Filter)用于优化透射率。
  189. 189/// 适用于灰度图(channels=1)作为引导图。
  190. 190pub fn guided_filter(
  191. 191 img : @types.Image,
  192. 192 input : Array[Float],
  193. 193 radius~ : Int32 = 20,
  194. 194 eps~ : Float = 0.001F,
  195. 195) -> Array[Float] raise @types.LoadError {
  196. 196 // 输入验证
  197. 197● if img.width < 0 || img.height < 0 {
  198. 198○ raise @types.LoadError::DecodeFailed(
  199. 199 "guided_filter: 图像尺寸不能为负",
  200. 200 )
  201. 201 }
  202. 202 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  203. 203● raise @types.LoadError::DecodeFailed(
  204. 204 "guided_filter 仅支持 channels=1/3/4",
  205. 205 )
  206. 206 }
  207. 207 let expected_size = img.width * img.height * img.channels
  208. 208● if img.data.length() != expected_size {
  209. 209○ raise @types.LoadError::DecodeFailed(
  210. 210 "guided_filter: 数据大小不匹配,期望 " +
  211. 211○ expected_size.to_string() +
  212. 212 ",实际 " +
  213. 213○ img.data.length().to_string(),
  214. 214 )
  215. 215 }
  216. 216 if radius < 1 {
  217. 217● raise @types.LoadError::DecodeFailed("guided_filter: radius必须>=1")
  218. 218 }
  219. 219 if eps <= 0.0F {
  220. 220● raise @types.LoadError::DecodeFailed("guided_filter: eps必须大于0")
  221. 221 }
  222. 222 let w = img.width
  223. 223 let h = img.height
  224. 224 let n = w * h
  225. 225● if n == 0 || input.length() != n {
  226. 226● return []
  227. 227 }
  228. 228 // 转灰度作为引导图
  229. 229● let guide : Array[Float] = Array::make(n, 0.0F)
  230. 230 let ch = img.channels
  231. 231 for i in 0..<n {
  232. 232● let v = if ch == 1 {
  233. 233● img.data[i].to_int()
  234. 234 } else {
  235. 235● let r = img.data[i * ch].to_int()
  236. 236● let g = img.data[i * ch + 1].to_int()
  237. 237● let b = img.data[i * ch + 2].to_int()
  238. 238 (r * 299 + g * 587 + b * 114) / 1000
  239. 239 }
  240. 240● guide[i] = Float::from_int(v) / 255.0F
  241. 241 }
  242. 242 // 计算均值
  243. 243● let mean_i = box_filter(guide, w, h, radius)
  244. 244● let mean_p = box_filter(input, w, h, radius)
  245. 245 // 计算 I*P 的均值
  246. 246● let ip : Array[Float] = Array::make(n, 0.0F)
  247. 247 for i in 0..<n {
  248. 248● ip[i] = guide[i] * input[i]
  249. 249 }
  250. 250● let mean_ip = box_filter(ip, w, h, radius)
  251. 251 // 协方差
  252. 252● let cov_ip : Array[Float] = Array::make(n, 0.0F)
  253. 253 for i in 0..<n {
  254. 254● cov_ip[i] = mean_ip[i] - mean_i[i] * mean_p[i]
  255. 255 }
  256. 256 // I² 的均值
  257. 257● let i_sq : Array[Float] = Array::make(n, 0.0F)
  258. 258 for i in 0..<n {
  259. 259● i_sq[i] = guide[i] * guide[i]
  260. 260 }
  261. 261● let mean_i_sq = box_filter(i_sq, w, h, radius)
  262. 262 // 方差
  263. 263● let var_i : Array[Float] = Array::make(n, 0.0F)
  264. 264 for i in 0..<n {
  265. 265● var_i[i] = mean_i_sq[i] - mean_i[i] * mean_i[i]
  266. 266 }
  267. 267 // a = cov_ip / (var_i + eps), b = mean_p - a * mean_i
  268. 268● let a : Array[Float] = Array::make(n, 0.0F)
  269. 269● let b : Array[Float] = Array::make(n, 0.0F)
  270. 270 for i in 0..<n {
  271. 271● a[i] = cov_ip[i] / (var_i[i] + eps)
  272. 272 b[i] = mean_p[i] - a[i] * mean_i[i]
  273. 273 }
  274. 274● let mean_a = box_filter(a, w, h, radius)
  275. 275● let mean_b = box_filter(b, w, h, radius)
  276. 276 // 输出 = mean_a * guide + mean_b
  277. 277● let out : Array[Float] = Array::make(n, 0.0F)
  278. 278 for i in 0..<n {
  279. 279● out[i] = mean_a[i] * guide[i] + mean_b[i]
  280. 280 }
  281. 281 out
  282. 282}
  283. 283
  284. 284///|
  285. 285/// 盒式滤波(均值滤波),分离内部/边界区域以消除热点 clamp 调用
  286. 286fn box_filter(
  287. 287 data : Array[Float],
  288. 288 w : Int32,
  289. 289 h : Int32,
  290. 290 radius : Int32,
  291. 291) -> Array[Float] {
  292. 292● let n = w * h
  293. 293● let out : Array[Float] = Array::make(n, 0.0F)
  294. 294 // 内部区域 count 恒为 (2*radius+1)²
  295. 295● let inner_count = Float::from_int((2 * radius + 1) * (2 * radius + 1))
  296. 296 for y in radius..<(h - radius) {
  297. 297● for x in radius..<(w - radius) {
  298. 298● let mut sum = 0.0F
  299. 299 for py in (y - radius)..<=(y + radius) {
  300. 300● for px in (x - radius)..<=(x + radius) {
  301. 301● sum += data[py * w + px]
  302. 302 }
  303. 303 }
  304. 304 out[y * w + x] = sum / inner_count
  305. 305 }
  306. 306 }
  307. 307 // 边界区域
  308. 308 for y in 0..<h {
  309. 309● let y_inner = y >= radius && y < h - radius
  310. 310 for x in 0..<w {
  311. 311● if y_inner && x >= radius && x < w - radius {
  312. 312● continue
  313. 313 }
  314. 314 let mut sum = 0.0F
  315. 315 let mut count = 0
  316. 316● let y_start = (y - radius).clamp(min=0, max=h - 1)
  317. 317● let y_end = (y + radius).clamp(min=0, max=h - 1)
  318. 318● let x_start = (x - radius).clamp(min=0, max=w - 1)
  319. 319● let x_end = (x + radius).clamp(min=0, max=w - 1)
  320. 320 for py in y_start..<=y_end {
  321. 321● for px in x_start..<=x_end {
  322. 322● sum += data[py * w + px]
  323. 323 count += 1
  324. 324 }
  325. 325 }
  326. 326● out[y * w + x] = sum / Float::from_int(count)
  327. 327 }
  328. 328 }
  329. 329 out
  330. 330}