image.vision\video\video_interp.mbt
81.5% · 101 / 124 points · 379 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_interp - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 视频插帧 — 光流/相位/深度 - 7
// Video frame interpolation: optical flow / phase / depth - 8
// - 9
// Phase 17 v403: 视频处理大扩展 — 视频插帧。 - 10
- 11
///| - 12
/// 视频插帧方法。 - 13
/// 视频帧插值方法 Video frame interpolation method. - 14
/// `#non_exhaustive` 表示未来可能添加新构造器 - 15
#derive(Eq, @debug.Debug) - 16
#non_exhaustive - 17
pub(all) enum FrameInterpMethod { - 18
LinearInterp // 线性插值 - 19
OpticalFlowInterp // 光流插值 - 20
PhaseInterp // 相位插值 - 21
} - 22
- 23
///| - 24
pub extend FrameInterpMethod with @debug.Debug::{to_repr} - 25
- 26
///| - 27
pub extend FrameInterpMethod with Eq::{not_equal, equal} - 28
- 29
///| - 30
/// 视频插帧配置。 - 31
/// 视频帧插值配置 Video frame interpolation configuration. - 32
#derive(Eq, @debug.Debug) - 33
pub(all) struct FrameInterpConfig { - 34
interp_method : FrameInterpMethod - 35
num_interp_frames : Int32 // 每对帧间插入帧数 - 36
motion_threshold : Double // 运动阈值 - 37
blend_factor : Double // 混合因子 (0.0-1.0) - 38
} - 39
- 40
///| - 41
pub extend FrameInterpConfig with @debug.Debug::{to_repr} - 42
- 43
///| - 44
pub extend FrameInterpConfig with Eq::{not_equal, equal} - 45
- 46
///| - 47
/// 帧间运动估计结果。 - 48
/// 帧间运动估计结果 Inter-frame motion estimation result. - 49
#derive(Eq, @debug.Debug) - 50
pub(all) struct MotionEstimateResult { - 51
motion_score : Double // 运动分数 (0.0-1.0) - 52
dominant_direction : (Double, Double) // 主运动方向 (dx, dy) - 53
static_ratio : Double // 静态像素比例 - 54
} - 55
- 56
///| - 57
pub extend MotionEstimateResult with @debug.Debug::{to_repr} - 58
- 59
///| - 60
pub extend MotionEstimateResult with Eq::{not_equal, equal} - 61
- 62
///| - 63
/// 默认插帧配置。 - 64
/// 默认插值配置 Default interpolation configuration. - 65
pub fn default_interp_config() -> FrameInterpConfig { - 66●
{ - 67
interp_method: LinearInterp, - 68
num_interp_frames: 1, - 69
motion_threshold: 10.0, - 70
blend_factor: 0.5, - 71
} - 72
} - 73
- 74
///| - 75
/// 线性插值两帧之间。 - 76
/// 两帧间线性插值 Linear interpolation between two frames. - 77
pub fn linear_interpolate_frames( - 78
frame1 : @types.Image, - 79
frame2 : @types.Image, - 80
num_frames : Int32, - 81
) -> Array[@types.Image] { - 82●
let result : Array[@types.Image] = [] - 83
let w = frame1.width - 84
let h = frame1.height - 85
let ch = frame1.channels - 86
for k in 1..<=num_frames { - 87●
let alpha = k.to_double() / (num_frames + 1).to_double() - 88
let data : Array[Byte] = [] - 89
for i in 0..<(w * h * ch) { - 90●
let v1 = frame1.data[i].to_double() - 91●
let v2 = frame2.data[i].to_double() - 92
let val = v1 * (1.0 - alpha) + v2 * alpha - 93●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 94
} - 95●
result.push({ - 96
width: w, - 97
height: h, - 98
channels: ch, - 99●
data: Bytes::from_array(data), - 100
}) - 101
} - 102
result - 103
} - 104
- 105
///| - 106
/// 计算帧间运动估计。 - 107
/// 计算帧间运动估计 Compute motion estimation between frames. - 108
pub fn compute_motion_between_frames( - 109
frame1 : @types.Image, - 110
frame2 : @types.Image, - 111
) -> MotionEstimateResult { - 112●
let w = frame1.width - 113
let h = frame1.height - 114
let ch = frame1.channels - 115
let mut total_diff = 0.0 - 116
let mut sum_dx = 0.0 - 117
let mut sum_dy = 0.0 - 118
let mut static_count = 0 - 119
let total_pixels = w * h - 120
for y in 0..<h { - 121●
for x in 0..<w { - 122●
let mut diff = 0.0 - 123
for c in 0..<ch { - 124●
let idx = (y * w + x) * ch + c - 125
diff = diff + - 126●
(frame1.data[idx].to_double() - frame2.data[idx].to_double()).abs() - 127
} - 128●
diff = diff / ch.to_double() - 129
total_diff = total_diff + diff - 130
if diff < 5.0 { - 131●
static_count = static_count + 1 - 132
} - 133
if x > 0 && x < w - 1 { - 134○
let mut grad_x = 0.0 - 135
for c in 0..<ch { - 136○
let left = frame2.data[(y * w + (x - 1)) * ch + c].to_double() - 137○
let right = frame2.data[(y * w + (x + 1)) * ch + c].to_double() - 138
grad_x = grad_x + (right - left) * 0.5 - 139
} - 140○
if grad_x.abs() > 1.0 { - 141○
sum_dx = sum_dx + diff / grad_x - 142
} - 143
} - 144
if y > 0 && y < h - 1 { - 145○
let mut grad_y = 0.0 - 146
for c in 0..<ch { - 147○
let top = frame2.data[((y - 1) * w + x) * ch + c].to_double() - 148○
let bottom = frame2.data[((y + 1) * w + x) * ch + c].to_double() - 149
grad_y = grad_y + (bottom - top) * 0.5 - 150
} - 151○
if grad_y.abs() > 1.0 { - 152○
sum_dy = sum_dy + diff / grad_y - 153
} - 154
} - 155
} - 156
} - 157●
let motion_score = (total_diff / total_pixels.to_double() / 255.0).clamp( - 158
min=0.0, - 159
max=1.0, - 160
) - 161
let dx = if total_pixels > 0 { - 162●
sum_dx / total_pixels.to_double() - 163
} else { - 164○
0.0 - 165
} - 166
let dy = if total_pixels > 0 { - 167●
sum_dy / total_pixels.to_double() - 168
} else { - 169○
0.0 - 170
} - 171
let static_ratio = if total_pixels > 0 { - 172●
static_count.to_double() / total_pixels.to_double() - 173
} else { - 174○
0.0 - 175
} - 176
{ motion_score, dominant_direction: (dx, dy), static_ratio, } - 177
} - 178
- 179
///| - 180
/// 光流插值两帧之间。 - 181
/// 光流插值 Optical flow interpolation - 182
pub fn optical_flow_interpolate( - 183
frame1 : @types.Image, - 184
frame2 : @types.Image, - 185
num_frames : Int32, - 186
config : FrameInterpConfig, - 187
) -> Array[@types.Image] { - 188●
let result : Array[@types.Image] = [] - 189
let w = frame1.width - 190
let h = frame1.height - 191
let ch = frame1.channels - 192●
let motion = compute_motion_between_frames(frame1, frame2) - 193
let (dx, dy) = motion.dominant_direction - 194
for k in 1..<=num_frames { - 195●
let alpha = k.to_double() / (num_frames + 1).to_double() - 196
let data : Array[Byte] = [] - 197
for y in 0..<h { - 198●
for x in 0..<w { - 199●
let src_x = x.to_double() - dx * alpha - 200●
let src_y = y.to_double() - dy * alpha - 201●
let x0 = src_x.to_int() - 202●
let y0 = src_y.to_int() - 203●
let x1 = if x0 + 1 < w { x0 + 1 } else { x0 } - 204◐
let y1 = if y0 + 1 < h { y0 + 1 } else { y0 } - 205●
let cx = src_x - x0.to_double() - 206●
let cy = src_y - y0.to_double() - 207
for c in 0..<ch { - 208●
let v00 = if x0 >= 0 && y0 >= 0 && x0 < w && y0 < h { - 209●
frame1.data[(y0 * w + x0) * ch + c].to_double() - 210
} else { - 211○
0.0 - 212
} - 213
let v01 = if x1 >= 0 && y0 >= 0 && x1 < w && y0 < h { - 214●
frame1.data[(y0 * w + x1) * ch + c].to_double() - 215
} else { - 216○
0.0 - 217
} - 218
let v10 = if x0 >= 0 && y1 >= 0 && x0 < w && y1 < h { - 219●
frame1.data[(y1 * w + x0) * ch + c].to_double() - 220
} else { - 221○
0.0 - 222
} - 223
let v11 = if x1 >= 0 && y1 >= 0 && x1 < w && y1 < h { - 224●
frame1.data[(y1 * w + x1) * ch + c].to_double() - 225
} else { - 226○
0.0 - 227
} - 228
let warped = v00 * (1.0 - cx) * (1.0 - cy) + - 229
v01 * cx * (1.0 - cy) + - 230
v10 * (1.0 - cx) * cy + - 231
v11 * cx * cy - 232●
let v2 = frame2.data[(y * w + x) * ch + c].to_double() - 233●
let motion_weight = if (v2 - warped).abs() > config.motion_threshold { - 234●
config.blend_factor - 235
} else { - 236○
1.0 - 237
} - 238
let val = (warped * (1.0 - alpha) + v2 * alpha) * motion_weight + - 239
v2 * (1.0 - motion_weight) - 240●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 241
} - 242
} - 243
} - 244●
result.push({ - 245
width: w, - 246
height: h, - 247
channels: ch, - 248●
data: Bytes::from_array(data), - 249
}) - 250
} - 251
result - 252
} - 253
- 254
///| - 255
/// 相位插值两帧之间。 - 256
/// 两帧间相位插值 Phase interpolation between two frames. - 257
pub fn phase_interpolate( - 258
frame1 : @types.Image, - 259
frame2 : @types.Image, - 260
num_frames : Int32, - 261
config : FrameInterpConfig, - 262
) -> Array[@types.Image] { - 263●
let result : Array[@types.Image] = [] - 264
let w = frame1.width - 265
let h = frame1.height - 266
let ch = frame1.channels - 267
for k in 1..<=num_frames { - 268●
let alpha = k.to_double() / (num_frames + 1).to_double() - 269
let data : Array[Byte] = [] - 270
for i in 0..<(w * h * ch) { - 271●
let v1 = frame1.data[i].to_double() - 272●
let v2 = frame2.data[i].to_double() - 273
let phase1 = v1 / 255.0 * 2.0 * 3.141592653589793 - 274
let phase2 = v2 / 255.0 * 2.0 * 3.141592653589793 - 275
let mag1 = v1 - 276
let mag2 = v2 - 277
let interp_phase = phase1 * (1.0 - alpha) + phase2 * alpha - 278
let interp_mag = mag1 * (1.0 - alpha) + mag2 * alpha - 279
let val = interp_mag * - 280●
(0.5 + 0.5 * (interp_phase / 3.141592653589793).abs()) - 281
let blended = val * config.blend_factor + - 282
(v1 * (1.0 - alpha) + v2 * alpha) * (1.0 - config.blend_factor) - 283●
data.push(blended.clamp(min=0.0, max=255.0).to_int().to_byte()) - 284
} - 285●
result.push({ - 286
width: w, - 287
height: h, - 288
channels: ch, - 289●
data: Bytes::from_array(data), - 290
}) - 291
} - 292
result - 293
} - 294
- 295
///| - 296
/// 插帧主流程:在每对帧间插入新帧。 - 297
/// 帧插值管线:在每对帧间插入新帧 Frame interpolation pipeline: insert new frames between each pair. - 298
pub fn video_interpolate_frames( - 299
frames : Array[@types.Image], - 300
config : FrameInterpConfig, - 301
) -> Array[@types.Image] { - 302●
let n = frames.length() - 303
if n < 2 { - 304●
return frames - 305
} - 306
let result : Array[@types.Image] = [] - 307
for i in 0..<(n - 1) { - 308●
result.push(frames[i]) - 309●
let interp = match config.interp_method { - 310
LinearInterp => - 311●
linear_interpolate_frames( - 312
frames[i], - 313
frames[i + 1], - 314
config.num_interp_frames, - 315
) - 316
OpticalFlowInterp => - 317○
optical_flow_interpolate( - 318
frames[i], - 319
frames[i + 1], - 320
config.num_interp_frames, - 321
config, - 322
) - 323
PhaseInterp => - 324○
phase_interpolate( - 325
frames[i], - 326
frames[i + 1], - 327
config.num_interp_frames, - 328
config, - 329
) - 330
} - 331
for frame in interp { - 332●
result.push(frame) - 333
} - 334
} - 335●
result.push(frames[n - 1]) - 336
result - 337
} - 338
- 339
///| - 340
/// 帧率转换:从原帧率转换到目标帧率。 - 341
/// 帧率转换:从原始到目标帧率 Frame rate conversion: from original to target frame rate. - 342
pub fn frame_rate_conversion( - 343
frames : Array[@types.Image], - 344
original_fps : Double, - 345
target_fps : Double, - 346
config : FrameInterpConfig, - 347
) -> Array[@types.Image] { - 348●
if target_fps <= original_fps { - 349●
let step = (original_fps / target_fps).to_int() - 350
let result : Array[@types.Image] = [] - 351●
let n = frames.length() - 352
for i in 0..<n { - 353●
if i % step == 0 { - 354●
result.push(frames[i]) - 355
} - 356
} - 357
result - 358
} else { - 359●
let ratio = target_fps / original_fps - 360●
let num_interp = (ratio - 1.0).to_int() - 361
let updated_config : FrameInterpConfig = { - 362
interp_method: config.interp_method, - 363
num_interp_frames: num_interp, - 364
motion_threshold: config.motion_threshold, - 365
blend_factor: config.blend_factor, - 366
} - 367●
video_interpolate_frames(frames, updated_config) - 368
} - 369
} - 370
- 371
///| - 372
/// 插帧流水线:插帧 + 运动分析。 - 373
/// 插值管线:插值 + 运动分析 Interpolation pipeline: interpolate + motion analysis. - 374
pub fn interp_pipeline( - 375
frames : Array[@types.Image], - 376
config : FrameInterpConfig, - 377
) -> Array[@types.Image] { - 378●
video_interpolate_frames(frames, config) - 379
}