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
9#include <cmath>
10#include <limits>
11#include <opencv2/core/hal/interface.h>
12#include <opencv2/core/mat.hpp>
13#include <opencv2/core/mat.inl.hpp>
14#include <opencv2/core/traits.hpp>
15#include <ostream>
16#include <type_traits>
17
18namespace ev {
20template <typename T>
21class Event_;
23
24namespace Mat {
25template <typename Tb>
26class Binary_ : public cv::Mat_<Tb> {
27public:
28 using cv::Mat_<Tb>::Mat_;
29
30 template <typename T>
31 inline Tb insert(const Event_<T> &e) {
32 return set(e.x, e.y);
33 }
34
35 template <typename T>
36 inline Tb emplace(const T x, const T y) {
37 return set(x, y);
38 }
39
40 inline void clear() {
41 cv::Mat_<Tb>::setTo(0);
42 }
43
44 static constexpr Tb ON = std::numeric_limits<Tb>::max();
45 static constexpr Tb OFF = static_cast<Tb>(0);
46
47 friend std::ostream &operator<<(std::ostream &os, const Binary_ &binary) {
48 os << "Binary " << binary.cols << "x" << binary.rows;
49 return os;
50 }
51
52private:
53 template <typename T>
54 inline Tb set(const T x, const T y) {
55 if constexpr(std::is_floating_point_v<T>) {
56 return *(this->template ptr<Tb>(std::lround(y)) + std::lround(x)) = ON;
57 } else {
58 return *(this->template ptr<Tb>(y) + x) = ON;
59 }
60 }
61};
62using Binary = Binary_<uchar>;
63
64class Time : public cv::Mat_<double> {
65public:
66 using cv::Mat_<double>::Mat_;
67
68 template <typename T>
69 inline double insert(const Event_<T> &e) {
70 return set(e.x, e.y, e.t);
71 }
72
73 template <typename T>
74 inline double emplace(const T x, const T y, const double t) {
75 return set(x, y, t);
76 }
77
78 inline void clear() {
79 cv::Mat_<double>::setTo(0);
80 }
81
82 friend std::ostream &operator<<(std::ostream &os, const Time &time) {
83 os << "Time " << time.cols << "x" << time.rows;
84 return os;
85 }
86
87private:
88 template <typename T>
89 inline double set(const T x, const T y, const double t) {
90 if constexpr(std::is_floating_point_v<T>) {
91 return *(this->ptr<double>(std::lround(y)) + std::lround(x)) = t;
92 } else {
93 return *(this->ptr<double>(y) + x) = t;
94 }
95 }
96};
97
98class Polarity : public cv::Mat_<bool> {
99public:
100 using cv::Mat_<bool>::Mat_;
101
102 template <typename T>
103 inline bool insert(const Event_<T> &e) {
104 return set(e.x, e.y, e.p);
105 }
106
107 template <typename T>
108 inline bool emplace(const T x, const T y, const bool p) {
109 return set(x, y, p);
110 }
111
112 inline void clear() {
113 cv::Mat_<bool>::setTo(false);
114 }
115
116 friend std::ostream &operator<<(std::ostream &os, const Polarity &polarity) {
117 os << "Polarity " << polarity.cols << "x" << polarity.rows;
118 return os;
119 }
120
121private:
122 template <typename T>
123 inline bool set(const T x, const T y, const bool p) {
124 if constexpr(std::is_floating_point_v<T>) {
125 return *(this->ptr<bool>(std::lround(y)) + std::lround(x)) = p;
126 } else {
127 return *(this->ptr<bool>(y) + x) = p;
128 }
129 }
130};
131
132class Counter : public cv::Mat_<int> {
133public:
134 using cv::Mat_<int>::Mat_;
135
136 template <typename T>
137 inline int insert(const Event_<T> &e) {
138 return set(e.x, e.y, e.p);
139 }
140
141 template <typename T>
142 inline int emplace(const T x, const T y, const bool p) {
143 return set(x, y, p);
144 }
145
146 inline void clear() {
147 cv::Mat_<int>::setTo(0);
148 }
149
150 friend std::ostream &operator<<(std::ostream &os, const Counter &counter) {
151 os << "Counter " << counter.cols << "x" << counter.rows;
152 return os;
153 }
154
155private:
156 template <typename T>
157 inline int set(const T x, const T y, const bool p) {
158 if constexpr(std::is_floating_point_v<T>) {
159 return *(this->ptr<int>(std::lround(y)) + std::lround(x)) += (p ? +1 : -1);
160 } else {
161 return *(this->ptr<int>(y) + x) += (p ? +1 : -1);
162 }
163 }
164};
165} // namespace Mat
166} // namespace ev
167
168#endif
This class extends cv::Point_<T> for event data. For more information, please refer here.
Definition types.hpp:56
bool p
Definition types.hpp:59
double t
Definition types.hpp:58
Definition matrices.hpp:26
Definition matrices.hpp:132
Definition matrices.hpp:98
Definition matrices.hpp:64