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 <cstdint>
12#include <limits>
13#include <opencv2/core/mat.hpp>
14#include <opencv2/core/mat.inl.hpp>
15#include <opencv2/core/traits.hpp>
16#include <type_traits>
17
18namespace ev {
19template <typename T>
20class Event_;
21
22namespace Mat {
23template <typename Tm = std::uint8_t>
24class Binary : public cv::Mat_<Tm> {
25public:
26 using cv::Mat_<Tm>::Mat_;
27
28 template <typename T>
29 inline Tm insert(const Event_<T> &e) {
30 return set(e.x, e.y);
31 }
32
33 template <typename T>
34 inline Tm emplace(const T x, const T y) {
35 return set(x, y);
36 }
37
38 inline void clear() {
39 cv::Mat_<Tm>::setTo(0);
40 }
41
42private:
43 static constexpr Tm value_ = std::numeric_limits<Tm>::max();
44
45 template <typename T>
46 inline Tm set(const T x, const T y) {
47 if constexpr(std::is_floating_point_v<T>) {
48 return *(this->template ptr<Tm>(std::lround(y)) + std::lround(x)) = value_;
49 } else {
50 return *(this->template ptr<Tm>(y) + x) = value_;
51 }
52 }
53};
54
55class Time : public cv::Mat_<double> {
56public:
57 using cv::Mat_<double>::Mat_;
58
59 template <typename T>
60 inline double insert(const Event_<T> &e) {
61 return set(e.x, e.y, e.t);
62 }
63
64 template <typename T>
65 inline double emplace(const T x, const T y, const double t) {
66 return set(x, y, t);
67 }
68
69 inline void clear() {
70 cv::Mat_<double>::setTo(0);
71 }
72
73private:
74 template <typename T>
75 inline double set(const T x, const T y, const double t) {
76 if constexpr(std::is_floating_point_v<T>) {
77 return *(this->ptr<double>(std::lround(y)) + std::lround(x)) = t;
78 } else {
79 return *(this->ptr<double>(y) + x) = t;
80 }
81 }
82};
83
84class Polarity : public cv::Mat_<bool> {
85public:
86 using cv::Mat_<bool>::Mat_;
87
88 template <typename T>
89 inline bool insert(const Event_<T> &e) {
90 return set(e.x, e.y, e.p);
91 }
92
93 template <typename T>
94 inline bool emplace(const T x, const T y, const bool p) {
95 return set(x, y, p);
96 }
97
98 inline void clear() {
99 cv::Mat_<bool>::setTo(false);
100 }
101
102private:
103 template <typename T>
104 inline bool set(const T x, const T y, const bool p) {
105 if constexpr(std::is_floating_point_v<T>) {
106 return *(this->ptr<bool>(std::lround(y)) + std::lround(x)) = p;
107 } else {
108 return *(this->ptr<bool>(y) + x) = p;
109 }
110 }
111};
112
113class Counter : public cv::Mat_<int> {
114public:
115 using cv::Mat_<int>::Mat_;
116
117 template <typename T>
118 inline int insert(const Event_<T> &e) {
119 return set(e.x, e.y, e.p);
120 }
121
122 template <typename T>
123 inline int emplace(const T x, const T y, const bool p) {
124 return set(x, y, p);
125 }
126
127 inline void clear() {
128 cv::Mat_<int>::setTo(0);
129 }
130
131private:
132 template <typename T>
133 inline int set(const T x, const T y, const bool p) {
134 if constexpr(std::is_floating_point_v<T>) {
135 return *(this->ptr<int>(std::lround(y)) + std::lround(x)) += (p ? +1 : -1);
136 } else {
137 return *(this->ptr<int>(y) + x) += (p ? +1 : -1);
138 }
139 }
140};
141} // namespace Mat
142} // namespace ev
143
144#endif
This class extends cv::Point_<T> for event data. For more information, please refer here.
Definition types.hpp:62
bool p
Definition types.hpp:65
double t
Definition types.hpp:64
Definition matrices.hpp:24
Definition matrices.hpp:113
Definition matrices.hpp:84
Definition matrices.hpp:55
Basic event-based vision structures based on OpenCV components.