image.process\filter\inpaint.mbt
72.8% · 59 / 81 points · 252 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 图像修复 inpaint - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 图像修复(纯 MoonBit) - 7
// 基于扩散法:对掩码区域求解 Laplace 方程 - 8
// 已知像素作为 Dirichlet 边界条件,迭代至收敛 - 9
// 参考: Bertalmio et al., "Image Inpainting" (2000) - 10
- 11
///| - 12
/// 图像修复。 - 13
/// mask 为单通道掩码图像:0=已知像素,非0=待修复像素。 - 14
/// iterations 控制最大迭代次数(默认100)。 - 15
/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4)。 - 16
pub fn inpaint( - 17
img : @types.Image, - 18
mask : @types.Image, - 19
iterations~ : Int32 = 100, - 20
) -> @types.Image raise @types.LoadError { - 21
// 输入验证 - 22●
if img.width <= 0 || img.height <= 0 { - 23●
raise @types.LoadError::DecodeFailed("inpaint: 图像尺寸必须大于0") - 24
} - 25
if img.channels < 1 || img.channels > 4 { - 26○
raise @types.LoadError::DecodeFailed("inpaint: 通道数必须在1-4之间") - 27
} - 28
let expected_size = img.width * img.height * img.channels - 29●
if img.data.length() != expected_size { - 30○
raise @types.LoadError::DecodeFailed( - 31
"inpaint: 图像数据大小不匹配,期望 " + - 32○
expected_size.to_string() + - 33
",实际 " + - 34○
img.data.length().to_string(), - 35
) - 36
} - 37
if img.width != mask.width || img.height != mask.height { - 38●
raise @types.LoadError::DecodeFailed( - 39
"inpaint: 掩码尺寸必须与图像一致", - 40
) - 41
} - 42
if mask.channels != 1 { - 43●
raise @types.LoadError::DecodeFailed( - 44
"inpaint: 掩码必须为单通道图像", - 45
) - 46
} - 47
let mask_expected_size = mask.width * mask.height - 48●
if mask.data.length() != mask_expected_size { - 49○
raise @types.LoadError::DecodeFailed( - 50
"inpaint: 掩码数据大小不匹配,期望 " + - 51○
mask_expected_size.to_string() + - 52
",实际 " + - 53○
mask.data.length().to_string(), - 54
) - 55
} - 56
if iterations <= 0 { - 57○
raise @types.LoadError::DecodeFailed("inpaint: iterations必须大于0") - 58
} - 59
let w = img.width - 60
let h = img.height - 61
let ch = img.channels - 62
let n = w * h - 63
// 构建待修复标记 - 64●
let unknown : Array[Bool] = Array::make(n, false) - 65
let mut has_unknown = false - 66
for i in 0..<n { - 67●
if mask.data[i] != b'\x00' { - 68●
unknown[i] = true - 69
has_unknown = true - 70
} - 71
} - 72
// 若无待修复像素,直接返回原图 - 73
if !has_unknown { - 74●
return img - 75
} - 76
// 复制原图数据 - 77●
let out : Array[Int32] = Array::make(n * ch, 0) - 78
for i in 0..<(n * ch) { - 79●
out[i] = img.data[i].to_int() - 80
} - 81
// Jacobi 迭代求解 Laplace 方程 - 82
for _ in 0..<iterations { - 83●
let prev = out.copy() - 84
let mut changed = false - 85
for y in 0..<h { - 86●
for x in 0..<w { - 87●
let idx = y * w + x - 88
if !unknown[idx] { - 89●
continue - 90
} - 91
// 收集 8 邻域中已知/已更新像素 - 92
for c in 0..<ch { - 93●
let mut sum = 0 - 94
let mut count = 0 - 95
for dy in -1..<=1 { - 96●
let ny = y + dy - 97
if ny < 0 || ny >= h { - 98●
continue - 99
} - 100
for dx in -1..<=1 { - 101●
if dx == 0 && dy == 0 { - 102●
continue - 103
} - 104
let nx = x + dx - 105
if nx < 0 || nx >= w { - 106○
continue - 107
} - 108
sum += prev[(ny * w + nx) * ch + c] - 109
count += 1 - 110
} - 111
} - 112
if count > 0 { - 113●
let new_val = sum / count - 114
if new_val != out[idx * ch + c] { - 115●
changed = true - 116
} - 117
out[idx * ch + c] = new_val - 118
} - 119
} - 120
} - 121
} - 122
// 收敛检测 - 123
if !changed { - 124●
break - 125
} - 126
} - 127
// 转换输出 - 128●
let out_data : Array[Byte] = Array::make(n * ch, b'\x00') - 129
for i in 0..<(n * ch) { - 130●
out_data[i] = out[i].clamp(min=0, max=255).to_byte() - 131
} - 132
@types.Image::{ - 133
width: w, - 134
height: h, - 135
channels: ch, - 136●
data: Bytes::from_array(out_data), - 137
} - 138
} - 139
- 140
///| - 141
/// 快速图像修复(基于距离加权平均)。 - 142
/// 对每个待修复像素,取周围已知像素的加权平均(距离越近权重越大)。 - 143
/// 比 inpaint 快但质量略低,适合小面积修复。 - 144
pub fn inpaint_fast( - 145
img : @types.Image, - 146
mask : @types.Image, - 147
radius~ : Int32 = 5, - 148
) -> @types.Image raise @types.LoadError { - 149
// 输入验证 - 150●
if img.width <= 0 || img.height <= 0 { - 151●
raise @types.LoadError::DecodeFailed( - 152
"inpaint_fast: 图像尺寸必须大于0", - 153
) - 154
} - 155
if img.channels < 1 || img.channels > 4 { - 156○
raise @types.LoadError::DecodeFailed( - 157
"inpaint_fast: 通道数必须在1-4之间", - 158
) - 159
} - 160
let expected_size = img.width * img.height * img.channels - 161●
if img.data.length() != expected_size { - 162○
raise @types.LoadError::DecodeFailed( - 163
"inpaint_fast: 图像数据大小不匹配,期望 " + - 164○
expected_size.to_string() + - 165
",实际 " + - 166○
img.data.length().to_string(), - 167
) - 168
} - 169
if img.width != mask.width || img.height != mask.height { - 170●
raise @types.LoadError::DecodeFailed( - 171
"inpaint_fast: 掩码尺寸必须与图像一致", - 172
) - 173
} - 174
if mask.channels != 1 { - 175○
raise @types.LoadError::DecodeFailed( - 176
"inpaint_fast: 掩码必须为单通道图像", - 177
) - 178
} - 179
let mask_expected_size = mask.width * mask.height - 180●
if mask.data.length() != mask_expected_size { - 181○
raise @types.LoadError::DecodeFailed( - 182
"inpaint_fast: 掩码数据大小不匹配,期望 " + - 183○
mask_expected_size.to_string() + - 184
",实际 " + - 185○
mask.data.length().to_string(), - 186
) - 187
} - 188
if radius <= 0 { - 189○
raise @types.LoadError::DecodeFailed("inpaint_fast: radius必须大于0") - 190
} - 191
let w = img.width - 192
let h = img.height - 193
let ch = img.channels - 194
let n = w * h - 195
let r = radius - 196
// 复制原图数据 - 197●
let out : Array[Int32] = Array::make(n * ch, 0) - 198
for i in 0..<(n * ch) { - 199●
out[i] = img.data[i].to_int() - 200
} - 201
// 对每个待修复像素,用已知像素加权平均 - 202
for y in 0..<h { - 203●
for x in 0..<w { - 204●
let idx = y * w + x - 205
if mask.data[idx] == b'\x00' { - 206●
continue - 207
} - 208
for c in 0..<ch { - 209●
let mut sum = 0.0F - 210
let mut weight_sum = 0.0F - 211
for dy in -r..<=r { - 212●
let ny = y + dy - 213
if ny < 0 || ny >= h { - 214●
continue - 215
} - 216
for dx in -r..<=r { - 217●
if dx == 0 && dy == 0 { - 218●
continue - 219
} - 220
let nx = x + dx - 221
if nx < 0 || nx >= w { - 222●
continue - 223
} - 224
let nidx = ny * w + nx - 225
// 只用已知像素 - 226
if mask.data[nidx] != b'\x00' { - 227●
continue - 228
} - 229●
let dist = Float::from_int(dx * dx + dy * dy).sqrt() - 230
let weight = 1.0F / (dist + 1.0F) - 231●
sum += weight * Float::from_int(out[nidx * ch + c]) - 232
weight_sum += weight - 233
} - 234
} - 235
if weight_sum > 0.0F { - 236●
out[idx * ch + c] = (sum / weight_sum).to_int() - 237
} - 238
} - 239
} - 240
} - 241
// 转换输出 - 242●
let out_data : Array[Byte] = Array::make(n * ch, b'\x00') - 243
for i in 0..<(n * ch) { - 244●
out_data[i] = out[i].clamp(min=0, max=255).to_byte() - 245
} - 246
@types.Image::{ - 247
width: w, - 248
height: h, - 249
channels: ch, - 250●
data: Bytes::from_array(out_data), - 251
} - 252
}