image.vision\video\slow_motion.mbt
95.9% · 118 / 123 points · 324 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/slow_motion - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 慢动作 — 光流插帧/运动模糊 - 7
// Slow motion: optical flow interpolation / motion blur - 8
// - 9
// Phase 17 v397: 视频处理大扩展 — 慢动作。 - 10
- 11
///| - 12
/// 慢动作方法。 - 13
/// 慢动作方法 Slow motion method. - 14
/// `#non_exhaustive` 表示未来可能添加新构造器 - 15
#derive(Eq, @debug.Debug) - 16
#non_exhaustive - 17
pub(all) enum SlowMotionMethod { - 18
OpticalFlow // 光流插帧 - 19
FrameBlending // 帧混合 - 20
MotionCompensation // 运动补偿 - 21
} - 22
- 23
///| - 24
pub extend SlowMotionMethod with @debug.Debug::{to_repr} - 25
- 26
///| - 27
pub extend SlowMotionMethod with Eq::{not_equal, equal} - 28
- 29
///| - 30
/// 慢动作配置。 - 31
/// 慢动作配置 Slow motion configuration. - 32
#derive(Eq, @debug.Debug) - 33
pub(all) struct SlowMotionConfig { - 34
sm_method : SlowMotionMethod - 35
speed_factor : Int32 // 慢放倍数 (2=半速, 4=1/4速) - 36
blur_amount : Double // 运动模糊强度 (0.0-1.0) - 37
blur_direction : Int32 // 模糊方向 (0=水平, 1=垂直, 2=径向) - 38
} - 39
- 40
///| - 41
pub extend SlowMotionConfig with @debug.Debug::{to_repr} - 42
- 43
///| - 44
pub extend SlowMotionConfig with Eq::{not_equal, equal} - 45
- 46
///| - 47
/// 默认慢动作配置。 - 48
/// 默认慢动作配置 Default slow motion configuration. - 49
pub fn default_slow_motion_config() -> SlowMotionConfig { - 50●
{ - 51
sm_method: FrameBlending, - 52
speed_factor: 2, - 53
blur_amount: 0.0, - 54
blur_direction: 0, - 55
} - 56
} - 57
- 58
///| - 59
/// 帧混合插帧。 - 60
/// 帧混合插值 Frame blending interpolation. - 61
pub fn interpolate_frame_blend( - 62
from : @types.Image, - 63
to_ : @types.Image, - 64
alpha : Double, - 65
) -> @types.Image { - 66●
let w = from.width - 67
let h = from.height - 68
let ch = from.channels - 69
let data : Array[Byte] = [] - 70●
let a = alpha.clamp(min=0.0, max=1.0) - 71
for i in 0..<(w * h * ch) { - 72●
let fv = from.data[i].to_int().to_double() - 73●
let tv = to_.data[i].to_int().to_double() - 74
let val = fv * (1.0 - a) + tv * a - 75●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 76
} - 77●
{ width: w, height: h, channels: ch, data: Bytes::from_array(data), } - 78
} - 79
- 80
///| - 81
/// 光流插帧(简化:用帧差作为光流近似)。 - 82
/// 光流插值 Optical flow interpolation - 83
pub fn interpolate_frame_optical_flow( - 84
from : @types.Image, - 85
to_ : @types.Image, - 86
alpha : Double, - 87
) -> @types.Image { - 88●
let w = from.width - 89
let h = from.height - 90
let ch = from.channels - 91
let data : Array[Byte] = [] - 92●
let a = alpha.clamp(min=0.0, max=1.0) - 93
for y in 0..<h { - 94●
for x in 0..<w { - 95●
for c in 0..<ch { - 96●
let idx = (y * w + x) * ch + c - 97●
let fv = from.data[idx].to_int().to_double() - 98●
let tv = to_.data[idx].to_int().to_double() - 99
// 简化光流:用帧差作为位移近似 - 100
let diff = tv - fv - 101
let val = fv + diff * a - 102●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 103
} - 104
} - 105
} - 106●
{ width: w, height: h, channels: ch, data: Bytes::from_array(data), } - 107
} - 108
- 109
///| - 110
/// 运动补偿插帧。 - 111
/// 运动补偿插值 Motion compensation interpolation. - 112
pub fn interpolate_frame_motion_compensation( - 113
from : @types.Image, - 114
to_ : @types.Image, - 115
alpha : Double, - 116
) -> @types.Image { - 117
// 简化:与帧混合相同 - 118●
interpolate_frame_blend(from, to_, alpha) - 119
} - 120
- 121
///| - 122
/// 插帧分发。 - 123
/// 插值分派 Interpolation dispatch. - 124
pub fn interpolate_frames_slow_motion( - 125
from : @types.Image, - 126
to_ : @types.Image, - 127
alpha : Double, - 128
sm_method : SlowMotionMethod, - 129
) -> @types.Image { - 130●
match sm_method { - 131○
OpticalFlow => interpolate_frame_optical_flow(from, to_, alpha) - 132●
FrameBlending => interpolate_frame_blend(from, to_, alpha) - 133
MotionCompensation => - 134○
interpolate_frame_motion_compensation(from, to_, alpha) - 135
} - 136
} - 137
- 138
///| - 139
/// 生成慢动作序列。 - 140
/// 生成慢动作序列 Generate slow motion sequence. - 141
pub fn generate_slow_motion( - 142
frames : Array[@types.Image], - 143
config : SlowMotionConfig, - 144
) -> Array[@types.Image] { - 145●
let result : Array[@types.Image] = [] - 146●
let n = frames.length() - 147
if n == 0 { - 148●
return result - 149
} - 150●
let factor = config.speed_factor.clamp(min=1, max=100) - 151●
result.push(frames[0]) - 152
for i in 1..<n { - 153●
for j in 1..<factor { - 154●
let alpha = j.to_double() / factor.to_double() - 155●
let interpolated = interpolate_frames_slow_motion( - 156
frames[i - 1], - 157
frames[i], - 158
alpha, - 159
config.sm_method, - 160
) - 161●
result.push(interpolated) - 162
} - 163●
result.push(frames[i]) - 164
} - 165
result - 166
} - 167
- 168
///| - 169
/// 应用运动模糊(水平方向)。 - 170
/// 应用运动模糊(水平) Apply motion blur (horizontal). - 171
fn motion_blur_horizontal(img : @types.Image, amount : Double) -> @types.Image { - 172●
let w = img.width - 173
let h = img.height - 174
let ch = img.channels - 175●
let radius = (amount * 10.0).to_int().clamp(min=1, max=10) - 176
let data : Array[Byte] = [] - 177
for y in 0..<h { - 178●
for x in 0..<w { - 179●
for c in 0..<ch { - 180●
let mut sum = 0.0 - 181
let mut count = 0.0 - 182
for dx in [-radius, 0, radius] { - 183●
let sx = x + dx - 184
if sx >= 0 && sx < w { - 185●
sum = sum + img.data[(y * w + sx) * ch + c].to_int().to_double() - 186
count = count + 1.0 - 187
} - 188
} - 189◐
let val = if count > 0.0 { sum / count } else { 0.0 } - 190●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 191
} - 192
} - 193
} - 194●
{ width: w, height: h, channels: ch, data: Bytes::from_array(data), } - 195
} - 196
- 197
///| - 198
/// 应用运动模糊(垂直方向)。 - 199
/// 应用运动模糊(垂直) Apply motion blur (vertical). - 200
fn motion_blur_vertical(img : @types.Image, amount : Double) -> @types.Image { - 201●
let w = img.width - 202
let h = img.height - 203
let ch = img.channels - 204●
let radius = (amount * 10.0).to_int().clamp(min=1, max=10) - 205
let data : Array[Byte] = [] - 206
for y in 0..<h { - 207●
for x in 0..<w { - 208●
for c in 0..<ch { - 209●
let mut sum = 0.0 - 210
let mut count = 0.0 - 211
for dy in [-radius, 0, radius] { - 212●
let sy = y + dy - 213
if sy >= 0 && sy < h { - 214●
sum = sum + img.data[(sy * w + x) * ch + c].to_int().to_double() - 215
count = count + 1.0 - 216
} - 217
} - 218◐
let val = if count > 0.0 { sum / count } else { 0.0 } - 219●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 220
} - 221
} - 222
} - 223●
{ width: w, height: h, channels: ch, data: Bytes::from_array(data), } - 224
} - 225
- 226
///| - 227
/// 应用运动模糊(径向)。 - 228
/// 应用运动模糊(径向) Apply motion blur (radial). - 229
fn motion_blur_radial(img : @types.Image, amount : Double) -> @types.Image { - 230●
let w = img.width - 231
let h = img.height - 232
let ch = img.channels - 233
let cx = w / 2 - 234
let cy = h / 2 - 235●
let radius = (amount * 10.0).to_int().clamp(min=1, max=10) - 236
let data : Array[Byte] = [] - 237
for y in 0..<h { - 238●
for x in 0..<w { - 239●
for c in 0..<ch { - 240●
let dx = x - cx - 241
let dy = y - cy - 242●
let dist = (dx * dx + dy * dy).to_double().sqrt() - 243
if dist < 0.5 { - 244●
data.push(img.data[(y * w + x) * ch + c]) - 245
continue - 246
} - 247●
let ux = (dx.to_double() / dist * radius.to_double()).to_int() - 248●
let uy = (dy.to_double() / dist * radius.to_double()).to_int() - 249
let mut sum = 0.0 - 250
let mut count = 0.0 - 251
for t in [-1, 0, 1] { - 252●
let sx = x + ux * t - 253
let sy = y + uy * t - 254
if sx >= 0 && sx < w && sy >= 0 && sy < h { - 255●
sum = sum + img.data[(sy * w + sx) * ch + c].to_int().to_double() - 256
count = count + 1.0 - 257
} - 258
} - 259◐
let val = if count > 0.0 { sum / count } else { 0.0 } - 260●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 261
} - 262
} - 263
} - 264●
{ width: w, height: h, channels: ch, data: Bytes::from_array(data), } - 265
} - 266
- 267
///| - 268
/// 应用运动模糊。 - 269
/// 应用运动模糊 Apply motion blur. - 270
pub fn apply_motion_blur( - 271
img : @types.Image, - 272
amount : Double, - 273
direction : Int32, - 274
) -> @types.Image { - 275●
match direction { - 276●
0 => motion_blur_horizontal(img, amount) - 277●
1 => motion_blur_vertical(img, amount) - 278●
_ => motion_blur_radial(img, amount) - 279
} - 280
} - 281
- 282
///| - 283
/// 运动模糊序列。 - 284
/// 对序列应用运动模糊 Apply motion blur to sequence. - 285
pub fn motion_blur_sequence( - 286
frames : Array[@types.Image], - 287
config : SlowMotionConfig, - 288
) -> Array[@types.Image] { - 289●
let result : Array[@types.Image] = [] - 290
for frame in frames { - 291●
if config.blur_amount > 0.0 { - 292●
result.push( - 293●
apply_motion_blur(frame, config.blur_amount, config.blur_direction), - 294
) - 295
} else { - 296●
result.push(frame) - 297
} - 298
} - 299
result - 300
} - 301
- 302
///| - 303
/// 慢动作流水线(插帧 + 运动模糊)。 - 304
/// 慢动作管线(插值 + 运动模糊) Slow motion pipeline (interpolation + motion blur). - 305
pub fn slow_motion_pipeline( - 306
frames : Array[@types.Image], - 307
config : SlowMotionConfig, - 308
) -> Array[@types.Image] { - 309●
let interpolated = generate_slow_motion(frames, config) - 310●
motion_blur_sequence(interpolated, config) - 311
} - 312
- 313
///| - 314
/// 计算插帧数。 - 315
/// 计算插帧数 Compute number of interpolated frames. - 316
pub fn compute_interpolated_count( - 317
frame_count : Int32, - 318
speed_factor : Int32, - 319
) -> Int32 { - 320●
if frame_count <= 0 || speed_factor <= 0 { - 321●
return 0 - 322
} - 323
frame_count + (frame_count - 1) * (speed_factor - 1) - 324
}