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 {
22template <typename T>
23class Event_;
25
26namespace Mat {
27namespace detail {
28template <typename MatType>
29inline void clearZero(MatType &mat) {
30 if(mat.empty()) {
31 return;
32 }
33 if(mat.isContinuous()) {
34 std::memset(mat.data, 0, mat.total() * mat.elemSize());
35 return;
36 }
37 mat.setTo(0);
38}
39} // namespace detail
40
41template <typename Tb>
42class Binary_ : public cv::Mat_<Tb> {
43public:
44 using cv::Mat_<Tb>::Mat_;
45
46 template <typename T>
47 inline Tb insert(const Event_<T> &e) {
48 return set(e.x, e.y);
49 }
50
51 template <typename T>
52 inline Tb emplace(const T x, const T y) {
53 return set(x, y);
54 }
55
56 inline void clear() {
57 detail::clearZero(*this);
58 }
59
60 static constexpr Tb ON = std::numeric_limits<Tb>::max();
61 static constexpr Tb OFF = static_cast<Tb>(0);
62
63 friend std::ostream &operator<<(std::ostream &os, const Binary_ &binary) {
64 os << "Binary " << binary.cols << "x" << binary.rows;
65 return os;
66 }
67
68private:
69 template <typename T>
70 inline Tb set(const T x, const T y) {
71 if constexpr(std::is_floating_point_v<T>) {
72 return *(this->template ptr<Tb>(std::lround(y)) + std::lround(x)) = ON;
73 } else {
74 return *(this->template ptr<Tb>(y) + x) = ON;
75 }
76 }
77};
78using Binary = Binary_<uchar>;
79
80template <typename Tb>
81class Ternary_ : public cv::Mat_<Tb> {
82public:
83 using cv::Mat_<Tb>::Mat_;
84
85 template <typename T>
86 inline Tb insert(const Event_<T> &e) {
87 return set(e.x, e.y, e.p);
88 }
89
90 template <typename T>
91 inline Tb emplace(const T x, const T y, const bool p) {
92 return set(x, y, p);
93 }
94
95 inline void clear() {
96 detail::clearZero(*this);
97 }
98
99 static constexpr Tb POSITIVE = std::numeric_limits<Tb>::max();
100 static constexpr Tb ZERO = static_cast<Tb>(0);
101 static constexpr Tb NEGATIVE = std::numeric_limits<Tb>::min();
102
103 friend std::ostream &operator<<(std::ostream &os, const Ternary_ &ternary) {
104 os << "Ternary " << ternary.cols << "x" << ternary.rows;
105 return os;
106 }
107
108private:
109 template <typename T>
110 inline Tb set(const T x, const T y, const bool p) {
111 if constexpr(std::is_floating_point_v<T>) {
112 return *(this->template ptr<Tb>(std::lround(y)) + std::lround(x)) = (p ? POSITIVE : NEGATIVE);
113 } else {
114 return *(this->template ptr<Tb>(y) + x) = (p ? POSITIVE : NEGATIVE);
115 }
116 }
117};
118using Ternary = Ternary_<char>;
119
120class Time : public cv::Mat_<TimeType> {
121public:
122 using cv::Mat_<TimeType>::Mat_;
123
124 template <typename T>
125 inline TimeType insert(const Event_<T> &e) {
126 return set(e.x, e.y, e.t);
127 }
128
129 template <typename T>
130 inline TimeType emplace(const T x, const T y, const TimeType t) {
131 return set(x, y, t);
132 }
133
134 inline void clear() {
135 detail::clearZero(*this);
136 }
137
138 friend std::ostream &operator<<(std::ostream &os, const Time &time) {
139 os << "Time " << time.cols << "x" << time.rows;
140 return os;
141 }
142
143private:
144 template <typename T>
145 inline TimeType set(const T x, const T y, const TimeType t) {
146 if constexpr(std::is_floating_point_v<T>) {
147 return *(this->ptr<TimeType>(std::lround(y)) + std::lround(x)) = t;
148 } else {
149 return *(this->ptr<TimeType>(y) + x) = t;
150 }
151 }
152};
153
154class Polarity : public cv::Mat_<PolarityType> {
155public:
156 using cv::Mat_<PolarityType>::Mat_;
157
158 template <typename T>
159 inline PolarityType insert(const Event_<T> &e) {
160 return set(e.x, e.y, e.p);
161 }
162
163 template <typename T>
164 inline PolarityType emplace(const T x, const T y, const PolarityType p) {
165 return set(x, y, p);
166 }
167
168 inline void clear() {
169 detail::clearZero(*this);
170 }
171
172 friend std::ostream &operator<<(std::ostream &os, const Polarity &polarity) {
173 os << "Polarity " << polarity.cols << "x" << polarity.rows;
174 return os;
175 }
176
177private:
178 template <typename T>
179 inline PolarityType set(const T x, const T y, const PolarityType p) {
180 if constexpr(std::is_floating_point_v<T>) {
181 return *(this->ptr<PolarityType>(std::lround(y)) + std::lround(x)) = p;
182 } else {
183 return *(this->ptr<PolarityType>(y) + x) = p;
184 }
185 }
186};
187
188class Counter : public cv::Mat_<CounterType> {
189public:
190 using cv::Mat_<CounterType>::Mat_;
191
192 template <typename T>
193 inline CounterType insert(const Event_<T> &e) {
194 return set(e.x, e.y, e.p);
195 }
196
197 template <typename T>
198 inline CounterType emplace(const T x, const T y, const bool p) {
199 return set(x, y, p);
200 }
201
202 inline void clear() {
203 detail::clearZero(*this);
204 }
205
206 friend std::ostream &operator<<(std::ostream &os, const Counter &counter) {
207 os << "Counter " << counter.cols << "x" << counter.rows;
208 return os;
209 }
210
211private:
212 template <typename T>
213 inline CounterType set(const T x, const T y, const bool p) {
214 if constexpr(std::is_floating_point_v<T>) {
215 return *(this->ptr<CounterType>(std::lround(y)) + std::lround(x)) += (p ? +1 : -1);
216 } else {
217 return *(this->ptr<CounterType>(y) + x) += (p ? +1 : -1);
218 }
219 }
220};
221} // namespace Mat
222} // namespace ev
223
224#endif
This class extends cv::Point_<T> for event data. For more information, please refer here.
Definition types.hpp:62
PolarityType p
Definition types.hpp:65
TimeType t
Definition types.hpp:64
Definition matrices.hpp:42
Definition matrices.hpp:188
Definition matrices.hpp:154
Definition matrices.hpp:81
Definition matrices.hpp:120
Basic event-based vision structures based on OpenCV components.