eventcv-core 1.0.8

Rust core of EventCV — OpenCV for event-based vision.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
308
309
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
376
377
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
409
//! Spatial (geometric) transforms on the event stream. Coordinates are remapped and rounded;
//! out-of-bounds events drop and the sensor size is recomputed per op.

use crate::camera::Camera;
use crate::EventStream;

/// Maps every coordinate of one axis from a `from`-wide grid onto a `to`-wide one: `floor(c * to / from)`.
///
/// `c < 2^16` and `to` is a sensor dimension, so `c * to` fits comfortably in `u64`; the result is
/// below `to` whenever `c < from`, which `EventStream` guarantees for its own coordinates.
fn rebin_axis(coords: &mut [u16], from: usize, to: usize) {
    if from == 0 || to == 0 || from == to {
        return;
    }
    if from.is_multiple_of(to) {
        let factor = (from / to) as u16;
        if factor.is_power_of_two() {
            let shift = factor.trailing_zeros();
            for c in coords {
                *c >>= shift;
            }
        } else {
            for c in coords {
                *c /= factor;
            }
        }
    } else {
        let (to, from) = (to as u64, from as u64);
        for c in coords {
            *c = (u64::from(*c) * to / from) as u16;
        }
    }
}

impl EventStream {
    /// Keeps events inside the `w`×`h` window at `(x0, y0)` and shifts them to a new origin.
    /// The result is a `w`×`h` stream.
    pub fn crop(&self, x0: i64, y0: i64, w: usize, h: usize) -> EventStream {
        self.remap(w, h, |x, y, t, p| Some((x - x0, y - y0, t, p)))
    }

    /// Mirrors horizontally (`x → width-1-x`). Sensor size unchanged.
    pub fn flip_x(&self) -> EventStream {
        let (width, height) = self.sensor_size();
        let max_x = width.saturating_sub(1) as u16;
        self.map_columns(width, height, |out| {
            for x in &mut out.xs {
                *x = max_x - *x;
            }
        })
    }

    /// Mirrors vertically (`y → height-1-y`). Sensor size unchanged.
    pub fn flip_y(&self) -> EventStream {
        let (width, height) = self.sensor_size();
        let max_y = height.saturating_sub(1) as u16;
        self.map_columns(width, height, |out| {
            for y in &mut out.ys {
                *y = max_y - *y;
            }
        })
    }

    /// Rotates by `k * 90°` clockwise. `k` is taken mod 4; quarter turns swap the sensor dims.
    ///
    /// A quarter turn swaps the two coordinate columns before rewriting one of them, so it copies
    /// one column rather than four.
    pub fn rotate90(&self, k: i32) -> EventStream {
        let (width, height) = self.sensor_size();
        let (max_x, max_y) = (
            width.saturating_sub(1) as u16,
            height.saturating_sub(1) as u16,
        );
        match k.rem_euclid(4) {
            0 => self.map_columns(width, height, |_| {}),
            1 => self.map_columns(height, width, |out| {
                std::mem::swap(&mut out.xs, &mut out.ys);
                for x in &mut out.xs {
                    *x = max_y - *x;
                }
            }),
            2 => self.map_columns(width, height, |out| {
                for x in &mut out.xs {
                    *x = max_x - *x;
                }
                for y in &mut out.ys {
                    *y = max_y - *y;
                }
            }),
            _ => self.map_columns(height, width, |out| {
                std::mem::swap(&mut out.xs, &mut out.ys);
                for y in &mut out.ys {
                    *y = max_x - *y;
                }
            }),
        }
    }

    /// Reflects across the main diagonal (`(x, y) → (y, x)`); swaps the sensor dims.
    pub fn transpose(&self) -> EventStream {
        let (width, height) = self.sensor_size();
        self.map_columns(height, width, |out| {
            std::mem::swap(&mut out.xs, &mut out.ys)
        })
    }

    /// Translates by `(dx, dy)`; events shifted off the sensor are dropped. Sensor unchanged.
    pub fn translate(&self, dx: i64, dy: i64) -> EventStream {
        let (width, height) = self.sensor_size();
        self.remap(width, height, |x, y, t, p| Some((x + dx, y + dy, t, p)))
    }

    /// Resizes the sensor grid to `w`×`h`, rebinning each coordinate proportionally (floored —
    /// the destination bin, no interpolation). Every event maps into `[0, w)×[0, h)`, so the
    /// count is conserved; on downscale several events may share a pixel (lossless).
    ///
    /// The bin is `floor(x * w / width)` computed in integers: exact, and no `floor` call per
    /// coordinate (on a baseline x86-64 build `f64::floor` is a libm call, which made this the
    /// most expensive step of a decode → downsample → histogram pipeline). An integer factor —
    /// the common `1280x720 → 640x360` — collapses to a shift or a division.
    pub fn resize(&self, w: usize, h: usize) -> EventStream {
        let (width, height) = self.sensor_size();
        self.map_columns(w, h, |out| {
            rebin_axis(&mut out.xs, width, w);
            rebin_axis(&mut out.ys, height, h);
        })
    }

    /// Scales the sensor by `(sx, sy)`, rounding the new dimensions. See [`Self::resize`].
    pub fn scale(&self, sx: f64, sy: f64) -> EventStream {
        let (width, height) = self.sensor_size();
        let w = (width as f64 * sx).round().max(0.0) as usize;
        let h = (height as f64 * sy).round().max(0.0) as usize;
        self.resize(w, h)
    }

    /// Applies a 2×3 affine matrix `[[a,b,c],[d,e,f]]` (`x' = a·x+b·y+c`, rounded). Sensor
    /// size unchanged; events warped off the sensor are dropped.
    pub fn warp_affine(&self, m: [[f64; 3]; 2]) -> EventStream {
        let (width, height) = self.sensor_size();
        self.remap(width, height, |x, y, t, p| {
            let (xf, yf) = (x as f64, y as f64);
            let nx = m[0][0] * xf + m[0][1] * yf + m[0][2];
            let ny = m[1][0] * xf + m[1][1] * yf + m[1][2];
            Some((nx.round() as i64, ny.round() as i64, t, p))
        })
    }

    /// Applies a 3×3 perspective (homography) matrix, dividing by the homogeneous coordinate.
    /// Events whose denominator is zero, or that warp off the sensor, are dropped.
    pub fn warp_perspective(&self, m: [[f64; 3]; 3]) -> EventStream {
        let (width, height) = self.sensor_size();
        self.remap(width, height, |x, y, t, p| {
            let (xf, yf) = (x as f64, y as f64);
            let w = m[2][0] * xf + m[2][1] * yf + m[2][2];
            if w == 0.0 {
                return None;
            }
            let nx = (m[0][0] * xf + m[0][1] * yf + m[0][2]) / w;
            let ny = (m[1][0] * xf + m[1][1] * yf + m[1][2]) / w;
            Some((nx.round() as i64, ny.round() as i64, t, p))
        })
    }

    /// Rectifies events with a [`Camera`]'s intrinsics + distortion, mapping each event from its
    /// distorted pixel to the undistorted location on the same grid. Builds a per-pixel lookup
    /// once (the sensor grid is small), then remaps every event through it; events landing off
    /// the sensor after rectification are dropped. Sensor size unchanged.
    pub fn undistort(&self, camera: &Camera) -> EventStream {
        let (width, height) = self.sensor_size();
        if width == 0 || height == 0 {
            return self.clone();
        }
        let lut: Vec<(i64, i64)> = (0..width * height)
            .map(|i| {
                let (u, v) = camera.undistort_point((i % width) as f64, (i / width) as f64);
                (u.round() as i64, v.round() as i64)
            })
            .collect();
        self.remap(width, height, |x, y, t, p| {
            let (nx, ny) = lut[y as usize * width + x as usize];
            Some((nx, ny, t, p))
        })
    }

    /// Keeps only events where the `mask_w`×`mask_h` row-major boolean grid is `true`. Events
    /// outside the mask are dropped. Sensor size unchanged.
    pub fn mask(&self, mask: &[bool], mask_w: usize, mask_h: usize) -> EventStream {
        let (width, height) = self.sensor_size();
        self.remap(width, height, |x, y, t, p| {
            let (ux, uy) = (x as usize, y as usize);
            let keep = ux < mask_w && uy < mask_h && mask[uy * mask_w + ux];
            keep.then_some((x, y, t, p))
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::{EventStream, EventStreamBuilder};

    /// A 4×3 stream with one event per column on a diagonal-ish path.
    fn sample() -> EventStream {
        let mut builder = EventStreamBuilder::new(4, 3, 0.001);
        builder.push(0, 0, 10, true);
        builder.push(1, 1, 20, false);
        builder.push(2, 2, 30, true);
        builder.push(3, 0, 40, false);
        builder.build()
    }

    fn coords(stream: &EventStream) -> Vec<(u16, u16)> {
        stream
            .xs()
            .iter()
            .copied()
            .zip(stream.ys().iter().copied())
            .collect()
    }

    #[test]
    fn crop_subsets_and_shifts_to_new_origin() {
        let cropped = sample().crop(1, 1, 2, 2);
        assert_eq!(cropped.sensor_size(), (2, 2));
        assert_eq!(coords(&cropped), vec![(0, 0), (1, 1)]); // events (1,1) and (2,2)
        assert_eq!(cropped.ts(), &[20, 30]);
    }

    #[test]
    fn flips_are_their_own_inverse() {
        let s = sample();
        assert_eq!(coords(&s.flip_x().flip_x()), coords(&s));
        assert_eq!(coords(&s.flip_y().flip_y()), coords(&s));
        assert_eq!(s.flip_x().sensor_size(), (4, 3));
        assert_eq!(coords(&s.flip_x()), vec![(3, 0), (2, 1), (1, 2), (0, 0)]);
    }

    #[test]
    fn rotate90_swaps_dims_and_round_trips() {
        let s = sample();
        assert_eq!(s.rotate90(1).sensor_size(), (3, 4)); // W×H -> H×W
        assert_eq!(s.rotate90(2).sensor_size(), (4, 3));
        // Four quarter turns and a (1 then 3) pair both return to the original.
        assert_eq!(coords(&s.rotate90(4)), coords(&s));
        assert_eq!(coords(&s.rotate90(1).rotate90(3)), coords(&s));
        assert_eq!(coords(&s.rotate90(-1)), coords(&s.rotate90(3)));
    }

    #[test]
    fn transpose_swaps_axes_and_dims() {
        let t = sample().transpose();
        assert_eq!(t.sensor_size(), (3, 4));
        assert_eq!(coords(&t), vec![(0, 0), (1, 1), (2, 2), (0, 3)]);
    }

    #[test]
    fn translate_shifts_and_drops_out_of_bounds() {
        // x + 2 = [2, 3, 4, 5]; only x = 2, 3 stay on the 4-wide sensor (the rest fall off).
        let shifted = sample().translate(2, 0);
        assert_eq!(shifted.xs(), &[2, 3]);
        assert_eq!(shifted.ys(), &[0, 1]);
        // Translating the survivors back restores their original coordinates.
        assert_eq!(coords(&shifted.translate(-2, 0)), vec![(0, 0), (1, 1)]);
    }

    #[test]
    fn resize_rebins_and_conserves_count() {
        let down = sample().resize(2, 2); // 4x3 -> 2x2; floor keeps every event
        assert_eq!(down.sensor_size(), (2, 2));
        assert_eq!(down.len(), 4);
        assert!(down.xs().iter().all(|&x| x < 2) && down.ys().iter().all(|&y| y < 2));
        assert_eq!(sample().scale(2.0, 2.0).sensor_size(), (8, 6));
    }

    #[test]
    fn warp_affine_identity_and_translation() {
        let s = sample();
        let identity = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
        assert_eq!(coords(&s.warp_affine(identity)), coords(&s));
        let shift = [[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]];
        assert_eq!(coords(&s.warp_affine(shift)), coords(&s.translate(1, 0)));
    }

    #[test]
    fn warp_perspective_identity_round_trips() {
        let identity = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
        let s = sample();
        assert_eq!(coords(&s.warp_perspective(identity)), coords(&s));
    }

    #[test]
    fn mask_keeps_only_selected_pixels() {
        let s = sample(); // 4x3
        let mut mask = vec![false; 4 * 3];
        mask[4 + 1] = true; // keep only pixel (1,1): row 1 (×4) + col 1
        let masked = s.mask(&mask, 4, 3);
        assert_eq!(coords(&masked), vec![(1, 1)]);
    }

    #[test]
    fn undistort_without_distortion_keeps_events_in_place() {
        use crate::camera::Camera;
        let s = sample(); // 4×3
        let camera = Camera::new(100.0, 100.0, 2.0, 1.5); // no distortion -> identity map
        assert_eq!(coords(&s.undistort(&camera)), coords(&s));
        assert_eq!(s.undistort(&camera).sensor_size(), (4, 3));
    }

    #[test]
    fn undistort_remaps_under_distortion() {
        use crate::camera::Camera;
        let s = sample();
        // Barrel distortion pulls events toward the principal point; the result stays on-grid
        // and conserves count here (no event leaves the 4×3 sensor).
        let camera = Camera::with_distortion(50.0, 50.0, 2.0, 1.5, -0.2, 0.0, 0.0, 0.0, 0.0);
        let out = s.undistort(&camera);
        assert_eq!(out.sensor_size(), (4, 3));
        assert!(out.len() <= s.len());
        assert!(out.xs().iter().all(|&x| x < 4) && out.ys().iter().all(|&y| y < 3));
    }

    /// The count-preserving transforms bypass `remap` and edit the columns directly, so the one
    /// thing that has to stay true is that they still agree with `remap` event for event.
    #[test]
    fn bijective_transforms_agree_with_the_general_path() {
        let mut builder = EventStreamBuilder::new(13, 7, 0.001);
        for index in 0..60_u16 {
            builder.push(index % 13, index % 7, i64::from(index) * 3, index % 3 == 0);
        }
        let s = builder.build();
        let (w, h) = s.sensor_size();
        let (max_x, max_y) = (w as i64 - 1, h as i64 - 1);
        let (sx, sy) = (5.0 / w as f64, 4.0 / h as f64);

        let cases: [(EventStream, EventStream); 6] = [
            (
                s.flip_x(),
                s.remap(w, h, |x, y, t, p| Some((max_x - x, y, t, p))),
            ),
            (
                s.flip_y(),
                s.remap(w, h, |x, y, t, p| Some((x, max_y - y, t, p))),
            ),
            (
                s.transpose(),
                s.remap(h, w, |x, y, t, p| Some((y, x, t, p))),
            ),
            (
                s.rotate90(1),
                s.remap(h, w, |x, y, t, p| Some((max_y - y, x, t, p))),
            ),
            (
                s.rotate90(3),
                s.remap(h, w, |x, y, t, p| Some((y, max_x - x, t, p))),
            ),
            (
                s.resize(5, 4),
                s.remap(5, 4, |x, y, t, p| {
                    Some((
                        (x as f64 * sx).floor() as i64,
                        (y as f64 * sy).floor() as i64,
                        t,
                        p,
                    ))
                }),
            ),
        ];

        for (fast, general) in cases {
            assert_eq!(fast.sensor_size(), general.sensor_size());
            assert_eq!(coords(&fast), coords(&general));
            assert_eq!(fast.ts(), general.ts());
            assert_eq!(fast.ps(), general.ps());
        }
    }

    /// Every transform that bypasses `remap` claims to map onto the grid it declares. If one ever
    /// did not, it would silently produce out-of-range coordinates instead of dropping the event.
    #[test]
    fn bijective_transforms_keep_every_event_in_bounds() {
        let s = sample();
        for out in [
            s.flip_x(),
            s.flip_y(),
            s.transpose(),
            s.rotate90(1),
            s.rotate90(2),
            s.rotate90(3),
            s.resize(2, 2),
            s.resize(9, 7),
            s.time_shift(-5),
            s.invert_polarity(),
        ] {
            let (width, height) = out.sensor_size();
            assert_eq!(out.len(), s.len());
            assert!(out.xs().iter().all(|&x| (x as usize) < width));
            assert!(out.ys().iter().all(|&y| (y as usize) < height));
        }
    }

    #[test]
    fn transforms_handle_the_empty_stream() {
        let empty = EventStreamBuilder::new(4, 3, 0.001).build();
        assert!(empty.flip_x().is_empty());
        assert!(empty.rotate90(1).is_empty());
        assert_eq!(empty.crop(0, 0, 2, 2).sensor_size(), (2, 2));
        assert!(empty.resize(2, 2).is_empty());
    }
}