Coverage for src/evutils/repr/_frame.py: 100%
38 statements
« prev ^ index » next coverage.py v7.15.1, created at 2026-07-15 09:33 +0000
« prev ^ index » next coverage.py v7.15.1, created at 2026-07-15 09:33 +0000
1"""Module for generating frame-based representations from events."""
3import numba
4import numpy as np
5from typing import Any
8@numba.njit
9def frame_gray(events: np.ndarray, width: int = 1280, height: int = 720, dtype: Any = np.uint8) -> np.ndarray:
10 """Generate a grayscale frame from the events.
12 Parameters
13 ----------
14 events : np.ndarray
15 Array of events in the :class:`~evutils.types.Events` format
16 width : int, optional
17 Width of the frame, by default 1280
18 height : int, optional
19 Height of the frame, by default 720
20 dtype : np.dtype, optional
21 Data type of the output array, by default np.uint8
23 Returns
24 -------
25 np.ndarray
26 A numpy array with the frame (height, width)
28 Examples
29 --------
30 >>> import numpy as np
31 >>> from evutils.repr import frame_gray
32 >>> events = np.array([(10, 20, 100, 1), (15, 25, 200, 0)],
33 ... dtype=[('x', '<u2'), ('y', '<u2'), ('t', '<i8'), ('p', 'i1')])
34 >>> frame = frame_gray(events, width=100, height=100)
35 >>> frame.shape
36 (100, 100)
37 """
38 buffer = np.full((height, width), 128, dtype=dtype)
41 for e in events:
42 x = e['x']
43 y = e['y']
44 p = e['p']
46 if p == 1:
47 p = 255
48 else:
49 p = 0
51 buffer[y, x] = p
53 return buffer
56@numba.njit
57def frame_rgb(ev: np.ndarray, width: int = 1280, height: int = 720, bg_color: Any = np.array((0, 0, 0)), pos_color: Any = np.array((255, 0, 0)), neg_color: Any = np.array((0, 0, 255))) -> np.ndarray:
58 """Generate an RGB frame from the events.
60 Parameters
61 ----------
62 ev : np.ndarray
63 Array of events in the :class:`~evutils.types.Events` format
64 width : int, optional
65 Width of the frame, by default 1280
66 height : int, optional
67 Height of the frame, by default 720
68 bg_color : np.ndarray, optional
69 Background color, by default np.array((0, 0, 0))
70 pos_color : np.ndarray, optional
71 Color for positive events, by default np.array((255, 0, 0))
72 neg_color : np.ndarray, optional
73 Color for negative events, by default np.array((0, 0, 255))
75 Returns
76 -------
77 np.ndarray
78 A numpy array with the frame (height, width, 3)
80 Examples
81 --------
82 >>> import numpy as np
83 >>> from evutils.repr import frame_rgb
84 >>> events = np.array([(10, 20, 100, 1), (15, 25, 200, 0)],
85 ... dtype=[('x', '<u2'), ('y', '<u2'), ('t', '<i8'), ('p', 'i1')])
86 >>> frame = frame_rgb(events, width=100, height=100)
87 >>> frame.shape
88 (100, 100, 3)
89 """
90 buffer = np.zeros((height, width, 3), dtype=np.uint8)
91 buffer[:, :] = bg_color
93 for e in ev:
94 x = e['x']
95 y = e['y']
96 p = e['p']
98 if p == 1:
99 buffer[y, x] = pos_color
100 else:
101 buffer[y, x] = neg_color
103 return buffer
106@numba.njit
107def frame_diff(ev: np.ndarray, width: int = 1280, height: int = 720, dtype: Any = np.int8) -> np.ndarray:
108 """Generate a differential frame from the events.
110 Parameters
111 ----------
112 ev : np.ndarray
113 Array of events in the :class:`~evutils.types.Events` format
114 width : int, optional
115 Width of the frame, by default 1280
116 height : int, optional
117 Height of the frame, by default 720
118 dtype : np.dtype, optional
119 Data type of the output array, by default np.int8
121 Returns
122 -------
123 np.ndarray
124 A numpy array with the frame (height, width)
126 Examples
127 --------
128 >>> import numpy as np
129 >>> from evutils.repr import frame_diff
130 >>> events = np.array([(10, 20, 100, 1), (15, 25, 200, 0)],
131 ... dtype=[('x', '<u2'), ('y', '<u2'), ('t', '<i8'), ('p', 'i1')])
132 >>> frame = frame_diff(events, width=100, height=100)
133 >>> frame.shape
134 (100, 100)
135 """
136 buffer = np.zeros((height, width), dtype=dtype)
138 for e in ev:
139 x = e['x']
140 y = e['y']
141 p = e['p']
143 if p == 1:
144 buffer[y, x] += 1
145 else:
146 buffer[y, x] -= 1
148 return buffer