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