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 "openev/core/types.hpp"
11#include <cmath>
12#include <cstddef>
13#include <cstring>
14#include <limits>
15#include <opencv2/core/hal/interface.h>
16#include <opencv2/core/mat.hpp>
17#include <opencv2/core/mat.inl.hpp>
18#include <opencv2/core/traits.hpp>
19#include <ostream>
20#include <type_traits>
21
22namespace ev {
23[[maybe_unused]] constexpr bool USING_MATRICES_HPP = true;
24
26template <typename T>
27class Event_;
29
30namespace Mat {
35template <typename T>
36class Mat_ : public cv::Mat_<T>, public Stats_<Mat_<T>> {
37 static_assert(std::is_arithmetic_v<T>, "ev::Mat_: the pixel type must be arithmetic.");
38
39public:
40 using cv::Mat_<T>::Mat_;
41
43 Mat_() = default;
44 Mat_(const int rows, const int cols) : cv::Mat_<T>(rows, cols, static_cast<T>(0)) {}
45 explicit Mat_(const cv::Size size) : cv::Mat_<T>(size, static_cast<T>(0)) {}
47
52 void updateStats(const Event &e) {
53 if(count_ == 0) {
54 first_ = e.t;
55 }
56 last_ = e.t;
57 count_++;
58 }
59
64 [[nodiscard]] std::size_t count() const {
65 return count_;
66 }
67
72 [[nodiscard]] TimeType firstTimestamp() const {
73 return first_;
74 }
75
80 [[nodiscard]] TimeType lastTimestamp() const {
81 return last_;
82 }
83
87 inline void resetStats() {
88 first_ = 0;
89 last_ = 0;
90 count_ = 0;
91 }
92
96 inline void clear() {
97 if(cv::Mat_<T>::empty()) {
98 return;
99 }
100 if(cv::Mat_<T>::isContinuous()) {
101 std::memset(cv::Mat_<T>::data, 0, cv::Mat_<T>::total() * cv::Mat_<T>::elemSize());
102 return;
103 }
104 cv::Mat_<T>::setTo(0);
105 }
106
107private:
108 TimeType first_{0};
109 TimeType last_{0};
110 std::size_t count_{0};
111};
112
123template <typename Tb>
124class Binary_ : public Mat_<Tb> {
125 static_assert(std::is_integral_v<Tb>, "ev::Binary_: the pixel type must be integral.");
126
127public:
128 using Mat_<Tb>::Mat_;
129
135 template <typename T>
136 inline Tb insert(const Event_<T> &e) {
137 return set(e.x, e.y);
138 }
139
146 template <typename T>
147 inline Tb emplace(const T x, const T y) {
148 return set(x, y);
149 }
150
151 static constexpr Tb ON = std::numeric_limits<Tb>::max();
152 static constexpr Tb OFF = static_cast<Tb>(0);
153
154 friend std::ostream &operator<<(std::ostream &os, const Binary_ &binary) {
155 os << "Binary " << binary.cols << "x" << binary.rows;
156 return os;
157 }
158
159private:
160 template <typename T>
161 inline Tb set(const T x, const T y) {
162 if constexpr(std::is_floating_point_v<T>) {
163 return *(this->template ptr<Tb>(round_(y)) + round_(x)) = ON;
164 } else {
165 return *(this->template ptr<Tb>(y) + x) = ON;
166 }
167 }
168};
169using Binary = Binary_<uchar>;
170
182template <typename Tb>
183class Ternary_ : public Mat_<Tb> {
184 static_assert(std::is_integral_v<Tb> && std::is_signed_v<Tb>, "ev::Ternary_: the pixel type must be signed, otherwise NEGATIVE and ZERO are both zero.");
185
186public:
187 using Mat_<Tb>::Mat_;
188
194 template <typename T>
195 inline Tb insert(const Event_<T> &e) {
196 return set(e.x, e.y, e.p);
197 }
198
206 template <typename T>
207 inline Tb emplace(const T x, const T y, const bool p) {
208 return set(x, y, p);
209 }
210
211 static constexpr Tb POSITIVE = std::numeric_limits<Tb>::max();
212 static constexpr Tb ZERO = static_cast<Tb>(0);
213 static constexpr Tb NEGATIVE = std::numeric_limits<Tb>::min();
214
215 friend std::ostream &operator<<(std::ostream &os, const Ternary_ &ternary) {
216 os << "Ternary " << ternary.cols << "x" << ternary.rows;
217 return os;
218 }
219
220private:
221 static constexpr Tb VALUE[2]{NEGATIVE, POSITIVE};
222
223 template <typename T>
224 inline Tb set(const T x, const T y, const bool p) {
225 if constexpr(std::is_floating_point_v<T>) {
226 return *(this->template ptr<Tb>(round_(y)) + round_(x)) = VALUE[p];
227 } else {
228 return *(this->template ptr<Tb>(y) + x) = VALUE[p];
229 }
230 }
231};
232using Ternary = Ternary_<signed char>;
233
240class Time : public Mat_<TimeType> {
241public:
242 using Mat_<TimeType>::Mat_;
243
249 template <typename T>
250 inline TimeType insert(const Event_<T> &e) {
251 return set(e.x, e.y, e.t);
252 }
253
261 template <typename T>
262 inline TimeType emplace(const T x, const T y, const TimeType t) {
263 return set(x, y, t);
264 }
265
266 friend std::ostream &operator<<(std::ostream &os, const Time &time) {
267 os << "Time " << time.cols << "x" << time.rows;
268 return os;
269 }
270
271private:
272 template <typename T>
273 inline TimeType set(const T x, const T y, const TimeType t) {
274 if constexpr(std::is_floating_point_v<T>) {
275 return *(this->ptr<TimeType>(round_(y)) + round_(x)) = t;
276 } else {
277 return *(this->ptr<TimeType>(y) + x) = t;
278 }
279 }
280};
281
288class Polarity : public Mat_<PolarityType> {
289public:
290 using Mat_<PolarityType>::Mat_;
291
297 template <typename T>
298 inline PolarityType insert(const Event_<T> &e) {
299 return set(e.x, e.y, e.p);
300 }
301
309 template <typename T>
310 inline PolarityType emplace(const T x, const T y, const PolarityType p) {
311 return set(x, y, p);
312 }
313
314 friend std::ostream &operator<<(std::ostream &os, const Polarity &polarity) {
315 os << "Polarity " << polarity.cols << "x" << polarity.rows;
316 return os;
317 }
318
319private:
320 template <typename T>
321 inline PolarityType set(const T x, const T y, const PolarityType p) {
322 if constexpr(std::is_floating_point_v<T>) {
323 return *(this->ptr<PolarityType>(round_(y)) + round_(x)) = p;
324 } else {
325 return *(this->ptr<PolarityType>(y) + x) = p;
326 }
327 }
328};
329
336class Counter : public Mat_<CounterType> {
337public:
338 using Mat_<CounterType>::Mat_;
339
345 template <typename T>
346 inline CounterType insert(const Event_<T> &e) {
347 return set(e.x, e.y, e.p);
348 }
349
357 template <typename T>
358 inline CounterType emplace(const T x, const T y, const bool p) {
359 return set(x, y, p);
360 }
361
362 friend std::ostream &operator<<(std::ostream &os, const Counter &counter) {
363 os << "Counter " << counter.cols << "x" << counter.rows;
364 return os;
365 }
366
367private:
368 static constexpr CounterType INCREMENT[2]{-1, +1};
369
370 template <typename T>
371 inline CounterType set(const T x, const T y, const bool p) {
372 if constexpr(std::is_floating_point_v<T>) {
373 return *(this->ptr<CounterType>(round_(y)) + round_(x)) += INCREMENT[p];
374 } else {
375 return *(this->ptr<CounterType>(y) + x) += INCREMENT[p];
376 }
377 }
378};
379} // namespace Mat
380} // namespace ev
381
382#endif // OPENEV_CORE_MATRICES_HPP
This class extends cv::Point_<T> for event data. For more information, please refer here.
Definition types.hpp:86
PolarityType p
Definition types.hpp:91
TimeType t
Definition types.hpp:90
Spatial map marking whether any event has occurred at each pixel.
Definition matrices.hpp:124
Tb insert(const Event_< T > &e)
Insert an event, setting the pixel at (e.x, e.y) to ON.
Definition matrices.hpp:136
Tb emplace(const T x, const T y)
Set the pixel at (x, y) to ON without constructing an Event_.
Definition matrices.hpp:147
static constexpr uchar ON
Definition matrices.hpp:151
static constexpr uchar OFF
Definition matrices.hpp:152
Spatial map accumulating signed event counts per pixel.
Definition matrices.hpp:336
CounterType insert(const Event_< T > &e)
Insert an event, incrementing (p=true) or decrementing (p=false) the counter at (e....
Definition matrices.hpp:346
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:358
Matrix of event data. On top of cv::Mat_, it can account for the events it receives through updateSta...
Definition matrices.hpp:36
TimeType firstTimestamp() const
Timestamp of the first event accounted for by updateStats().
Definition matrices.hpp:72
void clear()
Reset all pixels.
Definition matrices.hpp:96
void resetStats()
Reset statistics.
Definition matrices.hpp:87
TimeType lastTimestamp() const
Timestamp of the last event accounted for by updateStats().
Definition matrices.hpp:80
void updateStats(const Event &e)
Account for an event in the statistics reported by count() and duration().
Definition matrices.hpp:52
std::size_t count() const
Number of events accounted for by updateStats().
Definition matrices.hpp:64
Spatial map storing the polarity of the most recent event at each pixel.
Definition matrices.hpp:288
PolarityType emplace(const T x, const T y, const PolarityType p)
Store polarity p at (x, y) without constructing an Event_.
Definition matrices.hpp:310
PolarityType insert(const Event_< T > &e)
Insert an event, storing e.p at pixel (e.x, e.y).
Definition matrices.hpp:298
Spatial map encoding event polarity at each pixel.
Definition matrices.hpp:183
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:195
static constexpr signed char ZERO
Definition matrices.hpp:212
Tb emplace(const T x, const T y, const bool p)
Write polarity p at (x, y) without constructing an Event_.
Definition matrices.hpp:207
static constexpr signed char NEGATIVE
Definition matrices.hpp:213
static constexpr signed char POSITIVE
Definition matrices.hpp:211
Spatial map storing the timestamp of the most recent event at each pixel.
Definition matrices.hpp:240
TimeType insert(const Event_< T > &e)
Insert an event, storing e.t at pixel (e.x, e.y).
Definition matrices.hpp:250
TimeType emplace(const T x, const T y, const TimeType t)
Store timestamp t at (x, y) without constructing an Event_.
Definition matrices.hpp:262
This is an auxiliary class. This class cannot be instanced.
Definition stats.hpp:51
Statistics shared by everything that accounts for events.
Basic event-based vision structures based on OpenCV components.
constexpr PolarityType POSITIVE
Definition types.hpp:38
Eventi Event
Definition types.hpp:262
constexpr PolarityType NEGATIVE
Definition types.hpp:39