image.vision\video\motion_estimation.mbt

88.2% · 82 / 93 points · 472 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/motion_estimation
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 运动估计 — 块匹配/亚像素/分数像素
  7. 7// Motion estimation: block matching / subpixel / fractional
  8. 8//
  9. 9// Phase 17 v395: 视频处理大扩展 — 运动估计。
  10. 10
  11. 11///|
  12. 12/// 运动估计方法。
  13. 13/// 运动估计方法 Motion estimation method.
  14. 14/// `#non_exhaustive` 表示未来可能添加新构造器
  15. 15#derive(Eq, @debug.Debug)
  16. 16#non_exhaustive
  17. 17pub(all) enum MotionEstimationMethod {
  18. 18 FullSearch // 全搜索
  19. 19 DiamondSearch // 菱形搜索
  20. 20 HexSearch // 六边形搜索
  21. 21 ThreeStep // 三步搜索
  22. 22}
  23. 23
  24. 24///|
  25. 25pub extend MotionEstimationMethod with @debug.Debug::{to_repr}
  26. 26
  27. 27///|
  28. 28pub extend MotionEstimationMethod with Eq::{not_equal, equal}
  29. 29
  30. 30///|
  31. 31/// 运动矢量。
  32. 32/// 运动向量 Motion vector.
  33. 33#derive(Eq, @debug.Debug)
  34. 34pub(all) struct MotionVector {
  35. 35 dx : Int32
  36. 36 dy : Int32
  37. 37 sad : Double // 绝对误差和
  38. 38}
  39. 39
  40. 40///|
  41. 41pub extend MotionVector with @debug.Debug::{to_repr}
  42. 42
  43. 43///|
  44. 44pub extend MotionVector with Eq::{not_equal, equal}
  45. 45
  46. 46///|
  47. 47/// 亚像素运动矢量。
  48. 48/// 亚像素运动向量 Subpixel motion vector.
  49. 49#derive(Eq, @debug.Debug)
  50. 50pub(all) struct SubpixelMotionVector {
  51. 51 dx : Double
  52. 52 dy : Double
  53. 53 sad : Double
  54. 54}
  55. 55
  56. 56///|
  57. 57pub extend SubpixelMotionVector with @debug.Debug::{to_repr}
  58. 58
  59. 59///|
  60. 60pub extend SubpixelMotionVector with Eq::{not_equal, equal}
  61. 61
  62. 62///|
  63. 63/// 搜索配置。
  64. 64/// 搜索配置 Search configuration.
  65. 65#derive(Eq, @debug.Debug)
  66. 66pub(all) struct SearchConfig {
  67. 67 me_method : MotionEstimationMethod
  68. 68 block_size : Int32
  69. 69 search_range : Int32
  70. 70 subpixel_refinement : Bool // 是否启用亚像素精化
  71. 71}
  72. 72
  73. 73///|
  74. 74pub extend SearchConfig with @debug.Debug::{to_repr}
  75. 75
  76. 76///|
  77. 77pub extend SearchConfig with Eq::{not_equal, equal}
  78. 78
  79. 79///|
  80. 80/// 默认搜索配置。
  81. 81/// 默认搜索配置 Default search configuration.
  82. 82pub fn default_search_config() -> SearchConfig {
  83. 83● {
  84. 84 me_method: DiamondSearch,
  85. 85 block_size: 8,
  86. 86 search_range: 16,
  87. 87 subpixel_refinement: true,
  88. 88 }
  89. 89}
  90. 90
  91. 91///|
  92. 92/// 计算块的 SAD(绝对误差和)。
  93. 93/// 计算块 SAD(绝对差值和) Compute SAD (Sum of Absolute Differences) for a block.
  94. 94fn compute_sad(
  95. 95 current : @types.Image,
  96. 96 reference : @types.Image,
  97. 97 bx : Int32,
  98. 98 by : Int32,
  99. 99 dx : Int32,
  100. 100 dy : Int32,
  101. 101 block_size : Int32,
  102. 102) -> Double {
  103. 103● let w = current.width
  104. 104 let h = current.height
  105. 105 let ch = current.channels
  106. 106 let mut sad = 0.0
  107. 107 for py in 0..<block_size {
  108. 108● for px in 0..<block_size {
  109. 109● let cx = bx + px
  110. 110 let cy = by + py
  111. 111 let rx = cx + dx
  112. 112 let ry = cy + dy
  113. 113 if cx >= w || cy >= h || rx < 0 || rx >= w || ry < 0 || ry >= h {
  114. 114● sad = sad + 255.0
  115. 115 continue
  116. 116 }
  117. 117 for c in 0..<ch {
  118. 118● let ci = (cy * w + cx) * ch + c
  119. 119 let ri = (ry * w + rx) * ch + c
  120. 120● let diff = current.data[ci].to_int() - reference.data[ri].to_int()
  121. 121● sad = sad + diff.to_double().abs()
  122. 122 }
  123. 123 }
  124. 124 }
  125. 125 sad
  126. 126}
  127. 127
  128. 128///|
  129. 129/// 全搜索运动估计。
  130. 130/// 全搜索运动估计 Full search motion estimation.
  131. 131pub fn estimate_motion_full_search(
  132. 132 current : @types.Image,
  133. 133 reference : @types.Image,
  134. 134 bx : Int32,
  135. 135 by : Int32,
  136. 136 block_size : Int32,
  137. 137 search_range : Int32,
  138. 138) -> MotionVector {
  139. 139● let mut best : MotionVector = { dx: 0, dy: 0, sad: 999999999.0, }
  140. 140 for dy in [-search_range, 0, search_range] {
  141. 141● for dx in [-search_range, 0, search_range] {
  142. 142● let sad = compute_sad(current, reference, bx, by, dx, dy, block_size)
  143. 143 if sad < best.sad {
  144. 144● best = ({ dx, dy, sad, } : MotionVector)
  145. 145 }
  146. 146 }
  147. 147 }
  148. 148 best
  149. 149}
  150. 150
  151. 151///|
  152. 152/// 菱形搜索运动估计。
  153. 153/// 菱形搜索运动估计 Diamond search motion estimation.
  154. 154pub fn estimate_motion_diamond(
  155. 155 current : @types.Image,
  156. 156 reference : @types.Image,
  157. 157 bx : Int32,
  158. 158 by : Int32,
  159. 159 block_size : Int32,
  160. 160 search_range : Int32,
  161. 161) -> MotionVector {
  162. 162 // 大菱形搜索模式: (0,±1), (±1,0)
  163. 163● let mut best_dx = 0
  164. 164 let mut best_dy = 0
  165. 165● let mut best_sad = compute_sad(current, reference, bx, by, 0, 0, block_size)
  166. 166 let mut step = 1
  167. 167 while step <= search_range {
  168. 168● let mut new_dx = best_dx
  169. 169 let mut new_dy = best_dy
  170. 170 let mut new_sad = best_sad
  171. 171 for (dx, dy) in [(0, step), (0, -step), (step, 0), (-step, 0)] {
  172. 172● let cand_dx = best_dx + dx
  173. 173 let cand_dy = best_dy + dy
  174. 174● let sad = compute_sad(
  175. 175 current, reference, bx, by, cand_dx, cand_dy, block_size,
  176. 176 )
  177. 177 if sad < new_sad {
  178. 178○ new_dx = cand_dx
  179. 179 new_dy = cand_dy
  180. 180 new_sad = sad
  181. 181 }
  182. 182 }
  183. 183 if new_sad < best_sad {
  184. 184○ best_dx = new_dx
  185. 185 best_dy = new_dy
  186. 186 best_sad = new_sad
  187. 187 } else {
  188. 188● step = step * 2
  189. 189 }
  190. 190 }
  191. 191 { dx: best_dx, dy: best_dy, sad: best_sad, }
  192. 192}
  193. 193
  194. 194///|
  195. 195/// 六边形搜索运动估计。
  196. 196/// 六角搜索运动估计 Hexagonal search motion estimation.
  197. 197pub fn estimate_motion_hex(
  198. 198 current : @types.Image,
  199. 199 reference : @types.Image,
  200. 200 bx : Int32,
  201. 201 by : Int32,
  202. 202 block_size : Int32,
  203. 203 search_range : Int32,
  204. 204) -> MotionVector {
  205. 205 // 六边形搜索: 6个点
  206. 206● let mut best_dx = 0
  207. 207 let mut best_dy = 0
  208. 208● let mut best_sad = compute_sad(current, reference, bx, by, 0, 0, block_size)
  209. 209● let step = (search_range / 2).clamp(min=1, max=search_range)
  210. 210 for
  211. 211 (dx, dy) in [
  212. 212 (step, 0),
  213. 213 (-step, 0),
  214. 214 (step / 2, step),
  215. 215 (-step / 2, step),
  216. 216 (step / 2, -step),
  217. 217 (-step / 2, -step),
  218. 218 ] {
  219. 219● let sad = compute_sad(current, reference, bx, by, dx, dy, block_size)
  220. 220 if sad < best_sad {
  221. 221○ best_dx = dx
  222. 222 best_dy = dy
  223. 223 best_sad = sad
  224. 224 }
  225. 225 }
  226. 226 { dx: best_dx, dy: best_dy, sad: best_sad, }
  227. 227}
  228. 228
  229. 229///|
  230. 230/// 三步搜索运动估计。
  231. 231/// 三步搜索 Three-step search
  232. 232pub fn estimate_motion_three_step(
  233. 233 current : @types.Image,
  234. 234 reference : @types.Image,
  235. 235 bx : Int32,
  236. 236 by : Int32,
  237. 237 block_size : Int32,
  238. 238 search_range : Int32,
  239. 239) -> MotionVector {
  240. 240● let mut best_dx = 0
  241. 241 let mut best_dy = 0
  242. 242● let mut best_sad = compute_sad(current, reference, bx, by, 0, 0, block_size)
  243. 243 let mut step = search_range
  244. 244 while step >= 1 {
  245. 245● for
  246. 246 (dx, dy) in [
  247. 247 (0, 0),
  248. 248 (step, 0),
  249. 249 (-step, 0),
  250. 250 (0, step),
  251. 251 (0, -step),
  252. 252 (step, step),
  253. 253 (step, -step),
  254. 254 (-step, step),
  255. 255 (-step, -step),
  256. 256 ] {
  257. 257● let cand_dx = best_dx + dx
  258. 258 let cand_dy = best_dy + dy
  259. 259● let sad = compute_sad(
  260. 260 current, reference, bx, by, cand_dx, cand_dy, block_size,
  261. 261 )
  262. 262 if sad < best_sad {
  263. 263○ best_dx = cand_dx
  264. 264 best_dy = cand_dy
  265. 265 best_sad = sad
  266. 266 }
  267. 267 }
  268. 268 step = step / 2
  269. 269 }
  270. 270 { dx: best_dx, dy: best_dy, sad: best_sad, }
  271. 271}
  272. 272
  273. 273///|
  274. 274/// 亚像素精化(半像素)。
  275. 275/// 亚像素精化(半像素) Subpixel refinement (half-pixel).
  276. 276pub fn refine_subpixel(
  277. 277 current : @types.Image,
  278. 278 reference : @types.Image,
  279. 279 bx : Int32,
  280. 280 by : Int32,
  281. 281 mv : MotionVector,
  282. 282 block_size : Int32,
  283. 283) -> SubpixelMotionVector {
  284. 284 // 在整像素最优解周围搜索半像素位置
  285. 285● let mut best : SubpixelMotionVector = {
  286. 286● dx: mv.dx.to_double(),
  287. 287● dy: mv.dy.to_double(),
  288. 288 sad: mv.sad,
  289. 289 }
  290. 290 for frac_dx in [-0.5, 0.0, 0.5] {
  291. 291● for frac_dy in [-0.5, 0.0, 0.5] {
  292. 292● let cand_dx = mv.dx.to_double() + frac_dx
  293. 293● let cand_dy = mv.dy.to_double() + frac_dy
  294. 294 // 简化:用插值 SAD
  295. 295● let ix = cand_dx.to_int()
  296. 296● let iy = cand_dy.to_int()
  297. 297● let fx = cand_dx - ix.to_double()
  298. 298● let fy = cand_dy - iy.to_double()
  299. 299● let sad0 = compute_sad(current, reference, bx, by, ix, iy, block_size)
  300. 300● let sad1 = compute_sad(current, reference, bx, by, ix + 1, iy, block_size)
  301. 301● let sad2 = compute_sad(current, reference, bx, by, ix, iy + 1, block_size)
  302. 302● let sad3 = compute_sad(
  303. 303 current,
  304. 304 reference,
  305. 305 bx,
  306. 306 by,
  307. 307 ix + 1,
  308. 308 iy + 1,
  309. 309 block_size,
  310. 310 )
  311. 311 let interpolated_sad = sad0 * (1.0 - fx) * (1.0 - fy) +
  312. 312 sad1 * fx * (1.0 - fy) +
  313. 313 sad2 * (1.0 - fx) * fy +
  314. 314 sad3 * fx * fy
  315. 315 if interpolated_sad < best.sad {
  316. 316● best = (
  317. 317 { dx: cand_dx, dy: cand_dy, sad: interpolated_sad, } :
  318. 318 SubpixelMotionVector)
  319. 319 }
  320. 320 }
  321. 321 }
  322. 322 best
  323. 323}
  324. 324
  325. 325///|
  326. 326/// 计算运动场。
  327. 327/// 计算整图运动场 Compute motion field for entire image.
  328. 328pub fn compute_motion_field(
  329. 329 current : @types.Image,
  330. 330 reference : @types.Image,
  331. 331 config : SearchConfig,
  332. 332) -> Array[MotionVector] {
  333. 333● let field : Array[MotionVector] = []
  334. 334 let block = config.block_size
  335. 335 let mut by = 0
  336. 336 while by < current.height {
  337. 337● let mut bx = 0
  338. 338 while bx < current.width {
  339. 339● let mv = match config.me_method {
  340. 340 FullSearch =>
  341. 341○ estimate_motion_full_search(
  342. 342 current,
  343. 343 reference,
  344. 344 bx,
  345. 345 by,
  346. 346 block,
  347. 347 config.search_range,
  348. 348 )
  349. 349 DiamondSearch =>
  350. 350● estimate_motion_diamond(
  351. 351 current,
  352. 352 reference,
  353. 353 bx,
  354. 354 by,
  355. 355 block,
  356. 356 config.search_range,
  357. 357 )
  358. 358 HexSearch =>
  359. 359○ estimate_motion_hex(
  360. 360 current,
  361. 361 reference,
  362. 362 bx,
  363. 363 by,
  364. 364 block,
  365. 365 config.search_range,
  366. 366 )
  367. 367 ThreeStep =>
  368. 368○ estimate_motion_three_step(
  369. 369 current,
  370. 370 reference,
  371. 371 bx,
  372. 372 by,
  373. 373 block,
  374. 374 config.search_range,
  375. 375 )
  376. 376 }
  377. 377● field.push(mv)
  378. 378 bx = bx + block
  379. 379 }
  380. 380 by = by + block
  381. 381 }
  382. 382 field
  383. 383}
  384. 384
  385. 385///|
  386. 386/// 运动补偿。
  387. 387/// 运动补偿 Motion compensation.
  388. 388pub fn motion_compensation(
  389. 389 reference : @types.Image,
  390. 390 motion_field : Array[MotionVector],
  391. 391 block_size : Int32,
  392. 392) -> @types.Image {
  393. 393● let w = reference.width
  394. 394 let h = reference.height
  395. 395 let ch = reference.channels
  396. 396 let data : Array[Byte] = []
  397. 397 let mut mv_idx = 0
  398. 398 let mut by = 0
  399. 399 while by < h {
  400. 400● let mut bx = 0
  401. 401 while bx < w {
  402. 402● let mv = if mv_idx < motion_field.length() {
  403. 403● motion_field[mv_idx]
  404. 404 } else {
  405. 405○ { dx: 0, dy: 0, sad: 0.0, }
  406. 406 }
  407. 407 mv_idx = mv_idx + 1
  408. 408 for py in 0..<block_size {
  409. 409● for px in 0..<block_size {
  410. 410● let cx = bx + px
  411. 411 let cy = by + py
  412. 412 if cx >= w || cy >= h {
  413. 413○ for _ in 0..<ch {
  414. 414○ data.push((0 : Byte))
  415. 415 }
  416. 416 continue
  417. 417 }
  418. 418● let rx = (cx + mv.dx).clamp(min=0, max=w - 1)
  419. 419● let ry = (cy + mv.dy).clamp(min=0, max=h - 1)
  420. 420 for c in 0..<ch {
  421. 421● data.push(reference.data[(ry * w + rx) * ch + c])
  422. 422 }
  423. 423 }
  424. 424 }
  425. 425 bx = bx + block_size
  426. 426 }
  427. 427 by = by + block_size
  428. 428 }
  429. 429● { width: w, height: h, channels: ch, data: Bytes::from_array(data), }
  430. 430}
  431. 431
  432. 432///|
  433. 433/// 估计运动并补偿。
  434. 434/// 估计运动并补偿 Estimate motion and compensate.
  435. 435pub fn motion_estimate_and_compensate(
  436. 436 current : @types.Image,
  437. 437 reference : @types.Image,
  438. 438 config : SearchConfig,
  439. 439) -> @types.Image {
  440. 440● let field = compute_motion_field(current, reference, config)
  441. 441● motion_compensation(reference, field, config.block_size)
  442. 442}
  443. 443
  444. 444///|
  445. 445/// 计算运动场平均 SAD。
  446. 446/// 计算运动场平均 SAD Compute average SAD of motion field.
  447. 447pub fn average_motion_sad(motion_field : Array[MotionVector]) -> Double {
  448. 448● if motion_field.length() == 0 {
  449. 449● return 0.0
  450. 450 }
  451. 451 let mut total = 0.0
  452. 452 for mv in motion_field {
  453. 453● total = total + mv.sad
  454. 454 }
  455. 455● total / motion_field.length().to_double()
  456. 456}
  457. 457
  458. 458///|
  459. 459/// 计算运动矢量零率。
  460. 460/// 计算零运动向量占比 Compute fraction of zero motion vectors.
  461. 461pub fn zero_motion_ratio(motion_field : Array[MotionVector]) -> Double {
  462. 462● if motion_field.length() == 0 {
  463. 463● return 0.0
  464. 464 }
  465. 465 let mut zero_count = 0
  466. 466 for mv in motion_field {
  467. 467● if mv.dx == 0 && mv.dy == 0 {
  468. 468● zero_count = zero_count + 1
  469. 469 }
  470. 470 }
  471. 471● zero_count.to_double() / motion_field.length().to_double()
  472. 472}