image.vision\video\video_superres.mbt

98.2% · 111 / 113 points · 317 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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