image.process\transform\multi_band_blend.mbt

94% · 202 / 215 points · 411 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/// # 多频段融合 multi band blend
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 拉普拉斯金字塔融合(纯 MoonBit):multi_band_blend
  7. 7// 多频带融合:对两幅图像和掩码构建拉普拉斯/高斯金字塔,逐层混合后重建
  8. 8
  9. 9///|
  10. 10/// 多频带融合(Laplacian pyramid blend)。
  11. 11/// img_a 和 img_b 尺寸需相同,mask 为灰度图(非零区域取 img_a,零区域取 img_b)。
  12. 12/// num_bands 为金字塔层数(默认 5)。
  13. 13pub fn multi_band_blend(
  14. 14 img_a : @types.Image,
  15. 15 img_b : @types.Image,
  16. 16 mask : @types.Image,
  17. 17 num_bands~ : Int32 = 5,
  18. 18) -> @types.Image raise @types.LoadError {
  19. 19● if img_a.width != img_b.width || img_a.height != img_b.height {
  20. 20● raise @types.LoadError::DecodeFailed("img_a 和 img_b 尺寸需相同")
  21. 21 }
  22. 22 if mask.width != img_a.width || mask.height != img_a.height {
  23. 23● raise @types.LoadError::DecodeFailed("mask 尺寸需与输入图像相同")
  24. 24 }
  25. 25 let w = img_a.width
  26. 26 let h = img_a.height
  27. 27 let ch = img_a.channels
  28. 28 let n = w * h
  29. 29 // 归一化 mask 到 [0, 1](用 Float 数组)
  30. 30● let mask_f : Array[Float] = Array::make(n, 0.0F)
  31. 31 for i in 0..<n {
  32. 32● mask_f[i] = Float::from_int(mask.data[i].to_int()) / 255.0F
  33. 33 }
  34. 34 // 构建 img_a 和 img_b 的 Float 数组
  35. 35● let a_f : Array[Float] = Array::make(n * ch, 0.0F)
  36. 36● let b_f : Array[Float] = Array::make(n * ch, 0.0F)
  37. 37 for i in 0..<(n * ch) {
  38. 38● a_f[i] = Float::from_int(img_a.data[i].to_int())
  39. 39● b_f[i] = Float::from_int(img_b.data[i].to_int())
  40. 40 }
  41. 41 // 构建高斯金字塔(mask)和拉普拉斯金字塔(img_a, img_b)
  42. 42◐ let levels = if num_bands > 0 { num_bands } else { 5 }
  43. 43 // 高斯金字塔:mask
  44. 44 let mask_pyr : Array[Array[Float]] = [mask_f]
  45. 45 for _l in 1..<levels {
  46. 46● let prev = mask_pyr[mask_pyr.length() - 1]
  47. 47● let pw = get_w_from_n(prev.length(), w, h)
  48. 48● let ph = prev.length() / pw
  49. 49● mask_pyr.push(blur_downsample(prev, pw, ph))
  50. 50 }
  51. 51 // 拉普拉斯金字塔:img_a 和 img_b(每通道独立)
  52. 52● let out : Array[Float] = Array::make(n * ch, 0.0F)
  53. 53 for c in 0..<ch {
  54. 54● let a_chan : Array[Float] = Array::make(n, 0.0F)
  55. 55● let b_chan : Array[Float] = Array::make(n, 0.0F)
  56. 56 for i in 0..<n {
  57. 57● a_chan[i] = a_f[i * ch + c]
  58. 58 b_chan[i] = b_f[i * ch + c]
  59. 59 }
  60. 60 // 构建拉普拉斯金字塔
  61. 61● let a_pyr = build_laplacian_pyramid_f(a_chan, w, h, levels)
  62. 62● let b_pyr = build_laplacian_pyramid_f(b_chan, w, h, levels)
  63. 63 // 逐层混合
  64. 64 let blended : Array[Array[Float]] = []
  65. 65● for l in 0..<a_pyr.length() {
  66. 66● let a_l = a_pyr[l]
  67. 67 let b_l = b_pyr[l]
  68. 68 let m_l = mask_pyr[l]
  69. 69● let blended_l : Array[Float] = Array::make(a_l.length(), 0.0F)
  70. 70● for i in 0..<a_l.length() {
  71. 71◐ let m = if i < m_l.length() { m_l[i] } else { 0.5F }
  72. 72 blended_l[i] = m * a_l[i] + (1.0F - m) * b_l[i]
  73. 73 }
  74. 74● blended.push(blended_l)
  75. 75 }
  76. 76 // 重建
  77. 77● let reconstructed = collapse_laplacian_pyramid_f(blended, w, h)
  78. 78 for i in 0..<n {
  79. 79● out[i * ch + c] = reconstructed[i]
  80. 80 }
  81. 81 }
  82. 82 // 转为 Byte
  83. 83● let out_bytes : Array[Byte] = Array::make(n * ch, b'\x00')
  84. 84 for i in 0..<(n * ch) {
  85. 85● out_bytes[i] = (out[i] + 0.5F).to_int().clamp(min=0, max=255).to_byte()
  86. 86 }
  87. 87 @types.Image::{
  88. 88 width: w,
  89. 89 height: h,
  90. 90 channels: ch,
  91. 91● data: Bytes::from_array(out_bytes),
  92. 92 }
  93. 93}
  94. 94
  95. 95///|
  96. 96fn get_w_from_n(n : Int32, w : Int32, h : Int32) -> Int32 {
  97. 97● if h == 0 {
  98. 98○ return 0
  99. 99 }
  100. 100 let ratio = n / h
  101. 101 if ratio > 0 {
  102. 102● ratio
  103. 103 } else {
  104. 104● w
  105. 105 }
  106. 106}
  107. 107
  108. 108///|
  109. 109fn blur_downsample(data : Array[Float], w : Int32, h : Int32) -> Array[Float] {
  110. 110● let nw = w / 2
  111. 111 let nh = h / 2
  112. 112 if nw == 0 || nh == 0 {
  113. 113● return [data[0]]
  114. 114 }
  115. 115● let out : Array[Float] = Array::make(nw * nh, 0.0F)
  116. 116 for y in 0..<nh {
  117. 117● for x in 0..<nw {
  118. 118● let sx = x * 2
  119. 119 let sy = y * 2
  120. 120 let mut sum = 0.0F
  121. 121 let mut count = 0
  122. 122 for dy in 0..<2 {
  123. 123● for dx in 0..<2 {
  124. 124● let px = (sx + dx).clamp(min=0, max=w - 1)
  125. 125● let py = (sy + dy).clamp(min=0, max=h - 1)
  126. 126 sum += data[py * w + px]
  127. 127 count += 1
  128. 128 }
  129. 129 }
  130. 130● out[y * nw + x] = sum / Float::from_int(count)
  131. 131 }
  132. 132 }
  133. 133 out
  134. 134}
  135. 135
  136. 136///|
  137. 137fn build_laplacian_pyramid_f(
  138. 138 data : Array[Float],
  139. 139 w : Int32,
  140. 140 h : Int32,
  141. 141 levels : Int32,
  142. 142) -> Array[Array[Float]] {
  143. 143● let gaussian : Array[(Array[Float], Int32, Int32)] = [(data, w, h)]
  144. 144 for _l in 1..<levels {
  145. 145● let (prev, pw, ph) = gaussian[gaussian.length() - 1]
  146. 146 if pw <= 1 || ph <= 1 {
  147. 147● break
  148. 148 }
  149. 149● let down = blur_downsample(prev, pw, ph)
  150. 150● gaussian.push((down, pw / 2, ph / 2))
  151. 151 }
  152. 152 // 拉普拉斯 = 当前层 - 上采样(下一层)
  153. 153 let laplacian : Array[Array[Float]] = []
  154. 154● for l in 0..<(gaussian.length() - 1) {
  155. 155● let (curr, cw, ch) = gaussian[l]
  156. 156 let (next, nw, nh) = gaussian[l + 1]
  157. 157● let upsampled = upsample_f(next, nw, nh, cw, ch)
  158. 158● let diff : Array[Float] = Array::make(curr.length(), 0.0F)
  159. 159● for i in 0..<curr.length() {
  160. 160● diff[i] = curr[i] - upsampled[i]
  161. 161 }
  162. 162● laplacian.push(diff)
  163. 163 }
  164. 164 // 最后一层(最粗分辨率)直接保留
  165. 165● let (last, _, _) = gaussian[gaussian.length() - 1]
  166. 166● laplacian.push(last)
  167. 167 laplacian
  168. 168}
  169. 169
  170. 170///|
  171. 171fn upsample_f(
  172. 172 data : Array[Float],
  173. 173 sw : Int32,
  174. 174 sh : Int32,
  175. 175 dw : Int32,
  176. 176 dh : Int32,
  177. 177) -> Array[Float] {
  178. 178● let out : Array[Float] = Array::make(dw * dh, 0.0F)
  179. 179 for y in 0..<dh {
  180. 180● for x in 0..<dw {
  181. 181● let sx = (x * sw / dw).clamp(min=0, max=sw - 1)
  182. 182● let sy = (y * sh / dh).clamp(min=0, max=sh - 1)
  183. 183 out[y * dw + x] = data[sy * sw + sx]
  184. 184 }
  185. 185 }
  186. 186 out
  187. 187}
  188. 188
  189. 189///|
  190. 190fn collapse_laplacian_pyramid_f(
  191. 191 pyramid : Array[Array[Float]],
  192. 192 w : Int32,
  193. 193 h : Int32,
  194. 194) -> Array[Float] {
  195. 195● if pyramid.length() == 0 {
  196. 196○ return Array::make(w * h, 0.0F)
  197. 197 }
  198. 198 // 从最粗层开始重建
  199. 199● let mut current = pyramid[pyramid.length() - 1]
  200. 200 let mut cw = 1
  201. 201 let mut ch = 1
  202. 202 // 推算最粗层尺寸
  203. 203 let mut tw = w
  204. 204 let mut th = h
  205. 205 let sizes : Array[(Int32, Int32)] = [(tw, th)]
  206. 206● for _l in 1..<pyramid.length() {
  207. 207● tw = tw / 2
  208. 208 th = th / 2
  209. 209 if tw == 0 {
  210. 210○ tw = 1
  211. 211 }
  212. 212 if th == 0 {
  213. 213○ th = 1
  214. 214 }
  215. 215● sizes.push((tw, th))
  216. 216 }
  217. 217● let (lcw, lch) = sizes[sizes.length() - 1]
  218. 218 cw = lcw
  219. 219 ch = lch
  220. 220 // 从倒数第二层开始向上重建
  221. 221● for k in 0..<(pyramid.length() - 1) {
  222. 222● let l = pyramid.length() - 2 - k
  223. 223 let (nw, nh) = sizes[l]
  224. 224● let upsampled = upsample_f(current, cw, ch, nw, nh)
  225. 225● current = Array::make(nw * nh, 0.0F)
  226. 226 for i in 0..<(nw * nh) {
  227. 227● current[i] = pyramid[l][i] + upsampled[i]
  228. 228 }
  229. 229 cw = nw
  230. 230 ch = nh
  231. 231 }
  232. 232 current
  233. 233}
  234. 234
  235. 235// ========== multi_band_blend 16-bit ==========
  236. 236
  237. 237///|
  238. 238/// 多频带融合 16-bit 版本。
  239. 239/// @see multi_band_blend 8-bit 版本,本函数为 Image16 变体
  240. 240pub fn multi_band_blend_16(
  241. 241 img_a : @types.Image16,
  242. 242 img_b : @types.Image16,
  243. 243 mask : @types.Image16,
  244. 244 num_bands~ : Int32 = 5,
  245. 245) -> @types.Image16 raise @types.LoadError {
  246. 246● if img_a.width != img_b.width || img_a.height != img_b.height {
  247. 247● raise @types.LoadError::DecodeFailed("img_a 和 img_b 尺寸需相同")
  248. 248 }
  249. 249 if mask.width != img_a.width || mask.height != img_a.height {
  250. 250● raise @types.LoadError::DecodeFailed("mask 尺寸需与输入图像相同")
  251. 251 }
  252. 252 let w = img_a.width
  253. 253 let h = img_a.height
  254. 254 let ch = img_a.channels
  255. 255 let n = w * h
  256. 256● let mask_f : Array[Float] = Array::make(n, 0.0F)
  257. 257 for i in 0..<n {
  258. 258● let v = mask.data[i * 2].to_int() | (mask.data[i * 2 + 1].to_int() << 8)
  259. 259● mask_f[i] = Float::from_int(v) / 65535.0F
  260. 260 }
  261. 261● let a_f : Array[Float] = Array::make(n * ch, 0.0F)
  262. 262● let b_f : Array[Float] = Array::make(n * ch, 0.0F)
  263. 263 for i in 0..<(n * ch) {
  264. 264● let va = img_a.data[i * 2].to_int() | (img_a.data[i * 2 + 1].to_int() << 8)
  265. 265● let vb = img_b.data[i * 2].to_int() | (img_b.data[i * 2 + 1].to_int() << 8)
  266. 266● a_f[i] = Float::from_int(va)
  267. 267● b_f[i] = Float::from_int(vb)
  268. 268 }
  269. 269◐ let levels = if num_bands > 0 { num_bands } else { 5 }
  270. 270 let mask_pyr : Array[Array[Float]] = [mask_f]
  271. 271 for _l in 1..<levels {
  272. 272● let prev = mask_pyr[mask_pyr.length() - 1]
  273. 273● let pw = get_w_from_n(prev.length(), w, h)
  274. 274● let ph = prev.length() / pw
  275. 275● mask_pyr.push(blur_downsample(prev, pw, ph))
  276. 276 }
  277. 277● let out : Array[Float] = Array::make(n * ch, 0.0F)
  278. 278 for c in 0..<ch {
  279. 279● let a_chan : Array[Float] = Array::make(n, 0.0F)
  280. 280● let b_chan : Array[Float] = Array::make(n, 0.0F)
  281. 281 for i in 0..<n {
  282. 282● a_chan[i] = a_f[i * ch + c]
  283. 283 b_chan[i] = b_f[i * ch + c]
  284. 284 }
  285. 285● let a_pyr = build_laplacian_pyramid_f(a_chan, w, h, levels)
  286. 286● let b_pyr = build_laplacian_pyramid_f(b_chan, w, h, levels)
  287. 287 let blended : Array[Array[Float]] = []
  288. 288● for l in 0..<a_pyr.length() {
  289. 289● let a_l = a_pyr[l]
  290. 290 let b_l = b_pyr[l]
  291. 291 let m_l = mask_pyr[l]
  292. 292● let blended_l : Array[Float] = Array::make(a_l.length(), 0.0F)
  293. 293● for i in 0..<a_l.length() {
  294. 294◐ let m = if i < m_l.length() { m_l[i] } else { 0.5F }
  295. 295 blended_l[i] = m * a_l[i] + (1.0F - m) * b_l[i]
  296. 296 }
  297. 297● blended.push(blended_l)
  298. 298 }
  299. 299● let reconstructed = collapse_laplacian_pyramid_f(blended, w, h)
  300. 300 for i in 0..<n {
  301. 301● out[i * ch + c] = reconstructed[i]
  302. 302 }
  303. 303 }
  304. 304● let out_bytes : Array[Byte] = Array::make(n * ch * 2, b'\x00')
  305. 305 for i in 0..<(n * ch) {
  306. 306● let v = (out[i] + 0.5F).to_int()
  307. 307◐ let clamped = if v < 0 { 0 } else if v > 65535 { 65535 } else { v }
  308. 308● out_bytes[i * 2] = (clamped & 0xFF).to_byte()
  309. 309● out_bytes[i * 2 + 1] = ((clamped >> 8) & 0xFF).to_byte()
  310. 310 }
  311. 311 @types.Image16::{
  312. 312 width: w,
  313. 313 height: h,
  314. 314 channels: ch,
  315. 315● data: Bytes::from_array(out_bytes),
  316. 316 }
  317. 317}
  318. 318
  319. 319// ========== multi_band_blend float ==========
  320. 320
  321. 321///|
  322. 322/// 多频带融合 float 版本。
  323. 323/// @see multi_band_blend 8-bit 版本,本函数为 ImageF 变体
  324. 324pub fn multi_band_blend_f(
  325. 325 img_a : @types.ImageF,
  326. 326 img_b : @types.ImageF,
  327. 327 mask : @types.ImageF,
  328. 328 num_bands~ : Int32 = 5,
  329. 329) -> @types.ImageF raise @types.LoadError {
  330. 330● if img_a.width != img_b.width || img_a.height != img_b.height {
  331. 331● raise @types.LoadError::DecodeFailed("img_a 和 img_b 尺寸需相同")
  332. 332 }
  333. 333 if mask.width != img_a.width || mask.height != img_a.height {
  334. 334● raise @types.LoadError::DecodeFailed("mask 尺寸需与输入图像相同")
  335. 335 }
  336. 336 let w = img_a.width
  337. 337 let h = img_a.height
  338. 338 let ch = img_a.channels
  339. 339 let n = w * h
  340. 340● let mask_f : Array[Float] = Array::make(n, 0.0F)
  341. 341 for i in 0..<n {
  342. 342● let bits = mask.data[i * 4].to_int() |
  343. 343● (mask.data[i * 4 + 1].to_int() << 8) |
  344. 344● (mask.data[i * 4 + 2].to_int() << 16) |
  345. 345● (mask.data[i * 4 + 3].to_int() << 24)
  346. 346● mask_f[i] = Float::reinterpret_from_int(bits)
  347. 347 }
  348. 348● let a_f : Array[Float] = Array::make(n * ch, 0.0F)
  349. 349● let b_f : Array[Float] = Array::make(n * ch, 0.0F)
  350. 350 for i in 0..<(n * ch) {
  351. 351● let bits_a = img_a.data[i * 4].to_int() |
  352. 352● (img_a.data[i * 4 + 1].to_int() << 8) |
  353. 353● (img_a.data[i * 4 + 2].to_int() << 16) |
  354. 354● (img_a.data[i * 4 + 3].to_int() << 24)
  355. 355● let bits_b = img_b.data[i * 4].to_int() |
  356. 356● (img_b.data[i * 4 + 1].to_int() << 8) |
  357. 357● (img_b.data[i * 4 + 2].to_int() << 16) |
  358. 358● (img_b.data[i * 4 + 3].to_int() << 24)
  359. 359● a_f[i] = Float::reinterpret_from_int(bits_a)
  360. 360● b_f[i] = Float::reinterpret_from_int(bits_b)
  361. 361 }
  362. 362◐ let levels = if num_bands > 0 { num_bands } else { 5 }
  363. 363 let mask_pyr : Array[Array[Float]] = [mask_f]
  364. 364 for _l in 1..<levels {
  365. 365● let prev = mask_pyr[mask_pyr.length() - 1]
  366. 366● let pw = get_w_from_n(prev.length(), w, h)
  367. 367● let ph = prev.length() / pw
  368. 368● mask_pyr.push(blur_downsample(prev, pw, ph))
  369. 369 }
  370. 370● let out : Array[Float] = Array::make(n * ch, 0.0F)
  371. 371 for c in 0..<ch {
  372. 372● let a_chan : Array[Float] = Array::make(n, 0.0F)
  373. 373● let b_chan : Array[Float] = Array::make(n, 0.0F)
  374. 374 for i in 0..<n {
  375. 375● a_chan[i] = a_f[i * ch + c]
  376. 376 b_chan[i] = b_f[i * ch + c]
  377. 377 }
  378. 378● let a_pyr = build_laplacian_pyramid_f(a_chan, w, h, levels)
  379. 379● let b_pyr = build_laplacian_pyramid_f(b_chan, w, h, levels)
  380. 380 let blended : Array[Array[Float]] = []
  381. 381● for l in 0..<a_pyr.length() {
  382. 382● let a_l = a_pyr[l]
  383. 383 let b_l = b_pyr[l]
  384. 384 let m_l = mask_pyr[l]
  385. 385● let blended_l : Array[Float] = Array::make(a_l.length(), 0.0F)
  386. 386● for i in 0..<a_l.length() {
  387. 387◐ let m = if i < m_l.length() { m_l[i] } else { 0.5F }
  388. 388 blended_l[i] = m * a_l[i] + (1.0F - m) * b_l[i]
  389. 389 }
  390. 390● blended.push(blended_l)
  391. 391 }
  392. 392● let reconstructed = collapse_laplacian_pyramid_f(blended, w, h)
  393. 393 for i in 0..<n {
  394. 394● out[i * ch + c] = reconstructed[i]
  395. 395 }
  396. 396 }
  397. 397● let out_bytes : Array[Byte] = Array::make(n * ch * 4, b'\x00')
  398. 398 for i in 0..<(n * ch) {
  399. 399● let bits = out[i].reinterpret_as_int()
  400. 400● out_bytes[i * 4] = (bits & 0xFF).to_byte()
  401. 401● out_bytes[i * 4 + 1] = ((bits >> 8) & 0xFF).to_byte()
  402. 402● out_bytes[i * 4 + 2] = ((bits >> 16) & 0xFF).to_byte()
  403. 403● out_bytes[i * 4 + 3] = ((bits >> 24) & 0xFF).to_byte()
  404. 404 }
  405. 405 @types.ImageF::{
  406. 406 width: w,
  407. 407 height: h,
  408. 408 channels: ch,
  409. 409● data: Bytes::from_array(out_bytes),
  410. 410 }
  411. 411}