OpenEV
Extending OpenCV to event-based vision
Loading...
Searching...
No Matches
matrices.hpp
Go to the documentation of this file.
1
6#ifndef OPENEV_CORE_MATRICES_HPP
7#define OPENEV_CORE_MATRICES_HPP
8
10#include <cmath>
11#include <cstring>
12#include <limits>
13#include <opencv2/core/hal/interface.h>
14#include <opencv2/core/mat.hpp>
15#include <opencv2/core/mat.inl.hpp>
16#include <opencv2/core/traits.hpp>
17#include <ostream>
18#include <type_traits>
19
20namespace ev {
21constexpr bool USING_MATRICES_HPP = true;
22
24template <typename T>
25class Event_;
27
28namespace Mat {
29template <typename T>
30class Mat_ : public cv::Mat_<T> {
31public:
32 using cv::Mat_<T>::Mat_;
33
34 void updateStats(const Event &e) {
35 if(first_) {
36 last_ = e.t;
37 count_++;
38 return;
39 }
40 first_ = e.t;
41 }
42
43 CounterType count() const {
44 return count_;
45 }
46
47 TimeType duration() const {
48 return last_ - first_;
49 }
50
54 inline void resetStats() {
55 first_ = 0;
56 last_ = 0;
57 count_ = 0;
58 }
59
63 inline void clear() {
64 if(cv::Mat_<T>::empty()) {
65 return;
66 }
67 if(cv::Mat_<T>::isContinuous()) {
68 std::memset(cv::Mat_<T>::data, 0, cv::Mat_<T>::total() * cv::Mat_<T>::elemSize());
69 return;
70 }
71 cv::Mat_<T>::setTo(0);
72 }
73
74private:
75 TimeType first_{0};
76 TimeType last_{0};
77 CounterType count_{0};
78};
79
90template <typename Tb>
91class Binary_ : public Mat_<Tb> {
92public:
93 using Mat_<Tb>::Mat_;
94
100 template <typename T>
101 inline Tb insert(const Event_<T> &e) {
102 return set(e.x, e.y);
103 }
104
111 template <typename T>
112 inline Tb emplace(const T x, const T y) {
113 return set(x, y);
114 }
115
116 static constexpr Tb ON = std::numeric_limits<Tb>::max();
117 static constexpr Tb OFF = static_cast<Tb>(0);
118
119 friend std::ostream &operator<<(std::ostream &os, const Binary_ &binary) {
120 os << "Binary " << binary.cols << "x" << binary.rows;
121 return os;
122 }
123
124private:
125 template <typename T>
126 inline Tb set(const T x, const T y) {
127 if constexpr(std::is_floating_point_v<T>) {
128 return *(this->template ptr<Tb>(std::lround(y)) + std::lround(x)) = ON;
129 } else {
130 return *(this->template ptr<Tb>(y) + x) = ON;
131 }
132 }
133};
134using Binary = Binary_<uchar>;
135
147template <typename Tb>
148class Ternary_ : public Mat_<Tb> {
149public:
150 using Mat_<Tb>::Mat_;
151
157 template <typename T>
158 inline Tb insert(const Event_<T> &e) {
159 return set(e.x, e.y, e.p);
160 }
161
169 template <typename T>
170 inline Tb emplace(const T x, const T y, const bool p) {
171 return set(x, y, p);
172 }
173
174 static constexpr Tb POSITIVE = std::numeric_limits<Tb>::max();
175 static constexpr Tb ZERO = static_cast<Tb>(0);
176 static constexpr Tb NEGATIVE = std::numeric_limits<Tb>::min();
177
178 friend std::ostream &operator<<(std::ostream &os, const Ternary_ &ternary) {
179 os << "Ternary " << ternary.cols << "x" << ternary.rows;
180 return os;
181 }
182
183private:
184 template <typename T>
185 inline Tb set(const T x, const T y, const bool p) {
186 if constexpr(std::is_floating_point_v<T>) {
187 return *(this->template ptr<Tb>(std::lround(y)) + std::lround(x)) = (p ? POSITIVE : NEGATIVE);
188 } else {
189 return *(this->template ptr<Tb>(y) + x) = (p ? POSITIVE : NEGATIVE);
190 }
191 }
192};
193using Ternary = Ternary_<char>;
194
201class Time : public Mat_<TimeType> {
202public:
203 using Mat_<TimeType>::Mat_;
204
210 template <typename T>
211 inline TimeType insert(const Event_<T> &e) {
212 return set(e.x, e.y, e.t);
213 }
214
222 template <typename T>
223 inline TimeType emplace(const T x, const T y, const TimeType t) {
224 return set(x, y, t);
225 }
226
227 friend std::ostream &operator<<(std::ostream &os, const Time &time) {
228 os << "Time " << time.cols << "x" << time.rows;
229 return os;
230 }
231
232private:
233 template <typename T>
234 inline TimeType set(const T x, const T y, const TimeType t) {
235 if constexpr(std::is_floating_point_v<T>) {
236 return *(this->ptr<TimeType>(std::lround(y)) + std::lround(x)) = t;
237 } else {
238 return *(this->ptr<TimeType>(y) + x) = t;
239 }
240 }
241};
242
249class Polarity : public Mat_<PolarityType> {
250public:
251 using Mat_<PolarityType>::Mat_;
252
258 template <typename T>
259 inline PolarityType insert(const Event_<T> &e) {
260 return set(e.x, e.y, e.p);
261 }
262
270 template <typename T>
271 inline PolarityType emplace(const T x, const T y, const PolarityType p) {
272 return set(x, y, p);
273 }
274
275 friend std::ostream &operator<<(std::ostream &os, const Polarity &polarity) {
276 os << "Polarity " << polarity.cols << "x" << polarity.rows;
277 return os;
278 }
279
280private:
281 template <typename T>
282 inline PolarityType set(const T x, const T y, const PolarityType p) {
283 if constexpr(std::is_floating_point_v<T>) {
284 return *(this->ptr<PolarityType>(std::lround(y)) + std::lround(x)) = p;
285 } else {
286 return *(this->ptr<PolarityType>(y) + x) = p;
287 }
288 }
289};
290
297class Counter : public Mat_<CounterType> {
298public:
299 using Mat_<CounterType>::Mat_;
300
306 template <typename T>
307 inline CounterType insert(const Event_<T> &e) {
308 return set(e.x, e.y, e.p);
309 }
310
318 template <typename T>
319 inline CounterType emplace(const T x, const T y, const bool p) {
320 return set(x, y, p);
321 }
322
323 friend std::ostream &operator<<(std::ostream &os, const Counter &counter) {
324 os << "Counter " << counter.cols << "x" << counter.rows;
325 return os;
326 }
327
328private:
329 template <typename T>
330 inline CounterType set(const T x, const T y, const bool p) {
331 if constexpr(std::is_floating_point_v<T>) {
332 return *(this->ptr<CounterType>(std::lround(y)) + std::lround(x)) += (p ? +1 : -1);
333 } else {
334 return *(this->ptr<CounterType>(y) + x) += (p ? +1 : -1);
335 }
336 }
337};
338} // namespace Mat
339} // namespace ev
340
341#endif // OPENEV_CORE_MATRICES_HPP
This class extends cv::Point_<T> for event data. For more information, please refer here.
Definition types.hpp:77
PolarityType p
Definition types.hpp:80
TimeType t
Definition types.hpp:79
Spatial map marking whether any event has occurred at each pixel.
Definition matrices.hpp:91
Tb insert(const Event_< T > &e)
Insert an event, setting the pixel at (e.x, e.y) to ON.
Definition matrices.hpp:101
Tb emplace(const T x, const T y)
Set the pixel at (x, y) to ON without constructing an Event_.
Definition matrices.hpp:112
static constexpr uchar ON
Definition matrices.hpp:116
static constexpr uchar OFF
Definition matrices.hpp:117
Spatial map accumulating signed event counts per pixel.
Definition matrices.hpp:297
CounterType insert(const Event_< T > &e)
Insert an event, incrementing (p=true) or decrementing (p=false) the counter at (e....
Definition matrices.hpp:307
CounterType emplace(const T x, const T y, const bool p)
Increment or decrement the counter at (x, y) without constructing an Event_.
Definition matrices.hpp:319
Definition matrices.hpp:30
void clear()
Reset all pixels.
Definition matrices.hpp:63
void resetStats()
Reset statistics (count, first timestamp, last timestamp).
Definition matrices.hpp:54
Spatial map storing the polarity of the most recent event at each pixel.
Definition matrices.hpp:249
PolarityType emplace(const T x, const T y, const PolarityType p)
Store polarity p at (x, y) without constructing an Event_.
Definition matrices.hpp:271
PolarityType insert(const Event_< T > &e)
Insert an event, storing e.p at pixel (e.x, e.y).
Definition matrices.hpp:259
Spatial map encoding event polarity at each pixel.
Definition matrices.hpp:148
Tb insert(const Event_< T > &e)
Insert an event, writing POSITIVE or NEGATIVE at (e.x, e.y) based on e.p.
Definition matrices.hpp:158
static constexpr char ZERO
Definition matrices.hpp:175
Tb emplace(const T x, const T y, const bool p)
Write polarity p at (x, y) without constructing an Event_.
Definition matrices.hpp:170
static constexpr char NEGATIVE
Definition matrices.hpp:176
static constexpr char POSITIVE
Definition matrices.hpp:174
Spatial map storing the timestamp of the most recent event at each pixel.
Definition matrices.hpp:201
TimeType insert(const Event_< T > &e)
Insert an event, storing e.t at pixel (e.x, e.y).
Definition matrices.hpp:211
TimeType emplace(const T x, const T y, const TimeType t)
Store timestamp t at (x, y) without constructing an Event_.
Definition matrices.hpp:223
Basic event-based vision structures based on OpenCV components.
constexpr PolarityType POSITIVE
Definition types.hpp:29
Eventi Event
Definition types.hpp:252
constexpr PolarityType NEGATIVE
Definition types.hpp:30