image.vision\video\slow_motion.mbt

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