6#ifndef OPENEV_CORE_MATRICES_HPP
7#define OPENEV_CORE_MATRICES_HPP
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>
23[[maybe_unused]]
constexpr bool USING_MATRICES_HPP =
true;
37 static_assert(std::is_arithmetic_v<T>,
"ev::Mat_: the pixel type must be arithmetic.");
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)) {}
64 [[nodiscard]] std::size_t
count()
const {
97 if(cv::Mat_<T>::empty()) {
100 if(cv::Mat_<T>::isContinuous()) {
101 std::memset(cv::Mat_<T>::data, 0, cv::Mat_<T>::total() * cv::Mat_<T>::elemSize());
104 cv::Mat_<T>::setTo(0);
110 std::size_t count_{0};
123template <
typename Tb>
125 static_assert(std::is_integral_v<Tb>,
"ev::Binary_: the pixel type must be integral.");
135 template <
typename T>
137 return set(e.x, e.y);
146 template <
typename T>
151 static constexpr Tb
ON = std::numeric_limits<Tb>::max();
152 static constexpr Tb
OFF =
static_cast<Tb
>(0);
154 friend std::ostream &operator<<(std::ostream &os,
const Binary_ &binary) {
155 os <<
"Binary " << binary.cols <<
"x" << binary.rows;
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;
165 return *(this->
template ptr<Tb>(y) + x) =
ON;
182template <
typename 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.");
194 template <
typename T>
196 return set(e.x, e.y, e.
p);
206 template <
typename T>
207 inline Tb
emplace(
const T x,
const T y,
const bool p) {
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();
215 friend std::ostream &operator<<(std::ostream &os,
const Ternary_ &ternary) {
216 os <<
"Ternary " << ternary.cols <<
"x" << ternary.rows;
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];
228 return *(this->
template ptr<Tb>(y) + x) = VALUE[p];
249 template <
typename T>
251 return set(e.x, e.y, e.
t);
261 template <
typename T>
262 inline TimeType
emplace(
const T x,
const T y,
const TimeType t) {
266 friend std::ostream &operator<<(std::ostream &os,
const Time &time) {
267 os <<
"Time " << time.cols <<
"x" << time.rows;
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;
277 return *(this->ptr<TimeType>(y) + x) = t;
297 template <
typename T>
299 return set(e.x, e.y, e.
p);
309 template <
typename T>
310 inline PolarityType
emplace(
const T x,
const T y,
const PolarityType p) {
314 friend std::ostream &operator<<(std::ostream &os,
const Polarity &polarity) {
315 os <<
"Polarity " << polarity.cols <<
"x" << polarity.rows;
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;
325 return *(this->ptr<PolarityType>(y) + x) = p;
345 template <
typename T>
347 return set(e.x, e.y, e.
p);
357 template <
typename T>
358 inline CounterType
emplace(
const T x,
const T y,
const bool p) {
362 friend std::ostream &operator<<(std::ostream &os,
const Counter &counter) {
363 os <<
"Counter " << counter.cols <<
"x" << counter.rows;
368 static constexpr CounterType INCREMENT[2]{-1, +1};
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];
375 return *(this->ptr<CounterType>(y) + x) += INCREMENT[p];
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