image.vision\video\motion_estimation.mbt
88.2% · 82 / 93 points · 472 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/motion_estimation - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 运动估计 — 块匹配/亚像素/分数像素 - 7
// Motion estimation: block matching / subpixel / fractional - 8
// - 9
// Phase 17 v395: 视频处理大扩展 — 运动估计。 - 10
- 11
///| - 12
/// 运动估计方法。 - 13
/// 运动估计方法 Motion estimation method. - 14
/// `#non_exhaustive` 表示未来可能添加新构造器 - 15
#derive(Eq, @debug.Debug) - 16
#non_exhaustive - 17
pub(all) enum MotionEstimationMethod { - 18
FullSearch // 全搜索 - 19
DiamondSearch // 菱形搜索 - 20
HexSearch // 六边形搜索 - 21
ThreeStep // 三步搜索 - 22
} - 23
- 24
///| - 25
pub extend MotionEstimationMethod with @debug.Debug::{to_repr} - 26
- 27
///| - 28
pub extend MotionEstimationMethod with Eq::{not_equal, equal} - 29
- 30
///| - 31
/// 运动矢量。 - 32
/// 运动向量 Motion vector. - 33
#derive(Eq, @debug.Debug) - 34
pub(all) struct MotionVector { - 35
dx : Int32 - 36
dy : Int32 - 37
sad : Double // 绝对误差和 - 38
} - 39
- 40
///| - 41
pub extend MotionVector with @debug.Debug::{to_repr} - 42
- 43
///| - 44
pub extend MotionVector with Eq::{not_equal, equal} - 45
- 46
///| - 47
/// 亚像素运动矢量。 - 48
/// 亚像素运动向量 Subpixel motion vector. - 49
#derive(Eq, @debug.Debug) - 50
pub(all) struct SubpixelMotionVector { - 51
dx : Double - 52
dy : Double - 53
sad : Double - 54
} - 55
- 56
///| - 57
pub extend SubpixelMotionVector with @debug.Debug::{to_repr} - 58
- 59
///| - 60
pub extend SubpixelMotionVector with Eq::{not_equal, equal} - 61
- 62
///| - 63
/// 搜索配置。 - 64
/// 搜索配置 Search configuration. - 65
#derive(Eq, @debug.Debug) - 66
pub(all) struct SearchConfig { - 67
me_method : MotionEstimationMethod - 68
block_size : Int32 - 69
search_range : Int32 - 70
subpixel_refinement : Bool // 是否启用亚像素精化 - 71
} - 72
- 73
///| - 74
pub extend SearchConfig with @debug.Debug::{to_repr} - 75
- 76
///| - 77
pub extend SearchConfig with Eq::{not_equal, equal} - 78
- 79
///| - 80
/// 默认搜索配置。 - 81
/// 默认搜索配置 Default search configuration. - 82
pub fn default_search_config() -> SearchConfig { - 83●
{ - 84
me_method: DiamondSearch, - 85
block_size: 8, - 86
search_range: 16, - 87
subpixel_refinement: true, - 88
} - 89
} - 90
- 91
///| - 92
/// 计算块的 SAD(绝对误差和)。 - 93
/// 计算块 SAD(绝对差值和) Compute SAD (Sum of Absolute Differences) for a block. - 94
fn compute_sad( - 95
current : @types.Image, - 96
reference : @types.Image, - 97
bx : Int32, - 98
by : Int32, - 99
dx : Int32, - 100
dy : Int32, - 101
block_size : Int32, - 102
) -> Double { - 103●
let w = current.width - 104
let h = current.height - 105
let ch = current.channels - 106
let mut sad = 0.0 - 107
for py in 0..<block_size { - 108●
for px in 0..<block_size { - 109●
let cx = bx + px - 110
let cy = by + py - 111
let rx = cx + dx - 112
let ry = cy + dy - 113
if cx >= w || cy >= h || rx < 0 || rx >= w || ry < 0 || ry >= h { - 114●
sad = sad + 255.0 - 115
continue - 116
} - 117
for c in 0..<ch { - 118●
let ci = (cy * w + cx) * ch + c - 119
let ri = (ry * w + rx) * ch + c - 120●
let diff = current.data[ci].to_int() - reference.data[ri].to_int() - 121●
sad = sad + diff.to_double().abs() - 122
} - 123
} - 124
} - 125
sad - 126
} - 127
- 128
///| - 129
/// 全搜索运动估计。 - 130
/// 全搜索运动估计 Full search motion estimation. - 131
pub fn estimate_motion_full_search( - 132
current : @types.Image, - 133
reference : @types.Image, - 134
bx : Int32, - 135
by : Int32, - 136
block_size : Int32, - 137
search_range : Int32, - 138
) -> MotionVector { - 139●
let mut best : MotionVector = { dx: 0, dy: 0, sad: 999999999.0, } - 140
for dy in [-search_range, 0, search_range] { - 141●
for dx in [-search_range, 0, search_range] { - 142●
let sad = compute_sad(current, reference, bx, by, dx, dy, block_size) - 143
if sad < best.sad { - 144●
best = ({ dx, dy, sad, } : MotionVector) - 145
} - 146
} - 147
} - 148
best - 149
} - 150
- 151
///| - 152
/// 菱形搜索运动估计。 - 153
/// 菱形搜索运动估计 Diamond search motion estimation. - 154
pub fn estimate_motion_diamond( - 155
current : @types.Image, - 156
reference : @types.Image, - 157
bx : Int32, - 158
by : Int32, - 159
block_size : Int32, - 160
search_range : Int32, - 161
) -> MotionVector { - 162
// 大菱形搜索模式: (0,±1), (±1,0) - 163●
let mut best_dx = 0 - 164
let mut best_dy = 0 - 165●
let mut best_sad = compute_sad(current, reference, bx, by, 0, 0, block_size) - 166
let mut step = 1 - 167
while step <= search_range { - 168●
let mut new_dx = best_dx - 169
let mut new_dy = best_dy - 170
let mut new_sad = best_sad - 171
for (dx, dy) in [(0, step), (0, -step), (step, 0), (-step, 0)] { - 172●
let cand_dx = best_dx + dx - 173
let cand_dy = best_dy + dy - 174●
let sad = compute_sad( - 175
current, reference, bx, by, cand_dx, cand_dy, block_size, - 176
) - 177
if sad < new_sad { - 178○
new_dx = cand_dx - 179
new_dy = cand_dy - 180
new_sad = sad - 181
} - 182
} - 183
if new_sad < best_sad { - 184○
best_dx = new_dx - 185
best_dy = new_dy - 186
best_sad = new_sad - 187
} else { - 188●
step = step * 2 - 189
} - 190
} - 191
{ dx: best_dx, dy: best_dy, sad: best_sad, } - 192
} - 193
- 194
///| - 195
/// 六边形搜索运动估计。 - 196
/// 六角搜索运动估计 Hexagonal search motion estimation. - 197
pub fn estimate_motion_hex( - 198
current : @types.Image, - 199
reference : @types.Image, - 200
bx : Int32, - 201
by : Int32, - 202
block_size : Int32, - 203
search_range : Int32, - 204
) -> MotionVector { - 205
// 六边形搜索: 6个点 - 206●
let mut best_dx = 0 - 207
let mut best_dy = 0 - 208●
let mut best_sad = compute_sad(current, reference, bx, by, 0, 0, block_size) - 209●
let step = (search_range / 2).clamp(min=1, max=search_range) - 210
for - 211
(dx, dy) in [ - 212
(step, 0), - 213
(-step, 0), - 214
(step / 2, step), - 215
(-step / 2, step), - 216
(step / 2, -step), - 217
(-step / 2, -step), - 218
] { - 219●
let sad = compute_sad(current, reference, bx, by, dx, dy, block_size) - 220
if sad < best_sad { - 221○
best_dx = dx - 222
best_dy = dy - 223
best_sad = sad - 224
} - 225
} - 226
{ dx: best_dx, dy: best_dy, sad: best_sad, } - 227
} - 228
- 229
///| - 230
/// 三步搜索运动估计。 - 231
/// 三步搜索 Three-step search - 232
pub fn estimate_motion_three_step( - 233
current : @types.Image, - 234
reference : @types.Image, - 235
bx : Int32, - 236
by : Int32, - 237
block_size : Int32, - 238
search_range : Int32, - 239
) -> MotionVector { - 240●
let mut best_dx = 0 - 241
let mut best_dy = 0 - 242●
let mut best_sad = compute_sad(current, reference, bx, by, 0, 0, block_size) - 243
let mut step = search_range - 244
while step >= 1 { - 245●
for - 246
(dx, dy) in [ - 247
(0, 0), - 248
(step, 0), - 249
(-step, 0), - 250
(0, step), - 251
(0, -step), - 252
(step, step), - 253
(step, -step), - 254
(-step, step), - 255
(-step, -step), - 256
] { - 257●
let cand_dx = best_dx + dx - 258
let cand_dy = best_dy + dy - 259●
let sad = compute_sad( - 260
current, reference, bx, by, cand_dx, cand_dy, block_size, - 261
) - 262
if sad < best_sad { - 263○
best_dx = cand_dx - 264
best_dy = cand_dy - 265
best_sad = sad - 266
} - 267
} - 268
step = step / 2 - 269
} - 270
{ dx: best_dx, dy: best_dy, sad: best_sad, } - 271
} - 272
- 273
///| - 274
/// 亚像素精化(半像素)。 - 275
/// 亚像素精化(半像素) Subpixel refinement (half-pixel). - 276
pub fn refine_subpixel( - 277
current : @types.Image, - 278
reference : @types.Image, - 279
bx : Int32, - 280
by : Int32, - 281
mv : MotionVector, - 282
block_size : Int32, - 283
) -> SubpixelMotionVector { - 284
// 在整像素最优解周围搜索半像素位置 - 285●
let mut best : SubpixelMotionVector = { - 286●
dx: mv.dx.to_double(), - 287●
dy: mv.dy.to_double(), - 288
sad: mv.sad, - 289
} - 290
for frac_dx in [-0.5, 0.0, 0.5] { - 291●
for frac_dy in [-0.5, 0.0, 0.5] { - 292●
let cand_dx = mv.dx.to_double() + frac_dx - 293●
let cand_dy = mv.dy.to_double() + frac_dy - 294
// 简化:用插值 SAD - 295●
let ix = cand_dx.to_int() - 296●
let iy = cand_dy.to_int() - 297●
let fx = cand_dx - ix.to_double() - 298●
let fy = cand_dy - iy.to_double() - 299●
let sad0 = compute_sad(current, reference, bx, by, ix, iy, block_size) - 300●
let sad1 = compute_sad(current, reference, bx, by, ix + 1, iy, block_size) - 301●
let sad2 = compute_sad(current, reference, bx, by, ix, iy + 1, block_size) - 302●
let sad3 = compute_sad( - 303
current, - 304
reference, - 305
bx, - 306
by, - 307
ix + 1, - 308
iy + 1, - 309
block_size, - 310
) - 311
let interpolated_sad = sad0 * (1.0 - fx) * (1.0 - fy) + - 312
sad1 * fx * (1.0 - fy) + - 313
sad2 * (1.0 - fx) * fy + - 314
sad3 * fx * fy - 315
if interpolated_sad < best.sad { - 316●
best = ( - 317
{ dx: cand_dx, dy: cand_dy, sad: interpolated_sad, } : - 318
SubpixelMotionVector) - 319
} - 320
} - 321
} - 322
best - 323
} - 324
- 325
///| - 326
/// 计算运动场。 - 327
/// 计算整图运动场 Compute motion field for entire image. - 328
pub fn compute_motion_field( - 329
current : @types.Image, - 330
reference : @types.Image, - 331
config : SearchConfig, - 332
) -> Array[MotionVector] { - 333●
let field : Array[MotionVector] = [] - 334
let block = config.block_size - 335
let mut by = 0 - 336
while by < current.height { - 337●
let mut bx = 0 - 338
while bx < current.width { - 339●
let mv = match config.me_method { - 340
FullSearch => - 341○
estimate_motion_full_search( - 342
current, - 343
reference, - 344
bx, - 345
by, - 346
block, - 347
config.search_range, - 348
) - 349
DiamondSearch => - 350●
estimate_motion_diamond( - 351
current, - 352
reference, - 353
bx, - 354
by, - 355
block, - 356
config.search_range, - 357
) - 358
HexSearch => - 359○
estimate_motion_hex( - 360
current, - 361
reference, - 362
bx, - 363
by, - 364
block, - 365
config.search_range, - 366
) - 367
ThreeStep => - 368○
estimate_motion_three_step( - 369
current, - 370
reference, - 371
bx, - 372
by, - 373
block, - 374
config.search_range, - 375
) - 376
} - 377●
field.push(mv) - 378
bx = bx + block - 379
} - 380
by = by + block - 381
} - 382
field - 383
} - 384
- 385
///| - 386
/// 运动补偿。 - 387
/// 运动补偿 Motion compensation. - 388
pub fn motion_compensation( - 389
reference : @types.Image, - 390
motion_field : Array[MotionVector], - 391
block_size : Int32, - 392
) -> @types.Image { - 393●
let w = reference.width - 394
let h = reference.height - 395
let ch = reference.channels - 396
let data : Array[Byte] = [] - 397
let mut mv_idx = 0 - 398
let mut by = 0 - 399
while by < h { - 400●
let mut bx = 0 - 401
while bx < w { - 402●
let mv = if mv_idx < motion_field.length() { - 403●
motion_field[mv_idx] - 404
} else { - 405○
{ dx: 0, dy: 0, sad: 0.0, } - 406
} - 407
mv_idx = mv_idx + 1 - 408
for py in 0..<block_size { - 409●
for px in 0..<block_size { - 410●
let cx = bx + px - 411
let cy = by + py - 412
if cx >= w || cy >= h { - 413○
for _ in 0..<ch { - 414○
data.push((0 : Byte)) - 415
} - 416
continue - 417
} - 418●
let rx = (cx + mv.dx).clamp(min=0, max=w - 1) - 419●
let ry = (cy + mv.dy).clamp(min=0, max=h - 1) - 420
for c in 0..<ch { - 421●
data.push(reference.data[(ry * w + rx) * ch + c]) - 422
} - 423
} - 424
} - 425
bx = bx + block_size - 426
} - 427
by = by + block_size - 428
} - 429●
{ width: w, height: h, channels: ch, data: Bytes::from_array(data), } - 430
} - 431
- 432
///| - 433
/// 估计运动并补偿。 - 434
/// 估计运动并补偿 Estimate motion and compensate. - 435
pub fn motion_estimate_and_compensate( - 436
current : @types.Image, - 437
reference : @types.Image, - 438
config : SearchConfig, - 439
) -> @types.Image { - 440●
let field = compute_motion_field(current, reference, config) - 441●
motion_compensation(reference, field, config.block_size) - 442
} - 443
- 444
///| - 445
/// 计算运动场平均 SAD。 - 446
/// 计算运动场平均 SAD Compute average SAD of motion field. - 447
pub fn average_motion_sad(motion_field : Array[MotionVector]) -> Double { - 448●
if motion_field.length() == 0 { - 449●
return 0.0 - 450
} - 451
let mut total = 0.0 - 452
for mv in motion_field { - 453●
total = total + mv.sad - 454
} - 455●
total / motion_field.length().to_double() - 456
} - 457
- 458
///| - 459
/// 计算运动矢量零率。 - 460
/// 计算零运动向量占比 Compute fraction of zero motion vectors. - 461
pub fn zero_motion_ratio(motion_field : Array[MotionVector]) -> Double { - 462●
if motion_field.length() == 0 { - 463●
return 0.0 - 464
} - 465
let mut zero_count = 0 - 466
for mv in motion_field { - 467●
if mv.dx == 0 && mv.dy == 0 { - 468●
zero_count = zero_count + 1 - 469
} - 470
} - 471●
zero_count.to_double() / motion_field.length().to_double() - 472
}