image.process\enhance\inpaint.mbt
96.6% · 113 / 117 points · 386 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
// 图像修复(v12.0.0) - 7
// 基于快速行进法(Fast Marching Method)的图像修复 - 8
- 9
///| - 10
/// 图像修复配置 - 11
pub(all) struct InpaintConfig { - 12
radius : Int32 // 修复半径 - 13
method_type : String // "telea" / "ns"(Navier-Stokes) - 14
max_iterations : Int32 // 最大迭代次数 - 15
} - 16
- 17
///| - 18
/// 创建默认图像修复配置 - 19
pub fn inpaint_default_config() -> InpaintConfig { - 20●
InpaintConfig::{ radius: 5, method_type: "telea", max_iterations: 1000, } - 21
} - 22
- 23
///| - 24
/// 图像修复(Telea 算法,简化版) - 25
/// 基于快速行进法,从边界向内部逐步修复 - 26
pub fn inpaint_advanced( - 27
img : @types.Image, - 28
mask : @types.Image, - 29
radius~ : Int32 = 5, - 30
) -> @types.Image raise @types.LoadError { - 31
// 输入验证 - 32●
if img.width <= 0 || img.height <= 0 { - 33●
raise @types.LoadError::DecodeFailed( - 34
"inpaint_advanced: 图像尺寸必须大于0", - 35
) - 36
} - 37
if img.channels < 1 || img.channels > 4 { - 38●
raise @types.LoadError::DecodeFailed( - 39
"inpaint_advanced: 通道数必须在1-4之间", - 40
) - 41
} - 42
let expected_size = img.width * img.height * img.channels - 43●
if img.data.length() != expected_size { - 44●
raise @types.LoadError::DecodeFailed( - 45
"inpaint_advanced: 图像数据大小不匹配,期望 " + - 46●
expected_size.to_string() + - 47
",实际 " + - 48●
img.data.length().to_string(), - 49
) - 50
} - 51
if mask.width != img.width || mask.height != img.height { - 52●
raise @types.LoadError::DecodeFailed( - 53
"inpaint_advanced: 掩码图像尺寸必须与原图一致", - 54
) - 55
} - 56
if mask.channels < 1 { - 57○
raise @types.LoadError::DecodeFailed( - 58
"inpaint_advanced: 掩码通道数必须大于0", - 59
) - 60
} - 61
if radius <= 0 { - 62●
raise @types.LoadError::DecodeFailed( - 63
"inpaint_advanced: radius必须大于0", - 64
) - 65
} - 66
- 67
let r = radius - 68
let w = img.width - 69
let h = img.height - 70
let channels = img.channels - 71
let img_data = img.data - 72
let mask_data = mask.data - 73
// 复制原图 - 74●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 75
for i in 0..<(w * h * channels) { - 76●
out_data[i] = img_data[i] - 77
} - 78
// 标记需要修复的区域(mask 中非零像素) - 79
let to_inpaint : Array[Bool] = [] - 80
for i in 0..<(w * h) { - 81●
let mask_val = if mask.channels >= 1 { - 82●
mask_data[i * mask.channels].to_int() - 83
} else { - 84○
0 - 85
} - 86●
to_inpaint.push(mask_val > 128) - 87
} - 88
// 快速行进法:迭代修复边界像素 - 89
let mut changed = true - 90
let mut iteration = 0 - 91
while changed && iteration < 1000 { - 92●
changed = false - 93
iteration += 1 - 94
// 找到所有待修复区域的边界像素 - 95
let boundary : Array[(Int32, Int32)] = [] - 96
for y in 0..<h { - 97●
for x in 0..<w { - 98●
if to_inpaint[y * w + x] { - 99
// 检查是否有已修复的邻域 - 100●
let mut has_known_neighbor = false - 101
let neighbors = [(-1, 0), (1, 0), (0, -1), (0, 1)] - 102
for _tup0 in neighbors { - 103●
let (dx, dy) = _tup0 - 104
let nx = x + dx - 105
let ny = y + dy - 106
if nx >= 0 && - 107
nx < w && - 108
ny >= 0 && - 109
ny < h && - 110
!to_inpaint[ny * w + nx] { - 111●
has_known_neighbor = true - 112
} - 113
} - 114
if has_known_neighbor { - 115●
boundary.push((x, y)) - 116
} - 117
} - 118
} - 119
} - 120
// 对边界像素进行修复 - 121
for _tup1 in boundary { - 122●
let (x, y) = _tup1 - 123
// 计算邻域已知像素的加权平均 - 124
let mut sum_r = 0.0 - 125
let mut sum_g = 0.0 - 126
let mut sum_b = 0.0 - 127
let mut weight_sum = 0.0 - 128
for dy in -r..<(r + 1) { - 129●
for dx in -r..<(r + 1) { - 130●
let nx = x + dx - 131
let ny = y + dy - 132
if nx >= 0 && nx < w && ny >= 0 && ny < h && !to_inpaint[ny * w + nx] { - 133
// 距离加权(越近权重越大) - 134●
let dist = (dx * dx + dy * dy).to_double().sqrt() - 135
let weight = 1.0 / (dist + 1.0) - 136
let idx = (ny * w + nx) * channels - 137●
sum_r += out_data[idx].to_double() * weight - 138
if channels >= 2 { - 139●
sum_g += out_data[idx + 1].to_double() * weight - 140
} - 141
if channels >= 3 { - 142●
sum_b += out_data[idx + 2].to_double() * weight - 143
} - 144
weight_sum += weight - 145
} - 146
} - 147
} - 148
if weight_sum > 0.0 { - 149●
let idx = (y * w + x) * channels - 150
out_data[idx] = (sum_r / weight_sum) - 151●
.min(255.0) - 152●
.max(0.0) - 153●
.to_int() - 154●
.to_byte() - 155
if channels >= 2 { - 156●
out_data[idx + 1] = (sum_g / weight_sum) - 157●
.min(255.0) - 158●
.max(0.0) - 159●
.to_int() - 160●
.to_byte() - 161
} - 162
if channels >= 3 { - 163●
out_data[idx + 2] = (sum_b / weight_sum) - 164●
.min(255.0) - 165●
.max(0.0) - 166●
.to_int() - 167●
.to_byte() - 168
} - 169
if channels >= 4 { - 170●
out_data[idx + 3] = img_data[idx + 3] - 171
} - 172
// 标记为已修复 - 173
to_inpaint[y * w + x] = false - 174
changed = true - 175
} - 176
} - 177
} - 178
@types.Image::{ - 179
width: w, - 180
height: h, - 181
channels, - 182●
data: Bytes::from_array(out_data), - 183
} - 184
} - 185
- 186
///| - 187
/// 创建矩形修复掩码 - 188
pub fn create_rect_mask( - 189
width : Int32, - 190
height : Int32, - 191
x : Int32, - 192
y : Int32, - 193
rect_w : Int32, - 194
rect_h : Int32, - 195
) -> @types.Image raise @types.LoadError { - 196
// 输入验证 - 197●
if width <= 0 || height <= 0 { - 198●
raise @types.LoadError::DecodeFailed( - 199
"create_rect_mask: 图像尺寸必须大于0", - 200
) - 201
} - 202
if x < 0 || y < 0 { - 203●
raise @types.LoadError::DecodeFailed( - 204
"create_rect_mask: 矩形坐标不能为负", - 205
) - 206
} - 207
if rect_w <= 0 || rect_h <= 0 { - 208●
raise @types.LoadError::DecodeFailed( - 209
"create_rect_mask: 矩形尺寸必须大于0", - 210
) - 211
} - 212
if x + rect_w > width || y + rect_h > height { - 213●
raise @types.LoadError::DecodeFailed( - 214
"create_rect_mask: 矩形超出图像边界", - 215
) - 216
} - 217
- 218●
let mask_data : Array[Byte] = Array::make(width * height, b'\x00') - 219
for py in 0..<height { - 220●
for px in 0..<width { - 221●
if px >= x && px < x + rect_w && py >= y && py < y + rect_h { - 222●
mask_data[py * width + px] = b'\xFF' - 223
} - 224
} - 225
} - 226
@types.Image::{ - 227
width, - 228
height, - 229
channels: 1, - 230●
data: Bytes::from_array(mask_data), - 231
} - 232
} - 233
- 234
///| - 235
/// 去除图像中的小物体(基于连通域分析) - 236
pub fn remove_small_objects( - 237
img : @types.Image, - 238
max_size~ : Int32 = 100, - 239
) -> @types.Image raise @types.LoadError { - 240
// 输入验证 - 241●
if img.width <= 0 || img.height <= 0 { - 242●
raise @types.LoadError::DecodeFailed( - 243
"remove_small_objects: 图像尺寸必须大于0", - 244
) - 245
} - 246
if img.channels < 1 || img.channels > 4 { - 247●
raise @types.LoadError::DecodeFailed( - 248
"remove_small_objects: 通道数必须在1-4之间", - 249
) - 250
} - 251
let expected_size = img.width * img.height * img.channels - 252●
if img.data.length() != expected_size { - 253●
raise @types.LoadError::DecodeFailed( - 254
"remove_small_objects: 数据大小不匹配,期望 " + - 255●
expected_size.to_string() + - 256
",实际 " + - 257●
img.data.length().to_string(), - 258
) - 259
} - 260
if max_size <= 0 { - 261○
raise @types.LoadError::DecodeFailed( - 262
"remove_small_objects: max_size必须大于0", - 263
) - 264
} - 265
- 266
let ms = max_size - 267
let w = img.width - 268
let h = img.height - 269
let channels = img.channels - 270
let data = img.data - 271
// 转换为灰度 - 272
let gray : Array[Int32] = [] - 273
for i in 0..<(w * h) { - 274●
let idx = i * channels - 275●
let r = data[idx].to_int() - 276●
let g = if channels >= 2 { data[idx + 1].to_int() } else { r } - 277●
let b = if channels >= 3 { data[idx + 2].to_int() } else { r } - 278●
gray.push( - 279●
(0.299 * r.to_double() + 0.587 * g.to_double() + 0.114 * b.to_double()).to_int(), - 280
) - 281
} - 282
// 连通域分析(洪水填充) - 283●
let labels : Array[Int32] = Array::make(w * h, 0) - 284
let mut next_label = 1 - 285
let sizes : Array[Int32] = [] - 286
for y in 0..<h { - 287●
for x in 0..<w { - 288●
if gray[y * w + x] > 128 && labels[y * w + x] == 0 { - 289
// 洪水填充 - 290●
let stack : Array[(Int32, Int32)] = [] - 291●
stack.push((x, y)) - 292
labels[y * w + x] = next_label - 293
let mut size = 0 - 294●
while stack.length() > 0 { - 295●
match stack.pop() { - 296●
Some((cx, cy)) => { - 297
size += 1 - 298
let neighbors = [(-1, 0), (1, 0), (0, -1), (0, 1)] - 299
for _tup2 in neighbors { - 300●
let (dx, dy) = _tup2 - 301
let nx = cx + dx - 302
let ny = cy + dy - 303
if nx >= 0 && - 304
nx < w && - 305
ny >= 0 && - 306
ny < h && - 307
gray[ny * w + nx] > 128 && - 308
labels[ny * w + nx] == 0 { - 309●
labels[ny * w + nx] = next_label - 310●
stack.push((nx, ny)) - 311
} - 312
} - 313
} - 314○
None => ignore(()) - 315
} - 316
} - 317●
sizes.push(size) - 318
next_label += 1 - 319
} - 320
} - 321
} - 322
// 去除小物体(将小连通域的像素设为背景色) - 323●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 324
for i in 0..<(w * h) { - 325●
let idx = i * channels - 326
let label = labels[i] - 327
if label > 0 && sizes[label - 1] <= ms { - 328
// 小物体:设为黑色(或周围背景色) - 329●
for c in 0..<channels { - 330●
out_data[idx + c] = b'\x00' - 331
} - 332
} else { - 333
// 保留原像素 - 334●
for c in 0..<channels { - 335●
out_data[idx + c] = data[idx + c] - 336
} - 337
} - 338
} - 339
@types.Image::{ - 340
width: w, - 341
height: h, - 342
channels, - 343●
data: Bytes::from_array(out_data), - 344
} - 345
} - 346
- 347
///| - 348
/// 图像去水印(基于修复的简化版) - 349
/// 将指定区域的像素用周围像素填充 - 350
pub fn remove_watermark( - 351
img : @types.Image, - 352
x : Int32, - 353
y : Int32, - 354
width : Int32, - 355
height : Int32, - 356
) -> @types.Image raise @types.LoadError { - 357
// 输入验证 - 358●
if img.width <= 0 || img.height <= 0 { - 359●
raise @types.LoadError::DecodeFailed( - 360
"remove_watermark: 图像尺寸必须大于0", - 361
) - 362
} - 363
if img.channels < 1 || img.channels > 4 { - 364●
raise @types.LoadError::DecodeFailed( - 365
"remove_watermark: 通道数必须在1-4之间", - 366
) - 367
} - 368
if x < 0 || y < 0 { - 369●
raise @types.LoadError::DecodeFailed( - 370
"remove_watermark: 区域坐标不能为负", - 371
) - 372
} - 373
if width <= 0 || height <= 0 { - 374●
raise @types.LoadError::DecodeFailed( - 375
"remove_watermark: 区域尺寸必须大于0", - 376
) - 377
} - 378
if x + width > img.width || y + height > img.height { - 379●
raise @types.LoadError::DecodeFailed( - 380
"remove_watermark: 区域超出图像边界", - 381
) - 382
} - 383
- 384●
let mask = create_rect_mask(img.width, img.height, x, y, width, height) - 385●
inpaint_advanced(img, mask, radius=5) - 386
}