image.process\transform\pyramid.mbt
96.5% · 83 / 86 points · 203 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 图像金字塔 pyramid - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 图像金字塔(纯 MoonBit) - 7
- 8
///| - 9
/// 高斯金字塔下采样。将图像尺寸减半(宽高各 /2),使用 2x2 均值滤波。 - 10
pub fn pyr_down(img : @types.Image) -> @types.Image raise @types.LoadError { - 11●
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 12●
raise @types.LoadError::DecodeFailed("pyr_down 仅支持 channels=1/3/4") - 13
} - 14
let ch = img.channels - 15
let w = img.width - 16
let h = img.height - 17
let new_w = w / 2 - 18
let new_h = h / 2 - 19
if new_w == 0 || new_h == 0 { - 20●
raise @types.LoadError::DecodeFailed("图像太小,无法下采样") - 21
} - 22●
let data_int : Array[Int32] = Array::make(w * h * ch, 0) - 23
for i in 0..<(w * h * ch) { - 24●
data_int[i] = img.data[i].to_int() - 25
} - 26●
let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00') - 27
for y in 0..<new_h { - 28●
for x in 0..<new_w { - 29●
for c in 0..<ch { - 30●
let mut sum = 0 - 31
let mut count = 0 - 32
for dy in 0..<2 { - 33●
for dx in 0..<2 { - 34●
let sx = x * 2 + dx - 35
let sy = y * 2 + dy - 36
if sx < w && sy < h { - 37●
sum += data_int[(sy * w + sx) * ch + c] - 38
count += 1 - 39
} - 40
} - 41
} - 42●
out[(y * new_w + x) * ch + c] = (sum / count).to_byte() - 43
} - 44
} - 45
} - 46
@types.Image::{ - 47
width: new_w, - 48
height: new_h, - 49
channels: ch, - 50●
data: Bytes::from_array(out), - 51
} - 52
} - 53
- 54
///| - 55
/// 拉普拉斯金字塔上采样。将图像尺寸加倍(宽高各 *2),使用双线性插值。 - 56
pub fn pyr_up(img : @types.Image) -> @types.Image raise @types.LoadError { - 57●
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 58●
raise @types.LoadError::DecodeFailed("pyr_up 仅支持 channels=1/3/4") - 59
} - 60
let ch = img.channels - 61
let w = img.width - 62
let h = img.height - 63
let new_w = w * 2 - 64
let new_h = h * 2 - 65●
let data_int : Array[Int32] = Array::make(w * h * ch, 0) - 66
for i in 0..<(w * h * ch) { - 67●
data_int[i] = img.data[i].to_int() - 68
} - 69
// 预计算 x 方向插值参数 - 70●
let x0_arr : Array[Int32] = Array::make(new_w, 0) - 71●
let x1_arr : Array[Int32] = Array::make(new_w, 0) - 72●
let wx_arr : Array[Float] = Array::make(new_w, 0.0F) - 73
for x in 0..<new_w { - 74●
let fx = (Float::from_int(x) - 0.5F) / 2.0F - 75●
let x0 = fx.to_int().clamp(min=0, max=w - 1) - 76
x0_arr[x] = x0 - 77●
x1_arr[x] = (x0 + 1).clamp(min=0, max=w - 1) - 78●
wx_arr[x] = fx - Float::from_int(x0) - 79
} - 80
// 预计算 y 方向插值参数 - 81●
let y0_arr : Array[Int32] = Array::make(new_h, 0) - 82●
let y1_arr : Array[Int32] = Array::make(new_h, 0) - 83●
let wy_arr : Array[Float] = Array::make(new_h, 0.0F) - 84
for y in 0..<new_h { - 85●
let fy = (Float::from_int(y) - 0.5F) / 2.0F - 86●
let y0 = fy.to_int().clamp(min=0, max=h - 1) - 87
y0_arr[y] = y0 - 88●
y1_arr[y] = (y0 + 1).clamp(min=0, max=h - 1) - 89●
wy_arr[y] = fy - Float::from_int(y0) - 90
} - 91●
let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00') - 92
for y in 0..<new_h { - 93●
let y0 = y0_arr[y] - 94
let y1 = y1_arr[y] - 95
let wy = wy_arr[y] - 96
let wy_inv = 1.0F - wy - 97
let row0 = y0 * w - 98
let row1 = y1 * w - 99
for x in 0..<new_w { - 100●
let x0 = x0_arr[x] - 101
let x1 = x1_arr[x] - 102
let wx = wx_arr[x] - 103
let wx_inv = 1.0F - wx - 104
for c in 0..<ch { - 105●
let v00 = Float::from_int(data_int[(row0 + x0) * ch + c]) - 106●
let v01 = Float::from_int(data_int[(row0 + x1) * ch + c]) - 107●
let v10 = Float::from_int(data_int[(row1 + x0) * ch + c]) - 108●
let v11 = Float::from_int(data_int[(row1 + x1) * ch + c]) - 109
let top = v00 * wx_inv + v01 * wx - 110
let bot = v10 * wx_inv + v11 * wx - 111
let v = top * wy_inv + bot * wy - 112
let clamped = if v < 0.0F { - 113●
0 - 114●
} else if v > 255.0F { - 115○
255 - 116
} else { - 117●
v.to_int() - 118
} - 119●
out[(y * new_w + x) * ch + c] = clamped.to_byte() - 120
} - 121
} - 122
} - 123
@types.Image::{ - 124
width: new_w, - 125
height: new_h, - 126
channels: ch, - 127●
data: Bytes::from_array(out), - 128
} - 129
} - 130
- 131
///| - 132
/// 构建高斯金字塔。返回各级图像(从原图到最小级),levels 为最大层数(0=自动到 1x1)。 - 133
pub fn build_gaussian_pyramid( - 134
img : @types.Image, - 135
levels : Int32, - 136
) -> Array[@types.Image] raise @types.LoadError { - 137●
let result : Array[@types.Image] = [img] - 138
let mut current = img - 139●
let max_levels = if levels == 0 { 100 } else { levels } - 140
for _ in 1..<max_levels { - 141●
if current.width < 2 || current.height < 2 { - 142●
break - 143
} - 144●
current = pyr_down(current) - 145●
result.push(current) - 146
} - 147
result - 148
} - 149
- 150
///| - 151
/// 构建拉普拉斯金字塔。返回各级拉普拉斯图像(高斯金字塔相邻层之差)。 - 152
/// 最后一层为高斯金字塔的最小级(不加差)。 - 153
pub fn build_laplacian_pyramid( - 154
img : @types.Image, - 155
levels : Int32, - 156
) -> Array[@types.Image] raise @types.LoadError { - 157●
let gaussian = build_gaussian_pyramid(img, levels) - 158
let result : Array[@types.Image] = [] - 159●
for i in 0..<(gaussian.length() - 1) { - 160●
let upsampled = pyr_up(gaussian[i + 1]) - 161●
let lap = laplacian_diff(gaussian[i], upsampled) - 162●
result.push(lap) - 163
} - 164●
result.push(gaussian[gaussian.length() - 1]) - 165
result - 166
} - 167
- 168
///| - 169
fn laplacian_diff(a : @types.Image, b : @types.Image) -> @types.Image { - 170●
let ch = a.channels - 171
let w = a.width - 172
let h = a.height - 173●
let a_int : Array[Int32] = Array::make(w * h * ch, 0) - 174
for i in 0..<(w * h * ch) { - 175●
a_int[i] = a.data[i].to_int() - 176
} - 177
let bw = b.width - 178
let bh = b.height - 179●
let b_int : Array[Int32] = Array::make(bw * bh * ch, 0) - 180
for i in 0..<(bw * bh * ch) { - 181●
b_int[i] = b.data[i].to_int() - 182
} - 183●
let out : Array[Byte] = Array::make(w * h * ch, b'\x00') - 184
for y in 0..<h { - 185●
let by = y * bh / h - 186
for x in 0..<w { - 187●
let bx = x * bw / w - 188
for c in 0..<ch { - 189●
let va = a_int[(y * w + x) * ch + c] - 190
let vb = b_int[(by * bw + bx) * ch + c] - 191
let diff = va - vb + 128 - 192◐
let clamped = if diff < 0 { 0 } else if diff > 255 { 255 } else { diff } - 193●
out[(y * w + x) * ch + c] = clamped.to_byte() - 194
} - 195
} - 196
} - 197
@types.Image::{ - 198
width: w, - 199
height: h, - 200
channels: ch, - 201●
data: Bytes::from_array(out), - 202
} - 203
}