image.vision\video\video_interp.mbt

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