image.vision\video\video_inpaint.mbt
95.8% · 159 / 166 points · 504 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 视频修复 video/video_inpaint - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 视频修复 — 补全/去抖/去闪烁 - 7
// Video inpainting: completion / de-flicker / de-shimmer - 8
// - 9
// Phase 17 v401: 视频处理大扩展 — 视频修复。 - 10
- 11
///| - 12
/// 视频修复方法。 - 13
/// 视频修复方法 Video inpainting method. - 14
/// `#non_exhaustive` 表示未来可能添加新构造器 - 15
#derive(Eq, @debug.Debug) - 16
#non_exhaustive - 17
pub(all) enum VideoInpaintMethod { - 18
Temporal // 时域修复(用前后帧填补) - 19
Spatial // 空域修复(用周围像素填补) - 20
Hybrid // 混合修复 - 21
} - 22
- 23
///| - 24
pub extend VideoInpaintMethod with @debug.Debug::{to_repr} - 25
- 26
///| - 27
pub extend VideoInpaintMethod with Eq::{not_equal, equal} - 28
- 29
///| - 30
/// 去闪烁方法。 - 31
/// 去闪烁方法 De-flicker method. - 32
/// `#non_exhaustive` 表示未来可能添加新构造器 - 33
#derive(Eq, @debug.Debug) - 34
#non_exhaustive - 35
pub(all) enum FlickerMethod { - 36
TemporalFlicker // 时域闪烁去除 - 37
HistogramFlicker // 直方图稳定 - 38
LuminanceFlicker // 亮度归一化 - 39
} - 40
- 41
///| - 42
pub extend FlickerMethod with @debug.Debug::{to_repr} - 43
- 44
///| - 45
pub extend FlickerMethod with Eq::{not_equal, equal} - 46
- 47
///| - 48
/// 视频修复配置。 - 49
/// 视频修复配置 Video inpainting configuration. - 50
#derive(Eq, @debug.Debug) - 51
pub(all) struct VideoInpaintConfig { - 52
inpaint_method : VideoInpaintMethod - 53
temporal_radius : Int32 // 时域半径(前后帧数) - 54
spatial_radius : Int32 // 空域半径(像素) - 55
blend_factor : Double // 混合因子 (0.0-1.0) - 56
} - 57
- 58
///| - 59
pub extend VideoInpaintConfig with @debug.Debug::{to_repr} - 60
- 61
///| - 62
pub extend VideoInpaintConfig with Eq::{not_equal, equal} - 63
- 64
///| - 65
/// 去闪烁配置。 - 66
/// 去闪烁配置 De-flicker configuration. - 67
#derive(Eq, @debug.Debug) - 68
pub(all) struct FlickerConfig { - 69
flicker_method : FlickerMethod - 70
temporal_window : Int32 // 时域窗口大小 - 71
strength : Double // 去闪烁强度 (0.0-1.0) - 72
} - 73
- 74
///| - 75
pub extend FlickerConfig with @debug.Debug::{to_repr} - 76
- 77
///| - 78
pub extend FlickerConfig with Eq::{not_equal, equal} - 79
- 80
///| - 81
/// 闪烁检测结果。 - 82
/// 闪烁检测结果 Flicker detection result. - 83
#derive(Eq, @debug.Debug) - 84
pub(all) struct FlickerDetection { - 85
has_flicker : Bool // 是否检测到闪烁 - 86
flicker_score : Double // 闪烁分数 (0.0-1.0) - 87
frame_indices : Array[Int32] // 闪烁帧索引 - 88
} - 89
- 90
///| - 91
pub extend FlickerDetection with @debug.Debug::{to_repr} - 92
- 93
///| - 94
pub extend FlickerDetection with Eq::{not_equal, equal} - 95
- 96
///| - 97
/// 默认修复配置。 - 98
/// 默认修复配置 Default inpainting configuration. - 99
pub fn default_inpaint_config() -> VideoInpaintConfig { - 100●
{ - 101
inpaint_method: Temporal, - 102
temporal_radius: 3, - 103
spatial_radius: 5, - 104
blend_factor: 0.5, - 105
} - 106
} - 107
- 108
///| - 109
/// 默认去闪烁配置。 - 110
/// 默认去闪烁配置 Default de-flicker configuration. - 111
pub fn default_flicker_config() -> FlickerConfig { - 112●
{ flicker_method: TemporalFlicker, temporal_window: 5, strength: 0.8, } - 113
} - 114
- 115
///| - 116
/// 计算图像平均亮度。 - 117
/// 计算图像平均亮度 Compute mean luminance of an image. - 118
fn compute_mean_luminance(img : @types.Image) -> Double { - 119●
let n = img.width * img.height * img.channels - 120
if n == 0 { - 121○
return 0.0 - 122
} - 123
let mut total = 0.0 - 124
for i in 0..<n { - 125●
total = total + img.data[i].to_double() - 126
} - 127●
total / n.to_double() - 128
} - 129
- 130
///| - 131
/// 时域修复:用前后帧填补掩码区域。 - 132
/// 时间修复:用相邻帧填充掩膜区域 Temporal inpainting: fill masked region using adjacent frames. - 133
pub fn temporal_inpaint( - 134
current : @types.Image, - 135
prev_frame : @types.Image, - 136
next_frame : @types.Image, - 137
mask : Array[Bool], - 138
config : VideoInpaintConfig, - 139
) -> @types.Image { - 140●
let w = current.width - 141
let h = current.height - 142
let ch = current.channels - 143
let data : Array[Byte] = [] - 144
for i in 0..<(w * h * ch) { - 145●
if mask[i / ch] { - 146●
let prev_val = prev_frame.data[i].to_double() - 147●
let next_val = next_frame.data[i].to_double() - 148
let blend = config.blend_factor - 149
let val = prev_val * (1.0 - blend) * 0.5 + next_val * blend * 0.5 - 150●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 151
} else { - 152●
data.push(current.data[i]) - 153
} - 154
} - 155●
{ width: w, height: h, channels: ch, data: Bytes::from_array(data), } - 156
} - 157
- 158
///| - 159
/// 空域修复:用周围像素填补掩码区域。 - 160
/// 空间修复:用周围像素填充掩膜区域 Spatial inpainting: fill masked region using surrounding pixels. - 161
pub fn spatial_inpaint( - 162
current : @types.Image, - 163
mask : Array[Bool], - 164
config : VideoInpaintConfig, - 165
) -> @types.Image { - 166●
let w = current.width - 167
let h = current.height - 168
let ch = current.channels - 169
let data : Array[Byte] = [] - 170
for y in 0..<h { - 171●
for x in 0..<w { - 172●
let idx = y * w + x - 173
if mask[idx] { - 174●
let radius = config.spatial_radius - 175
let sum : Array[Double] = [] - 176
let mut count = 0 - 177
for _ in 0..<ch { - 178●
sum.push(0.0) - 179
} - 180
for dy in [-1, 0, 1] { - 181●
for dx in [-1, 0, 1] { - 182●
if dx == 0 && dy == 0 { - 183●
continue - 184
} - 185
let nx = x + dx * radius - 186
let ny = y + dy * radius - 187
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 188●
let nidx = ny * w + nx - 189
if !mask[nidx] { - 190●
for c in 0..<ch { - 191●
let ci = nidx * ch + c - 192●
sum[c] = sum[c] + current.data[ci].to_double() - 193
} - 194
count = count + 1 - 195
} - 196
} - 197
} - 198
} - 199
if count > 0 { - 200●
for c in 0..<ch { - 201●
let val = sum[c] / count.to_double() - 202●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 203
} - 204
} else { - 205●
for c in 0..<ch { - 206●
data.push(current.data[idx * ch + c]) - 207
} - 208
} - 209
} else { - 210●
for c in 0..<ch { - 211●
data.push(current.data[idx * ch + c]) - 212
} - 213
} - 214
} - 215
} - 216●
{ width: w, height: h, channels: ch, data: Bytes::from_array(data), } - 217
} - 218
- 219
///| - 220
/// 视频修复主流程。 - 221
/// 视频修复管线 Video inpainting pipeline. - 222
pub fn video_inpaint( - 223
frames : Array[@types.Image], - 224
mask : Array[Bool], - 225
config : VideoInpaintConfig, - 226
) -> Array[@types.Image] { - 227●
let n = frames.length() - 228
let result : Array[@types.Image] = [] - 229
for i in 0..<n { - 230●
let prev_idx = if i > 0 { i - 1 } else { 0 } - 231●
let next_idx = if i < n - 1 { i + 1 } else { n - 1 } - 232●
let inpainted = match config.inpaint_method { - 233
Temporal => - 234●
temporal_inpaint( - 235
frames[i], - 236
frames[prev_idx], - 237
frames[next_idx], - 238
mask, - 239
config, - 240
) - 241●
Spatial => spatial_inpaint(frames[i], mask, config) - 242●
Hybrid => { - 243●
let temp = temporal_inpaint( - 244
frames[i], - 245
frames[prev_idx], - 246
frames[next_idx], - 247
mask, - 248
config, - 249
) - 250●
spatial_inpaint(temp, mask, config) - 251
} - 252
} - 253●
result.push(inpainted) - 254
} - 255
result - 256
} - 257
- 258
///| - 259
/// 检测闪烁:分析帧间亮度变化。 - 260
/// 检测闪烁:分析帧间亮度变化 Detect flicker: analyze inter-frame luminance changes. - 261
pub fn detect_flicker( - 262
frames : Array[@types.Image], - 263
threshold : Double, - 264
) -> FlickerDetection { - 265●
let n = frames.length() - 266
if n < 2 { - 267●
return { has_flicker: false, flicker_score: 0.0, frame_indices: [], } - 268
} - 269
let luminances : Array[Double] = [] - 270
for frame in frames { - 271●
luminances.push(compute_mean_luminance(frame)) - 272
} - 273
let mut mean_diff = 0.0 - 274
for i in 1..<n { - 275●
mean_diff = mean_diff + (luminances[i] - luminances[i - 1]).abs() - 276
} - 277●
mean_diff = mean_diff / (n - 1).to_double() - 278
let flicker_indices : Array[Int32] = [] - 279
for i in 1..<n { - 280●
let diff = (luminances[i] - luminances[i - 1]).abs() - 281
if diff > threshold { - 282●
flicker_indices.push(i) - 283
} - 284
} - 285●
let score = (mean_diff / 255.0).clamp(min=0.0, max=1.0) - 286
{ - 287
has_flicker: score > 0.05, - 288
flicker_score: score, - 289
frame_indices: flicker_indices, - 290
} - 291
} - 292
- 293
///| - 294
/// 时域去闪烁:用时域窗口平滑亮度。 - 295
/// 时间去闪烁:用时间窗口平滑亮度 Temporal de-flicker: smooth luminance using temporal window. - 296
pub fn deflicker_temporal( - 297
frames : Array[@types.Image], - 298
config : FlickerConfig, - 299
) -> Array[@types.Image] { - 300●
let n = frames.length() - 301
if n == 0 { - 302●
return [] - 303
} - 304
let window = config.temporal_window - 305
let luminances : Array[Double] = [] - 306
for frame in frames { - 307●
luminances.push(compute_mean_luminance(frame)) - 308
} - 309
let result : Array[@types.Image] = [] - 310
for i in 0..<n { - 311●
let mut sum_lum = 0.0 - 312
let mut count = 0 - 313
let half = window / 2 - 314●
let start = if i > half { i - half } else { 0 } - 315●
let end = if i + half < n { i + half } else { n - 1 } - 316
for j in start..<=end { - 317●
sum_lum = sum_lum + luminances[j] - 318
count = count + 1 - 319
} - 320●
let target_lum = sum_lum / count.to_double() - 321
let current_lum = luminances[i] - 322◐
let ratio = if current_lum > 0.0 { target_lum / current_lum } else { 1.0 } - 323
let blended_ratio = 1.0 + (ratio - 1.0) * config.strength - 324
let w = frames[i].width - 325
let h = frames[i].height - 326
let ch = frames[i].channels - 327
let data : Array[Byte] = [] - 328
for k in 0..<(w * h * ch) { - 329●
let val = frames[i].data[k].to_double() * blended_ratio - 330●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 331
} - 332●
result.push({ - 333
width: w, - 334
height: h, - 335
channels: ch, - 336●
data: Bytes::from_array(data), - 337
}) - 338
} - 339
result - 340
} - 341
- 342
///| - 343
/// 直方图去闪烁:稳定帧间直方图分布。 - 344
/// 直方图去闪烁 Histogram de-flicker - 345
pub fn deflicker_histogram( - 346
frames : Array[@types.Image], - 347
config : FlickerConfig, - 348
) -> Array[@types.Image] { - 349●
let n = frames.length() - 350
if n == 0 { - 351●
return [] - 352
} - 353
let result : Array[@types.Image] = [] - 354
let prev_hist : Array[Double] = [] - 355
for _ in 0..<256 { - 356●
prev_hist.push(0.0) - 357
} - 358
for i in 0..<n { - 359●
let w = frames[i].width - 360
let h = frames[i].height - 361
let ch = frames[i].channels - 362
let curr_hist : Array[Double] = [] - 363
for _ in 0..<256 { - 364●
curr_hist.push(0.0) - 365
} - 366●
let total = (w * h * ch).to_double() - 367
for k in 0..<(w * h * ch) { - 368●
let bin = frames[i].data[k].to_int() - 369
curr_hist[bin] = curr_hist[bin] + 1.0 / total - 370
} - 371
let data : Array[Byte] = [] - 372
if i == 0 { - 373●
for k in 0..<(w * h * ch) { - 374●
data.push(frames[i].data[k]) - 375
} - 376
} else { - 377●
for k in 0..<(w * h * ch) { - 378●
let bin = frames[i].data[k].to_int() - 379
let target = prev_hist[bin] * (1.0 - config.strength) + - 380
curr_hist[bin] * config.strength - 381
let curr = curr_hist[bin] - 382◐
let ratio = if curr > 0.0 { target / curr } else { 1.0 } - 383●
let val = frames[i].data[k].to_double() * ratio - 384●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 385
} - 386
} - 387
for b in 0..<256 { - 388●
prev_hist[b] = curr_hist[b] - 389
} - 390●
result.push({ - 391
width: w, - 392
height: h, - 393
channels: ch, - 394●
data: Bytes::from_array(data), - 395
}) - 396
} - 397
result - 398
} - 399
- 400
///| - 401
/// 亮度去闪烁:归一化帧间亮度。 - 402
/// 亮度去闪烁:归一化帧间亮度 Luminance de-flicker: normalize inter-frame luminance. - 403
pub fn deflicker_luminance( - 404
frames : Array[@types.Image], - 405
config : FlickerConfig, - 406
) -> Array[@types.Image] { - 407●
let n = frames.length() - 408
if n == 0 { - 409○
return [] - 410
} - 411
let luminances : Array[Double] = [] - 412
for frame in frames { - 413●
luminances.push(compute_mean_luminance(frame)) - 414
} - 415
let mut ref_lum = 0.0 - 416
for lum in luminances { - 417●
ref_lum = ref_lum + lum - 418
} - 419●
ref_lum = ref_lum / n.to_double() - 420
let result : Array[@types.Image] = [] - 421
for i in 0..<n { - 422●
let current_lum = luminances[i] - 423◐
let ratio = if current_lum > 0.0 { ref_lum / current_lum } else { 1.0 } - 424
let blended_ratio = 1.0 + (ratio - 1.0) * config.strength - 425
let w = frames[i].width - 426
let h = frames[i].height - 427
let ch = frames[i].channels - 428
let data : Array[Byte] = [] - 429
for k in 0..<(w * h * ch) { - 430●
let val = frames[i].data[k].to_double() * blended_ratio - 431●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 432
} - 433●
result.push({ - 434
width: w, - 435
height: h, - 436
channels: ch, - 437●
data: Bytes::from_array(data), - 438
}) - 439
} - 440
result - 441
} - 442
- 443
///| - 444
/// 视频去闪烁主流程。 - 445
/// 视频去闪烁管线 Video de-flicker pipeline. - 446
pub fn video_deflicker( - 447
frames : Array[@types.Image], - 448
config : FlickerConfig, - 449
) -> Array[@types.Image] { - 450●
match config.flicker_method { - 451●
TemporalFlicker => deflicker_temporal(frames, config) - 452○
HistogramFlicker => deflicker_histogram(frames, config) - 453●
LuminanceFlicker => deflicker_luminance(frames, config) - 454
} - 455
} - 456
- 457
///| - 458
/// 去抖动:减少帧间像素抖动。 - 459
/// 去微颤:减少帧间像素抖动 De-shimmer: reduce inter-frame pixel jitter. - 460
pub fn deshimmer( - 461
frames : Array[@types.Image], - 462
strength : Double, - 463
) -> Array[@types.Image] { - 464●
let n = frames.length() - 465
if n < 2 { - 466○
return frames - 467
} - 468
let result : Array[@types.Image] = [] - 469●
result.push(frames[0]) - 470
for i in 1..<n { - 471●
let w = frames[i].width - 472
let h = frames[i].height - 473
let ch = frames[i].channels - 474
let data : Array[Byte] = [] - 475
for k in 0..<(w * h * ch) { - 476●
let curr = frames[i].data[k].to_double() - 477●
let prev = frames[i - 1].data[k].to_double() - 478
let val = curr * (1.0 - strength) + prev * strength - 479●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 480
} - 481●
result.push({ - 482
width: w, - 483
height: h, - 484
channels: ch, - 485●
data: Bytes::from_array(data), - 486
}) - 487
} - 488
result - 489
} - 490
- 491
///| - 492
/// 视频修复流水线:修复 + 去闪烁 + 去抖动。 - 493
/// 视频修复管线:修复 + 去闪烁 + 去微颤 Video inpainting pipeline: inpaint + de-flicker + de-shimmer. - 494
pub fn inpaint_pipeline( - 495
frames : Array[@types.Image], - 496
mask : Array[Bool], - 497
inpaint_config : VideoInpaintConfig, - 498
flicker_config : FlickerConfig, - 499
shimmer_strength : Double, - 500
) -> Array[@types.Image] { - 501●
let inpainted = video_inpaint(frames, mask, inpaint_config) - 502●
let deflickered = video_deflicker(inpainted, flicker_config) - 503●
deshimmer(deflickered, shimmer_strength) - 504
}