OpenEV
Extending OpenCV to event-based vision
Loading...
Searching...
No Matches
abstract-representation.hpp
Go to the documentation of this file.
1
6#ifndef OPENEV_REPRESENTATIONS_ABSTRACT_REPRESENTATION_HPP
7#define OPENEV_REPRESENTATIONS_ABSTRACT_REPRESENTATION_HPP
8
9#include "openev/containers/array.hpp"
11#include "openev/containers/vector.hpp"
12#include "openev/core/types.hpp"
13#include "openev/options.hpp"
14#include <array>
15#include <cstddef>
16#include <float.h>
17#include <opencv2/core/mat.hpp>
18#include <opencv2/core/matx.hpp>
19#include <opencv2/core/traits.hpp>
20#include <stdint.h>
21#include <type_traits>
22
23#if OE_HAVE_VIZ
24#include <opencv2/viz/types.hpp>
25#endif
26
27namespace ev {
28
29enum RepresentationOptions : uint8_t {
30 NONE = 0b00000000,
31 IGNORE_POLARITY = 0b00000001,
32 ONLY_IF_POSITIVE = 0b00000010,
33 ONLY_IF_NEGATIVE = 0b00000100
34};
35constexpr bool REPRESENTATION_OPTION_CHECK(const uint8_t a, const RepresentationOptions b) {
36 return static_cast<bool>(a & static_cast<uint8_t>(b));
37}
38
40template <typename T, typename = void>
41struct DataTypeTrait {
42 using type = T;
43};
44
45template <typename T>
46struct DataTypeTrait<T, std::void_t<typename T::value_type>> {
47 using type = typename T::value_type;
48};
49
50template <typename T>
51class TypeHelper {
52public:
53 using Type = T;
54 using TypeArray = std::array<T, 3>;
55 using PrimitiveDataType = typename DataTypeTrait<T>::type;
56 using FloatingPointType = typename std::conditional<std::is_same<T, double>::value, double, float>::type;
57 using ChannelType = typename cv::Mat_<PrimitiveDataType>;
58 static constexpr int NumChannels = cv::DataType<T>::channels;
59
60 static constexpr TypeArray initialize() {
61 if constexpr(std::is_floating_point_v<PrimitiveDataType>) {
62 return TypeArray{repeat(1.0), repeat(-1.0), repeat(0.0)};
63 } else {
64 if constexpr(NumChannels == 1) {
65 constexpr Type white = 255;
66 constexpr Type black = 0;
67 constexpr Type gray = 128;
68 return TypeArray{white, black, gray};
69 } else {
70 return TypeArray{Type(255, 0, 0), Type(0, 0, 255), Type(0, 0, 0)};
71 }
72 }
73 }
74
75 static constexpr Type repeat(const double &value) {
76 if constexpr(NumChannels == 1) {
77 return static_cast<Type>(value);
78 } else {
79 Type ret;
80 for(int i = 0; i < NumChannels; i++) {
81 ret[i] = value;
82 }
83 return ret;
84 }
85 }
86
87#if OE_HAVE_VIZ
88 static constexpr Type convert(const cv::viz::Color &color) {
89 if constexpr(NumChannels == 1) {
90 return color[0];
91 } else {
92 Type ret;
93 for(int i = 0; i < NumChannels; i++) {
94 ret[i] = color[i];
95 }
96 return ret;
97 }
98 }
99
100 static cv::viz::Color convert(const Type &noncolor) {
101 if constexpr(NumChannels == 1) {
102 return cv::viz::Color(noncolor);
103 } else {
104 cv::viz::Color ret;
105 for(int i = 0; i < NumChannels; i++) {
106 ret[i] = noncolor[i];
107 }
108 return ret;
109 }
110 }
111#endif
112};
118template <typename T, const RepresentationOptions Options = RepresentationOptions::NONE, typename E = int>
120public:
121 using Type = typename TypeHelper<T>::Type;
127 [[nodiscard]] inline std::size_t count() const { return count_; }
128
133 [[nodiscard]] inline double duration() const {
134 if(tLimits_[MIN] == DBL_MAX || tLimits_[MAX] == DBL_MIN) {
135 return -1;
136 }
137 return tLimits_[MAX] - tLimits_[MIN];
138 }
139
144 [[nodiscard]] inline double midTime() const {
145 if(tLimits_[MIN] == DBL_MAX || tLimits_[MAX] == DBL_MIN) {
146 return -1;
147 }
148 return 0.5 * (tLimits_[MAX] + tLimits_[MIN]);
149 }
150
155 void clear();
156
161 void clear(const cv::Mat &background);
162
169 bool insert(const Event_<E> &e);
170
176 template <std::size_t N>
177 bool insert(const Array_<E, N> &array);
178
184 bool insert(const Vector_<E> &vector);
185
192 bool insert(Queue_<E> &queue, const bool keep_events_in_queue = false);
193
199 void setTimeOffset(const Event_<E> &e) {
200 timeOffset_ = -e.t;
201 }
202
208 inline void setValue(const bool polarity, const Type &value) {
209 if(polarity) {
210 V_ON = value;
211 } else {
212 V_OFF = value;
213 }
214 colormap_ = nullptr;
215 this->clear();
216 }
217
222 inline void setValue(const Type &value) {
223 V_RESET = value;
224 colormap_ = nullptr;
225 this->clear();
226 }
227
234 inline void setValues(const Type &positive, const Type &negative, const Type &reset) {
235 V_ON = positive;
236 V_OFF = negative;
237 V_RESET = reset;
238 colormap_ = nullptr;
239 this->clear();
240 }
241
242#if OE_HAVE_VIZ
249 inline void setColor(const bool polarity, const cv::viz::Color &color) {
250 setValue(polarity, TypeHelper<T>::convert(color));
251 }
252
258 inline void setColor(const cv::viz::Color &color) {
259 setValue(TypeHelper<T>::convert(color));
260 }
261
269 inline void setColors(const cv::viz::Color &positive, const cv::viz::Color &negative, const cv::viz::Color &reset) {
270 setValues(TypeHelper<T>::convert(positive), TypeHelper<T>::convert(negative), TypeHelper<T>::convert(reset));
271 }
272#endif
273
279 inline void setColormap(const cv::ColormapTypes cm) {
280 if constexpr(TypeHelper<T>::NumChannels == 1) {
281 ev::logger::error("setColorMap: Colormap can only be used with 3-channel representations");
282 } else {
283 colormap_ = std::make_unique<cv::ColormapTypes>(cm);
284
285 cv::Mat aux1 = (cv::Mat_<uchar>(1, 3) << 0, 128, 255);
286 cv::Mat aux3;
287 cv::applyColorMap(aux1, aux3, *colormap_);
288
289 if constexpr(REPRESENTATION_OPTION_CHECK(Options, RepresentationOptions::IGNORE_POLARITY)) {
290 V_ON = aux3.at<cv::Vec3b>(0, 2);
291 V_RESET = aux3.at<cv::Vec3b>(0, 0);
292 } else {
293 V_ON = aux3.at<cv::Vec3b>(0, 2);
294 V_OFF = aux3.at<cv::Vec3b>(0, 0);
295 V_RESET = aux3.at<cv::Vec3b>(0, 1);
296 }
297 this->clear();
298 }
299 }
300
301protected:
303 enum : uint8_t { MIN,
304 MAX };
305
306 Type V_ON = TypeHelper<T>::initialize()[0];
307 Type V_OFF = TypeHelper<T>::initialize()[1];
308 Type V_RESET = TypeHelper<T>::initialize()[2];
309
310 double timeOffset_{0};
311 std::array<double, 2> tLimits_{DBL_MAX, DBL_MIN};
312 std::size_t count_{0};
313 std::unique_ptr<cv::ColormapTypes> colormap_;
314
315 virtual void clear_() = 0;
316 virtual void clear_(const cv::Mat &background) = 0;
317 virtual bool insert_(const Event_<E> &e) = 0;
319};
320
321} // namespace ev
322
324#include "openev/representations/abstract-representation.tpp"
327#endif // OPENEV_REPRESENTATIONS_ABSTRACT_REPRESENTATION_HPP
This is an auxiliary class. This class cannot be instanced.
Definition abstract-representation.hpp:119
double midTime() const
Calculate the midpoint time between the oldest and the newest event.
Definition abstract-representation.hpp:144
void clear()
Remove all events from the representation.
bool insert(const Vector_< E > &vector)
Insert a vector of events in the representation.
typename TypeHelper< T >::Type Type
Definition abstract-representation.hpp:121
bool insert(const Event_< E > &e)
Insert one event in the representation.
void setColormap(const cv::ColormapTypes cm)
Set colormap for the representation.
Definition abstract-representation.hpp:279
double duration() const
Time difference between the oldest and the newest event integrated in the representation.
Definition abstract-representation.hpp:133
std::size_t count() const
Number of events integrated in the representation.
Definition abstract-representation.hpp:127
void setValue(const bool polarity, const Type &value)
Set values for ON and OFF pixels.
Definition abstract-representation.hpp:208
bool insert(const Array_< E, N > &array)
Insert an array of events in the representation.
bool insert(Queue_< E > &queue, const bool keep_events_in_queue=false)
Insert a queue of events in the representation.
void setValues(const Type &positive, const Type &negative, const Type &reset)
Set values for ON, OFF, and non-activated pixels.
Definition abstract-representation.hpp:234
void setTimeOffset(const Event_< E > &e)
Set time offset.
Definition abstract-representation.hpp:199
void clear(const cv::Mat &background)
Remove all events from the representation and add a background image.
void setValue(const Type &value)
Set value for non-activated pixels (background).
Definition abstract-representation.hpp:222
This class extends std::array to implement event arrays. For more information, please refer here.
Definition array.hpp:21
This class extends cv::Point_<T> for event data. For more information, please refer here.
Definition types.hpp:60
double t
Definition types.hpp:62
This class extends std::queue to implement event queues. For more information, please refer here.
Definition queue.hpp:35
This class extends std::vector to implement event vectors. For more information, please refer here.
Definition vector.hpp:36
OpenEV compilation options.
Queue container for basic event structures.
Basic event-based vision structures based on OpenCV components.