image.vision\video\video_segmentation.mbt
93.3% · 111 / 119 points · 378 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_segmentation - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 视频分割 — 前景/背景/运动 - 7
// Video segmentation: foreground / background / motion - 8
// - 9
// Phase 17 v399: 视频处理大扩展 — 视频分割。 - 10
- 11
///| - 12
/// 视频分割方法。 - 13
/// 视频分割方法 Video segmentation method. - 14
/// `#non_exhaustive` 表示未来可能添加新构造器 - 15
#derive(Eq, @debug.Debug) - 16
#non_exhaustive - 17
pub(all) enum VideoSegmentationMethod { - 18
FrameDifference // 帧差法 - 19
BackgroundSubtraction // 背景减除 - 20
MotionBased // 基于运动 - 21
} - 22
- 23
///| - 24
pub extend VideoSegmentationMethod with @debug.Debug::{to_repr} - 25
- 26
///| - 27
pub extend VideoSegmentationMethod with Eq::{not_equal, equal} - 28
- 29
///| - 30
/// 背景模型。 - 31
/// 背景模型 Background model. - 32
#derive(Eq, @debug.Debug) - 33
pub(all) struct BackgroundModel { - 34
background : @types.Image - 35
variance : @types.Image // 每像素方差 - 36
frame_count : Int32 - 37
} - 38
- 39
///| - 40
pub extend BackgroundModel with @debug.Debug::{to_repr} - 41
- 42
///| - 43
pub extend BackgroundModel with Eq::{not_equal, equal} - 44
- 45
///| - 46
/// 分割配置。 - 47
/// 分割配置 Segmentation configuration. - 48
#derive(Eq, @debug.Debug) - 49
pub(all) struct SegmentationConfig { - 50
seg_method : VideoSegmentationMethod - 51
threshold : Double // 二值化阈值 - 52
learning_rate : Double // 背景学习率 (0.0-1.0) - 53
min_motion_area : Int32 // 最小运动区域面积 - 54
} - 55
- 56
///| - 57
pub extend SegmentationConfig with @debug.Debug::{to_repr} - 58
- 59
///| - 60
pub extend SegmentationConfig with Eq::{not_equal, equal} - 61
- 62
///| - 63
/// 分割结果。 - 64
/// 分割结果 Segmentation result. - 65
#derive(Eq, @debug.Debug) - 66
pub(all) struct SegmentationResult { - 67
foreground_mask : Array[Bool] // 前景掩模 - 68
foreground_ratio : Double // 前景比例 - 69
motion_area : Int32 // 运动面积 - 70
width : Int32 - 71
height : Int32 - 72
} - 73
- 74
///| - 75
pub extend SegmentationResult with @debug.Debug::{to_repr} - 76
- 77
///| - 78
pub extend SegmentationResult with Eq::{not_equal, equal} - 79
- 80
///| - 81
/// 默认分割配置。 - 82
/// 默认分割配置 Default segmentation configuration. - 83
pub fn default_segmentation_config() -> SegmentationConfig { - 84●
{ - 85
seg_method: FrameDifference, - 86
threshold: 30.0, - 87
learning_rate: 0.05, - 88
min_motion_area: 10, - 89
} - 90
} - 91
- 92
///| - 93
/// 帧差法前景分割。 - 94
/// 帧差前景分割 Frame difference foreground segmentation. - 95
pub fn segment_frame_difference( - 96
current : @types.Image, - 97
previous : @types.Image, - 98
threshold : Double, - 99
) -> SegmentationResult { - 100●
let w = current.width - 101
let h = current.height - 102
let ch = current.channels - 103
let mask : Array[Bool] = [] - 104
let mut motion_count = 0 - 105
for y in 0..<h { - 106●
for x in 0..<w { - 107●
let mut diff = 0.0 - 108
for c in 0..<ch { - 109●
let ci = (y * w + x) * ch + c - 110●
let d = current.data[ci].to_int() - previous.data[ci].to_int() - 111●
diff = diff + d.to_double().abs() - 112
} - 113●
diff = diff / ch.to_double() - 114
if diff > threshold { - 115●
mask.push(true) - 116
motion_count = motion_count + 1 - 117
} else { - 118●
mask.push(false) - 119
} - 120
} - 121
} - 122
let total = w * h - 123
let ratio = if total > 0 { - 124●
motion_count.to_double() / total.to_double() - 125
} else { - 126○
0.0 - 127
} - 128
{ - 129
foreground_mask: mask, - 130
foreground_ratio: ratio, - 131
motion_area: motion_count, - 132
width: w, - 133
height: h, - 134
} - 135
} - 136
- 137
///| - 138
/// 构建背景模型。 - 139
/// 从帧构建背景模型 Build background model from frames. - 140
pub fn build_background_model(frames : Array[@types.Image]) -> BackgroundModel? { - 141●
let n = frames.length() - 142
if n == 0 { - 143●
return None - 144
} - 145
let w = frames[0].width - 146
let h = frames[0].height - 147
let ch = frames[0].channels - 148
// 均值背景 - 149
let bg_data : Array[Byte] = [] - 150
let var_data : Array[Byte] = [] - 151
for i in 0..<(w * h * ch) { - 152●
let mut sum = 0.0 - 153
for frame in frames { - 154●
sum = sum + frame.data[i].to_int().to_double() - 155
} - 156●
let mean = sum / n.to_double() - 157
let mut var_sum = 0.0 - 158
for frame in frames { - 159●
let diff = frame.data[i].to_int().to_double() - mean - 160
var_sum = var_sum + diff * diff - 161
} - 162●
let variance = var_sum / n.to_double() - 163●
bg_data.push(mean.clamp(min=0.0, max=255.0).to_int().to_byte()) - 164●
var_data.push(variance.clamp(min=0.0, max=255.0).to_int().to_byte()) - 165
} - 166
Some({ - 167
background: { - 168
width: w, - 169
height: h, - 170
channels: ch, - 171●
data: Bytes::from_array(bg_data), - 172
}, - 173
variance: { - 174
width: w, - 175
height: h, - 176
channels: ch, - 177●
data: Bytes::from_array(var_data), - 178
}, - 179
frame_count: n, - 180
}) - 181
} - 182
- 183
///| - 184
/// 更新背景模型。 - 185
/// 用新帧更新背景模型 Update background model with new frame. - 186
pub fn update_background_model( - 187
model : BackgroundModel, - 188
frame : @types.Image, - 189
learning_rate : Double, - 190
) -> BackgroundModel { - 191●
let w = model.background.width - 192
let h = model.background.height - 193
let ch = model.background.channels - 194●
let alpha = learning_rate.clamp(min=0.0, max=1.0) - 195
let bg_data : Array[Byte] = [] - 196
for i in 0..<(w * h * ch) { - 197●
let old = model.background.data[i].to_int().to_double() - 198●
let new_val = frame.data[i].to_int().to_double() - 199
let updated = old * (1.0 - alpha) + new_val * alpha - 200●
bg_data.push(updated.clamp(min=0.0, max=255.0).to_int().to_byte()) - 201
} - 202
{ - 203
background: { - 204
width: w, - 205
height: h, - 206
channels: ch, - 207●
data: Bytes::from_array(bg_data), - 208
}, - 209
variance: model.variance, - 210
frame_count: model.frame_count + 1, - 211
} - 212
} - 213
- 214
///| - 215
/// 背景减除。 - 216
/// 背景减除 Background subtraction. - 217
pub fn subtract_background( - 218
frame : @types.Image, - 219
model : BackgroundModel, - 220
threshold : Double, - 221
) -> SegmentationResult { - 222●
let w = frame.width - 223
let h = frame.height - 224
let ch = frame.channels - 225
let mask : Array[Bool] = [] - 226
let mut motion_count = 0 - 227
for y in 0..<h { - 228●
for x in 0..<w { - 229●
let mut diff = 0.0 - 230
for c in 0..<ch { - 231●
let ci = (y * w + x) * ch + c - 232●
let d = frame.data[ci].to_int() - model.background.data[ci].to_int() - 233●
diff = diff + d.to_double().abs() - 234
} - 235●
diff = diff / ch.to_double() - 236
if diff > threshold { - 237●
mask.push(true) - 238
motion_count = motion_count + 1 - 239
} else { - 240●
mask.push(false) - 241
} - 242
} - 243
} - 244
let total = w * h - 245
let ratio = if total > 0 { - 246●
motion_count.to_double() / total.to_double() - 247
} else { - 248○
0.0 - 249
} - 250
{ - 251
foreground_mask: mask, - 252
foreground_ratio: ratio, - 253
motion_area: motion_count, - 254
width: w, - 255
height: h, - 256
} - 257
} - 258
- 259
///| - 260
/// 基于运动的分割。 - 261
/// 基于运动的分割 Motion-based segmentation. - 262
pub fn segment_motion( - 263
frames : Array[@types.Image], - 264
threshold : Double, - 265
) -> Array[SegmentationResult] { - 266●
let results : Array[SegmentationResult] = [] - 267●
let n = frames.length() - 268
if n < 2 { - 269○
return results - 270
} - 271
for i in 1..<n { - 272●
results.push(segment_frame_difference(frames[i], frames[i - 1], threshold)) - 273
} - 274
results - 275
} - 276
- 277
///| - 278
/// 视频分割流水线。 - 279
/// 视频分割管线 Video segmentation pipeline. - 280
pub fn video_segmentation_pipeline( - 281
frames : Array[@types.Image], - 282
config : SegmentationConfig, - 283
) -> Array[SegmentationResult] { - 284●
match config.seg_method { - 285●
FrameDifference => { - 286
let results : Array[SegmentationResult] = [] - 287●
let n = frames.length() - 288
if n < 2 { - 289○
return results - 290
} - 291
for i in 1..<n { - 292●
results.push( - 293●
segment_frame_difference(frames[i], frames[i - 1], config.threshold), - 294
) - 295
} - 296
results - 297
} - 298●
BackgroundSubtraction => { - 299
let results : Array[SegmentationResult] = [] - 300●
let n = frames.length() - 301
if n < 2 { - 302○
return results - 303
} - 304●
let model = build_background_model(frames) - 305
if model is Some(m) { - 306●
for frame in frames { - 307●
results.push(subtract_background(frame, m, config.threshold)) - 308
} - 309
} - 310
results - 311
} - 312○
MotionBased => segment_motion(frames, config.threshold) - 313
} - 314
} - 315
- 316
///| - 317
/// 计算运动一致性。 - 318
/// 计算帧间运动一致性 Compute motion consistency across frames. - 319
pub fn motion_consistency(results : Array[SegmentationResult]) -> Double { - 320●
let n = results.length() - 321
if n == 0 { - 322●
return 0.0 - 323
} - 324
let w = results[0].width - 325
let h = results[0].height - 326
let total = w * h - 327
if total == 0 { - 328○
return 0.0 - 329
} - 330
// 计算每像素的运动频率 - 331●
let freq : Array[Int32] = Array::make(total, 0) - 332
for r in results { - 333●
for i in 0..<total { - 334●
if i < r.foreground_mask.length() && r.foreground_mask[i] { - 335●
freq[i] = freq[i] + 1 - 336
} - 337
} - 338
} - 339
// 一致性 = 高频运动像素比例 - 340
let mut consistent = 0 - 341
for f in freq { - 342●
if f.to_double() / n.to_double() > 0.5 { - 343●
consistent = consistent + 1 - 344
} - 345
} - 346●
consistent.to_double() / total.to_double() - 347
} - 348
- 349
///| - 350
/// 提取前景图像。 - 351
/// 使用掩膜提取前景图像 Extract foreground image using mask. - 352
pub fn extract_foreground( - 353
img : @types.Image, - 354
result : SegmentationResult, - 355
) -> @types.Image { - 356●
let w = img.width - 357
let h = img.height - 358
let ch = img.channels - 359
let data : Array[Byte] = [] - 360
for y in 0..<h { - 361●
for x in 0..<w { - 362●
let mask_idx = y * w + x - 363●
let is_fg = if mask_idx < result.foreground_mask.length() { - 364●
result.foreground_mask[mask_idx] - 365
} else { - 366○
false - 367
} - 368
for c in 0..<ch { - 369●
if is_fg { - 370●
data.push(img.data[(y * w + x) * ch + c]) - 371
} else { - 372●
data.push((0 : Byte)) - 373
} - 374
} - 375
} - 376
} - 377●
{ width: w, height: h, channels: ch, data: Bytes::from_array(data), } - 378
}