image.vision\video\video_superres.mbt
98.2% · 111 / 113 points · 317 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_superres - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 视频超分 — 时空/递归/深度 - 7
// Video super-resolution: spatio-temporal / recursive / depth - 8
// - 9
// Phase 17 v402: 视频处理大扩展 — 视频超分。 - 10
- 11
///| - 12
/// 视频超分方法。 - 13
/// 视频超分辨率方法 Video super-resolution method. - 14
/// `#non_exhaustive` 表示未来可能添加新构造器 - 15
#derive(Eq, @debug.Debug) - 16
#non_exhaustive - 17
pub(all) enum VideoSRMethod { - 18
BicubicSR // 双三次插值 - 19
SpatioTemporal // 时空超分 - 20
RecursiveSR // 递归超分 - 21
} - 22
- 23
///| - 24
pub extend VideoSRMethod with @debug.Debug::{to_repr} - 25
- 26
///| - 27
pub extend VideoSRMethod with Eq::{not_equal, equal} - 28
- 29
///| - 30
/// 视频超分配置。 - 31
/// 视频超分辨率配置 Video super-resolution configuration. - 32
#derive(Eq, @debug.Debug) - 33
pub(all) struct VideoSRConfig { - 34
sr_method : VideoSRMethod - 35
scale_factor : Int32 // 放大倍数 (2/4/8) - 36
temporal_window : Int32 // 时域窗口大小 - 37
blend_factor : Double // 混合因子 (0.0-1.0) - 38
} - 39
- 40
///| - 41
pub extend VideoSRConfig with @debug.Debug::{to_repr} - 42
- 43
///| - 44
pub extend VideoSRConfig with Eq::{not_equal, equal} - 45
- 46
///| - 47
/// 视频超分结果。 - 48
/// 视频超分辨率结果 Video super-resolution result. - 49
#derive(Eq, @debug.Debug) - 50
pub(all) struct VideoSRResult { - 51
frames : Array[@types.Image] // 超分后帧序列 - 52
scale_factor : Int32 // 实际放大倍数 - 53
quality_score : Double // 质量分数 (0.0-1.0) - 54
} - 55
- 56
///| - 57
pub extend VideoSRResult with @debug.Debug::{to_repr} - 58
- 59
///| - 60
pub extend VideoSRResult with Eq::{not_equal, equal} - 61
- 62
///| - 63
/// 默认超分配置。 - 64
/// 默认超分辨率配置 Default super-resolution configuration. - 65
pub fn default_sr_config() -> VideoSRConfig { - 66●
{ - 67
sr_method: BicubicSR, - 68
scale_factor: 2, - 69
temporal_window: 3, - 70
blend_factor: 0.5, - 71
} - 72
} - 73
- 74
///| - 75
/// 双线性上采样。 - 76
/// 双线性上采样 Bilinear upscaling. - 77
pub fn bilinear_upscale(img : @types.Image, scale : Int32) -> @types.Image { - 78●
let w = img.width - 79
let h = img.height - 80
let ch = img.channels - 81
let new_w = w * scale - 82
let new_h = h * scale - 83
let data : Array[Byte] = [] - 84
for y in 0..<new_h { - 85●
for x in 0..<new_w { - 86●
let src_x = x.to_double() / scale.to_double() - 87●
let src_y = y.to_double() / scale.to_double() - 88●
let x0 = src_x.to_int() - 89●
let y0 = src_y.to_int() - 90●
let x1 = if x0 + 1 < w { x0 + 1 } else { x0 } - 91●
let y1 = if y0 + 1 < h { y0 + 1 } else { y0 } - 92●
let fx = src_x - x0.to_double() - 93●
let fy = src_y - y0.to_double() - 94
for c in 0..<ch { - 95●
let v00 = img.data[(y0 * w + x0) * ch + c].to_double() - 96●
let v01 = img.data[(y0 * w + x1) * ch + c].to_double() - 97●
let v10 = img.data[(y1 * w + x0) * ch + c].to_double() - 98●
let v11 = img.data[(y1 * w + x1) * ch + c].to_double() - 99
let val = v00 * (1.0 - fx) * (1.0 - fy) + - 100
v01 * fx * (1.0 - fy) + - 101
v10 * (1.0 - fx) * fy + - 102
v11 * fx * fy - 103●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 104
} - 105
} - 106
} - 107●
{ width: new_w, height: new_h, channels: ch, data: Bytes::from_array(data), } - 108
} - 109
- 110
///| - 111
/// 双三次上采样(简化版)。 - 112
/// 双三次上采样(简化) Bicubic upscaling (simplified). - 113
fn bicubic_upscale(img : @types.Image, scale : Int32) -> @types.Image { - 114●
let w = img.width - 115
let h = img.height - 116
let ch = img.channels - 117
let new_w = w * scale - 118
let new_h = h * scale - 119
let data : Array[Byte] = [] - 120
for y in 0..<new_h { - 121●
for x in 0..<new_w { - 122●
let src_x = x.to_double() / scale.to_double() - 123●
let src_y = y.to_double() / scale.to_double() - 124●
let cx = src_x.to_int() - 125●
let cy = src_y.to_int() - 126
for c in 0..<ch { - 127●
let mut sum = 0.0 - 128
let mut weight_sum = 0.0 - 129
for dy in [-1, 0, 1, 2] { - 130●
for dx in [-1, 0, 1, 2] { - 131●
let nx = cx + dx - 132
let ny = cy + dy - 133
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 134●
let wx = cubic_weight(src_x - nx.to_double()) - 135●
let wy = cubic_weight(src_y - ny.to_double()) - 136
let weight = wx * wy - 137●
sum = sum + img.data[(ny * w + nx) * ch + c].to_double() * weight - 138
weight_sum = weight_sum + weight - 139
} - 140
} - 141
} - 142◐
let val = if weight_sum > 0.0 { sum / weight_sum } else { 0.0 } - 143●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 144
} - 145
} - 146
} - 147●
{ width: new_w, height: new_h, channels: ch, data: Bytes::from_array(data), } - 148
} - 149
- 150
///| - 151
/// 三次样条权重函数。 - 152
/// 三次样条权重函数 Cubic spline weight function. - 153
fn cubic_weight(t : Double) -> Double { - 154●
let t = t.abs() - 155
if t < 1.0 { - 156●
1.5 * t * t * t - 2.5 * t * t + 1.0 - 157●
} else if t < 2.0 { - 158●
-0.5 * t * t * t + 2.5 * t * t - 4.0 * t + 2.0 - 159
} else { - 160●
0.0 - 161
} - 162
} - 163
- 164
///| - 165
/// 双三次超分。 - 166
/// 双三次超分辨率 Bicubic super-resolution. - 167
pub fn bicubic_super_resolve( - 168
frames : Array[@types.Image], - 169
config : VideoSRConfig, - 170
) -> Array[@types.Image] { - 171●
let result : Array[@types.Image] = [] - 172
for frame in frames { - 173●
result.push(bicubic_upscale(frame, config.scale_factor)) - 174
} - 175
result - 176
} - 177
- 178
///| - 179
/// 时空超分:利用时域信息增强空间分辨率。 - 180
/// 时空超分辨率 Spatio-temporal super-resolution - 181
pub fn spatio_temporal_sr( - 182
frames : Array[@types.Image], - 183
config : VideoSRConfig, - 184
) -> Array[@types.Image] { - 185●
let n = frames.length() - 186
if n == 0 { - 187●
return [] - 188
} - 189
let result : Array[@types.Image] = [] - 190
let half = config.temporal_window / 2 - 191
for i in 0..<n { - 192●
let upscaled = bicubic_upscale(frames[i], config.scale_factor) - 193●
let start = if i > half { i - half } else { 0 } - 194●
let end = if i + half < n { i + half } else { n - 1 } - 195
let w = upscaled.width - 196
let h = upscaled.height - 197
let ch = upscaled.channels - 198
let data : Array[Byte] = [] - 199
for k in 0..<(w * h * ch) { - 200●
let mut sum = upscaled.data[k].to_double() - 201
let mut count = 1.0 - 202
for j in start..<=end { - 203●
if j != i { - 204●
let ref_upscaled = bicubic_upscale(frames[j], config.scale_factor) - 205●
sum = sum + ref_upscaled.data[k].to_double() * config.blend_factor - 206
count = count + config.blend_factor - 207
} - 208
} - 209
let val = sum / count - 210●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 211
} - 212●
result.push({ - 213
width: w, - 214
height: h, - 215
channels: ch, - 216●
data: Bytes::from_array(data), - 217
}) - 218
} - 219
result - 220
} - 221
- 222
///| - 223
/// 递归超分:多次小倍数超分级联。 - 224
/// 递归超分辨率:级联多小尺度超分 Recursive super-resolution: cascade multiple small-scale super-resolution. - 225
pub fn recursive_sr( - 226
frames : Array[@types.Image], - 227
config : VideoSRConfig, - 228
) -> Array[@types.Image] { - 229●
let scale = config.scale_factor - 230
if scale <= 1 { - 231●
return frames - 232
} - 233
let mut current = frames - 234
let mut remaining = scale - 235
while remaining > 1 { - 236◐
let step = if remaining >= 2 { 2 } else { remaining } - 237
let step_result : Array[@types.Image] = [] - 238
for frame in current { - 239●
step_result.push(bicubic_upscale(frame, step)) - 240
} - 241
current = step_result - 242
remaining = remaining / step - 243
} - 244
current - 245
} - 246
- 247
///| - 248
/// 视频超分主流程。 - 249
/// 视频超分辨率管线 Video super-resolution pipeline. - 250
pub fn video_super_resolve( - 251
frames : Array[@types.Image], - 252
config : VideoSRConfig, - 253
) -> VideoSRResult { - 254●
let result = match config.sr_method { - 255●
BicubicSR => bicubic_super_resolve(frames, config) - 256●
SpatioTemporal => spatio_temporal_sr(frames, config) - 257●
RecursiveSR => recursive_sr(frames, config) - 258
} - 259●
let quality = compute_sr_quality(frames, result, config.scale_factor) - 260
{ frames: result, scale_factor: config.scale_factor, quality_score: quality, } - 261
} - 262
- 263
///| - 264
/// 计算超分质量分数(基于下采样一致性)。 - 265
/// 计算超分质量分(基于下采样一致性) Compute super-resolution quality score (based on downsample consistency). - 266
pub fn compute_sr_quality( - 267
original : Array[@types.Image], - 268
super_resolved : Array[@types.Image], - 269
scale : Int32, - 270
) -> Double { - 271●
let n = original.length() - 272●
if n == 0 || super_resolved.length() == 0 { - 273●
return 0.0 - 274
} - 275
let mut total_score = 0.0 - 276
for i in 0..<n { - 277●
let sr = super_resolved[i] - 278
let w = sr.width - 279
let h = sr.height - 280
let ch = sr.channels - 281
let orig_w = original[i].width - 282
let orig_h = original[i].height - 283
if w != orig_w * scale || h != orig_h * scale { - 284●
continue - 285
} - 286
let mut diff_sum = 0.0 - 287
let mut count = 0 - 288
for y in 0..<orig_h { - 289●
for x in 0..<orig_w { - 290●
for c in 0..<ch { - 291●
let sr_x = x * scale - 292
let sr_y = y * scale - 293●
let sr_val = sr.data[(sr_y * w + sr_x) * ch + c].to_double() - 294●
let orig_val = original[i].data[(y * orig_w + x) * ch + c].to_double() - 295●
diff_sum = diff_sum + (sr_val - orig_val).abs() - 296
count = count + 1 - 297
} - 298
} - 299
} - 300
if count > 0 { - 301●
let avg_diff = diff_sum / count.to_double() - 302●
let score = 1.0 - (avg_diff / 255.0).clamp(min=0.0, max=1.0) - 303
total_score = total_score + score - 304
} - 305
} - 306●
total_score / n.to_double() - 307
} - 308
- 309
///| - 310
/// 超分流水线:超分 + 质量评估。 - 311
/// 超分辨率管线:超分 + 质量评估 Super-resolution pipeline: super-resolve + quality evaluation. - 312
pub fn sr_pipeline( - 313
frames : Array[@types.Image], - 314
config : VideoSRConfig, - 315
) -> VideoSRResult { - 316●
video_super_resolve(frames, config) - 317
}