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