edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits
| 1 | //! Motion tracking: a region of the picture followed from frame to frame, its centre |
| 2 | //! written out as a path that a mask (or anything else with a position) can follow. |
| 3 | //! |
| 4 | //! The pipeline, per clip, over the source frames from the start frame to the end of |
| 5 | //! the range: |
| 6 | //! |
| 7 | //! 1. **Reduce.** Each decoded frame is box-averaged to luma no wider than |
| 8 | //! [`TRACK_W`], so the search and the template are sized in these small pixels. |
| 9 | //! 2. **Match.** The template is the region's own luma at the start frame (its |
| 10 | //! centre, at most [`MAX_TEMPLATE`] pixels a side). Each frame, the template is |
| 11 | //! searched for by normalised cross-correlation in a window of [`SEARCH`] pixels |
| 12 | //! around its last place, and the peak is refined to a sub-pixel position by a |
| 13 | //! parabola through the three scores on each axis. |
| 14 | //! 3. **Learn.** After a match the template takes in [`LEARN`] of the matched |
| 15 | //! picture, so it follows slow change in the feature, yet keeps its identity. |
| 16 | //! 4. **Stop.** When the best score falls below [`MIN_CONFIDENCE`] the feature is |
| 17 | //! lost; the track ends there and says where. |
| 18 | //! |
| 19 | //! The matcher ([`Tracker`]) is a pure function over pictures, so it tests without a |
| 20 | //! decode. [`start`] runs `analyse` on a thread of its own, polled by [`status`] and |
| 21 | //! [`take`] and stopped by [`cancel`], the same way stabilisation runs. A backward |
| 22 | //! track decodes its range first, since it walks the frames in reverse. |
| 23 | |
| 24 | use HashMap; |
| 25 | use Path; |
| 26 | use ; |
| 27 | use ; |
| 28 | |
| 29 | use crate; |
| 30 | use crate; |
| 31 | |
| 32 | /// The width pictures are reduced to before they are matched. |
| 33 | pub const TRACK_W: usize = 320; |
| 34 | /// Below this normalised cross-correlation the feature is lost. |
| 35 | pub const MIN_CONFIDENCE: f32 = 0.6; |
| 36 | /// How far, in tracked pixels, the template may move between two frames. |
| 37 | const SEARCH: i64 = 24; |
| 38 | /// The largest template edge in tracked pixels; a bigger region is followed by its centre. |
| 39 | const MAX_TEMPLATE: usize = 48; |
| 40 | /// The smallest template edge in tracked pixels: a small box has too little texture to lock onto. |
| 41 | const MIN_TEMPLATE: usize = 24; |
| 42 | /// Below this standard deviation of luma, a template is flat or nearly so and has nothing to lock onto. |
| 43 | const MIN_TEXTURE: f32 = 8.0; |
| 44 | /// Matches within this many tracked pixels of the best one are the same match, not a rival. |
| 45 | const RIVAL_RADIUS: i64 = 3; |
| 46 | /// A best match this close to the best rival outside [`RIVAL_RADIUS`] is ambiguous (a |
| 47 | /// line, a repeat pattern): the feature cannot be told from its surroundings, so it is lost. |
| 48 | const MAX_RIVAL_RATIO: f32 = 0.9; |
| 49 | /// The share of each matched picture that is learned into the template. |
| 50 | const LEARN: f32 = 0.05; |
| 51 | /// Most pictures a backward track holds in memory (it decodes its range first). |
| 52 | pub const MAX_BACKWARD: usize = 2400; |
| 53 | /// The most a dropped key may stray, in canvas fractions, from the path it stands for. |
| 54 | pub const KEY_TOLERANCE: f32 = 0.002; |
| 55 | |
| 56 | /// A picture reduced to luma for tracking: `px` is `w * h` values, row by row. |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | /// A region of the picture in canvas fractions, stated the way a rect mask states |
| 65 | /// itself: its centre and its size, `0..=1` each. |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | /// The luma of a full picture box-averaged down to [`TRACK_W`] wide (a narrower one |
| 75 | /// is kept as it is). |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | /// Follows one template through successive pictures. |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | /// The `w` by `h` luma patch at `(x, y)`, or `None` when it leaves the picture. |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | /// The sub-pixel offset of a peak from its neighbours' scores, within half a pixel. |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | /// The clip's placement as the renderer puts it on the canvas: the base transform and its |
| 222 | /// keys (at source frames), the canvas size and the fit policy. A track matches the source |
| 223 | /// picture and reports its path on the canvas, so each point goes through this frame's |
| 224 | /// placement. |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | /// The box `region` (canvas fractions) covers, in picture fractions of a `src` picture |
| 241 | /// placed under `t`: the bounding box of its four corners mapped back. |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | /// The path of one track: `(frame, x, y)` in canvas fractions, in the order it was |
| 252 | /// followed (ascending for a forward track, descending for a backward one), and the |
| 253 | /// frame it was lost at when it stopped early. |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | /// Steps a [`Tracker`] through pictures, numbering the points it makes. |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | /// Tracks `region` from frame `start` through the frames `lo..hi` of `path`, forward |
| 308 | /// (`start` to `hi`) or backward (`lo` to `start`). `progress` is set in thousandths of |
| 309 | /// the range done; a set `cancel` stops it with an error. |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | /// Reduces a track to keys: the points kept by Douglas-Peucker simplification, so that |
| 376 | /// every dropped point lies within `tolerance` of the straight line between the kept |
| 377 | /// ones. `points` must be in ascending frame order. |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | // ---- background jobs -------------------------------------------------------- |
| 409 | |
| 410 | /// A running track, by its id. |
| 411 | pub type JobId = u64; |
| 412 | |
| 413 | /// Where a track stands. |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | /// Starts tracking `region` from frame `start` in the background; see `analyse`. |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | /// Where the track `id` stands. |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | /// The outcome of the finished track `id`, which is then forgotten; `None` while it |
| 466 | /// runs or when it is gone. |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | /// Stops the running track `id`. `false` when it is not running. |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 |