Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / measure / src / main / rust / tests / cv_core.rs

cv_core.rs119 lines · 4.2 KiBRaw
1//! Host tests for the per-frame CV core on synthetic frames: corner seeding,
2//! Lucas-Kanade tracking of a shifted pattern, and session behaviour.
3//! Essential-matrix recovery on synthetic correspondences is covered by the
4//! unit tests in `src/epipolar.rs`; corner quality by `src/corners.rs`.
5
6use measure_cv::CvSession;
7use vision_core::imgbuf::Gray;
8
9/// Smooth but richly textured frame, shifted by whole pixels.
10fn frame(w: usize, h: usize, shift_x: i32, shift_y: i32) -> Gray {
11 let mut g = Gray::new(w, h);
12 for y in 0..h {
13 for x in 0..w {
14 let sx = x as i32 - shift_x;
15 let sy = y as i32 - shift_y;
16 let v = ((sx as f32 * 0.31).sin() * (sy as f32 * 0.23).cos()
17 + ((sx + sy) as f32 * 0.13).sin()
18 + (sx as f32 * 0.07 - sy as f32 * 0.09).cos())
19 * 55.0
20 + 128.0;
21 g.px[y * w + x] = v.clamp(0.0, 255.0) as u8;
22 }
23 }
24 g
25}
26
27#[test]
28fn first_frame_seeds_points_and_reports_first() {
29 let mut s = CvSession::new(300.0, 300.0, 160.0, 120.0);
30 let flow = s.process_frame(&frame(320, 240, 0, 0));
31 assert!(flow.first_frame);
32 assert!(flow.pairs.is_empty());
33 assert!(flow.pose.is_none());
34 assert!(flow.point_count >= 20, "seeded {}", flow.point_count);
35}
36
37#[test]
38fn tracks_a_shifted_pattern() {
39 let mut s = CvSession::new(300.0, 300.0, 160.0, 120.0);
40 s.process_frame(&frame(320, 240, 0, 0));
41 let flow = s.process_frame(&frame(320, 240, 4, 2));
42 assert!(!flow.first_frame);
43 assert!(flow.pairs.len() >= 15, "tracked {}", flow.pairs.len());
44 // Mean displacement must match the synthetic shift.
45 let (mut mx, mut my) = (0.0, 0.0);
46 for p in &flow.pairs {
47 mx += p.curr.0 - p.prev.0;
48 my += p.curr.1 - p.prev.1;
49 }
50 mx /= flow.pairs.len() as f64;
51 my /= flow.pairs.len() as f64;
52 assert!((mx - 4.0).abs() < 0.75, "mean dx {mx}");
53 assert!((my - 2.0).abs() < 0.75, "mean dy {my}");
54 assert!(flow.point_count >= flow.pairs.len());
55}
56
57#[test]
58fn identical_frames_track_with_zero_motion() {
59 let mut s = CvSession::new(300.0, 300.0, 160.0, 120.0);
60 let f = frame(320, 240, 0, 0);
61 s.process_frame(&f);
62 let flow = s.process_frame(&f);
63 assert!(flow.pairs.len() >= 15);
64 for p in &flow.pairs {
65 assert!((p.curr.0 - p.prev.0).abs() < 0.5);
66 assert!((p.curr.1 - p.prev.1).abs() < 0.5);
67 }
68}
69
70#[test]
71fn flat_frames_never_track() {
72 let mut g = Gray::new(320, 240);
73 for v in g.px.iter_mut() {
74 *v = 77;
75 }
76 let mut s = CvSession::new(300.0, 300.0, 160.0, 120.0);
77 let first = s.process_frame(&g);
78 assert_eq!(first.point_count, 0);
79 let second = s.process_frame(&g);
80 assert!(second.pairs.is_empty());
81 assert!(second.pose.is_none());
82}
83
84#[test]
85fn pair_ids_are_stable_across_frames() {
86 // Keyframe triangulation matches keyframe<->current points by id, so
87 // an id must name the same feature for its whole tracked life and
88 // must not repeat within one frame.
89 let mut s = CvSession::new(300.0, 300.0, 160.0, 120.0);
90 s.process_frame(&frame(320, 240, 0, 0));
91 let f2 = s.process_frame(&frame(320, 240, 3, 1));
92 let f3 = s.process_frame(&frame(320, 240, 6, 2));
93 assert!(f2.pairs.len() >= 15 && f3.pairs.len() >= 15);
94 for flow in [&f2, &f3] {
95 let mut ids: Vec<u64> = flow.pairs.iter().map(|p| p.id).collect();
96 ids.sort_unstable();
97 ids.dedup();
98 assert_eq!(ids.len(), flow.pairs.len(), "duplicate id in one frame");
99 }
100 let persisted = f3.pairs.iter().filter(|p| f2.pairs.iter().any(|q| q.id == p.id)).count();
101 assert!(persisted >= 10, "expected most features to persist with stable ids, got {persisted}");
102}
103
104#[test]
105fn reset_returns_to_first_frame() {
106 let mut s = CvSession::new(300.0, 300.0, 160.0, 120.0);
107 s.process_frame(&frame(320, 240, 0, 0));
108 s.reset();
109 let flow = s.process_frame(&frame(320, 240, 1, 0));
110 assert!(flow.first_frame);
111}
112
113#[test]
114fn a_size_change_restarts_the_session() {
115 let mut s = CvSession::new(300.0, 300.0, 160.0, 120.0);
116 s.process_frame(&frame(320, 240, 0, 0));
117 let flow = s.process_frame(&frame(160, 120, 0, 0));
118 assert!(flow.first_frame, "size change must reseed, not track across sizes");
119}