image.vision\video\video_processing.mbt

95.9% · 93 / 97 points · 296 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_processing
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 视频处理基础(v12.8.0)
  7. 7// 帧动画创建、帧插值、简单视频效果
  8. 8
  9. 9///|
  10. 10/// 视频帧序列
  11. 11#derive(Eq, @debug.Debug)
  12. 12pub(all) struct VideoFrames {
  13. 13 frames : Array[@types.Image]
  14. 14 width : Int32
  15. 15 height : Int32
  16. 16 channels : Int32
  17. 17 fps : Double
  18. 18}
  19. 19
  20. 20///|
  21. 21pub extend VideoFrames with Eq::{not_equal, equal}
  22. 22
  23. 23///|
  24. 24pub extend VideoFrames with @debug.Debug::{to_repr}
  25. 25
  26. 26///|
  27. 27/// 创建视频帧序列
  28. 28pub fn video_frames_new(
  29. 29 frames : Array[@types.Image],
  30. 30 fps~ : Double = 30.0,
  31. 31) -> VideoFrames {
  32. 32● if frames.length() == 0 {
  33. 33○ return VideoFrames::{ frames: [], width: 0, height: 0, channels: 1, fps, }
  34. 34 }
  35. 35 let first = frames[0]
  36. 36 VideoFrames::{
  37. 37 frames,
  38. 38 width: first.width,
  39. 39 height: first.height,
  40. 40 channels: first.channels,
  41. 41 fps,
  42. 42 }
  43. 43}
  44. 44
  45. 45///|
  46. 46/// 获取帧数
  47. 47pub fn video_frame_count(video : VideoFrames) -> Int32 {
  48. 48● video.frames.length()
  49. 49}
  50. 50
  51. 51///|
  52. 52/// 获取指定帧
  53. 53pub fn video_get_frame(video : VideoFrames, index : Int32) -> @types.Image? {
  54. 54● if index >= 0 && index < video.frames.length() {
  55. 55● Some(video.frames[index])
  56. 56 } else {
  57. 57● None
  58. 58 }
  59. 59}
  60. 60
  61. 61///|
  62. 62/// 帧插值(在两帧之间生成中间帧)
  63. 63/// 使用简单的线性插值
  64. 64pub fn interpolate_frames(
  65. 65 frame1 : @types.Image,
  66. 66 frame2 : @types.Image,
  67. 67 num_frames~ : Int32 = 1,
  68. 68) -> Array[@types.Image] {
  69. 69● let n = num_frames
  70. 70 if frame1.width != frame2.width || frame1.height != frame2.height {
  71. 71○ return []
  72. 72 }
  73. 73 let w = frame1.width
  74. 74 let h = frame1.height
  75. 75● let channels = frame1.channels.min(frame2.channels)
  76. 76 let result : Array[@types.Image] = []
  77. 77 for i in 1..<(n + 1) {
  78. 78● let alpha = i.to_double() / (n + 1).to_double()
  79. 79● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  80. 80 for y in 0..<h {
  81. 81● for x in 0..<w {
  82. 82● let idx = (y * w + x) * channels
  83. 83 for c in 0..<channels {
  84. 84● let v1 = frame1.data[idx + c].to_double()
  85. 85● let v2 = frame2.data[idx + c].to_double()
  86. 86 let interpolated = v1 * (1.0 - alpha) + v2 * alpha
  87. 87 out_data[idx + c] = interpolated
  88. 88● .min(255.0)
  89. 89● .max(0.0)
  90. 90● .to_int()
  91. 91● .to_byte()
  92. 92 }
  93. 93 }
  94. 94 }
  95. 95● result.push(@types.Image::{
  96. 96 width: w,
  97. 97 height: h,
  98. 98 channels,
  99. 99● data: Bytes::from_array(out_data),
  100. 100 })
  101. 101 }
  102. 102 result
  103. 103}
  104. 104
  105. 105///|
  106. 106/// 视频淡入效果
  107. 107pub fn video_fade_in(
  108. 108 video : VideoFrames,
  109. 109 duration_frames~ : Int32 = 30,
  110. 110) -> VideoFrames {
  111. 111● let duration = duration_frames
  112. 112 let frames = video.frames
  113. 113 let result : Array[@types.Image] = []
  114. 114● for i in 0..<frames.length() {
  115. 115● let frame = frames[i]
  116. 116 if i < duration {
  117. 117● let alpha = i.to_double() / duration.to_double()
  118. 118 let w = frame.width
  119. 119 let h = frame.height
  120. 120 let channels = frame.channels
  121. 121● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  122. 122 for y in 0..<h {
  123. 123● for x in 0..<w {
  124. 124● let idx = (y * w + x) * channels
  125. 125 for c in 0..<channels {
  126. 126● let v = frame.data[idx + c].to_double() * alpha
  127. 127● out_data[idx + c] = v.min(255.0).max(0.0).to_int().to_byte()
  128. 128 }
  129. 129 }
  130. 130 }
  131. 131● result.push(@types.Image::{
  132. 132 width: w,
  133. 133 height: h,
  134. 134 channels,
  135. 135● data: Bytes::from_array(out_data),
  136. 136 })
  137. 137 } else {
  138. 138● result.push(frame)
  139. 139 }
  140. 140 }
  141. 141 VideoFrames::{
  142. 142 frames: result,
  143. 143 width: video.width,
  144. 144 height: video.height,
  145. 145 channels: video.channels,
  146. 146 fps: video.fps,
  147. 147 }
  148. 148}
  149. 149
  150. 150///|
  151. 151/// 视频淡出效果
  152. 152pub fn video_fade_out(
  153. 153 video : VideoFrames,
  154. 154 duration_frames~ : Int32 = 30,
  155. 155) -> VideoFrames {
  156. 156● let duration = duration_frames
  157. 157 let frames = video.frames
  158. 158● let n = frames.length()
  159. 159 let result : Array[@types.Image] = []
  160. 160 for i in 0..<n {
  161. 161● let frame = frames[i]
  162. 162 if i >= n - duration {
  163. 163● let alpha = (n - 1 - i).to_double() / duration.to_double()
  164. 164 let w = frame.width
  165. 165 let h = frame.height
  166. 166 let channels = frame.channels
  167. 167● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  168. 168 for y in 0..<h {
  169. 169● for x in 0..<w {
  170. 170● let idx = (y * w + x) * channels
  171. 171 for c in 0..<channels {
  172. 172● let v = frame.data[idx + c].to_double() * alpha
  173. 173● out_data[idx + c] = v.min(255.0).max(0.0).to_int().to_byte()
  174. 174 }
  175. 175 }
  176. 176 }
  177. 177● result.push(@types.Image::{
  178. 178 width: w,
  179. 179 height: h,
  180. 180 channels,
  181. 181● data: Bytes::from_array(out_data),
  182. 182 })
  183. 183 } else {
  184. 184● result.push(frame)
  185. 185 }
  186. 186 }
  187. 187 VideoFrames::{
  188. 188 frames: result,
  189. 189 width: video.width,
  190. 190 height: video.height,
  191. 191 channels: video.channels,
  192. 192 fps: video.fps,
  193. 193 }
  194. 194}
  195. 195
  196. 196///|
  197. 197/// 视频抽帧(按间隔抽取帧)
  198. 198pub fn video_sample(video : VideoFrames, interval~ : Int32 = 2) -> VideoFrames {
  199. 199● let step = interval
  200. 200 let frames = video.frames
  201. 201 let result : Array[@types.Image] = []
  202. 202 let mut i = 0
  203. 203● while i < frames.length() {
  204. 204● result.push(frames[i])
  205. 205 i += step
  206. 206 }
  207. 207 VideoFrames::{
  208. 208 frames: result,
  209. 209 width: video.width,
  210. 210 height: video.height,
  211. 211 channels: video.channels,
  212. 212● fps: video.fps / step.to_double(),
  213. 213 }
  214. 214}
  215. 215
  216. 216///|
  217. 217/// 视频倒放
  218. 218pub fn video_reverse(video : VideoFrames) -> VideoFrames {
  219. 219● let frames = video.frames
  220. 220 let result : Array[@types.Image] = []
  221. 221● let mut i = frames.length() - 1
  222. 222 while i >= 0 {
  223. 223● result.push(frames[i])
  224. 224 i -= 1
  225. 225 }
  226. 226 VideoFrames::{
  227. 227 frames: result,
  228. 228 width: video.width,
  229. 229 height: video.height,
  230. 230 channels: video.channels,
  231. 231 fps: video.fps,
  232. 232 }
  233. 233}
  234. 234
  235. 235///|
  236. 236/// 视频拼接(将两个视频首尾相接)
  237. 237pub fn video_concat(video1 : VideoFrames, video2 : VideoFrames) -> VideoFrames {
  238. 238● let frames : Array[@types.Image] = []
  239. 239 for frame in video1.frames {
  240. 240● frames.push(frame)
  241. 241 }
  242. 242 for frame in video2.frames {
  243. 243● frames.push(frame)
  244. 244 }
  245. 245 VideoFrames::{
  246. 246 frames,
  247. 247 width: video1.width,
  248. 248 height: video1.height,
  249. 249 channels: video1.channels,
  250. 250 fps: video1.fps,
  251. 251 }
  252. 252}
  253. 253
  254. 254///|
  255. 255/// 生成 GIF 动画的简化版(输出为帧序列,不编码)
  256. 256/// 实际 GIF 编码需要额外的库支持
  257. 257pub fn create_animation(
  258. 258 frames : Array[@types.Image],
  259. 259 fps~ : Double = 10.0,
  260. 260) -> VideoFrames {
  261. 261● video_frames_new(frames, fps~)
  262. 262}
  263. 263
  264. 264///|
  265. 265/// 视频帧平均(将多帧平均为一帧,用于降噪)
  266. 266pub fn video_frame_average(video : VideoFrames) -> @types.Image {
  267. 267● let frames = video.frames
  268. 268● if frames.length() == 0 {
  269. 269○ return @types.Image::{
  270. 270 width: 1,
  271. 271 height: 1,
  272. 272 channels: 1,
  273. 273○ data: Bytes::from_array([b'\x00']),
  274. 274 }
  275. 275 }
  276. 276 let w = video.width
  277. 277 let h = video.height
  278. 278 let channels = video.channels
  279. 279● let n = frames.length().to_double()
  280. 280● let sum : Array[Double] = Array::make(w * h * channels, 0.0)
  281. 281 for frame in frames {
  282. 282● for i in 0..<(w * h * channels) {
  283. 283● sum[i] += frame.data[i].to_double()
  284. 284 }
  285. 285 }
  286. 286● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  287. 287 for i in 0..<(w * h * channels) {
  288. 288● out_data[i] = (sum[i] / n).min(255.0).max(0.0).to_int().to_byte()
  289. 289 }
  290. 290 @types.Image::{
  291. 291 width: w,
  292. 292 height: h,
  293. 293 channels,
  294. 294● data: Bytes::from_array(out_data),
  295. 295 }
  296. 296}