image.vision\video\video_inpaint.mbt

95.8% · 159 / 166 points · 504 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_inpaint
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 视频修复 — 补全/去抖/去闪烁
  7. 7// Video inpainting: completion / de-flicker / de-shimmer
  8. 8//
  9. 9// Phase 17 v401: 视频处理大扩展 — 视频修复。
  10. 10
  11. 11///|
  12. 12/// 视频修复方法。
  13. 13/// 视频修复方法 Video inpainting method.
  14. 14/// `#non_exhaustive` 表示未来可能添加新构造器
  15. 15#derive(Eq, @debug.Debug)
  16. 16#non_exhaustive
  17. 17pub(all) enum VideoInpaintMethod {
  18. 18 Temporal // 时域修复(用前后帧填补)
  19. 19 Spatial // 空域修复(用周围像素填补)
  20. 20 Hybrid // 混合修复
  21. 21}
  22. 22
  23. 23///|
  24. 24pub extend VideoInpaintMethod with @debug.Debug::{to_repr}
  25. 25
  26. 26///|
  27. 27pub extend VideoInpaintMethod with Eq::{not_equal, equal}
  28. 28
  29. 29///|
  30. 30/// 去闪烁方法。
  31. 31/// 去闪烁方法 De-flicker method.
  32. 32/// `#non_exhaustive` 表示未来可能添加新构造器
  33. 33#derive(Eq, @debug.Debug)
  34. 34#non_exhaustive
  35. 35pub(all) enum FlickerMethod {
  36. 36 TemporalFlicker // 时域闪烁去除
  37. 37 HistogramFlicker // 直方图稳定
  38. 38 LuminanceFlicker // 亮度归一化
  39. 39}
  40. 40
  41. 41///|
  42. 42pub extend FlickerMethod with @debug.Debug::{to_repr}
  43. 43
  44. 44///|
  45. 45pub extend FlickerMethod with Eq::{not_equal, equal}
  46. 46
  47. 47///|
  48. 48/// 视频修复配置。
  49. 49/// 视频修复配置 Video inpainting configuration.
  50. 50#derive(Eq, @debug.Debug)
  51. 51pub(all) struct VideoInpaintConfig {
  52. 52 inpaint_method : VideoInpaintMethod
  53. 53 temporal_radius : Int32 // 时域半径(前后帧数)
  54. 54 spatial_radius : Int32 // 空域半径(像素)
  55. 55 blend_factor : Double // 混合因子 (0.0-1.0)
  56. 56}
  57. 57
  58. 58///|
  59. 59pub extend VideoInpaintConfig with @debug.Debug::{to_repr}
  60. 60
  61. 61///|
  62. 62pub extend VideoInpaintConfig with Eq::{not_equal, equal}
  63. 63
  64. 64///|
  65. 65/// 去闪烁配置。
  66. 66/// 去闪烁配置 De-flicker configuration.
  67. 67#derive(Eq, @debug.Debug)
  68. 68pub(all) struct FlickerConfig {
  69. 69 flicker_method : FlickerMethod
  70. 70 temporal_window : Int32 // 时域窗口大小
  71. 71 strength : Double // 去闪烁强度 (0.0-1.0)
  72. 72}
  73. 73
  74. 74///|
  75. 75pub extend FlickerConfig with @debug.Debug::{to_repr}
  76. 76
  77. 77///|
  78. 78pub extend FlickerConfig with Eq::{not_equal, equal}
  79. 79
  80. 80///|
  81. 81/// 闪烁检测结果。
  82. 82/// 闪烁检测结果 Flicker detection result.
  83. 83#derive(Eq, @debug.Debug)
  84. 84pub(all) struct FlickerDetection {
  85. 85 has_flicker : Bool // 是否检测到闪烁
  86. 86 flicker_score : Double // 闪烁分数 (0.0-1.0)
  87. 87 frame_indices : Array[Int32] // 闪烁帧索引
  88. 88}
  89. 89
  90. 90///|
  91. 91pub extend FlickerDetection with @debug.Debug::{to_repr}
  92. 92
  93. 93///|
  94. 94pub extend FlickerDetection with Eq::{not_equal, equal}
  95. 95
  96. 96///|
  97. 97/// 默认修复配置。
  98. 98/// 默认修复配置 Default inpainting configuration.
  99. 99pub fn default_inpaint_config() -> VideoInpaintConfig {
  100. 100● {
  101. 101 inpaint_method: Temporal,
  102. 102 temporal_radius: 3,
  103. 103 spatial_radius: 5,
  104. 104 blend_factor: 0.5,
  105. 105 }
  106. 106}
  107. 107
  108. 108///|
  109. 109/// 默认去闪烁配置。
  110. 110/// 默认去闪烁配置 Default de-flicker configuration.
  111. 111pub fn default_flicker_config() -> FlickerConfig {
  112. 112● { flicker_method: TemporalFlicker, temporal_window: 5, strength: 0.8, }
  113. 113}
  114. 114
  115. 115///|
  116. 116/// 计算图像平均亮度。
  117. 117/// 计算图像平均亮度 Compute mean luminance of an image.
  118. 118fn compute_mean_luminance(img : @types.Image) -> Double {
  119. 119● let n = img.width * img.height * img.channels
  120. 120 if n == 0 {
  121. 121○ return 0.0
  122. 122 }
  123. 123 let mut total = 0.0
  124. 124 for i in 0..<n {
  125. 125● total = total + img.data[i].to_double()
  126. 126 }
  127. 127● total / n.to_double()
  128. 128}
  129. 129
  130. 130///|
  131. 131/// 时域修复:用前后帧填补掩码区域。
  132. 132/// 时间修复:用相邻帧填充掩膜区域 Temporal inpainting: fill masked region using adjacent frames.
  133. 133pub fn temporal_inpaint(
  134. 134 current : @types.Image,
  135. 135 prev_frame : @types.Image,
  136. 136 next_frame : @types.Image,
  137. 137 mask : Array[Bool],
  138. 138 config : VideoInpaintConfig,
  139. 139) -> @types.Image {
  140. 140● let w = current.width
  141. 141 let h = current.height
  142. 142 let ch = current.channels
  143. 143 let data : Array[Byte] = []
  144. 144 for i in 0..<(w * h * ch) {
  145. 145● if mask[i / ch] {
  146. 146● let prev_val = prev_frame.data[i].to_double()
  147. 147● let next_val = next_frame.data[i].to_double()
  148. 148 let blend = config.blend_factor
  149. 149 let val = prev_val * (1.0 - blend) * 0.5 + next_val * blend * 0.5
  150. 150● data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte())
  151. 151 } else {
  152. 152● data.push(current.data[i])
  153. 153 }
  154. 154 }
  155. 155● { width: w, height: h, channels: ch, data: Bytes::from_array(data), }
  156. 156}
  157. 157
  158. 158///|
  159. 159/// 空域修复:用周围像素填补掩码区域。
  160. 160/// 空间修复:用周围像素填充掩膜区域 Spatial inpainting: fill masked region using surrounding pixels.
  161. 161pub fn spatial_inpaint(
  162. 162 current : @types.Image,
  163. 163 mask : Array[Bool],
  164. 164 config : VideoInpaintConfig,
  165. 165) -> @types.Image {
  166. 166● let w = current.width
  167. 167 let h = current.height
  168. 168 let ch = current.channels
  169. 169 let data : Array[Byte] = []
  170. 170 for y in 0..<h {
  171. 171● for x in 0..<w {
  172. 172● let idx = y * w + x
  173. 173 if mask[idx] {
  174. 174● let radius = config.spatial_radius
  175. 175 let sum : Array[Double] = []
  176. 176 let mut count = 0
  177. 177 for _ in 0..<ch {
  178. 178● sum.push(0.0)
  179. 179 }
  180. 180 for dy in [-1, 0, 1] {
  181. 181● for dx in [-1, 0, 1] {
  182. 182● if dx == 0 && dy == 0 {
  183. 183● continue
  184. 184 }
  185. 185 let nx = x + dx * radius
  186. 186 let ny = y + dy * radius
  187. 187 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  188. 188● let nidx = ny * w + nx
  189. 189 if !mask[nidx] {
  190. 190● for c in 0..<ch {
  191. 191● let ci = nidx * ch + c
  192. 192● sum[c] = sum[c] + current.data[ci].to_double()
  193. 193 }
  194. 194 count = count + 1
  195. 195 }
  196. 196 }
  197. 197 }
  198. 198 }
  199. 199 if count > 0 {
  200. 200● for c in 0..<ch {
  201. 201● let val = sum[c] / count.to_double()
  202. 202● data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte())
  203. 203 }
  204. 204 } else {
  205. 205● for c in 0..<ch {
  206. 206● data.push(current.data[idx * ch + c])
  207. 207 }
  208. 208 }
  209. 209 } else {
  210. 210● for c in 0..<ch {
  211. 211● data.push(current.data[idx * ch + c])
  212. 212 }
  213. 213 }
  214. 214 }
  215. 215 }
  216. 216● { width: w, height: h, channels: ch, data: Bytes::from_array(data), }
  217. 217}
  218. 218
  219. 219///|
  220. 220/// 视频修复主流程。
  221. 221/// 视频修复管线 Video inpainting pipeline.
  222. 222pub fn video_inpaint(
  223. 223 frames : Array[@types.Image],
  224. 224 mask : Array[Bool],
  225. 225 config : VideoInpaintConfig,
  226. 226) -> Array[@types.Image] {
  227. 227● let n = frames.length()
  228. 228 let result : Array[@types.Image] = []
  229. 229 for i in 0..<n {
  230. 230● let prev_idx = if i > 0 { i - 1 } else { 0 }
  231. 231● let next_idx = if i < n - 1 { i + 1 } else { n - 1 }
  232. 232● let inpainted = match config.inpaint_method {
  233. 233 Temporal =>
  234. 234● temporal_inpaint(
  235. 235 frames[i],
  236. 236 frames[prev_idx],
  237. 237 frames[next_idx],
  238. 238 mask,
  239. 239 config,
  240. 240 )
  241. 241● Spatial => spatial_inpaint(frames[i], mask, config)
  242. 242● Hybrid => {
  243. 243● let temp = temporal_inpaint(
  244. 244 frames[i],
  245. 245 frames[prev_idx],
  246. 246 frames[next_idx],
  247. 247 mask,
  248. 248 config,
  249. 249 )
  250. 250● spatial_inpaint(temp, mask, config)
  251. 251 }
  252. 252 }
  253. 253● result.push(inpainted)
  254. 254 }
  255. 255 result
  256. 256}
  257. 257
  258. 258///|
  259. 259/// 检测闪烁:分析帧间亮度变化。
  260. 260/// 检测闪烁:分析帧间亮度变化 Detect flicker: analyze inter-frame luminance changes.
  261. 261pub fn detect_flicker(
  262. 262 frames : Array[@types.Image],
  263. 263 threshold : Double,
  264. 264) -> FlickerDetection {
  265. 265● let n = frames.length()
  266. 266 if n < 2 {
  267. 267● return { has_flicker: false, flicker_score: 0.0, frame_indices: [], }
  268. 268 }
  269. 269 let luminances : Array[Double] = []
  270. 270 for frame in frames {
  271. 271● luminances.push(compute_mean_luminance(frame))
  272. 272 }
  273. 273 let mut mean_diff = 0.0
  274. 274 for i in 1..<n {
  275. 275● mean_diff = mean_diff + (luminances[i] - luminances[i - 1]).abs()
  276. 276 }
  277. 277● mean_diff = mean_diff / (n - 1).to_double()
  278. 278 let flicker_indices : Array[Int32] = []
  279. 279 for i in 1..<n {
  280. 280● let diff = (luminances[i] - luminances[i - 1]).abs()
  281. 281 if diff > threshold {
  282. 282● flicker_indices.push(i)
  283. 283 }
  284. 284 }
  285. 285● let score = (mean_diff / 255.0).clamp(min=0.0, max=1.0)
  286. 286 {
  287. 287 has_flicker: score > 0.05,
  288. 288 flicker_score: score,
  289. 289 frame_indices: flicker_indices,
  290. 290 }
  291. 291}
  292. 292
  293. 293///|
  294. 294/// 时域去闪烁:用时域窗口平滑亮度。
  295. 295/// 时间去闪烁:用时间窗口平滑亮度 Temporal de-flicker: smooth luminance using temporal window.
  296. 296pub fn deflicker_temporal(
  297. 297 frames : Array[@types.Image],
  298. 298 config : FlickerConfig,
  299. 299) -> Array[@types.Image] {
  300. 300● let n = frames.length()
  301. 301 if n == 0 {
  302. 302● return []
  303. 303 }
  304. 304 let window = config.temporal_window
  305. 305 let luminances : Array[Double] = []
  306. 306 for frame in frames {
  307. 307● luminances.push(compute_mean_luminance(frame))
  308. 308 }
  309. 309 let result : Array[@types.Image] = []
  310. 310 for i in 0..<n {
  311. 311● let mut sum_lum = 0.0
  312. 312 let mut count = 0
  313. 313 let half = window / 2
  314. 314● let start = if i > half { i - half } else { 0 }
  315. 315● let end = if i + half < n { i + half } else { n - 1 }
  316. 316 for j in start..<=end {
  317. 317● sum_lum = sum_lum + luminances[j]
  318. 318 count = count + 1
  319. 319 }
  320. 320● let target_lum = sum_lum / count.to_double()
  321. 321 let current_lum = luminances[i]
  322. 322◐ let ratio = if current_lum > 0.0 { target_lum / current_lum } else { 1.0 }
  323. 323 let blended_ratio = 1.0 + (ratio - 1.0) * config.strength
  324. 324 let w = frames[i].width
  325. 325 let h = frames[i].height
  326. 326 let ch = frames[i].channels
  327. 327 let data : Array[Byte] = []
  328. 328 for k in 0..<(w * h * ch) {
  329. 329● let val = frames[i].data[k].to_double() * blended_ratio
  330. 330● data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte())
  331. 331 }
  332. 332● result.push({
  333. 333 width: w,
  334. 334 height: h,
  335. 335 channels: ch,
  336. 336● data: Bytes::from_array(data),
  337. 337 })
  338. 338 }
  339. 339 result
  340. 340}
  341. 341
  342. 342///|
  343. 343/// 直方图去闪烁:稳定帧间直方图分布。
  344. 344/// 直方图去闪烁 Histogram de-flicker
  345. 345pub fn deflicker_histogram(
  346. 346 frames : Array[@types.Image],
  347. 347 config : FlickerConfig,
  348. 348) -> Array[@types.Image] {
  349. 349● let n = frames.length()
  350. 350 if n == 0 {
  351. 351● return []
  352. 352 }
  353. 353 let result : Array[@types.Image] = []
  354. 354 let prev_hist : Array[Double] = []
  355. 355 for _ in 0..<256 {
  356. 356● prev_hist.push(0.0)
  357. 357 }
  358. 358 for i in 0..<n {
  359. 359● let w = frames[i].width
  360. 360 let h = frames[i].height
  361. 361 let ch = frames[i].channels
  362. 362 let curr_hist : Array[Double] = []
  363. 363 for _ in 0..<256 {
  364. 364● curr_hist.push(0.0)
  365. 365 }
  366. 366● let total = (w * h * ch).to_double()
  367. 367 for k in 0..<(w * h * ch) {
  368. 368● let bin = frames[i].data[k].to_int()
  369. 369 curr_hist[bin] = curr_hist[bin] + 1.0 / total
  370. 370 }
  371. 371 let data : Array[Byte] = []
  372. 372 if i == 0 {
  373. 373● for k in 0..<(w * h * ch) {
  374. 374● data.push(frames[i].data[k])
  375. 375 }
  376. 376 } else {
  377. 377● for k in 0..<(w * h * ch) {
  378. 378● let bin = frames[i].data[k].to_int()
  379. 379 let target = prev_hist[bin] * (1.0 - config.strength) +
  380. 380 curr_hist[bin] * config.strength
  381. 381 let curr = curr_hist[bin]
  382. 382◐ let ratio = if curr > 0.0 { target / curr } else { 1.0 }
  383. 383● let val = frames[i].data[k].to_double() * ratio
  384. 384● data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte())
  385. 385 }
  386. 386 }
  387. 387 for b in 0..<256 {
  388. 388● prev_hist[b] = curr_hist[b]
  389. 389 }
  390. 390● result.push({
  391. 391 width: w,
  392. 392 height: h,
  393. 393 channels: ch,
  394. 394● data: Bytes::from_array(data),
  395. 395 })
  396. 396 }
  397. 397 result
  398. 398}
  399. 399
  400. 400///|
  401. 401/// 亮度去闪烁:归一化帧间亮度。
  402. 402/// 亮度去闪烁:归一化帧间亮度 Luminance de-flicker: normalize inter-frame luminance.
  403. 403pub fn deflicker_luminance(
  404. 404 frames : Array[@types.Image],
  405. 405 config : FlickerConfig,
  406. 406) -> Array[@types.Image] {
  407. 407● let n = frames.length()
  408. 408 if n == 0 {
  409. 409○ return []
  410. 410 }
  411. 411 let luminances : Array[Double] = []
  412. 412 for frame in frames {
  413. 413● luminances.push(compute_mean_luminance(frame))
  414. 414 }
  415. 415 let mut ref_lum = 0.0
  416. 416 for lum in luminances {
  417. 417● ref_lum = ref_lum + lum
  418. 418 }
  419. 419● ref_lum = ref_lum / n.to_double()
  420. 420 let result : Array[@types.Image] = []
  421. 421 for i in 0..<n {
  422. 422● let current_lum = luminances[i]
  423. 423◐ let ratio = if current_lum > 0.0 { ref_lum / current_lum } else { 1.0 }
  424. 424 let blended_ratio = 1.0 + (ratio - 1.0) * config.strength
  425. 425 let w = frames[i].width
  426. 426 let h = frames[i].height
  427. 427 let ch = frames[i].channels
  428. 428 let data : Array[Byte] = []
  429. 429 for k in 0..<(w * h * ch) {
  430. 430● let val = frames[i].data[k].to_double() * blended_ratio
  431. 431● data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte())
  432. 432 }
  433. 433● result.push({
  434. 434 width: w,
  435. 435 height: h,
  436. 436 channels: ch,
  437. 437● data: Bytes::from_array(data),
  438. 438 })
  439. 439 }
  440. 440 result
  441. 441}
  442. 442
  443. 443///|
  444. 444/// 视频去闪烁主流程。
  445. 445/// 视频去闪烁管线 Video de-flicker pipeline.
  446. 446pub fn video_deflicker(
  447. 447 frames : Array[@types.Image],
  448. 448 config : FlickerConfig,
  449. 449) -> Array[@types.Image] {
  450. 450● match config.flicker_method {
  451. 451● TemporalFlicker => deflicker_temporal(frames, config)
  452. 452○ HistogramFlicker => deflicker_histogram(frames, config)
  453. 453● LuminanceFlicker => deflicker_luminance(frames, config)
  454. 454 }
  455. 455}
  456. 456
  457. 457///|
  458. 458/// 去抖动:减少帧间像素抖动。
  459. 459/// 去微颤:减少帧间像素抖动 De-shimmer: reduce inter-frame pixel jitter.
  460. 460pub fn deshimmer(
  461. 461 frames : Array[@types.Image],
  462. 462 strength : Double,
  463. 463) -> Array[@types.Image] {
  464. 464● let n = frames.length()
  465. 465 if n < 2 {
  466. 466○ return frames
  467. 467 }
  468. 468 let result : Array[@types.Image] = []
  469. 469● result.push(frames[0])
  470. 470 for i in 1..<n {
  471. 471● let w = frames[i].width
  472. 472 let h = frames[i].height
  473. 473 let ch = frames[i].channels
  474. 474 let data : Array[Byte] = []
  475. 475 for k in 0..<(w * h * ch) {
  476. 476● let curr = frames[i].data[k].to_double()
  477. 477● let prev = frames[i - 1].data[k].to_double()
  478. 478 let val = curr * (1.0 - strength) + prev * strength
  479. 479● data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte())
  480. 480 }
  481. 481● result.push({
  482. 482 width: w,
  483. 483 height: h,
  484. 484 channels: ch,
  485. 485● data: Bytes::from_array(data),
  486. 486 })
  487. 487 }
  488. 488 result
  489. 489}
  490. 490
  491. 491///|
  492. 492/// 视频修复流水线:修复 + 去闪烁 + 去抖动。
  493. 493/// 视频修复管线:修复 + 去闪烁 + 去微颤 Video inpainting pipeline: inpaint + de-flicker + de-shimmer.
  494. 494pub fn inpaint_pipeline(
  495. 495 frames : Array[@types.Image],
  496. 496 mask : Array[Bool],
  497. 497 inpaint_config : VideoInpaintConfig,
  498. 498 flicker_config : FlickerConfig,
  499. 499 shimmer_strength : Double,
  500. 500) -> Array[@types.Image] {
  501. 501● let inpainted = video_inpaint(frames, mask, inpaint_config)
  502. 502● let deflickered = video_deflicker(inpainted, flicker_config)
  503. 503● deshimmer(deflickered, shimmer_strength)
  504. 504}