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
10#include <array>
11#include <cstddef>
12#include <float.h>
13#include <memory>
14#include <opencv2/core/hal/interface.h>
15#include <opencv2/core/mat.hpp>
16#include <opencv2/core/mat.inl.hpp>
17#include <opencv2/core/matx.hpp>
18#include <opencv2/core/traits.hpp>
19#include <opencv2/imgproc.hpp>
20#include <opencv2/viz/types.hpp>
21#include <stdint.h>
22#include <type_traits>
23
24namespace ev {
26template <typename T, std::size_t N>
27class Array_;
28template <typename T>
29class Event_;
30template <typename T>
31class Queue_;
32template <typename T>
33class Vector_;
35
36enum RepresentationOptions : uint8_t {
37 NONE = 0b00000000,
38 IGNORE_POLARITY = 0b00000001,
39 ONLY_IF_POSITIVE = 0b00000010,
40 ONLY_IF_NEGATIVE = 0b00000100
41};
42constexpr bool REPRESENTATION_OPTION_CHECK(const uint8_t a, const RepresentationOptions b) {
43 return static_cast<bool>(a & static_cast<uint8_t>(b));
44}
45
47template <typename T, typename = void>
48struct DataTypeTrait {
49 using type = T;
50};
51
52template <typename T>
53struct DataTypeTrait<T, std::void_t<typename T::value_type>> {
54 using type = typename T::value_type;
55};
56
57template <typename T>
58class TypeHelper {
59public:
60 using Type = T;
61 using TypeArray = std::array<T, 3>;
62 using PrimitiveDataType = typename DataTypeTrait<T>::type;
63 using FloatingPointType = typename std::conditional<std::is_same<T, double>::value, double, float>::type;
64 using ChannelType = typename cv::Mat_<PrimitiveDataType>;
65 static constexpr int NumChannels = cv::DataType<T>::channels;
66
67 static constexpr TypeArray initialize() {
68 if constexpr(std::is_floating_point_v<PrimitiveDataType>) {
69 return TypeArray{repeat(1.0), repeat(-1.0), repeat(0.0)};
70 } else {
71 if constexpr(NumChannels == 1) {
72 constexpr Type white = 255;
73 constexpr Type black = 0;
74 constexpr Type gray = 128;
75 return TypeArray{white, black, gray};
76 } else {
77 return TypeArray{Type(255, 0, 0), Type(0, 0, 255), Type(0, 0, 0)};
78 }
79 }
80 }
81
82 static constexpr Type repeat(const double &value) {
83 if constexpr(NumChannels == 1) {
84 return static_cast<Type>(value);
85 } else {
86 Type ret;
87 for(int i = 0; i < NumChannels; i++) {
88 ret[i] = value;
89 }
90 return ret;
91 }
92 }
93
94 static constexpr Type convert(const cv::viz::Color &color) {
95 if constexpr(NumChannels == 1) {
96 return color[0];
97 } else {
98 Type ret;
99 for(int i = 0; i < NumChannels; i++) {
100 ret[i] = color[i];
101 }
102 return ret;
103 }
104 }
105
106 static cv::viz::Color convert(const Type &noncolor) {
107 if constexpr(NumChannels == 1) {
108 return cv::viz::Color(noncolor);
109 } else {
110 cv::viz::Color ret;
111 for(int i = 0; i < NumChannels; i++) {
112 ret[i] = noncolor[i];
113 }
114 return ret;
115 }
116 }
117};
119
123template <typename T, const RepresentationOptions Options = RepresentationOptions::NONE, typename E = int>
125public:
126 using Type = typename TypeHelper<T>::Type;
127
132 [[nodiscard]] inline std::size_t count() const { return count_; }
133
138 [[nodiscard]] inline double duration() const {
139 if(tLimits_[MIN] == DBL_MAX || tLimits_[MAX] == DBL_MIN) {
140 return -1;
141 }
142 return tLimits_[MAX] - tLimits_[MIN];
143 }
144
149 [[nodiscard]] inline double midTime() const {
150 if(tLimits_[MIN] == DBL_MAX || tLimits_[MAX] == DBL_MIN) {
151 return -1;
152 }
153 return 0.5 * (tLimits_[MAX] + tLimits_[MIN]);
154 }
155
160 void clear();
161
166 void clear(const cv::Mat &background);
167
174 bool insert(const Event_<E> &e);
175
181 template <std::size_t N>
182 bool insert(const Array_<E, N> &array);
183
189 bool insert(const Vector_<E> &vector);
190
197 bool insert(Queue_<E> &queue, const bool keep_events_in_queue = false);
198
204 void setTimeOffset(const Event_<E> &e) {
205 timeOffset_ = -e.t;
206 }
207
213 inline void setValue(const bool polarity, const Type &value) {
214 if(polarity) {
215 V_ON = value;
216 } else {
217 V_OFF = value;
218 }
219 colormap_ = nullptr;
220 this->clear();
221 }
222
227 inline void setValue(const Type &value) {
228 V_RESET = value;
229 colormap_ = nullptr;
230 this->clear();
231 }
232
239 inline void setValues(const Type &positive, const Type &negative, const Type &reset) {
240 V_ON = positive;
241 V_OFF = negative;
242 V_RESET = reset;
243 colormap_ = nullptr;
244 this->clear();
245 }
246
253 inline void setColor(const bool polarity, const cv::viz::Color &color) {
254 setValue(polarity, TypeHelper<T>::convert(color));
255 }
256
262 inline void setColor(const cv::viz::Color &color) {
263 setValue(TypeHelper<T>::convert(color));
264 }
265
273 inline void setColors(const cv::viz::Color &positive, const cv::viz::Color &negative, const cv::viz::Color &reset) {
274 setValues(TypeHelper<T>::convert(positive), TypeHelper<T>::convert(negative), TypeHelper<T>::convert(reset));
275 }
276
282 inline void setColormap(const cv::ColormapTypes cm) {
283 if constexpr(TypeHelper<T>::NumChannels == 1) {
284 ev::logger::error("setColorMap: Colormap can only be used with 3-channel representations");
285 } else {
286 colormap_ = std::make_unique<cv::ColormapTypes>(cm);
287
288 constexpr std::array<uchar, 3> aux1_data = {0, 128, 255};
289 cv::Mat aux1(1, 3, CV_8UC1, const_cast<uchar *>(aux1_data.data()));
290 cv::Mat aux3;
291 cv::applyColorMap(aux1, aux3, *colormap_);
292
293 if constexpr(REPRESENTATION_OPTION_CHECK(Options, RepresentationOptions::IGNORE_POLARITY)) {
294 V_ON = aux3.at<cv::Vec3b>(0, 2);
295 V_RESET = aux3.at<cv::Vec3b>(0, 0);
296 } else {
297 V_ON = aux3.at<cv::Vec3b>(0, 2);
298 V_OFF = aux3.at<cv::Vec3b>(0, 0);
299 V_RESET = aux3.at<cv::Vec3b>(0, 1);
300 }
301 this->clear();
302 }
303 }
304
305protected:
307 enum : uint8_t { MIN,
308 MAX };
309
310 Type V_ON = TypeHelper<T>::initialize()[0];
311 Type V_OFF = TypeHelper<T>::initialize()[1];
312 Type V_RESET = TypeHelper<T>::initialize()[2];
313
314 double timeOffset_{0};
315 std::array<double, 2> tLimits_{DBL_MAX, DBL_MIN};
316 std::size_t count_{0};
317 std::unique_ptr<cv::ColormapTypes> colormap_;
318
319 virtual void clear_() = 0;
320 virtual void clear_(const cv::Mat &background) = 0;
321 virtual bool insert_(const Event_<E> &e) = 0;
323};
324
325} // namespace ev
326
328#include "openev/representations/abstract-representation.tpp"
330
331#endif // OPENEV_REPRESENTATIONS_ABSTRACT_REPRESENTATION_HPP
This is an auxiliary class. This class cannot be instanced.
Definition abstract-representation.hpp:124
double midTime() const
Calculate the midpoint time between the oldest and the newest event.
Definition abstract-representation.hpp:149
void setColors(const cv::viz::Color &positive, const cv::viz::Color &negative, const cv::viz::Color &reset)
Set colors for ON, OFF, and non-activated pixels. For more information, please refer here.
Definition abstract-representation.hpp:273
void clear()
Remove all events from the representation.
bool insert(const Vector_< E > &vector)
Insert a vector of events in the representation.
void setColor(const cv::viz::Color &color)
Set colors for non-activated pixels (background). For more information, please refer here.
Definition abstract-representation.hpp:262
typename TypeHelper< T >::Type Type
Definition abstract-representation.hpp:126
bool insert(const Event_< E > &e)
Insert one event in the representation.
void setColor(const bool polarity, const cv::viz::Color &color)
Set colors for ON and OFF pixels. For more information, please refer here.
Definition abstract-representation.hpp:253
void setColormap(const cv::ColormapTypes cm)
Set colormap for the representation.
Definition abstract-representation.hpp:282
double duration() const
Time difference between the oldest and the newest event integrated in the representation.
Definition abstract-representation.hpp:138
std::size_t count() const
Number of events integrated in the representation.
Definition abstract-representation.hpp:132
void setValue(const bool polarity, const Type &value)
Set values for ON and OFF pixels.
Definition abstract-representation.hpp:213
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:239
void setTimeOffset(const Event_< E > &e)
Set time offset.
Definition abstract-representation.hpp:204
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:227
This class extends std::array to implement event arrays. For more information, please refer here.
Definition array.hpp:22
This class extends cv::Point_<T> for event data. For more information, please refer here.
Definition types.hpp:55
double t
Definition types.hpp:57
This class extends std::queue to implement event queues. For more information, please refer here.
Definition queue.hpp:20
This class extends std::vector to implement event vectors. For more information, please refer here.
Definition vector.hpp:20
Logger utility.
void error(const char *message, const bool assert_condition=false)
Log message at error level.
Definition logger.hpp:38