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 "openev/core/types.hpp"
11#include <array>
12#include <cstddef>
13#include <limits>
14#include <memory>
15#include <opencv2/core/hal/interface.h>
16#include <opencv2/core/mat.hpp>
17#include <opencv2/core/mat.inl.hpp>
18#include <opencv2/core/matx.hpp>
19#include <opencv2/core/traits.hpp>
20#include <opencv2/core/utils/logger.hpp>
21#include <opencv2/imgproc.hpp>
22#include <opencv2/viz/types.hpp>
23#include <stdint.h>
24#include <type_traits>
25
26namespace ev {
28template <typename T, std::size_t N>
29class Array_;
30template <typename T>
31class Event_;
32template <typename T>
33class Queue_;
34template <typename T>
35class Vector_;
37
38enum RepresentationOptions : uint8_t {
39 NONE = 0b00000000,
40 IGNORE_POLARITY = 0b00000001,
41 ONLY_IF_POSITIVE = 0b00000010,
42 ONLY_IF_NEGATIVE = 0b00000100
43};
44constexpr bool REPRESENTATION_OPTION_CHECK(const uint8_t a, const RepresentationOptions b) {
45 return static_cast<bool>(a & static_cast<uint8_t>(b));
46}
47
49template <typename T, typename = void>
50struct DataTypeTrait {
51 using type = T;
52};
53
54template <typename T>
55struct DataTypeTrait<T, std::void_t<typename T::value_type>> {
56 using type = typename T::value_type;
57};
58
59template <typename T>
60class TypeHelper {
61public:
62 using Type = T;
63 using TypeArray = std::array<T, 3>;
64 using PrimitiveDataType = typename DataTypeTrait<T>::type;
65 using FloatingPointType = typename std::conditional<std::is_same<T, double>::value, double, float>::type;
66 using ChannelType = typename cv::Mat_<PrimitiveDataType>;
67 static constexpr int NumChannels = cv::DataType<T>::channels;
68
69 static constexpr TypeArray initialize() {
70 if constexpr(std::is_floating_point_v<PrimitiveDataType>) {
71 return TypeArray{repeat(1.0), repeat(-1.0), repeat(0.0)};
72 } else {
73 if constexpr(NumChannels == 1) {
74 constexpr Type white = 255;
75 constexpr Type black = 0;
76 constexpr Type gray = 128;
77 return TypeArray{white, black, gray};
78 } else {
79 return TypeArray{Type(255, 0, 0), Type(0, 0, 255), Type(0, 0, 0)};
80 }
81 }
82 }
83
84 static constexpr Type repeat(const double &value) {
85 if constexpr(NumChannels == 1) {
86 return static_cast<Type>(value);
87 } else {
88 Type ret;
89 for(int i = 0; i < NumChannels; i++) {
90 ret[i] = value;
91 }
92 return ret;
93 }
94 }
95
96 static constexpr Type convert(const cv::viz::Color &color) {
97 if constexpr(NumChannels == 1) {
98 return color[0];
99 } else {
100 Type ret;
101 for(int i = 0; i < NumChannels; i++) {
102 ret[i] = color[i];
103 }
104 return ret;
105 }
106 }
107
108 static cv::viz::Color convert(const Type &noncolor) {
109 if constexpr(NumChannels == 1) {
110 return cv::viz::Color(noncolor);
111 } else {
112 cv::viz::Color ret;
113 for(int i = 0; i < NumChannels; i++) {
114 ret[i] = noncolor[i];
115 }
116 return ret;
117 }
118 }
119};
121
125template <typename T, const RepresentationOptions Options = RepresentationOptions::NONE, typename E = int>
126class AbstractRepresentation_ : public Stats_<AbstractRepresentation_<T, Options, E>> {
127 static_assert(TypeHelper<T>::NumChannels == 1 || TypeHelper<T>::NumChannels == 3, "ev::AbstractRepresentation_: only 1- and 3-channel representations are supported.");
128 static_assert(std::is_arithmetic_v<E>, "ev::AbstractRepresentation_: the event coordinate type must be arithmetic.");
129
130public:
131 using Type = typename TypeHelper<T>::Type;
132
134 AbstractRepresentation_() = default;
135 virtual ~AbstractRepresentation_() = default;
138 AbstractRepresentation_ &operator=(const AbstractRepresentation_ &) = delete;
139 AbstractRepresentation_ &operator=(AbstractRepresentation_ &&) noexcept = delete;
141
146 [[nodiscard]] inline std::size_t count() const { return count_; }
147
152 [[nodiscard]] inline TimeType firstTimestamp() const { return count_ == 0 ? 0 : tLimits_[MIN]; }
153
158 [[nodiscard]] inline TimeType lastTimestamp() const { return count_ == 0 ? 0 : tLimits_[MAX]; }
159
164 void clear();
165
171 void clear(const cv::Mat &background, const cv::Point &origin = {0, 0});
172
179 bool insert(const Event_<E> &e);
180
186 template <std::size_t N>
187 bool insert(const Array_<E, N> &array);
188
194 bool insert(const Vector_<E> &vector);
195
202 bool insert(Queue_<E> &queue, const bool keep_events_in_queue = false);
203
209 void setTimeOffset(const Event_<E> &e) {
210 timeOffset_ = -e.t;
211 }
212
218 inline void setValue(const bool polarity, const Type &value) {
219 if(polarity) {
220 V_ON = value;
221 } else {
222 V_OFF = value;
223 }
224 colormap_ = nullptr;
225 this->clear();
226 }
227
232 inline void setValue(const Type &value) {
233 V_RESET = value;
234 colormap_ = nullptr;
235 this->clear();
236 }
237
244 inline void setValues(const Type &positive, const Type &negative, const Type &reset) {
245 V_ON = positive;
246 V_OFF = negative;
247 V_RESET = reset;
248 colormap_ = nullptr;
249 this->clear();
250 }
251
258 inline void setColor(const bool polarity, const cv::viz::Color &color) {
259 setValue(polarity, TypeHelper<T>::convert(color));
260 }
261
267 inline void setColor(const cv::viz::Color &color) {
268 setValue(TypeHelper<T>::convert(color));
269 }
270
278 inline void setColors(const cv::viz::Color &positive, const cv::viz::Color &negative, const cv::viz::Color &reset) {
279 setValues(TypeHelper<T>::convert(positive), TypeHelper<T>::convert(negative), TypeHelper<T>::convert(reset));
280 }
281
287 inline void setColormap(const cv::ColormapTypes cm) {
288 static_assert(TypeHelper<T>::NumChannels == 3, "ev::AbstractRepresentation_::setColormap: a colormap can only be used with 3-channel representations.");
289 colormap_ = std::make_unique<cv::ColormapTypes>(cm);
290
291 constexpr std::array<uchar, 3> aux1_data = {0, 128, 255};
292 cv::Mat aux1(1, 3, CV_8UC1, const_cast<uchar *>(aux1_data.data()));
293 cv::Mat aux3;
294 cv::applyColorMap(aux1, aux3, *colormap_);
295
296 if constexpr(REPRESENTATION_OPTION_CHECK(Options, RepresentationOptions::IGNORE_POLARITY)) {
297 V_ON = aux3.at<cv::Vec3b>(0, 2);
298 V_RESET = aux3.at<cv::Vec3b>(0, 0);
299 } else {
300 V_ON = aux3.at<cv::Vec3b>(0, 2);
301 V_OFF = aux3.at<cv::Vec3b>(0, 0);
302 V_RESET = aux3.at<cv::Vec3b>(0, 1);
303 }
304 this->clear();
305 }
306
307protected:
309 enum : uint8_t { MIN,
310 MAX };
311
312 Type V_ON = TypeHelper<T>::initialize()[0];
313 Type V_OFF = TypeHelper<T>::initialize()[1];
314 Type V_RESET = TypeHelper<T>::initialize()[2];
315
316 TimeType timeOffset_{0};
317 std::array<TimeType, 2> tLimits_{std::numeric_limits<TimeType>::max(), std::numeric_limits<TimeType>::min()};
318 std::size_t count_{0};
319 std::unique_ptr<cv::ColormapTypes> colormap_;
320 cv::Mat background_;
321
322 [[nodiscard]] virtual cv::Size frameSize_() const = 0;
323 virtual void clear_() = 0;
324 virtual void clear_(const cv::Mat &background) = 0;
325 virtual bool insert_(const Event_<E> &e) = 0;
327};
328
329} // namespace ev
330
332#include "openev/representations/abstract-representation.tpp"
334
335#endif // OPENEV_REPRESENTATIONS_ABSTRACT_REPRESENTATION_HPP
This is an auxiliary class. This class cannot be instanced.
Definition abstract-representation.hpp:126
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:278
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:267
typename TypeHelper< T >::Type Type
Definition abstract-representation.hpp:131
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:258
TimeType firstTimestamp() const
Timestamp of the oldest event integrated in the representation.
Definition abstract-representation.hpp:152
void setColormap(const cv::ColormapTypes cm)
Set colormap for the representation.
Definition abstract-representation.hpp:287
TimeType lastTimestamp() const
Timestamp of the newest event integrated in the representation.
Definition abstract-representation.hpp:158
std::size_t count() const
Number of events integrated in the representation.
Definition abstract-representation.hpp:146
void setValue(const bool polarity, const Type &value)
Set values for ON and OFF pixels.
Definition abstract-representation.hpp:218
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:244
void clear(const cv::Mat &background, const cv::Point &origin={0, 0})
Remove all events from the representation and add a background image.
void setTimeOffset(const Event_< E > &e)
Set time offset.
Definition abstract-representation.hpp:209
void setValue(const Type &value)
Set value for non-activated pixels (background).
Definition abstract-representation.hpp:232
This class extends std::array to implement event arrays. For more information, please refer here.
Definition array.hpp:24
This class extends cv::Point_<T> for event data. For more information, please refer here.
Definition types.hpp:86
TimeType t
Definition types.hpp:90
This class extends std::queue to implement event queues. For more information, please refer here.
Definition queue.hpp:32
This is an auxiliary class. This class cannot be instanced.
Definition stats.hpp:51
This class extends std::vector to implement event vectors. For more information, please refer here.
Definition vector.hpp:23
Statistics shared by everything that accounts for events.
Basic event-based vision structures based on OpenCV components.