6#ifndef OPENEV_REPRESENTATIONS_ABSTRACT_REPRESENTATION_HPP
7#define OPENEV_REPRESENTATIONS_ABSTRACT_REPRESENTATION_HPP
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/core/utils/logger.hpp>
20#include <opencv2/imgproc.hpp>
21#include <opencv2/viz/types.hpp>
27template <
typename T, std::
size_t N>
37enum RepresentationOptions : uint8_t {
39 IGNORE_POLARITY = 0b00000001,
40 ONLY_IF_POSITIVE = 0b00000010,
41 ONLY_IF_NEGATIVE = 0b00000100
43constexpr bool REPRESENTATION_OPTION_CHECK(
const uint8_t a,
const RepresentationOptions b) {
44 return static_cast<bool>(a &
static_cast<uint8_t
>(b));
48template <
typename T,
typename =
void>
54struct DataTypeTrait<T, std::void_t<typename T::value_type>> {
55 using type =
typename T::value_type;
62 using TypeArray = std::array<T, 3>;
63 using PrimitiveDataType =
typename DataTypeTrait<T>::type;
64 using FloatingPointType =
typename std::conditional<std::is_same<T, double>::value, double,
float>::type;
65 using ChannelType =
typename cv::Mat_<PrimitiveDataType>;
66 static constexpr int NumChannels = cv::DataType<T>::channels;
68 static constexpr TypeArray initialize() {
69 if constexpr(std::is_floating_point_v<PrimitiveDataType>) {
70 return TypeArray{repeat(1.0), repeat(-1.0), repeat(0.0)};
72 if constexpr(NumChannels == 1) {
73 constexpr Type white = 255;
74 constexpr Type black = 0;
75 constexpr Type gray = 128;
76 return TypeArray{white, black, gray};
78 return TypeArray{Type(255, 0, 0), Type(0, 0, 255), Type(0, 0, 0)};
83 static constexpr Type repeat(
const double &value) {
84 if constexpr(NumChannels == 1) {
85 return static_cast<Type
>(value);
88 for(
int i = 0; i < NumChannels; i++) {
95 static constexpr Type convert(
const cv::viz::Color &color) {
96 if constexpr(NumChannels == 1) {
100 for(
int i = 0; i < NumChannels; i++) {
107 static cv::viz::Color convert(
const Type &noncolor) {
108 if constexpr(NumChannels == 1) {
109 return cv::viz::Color(noncolor);
112 for(
int i = 0; i < NumChannels; i++) {
113 ret[i] = noncolor[i];
124template <
typename T, const RepresentationOptions Options = RepresentationOptions::NONE,
typename E =
int>
127 using Type =
typename TypeHelper<T>::Type;
142 [[nodiscard]] inline std::
size_t count()
const {
return count_; }
149 if(tLimits_[MIN] == std::numeric_limits<TimeType>::max() || tLimits_[MAX] == std::numeric_limits<TimeType>::min()) {
152 return tLimits_[MAX] - tLimits_[MIN];
159 [[nodiscard]]
inline TimeType
midTime()
const {
160 if(tLimits_[MIN] == std::numeric_limits<TimeType>::max() || tLimits_[MAX] == std::numeric_limits<TimeType>::min()) {
163 return 0.5f * (tLimits_[MAX] + tLimits_[MIN]);
176 void clear(
const cv::Mat &background);
191 template <std::
size_t N>
263 inline void setColor(
const bool polarity,
const cv::viz::Color &color) {
264 setValue(polarity, TypeHelper<T>::convert(color));
272 inline void setColor(
const cv::viz::Color &color) {
273 setValue(TypeHelper<T>::convert(color));
283 inline void setColors(
const cv::viz::Color &positive,
const cv::viz::Color &negative,
const cv::viz::Color &reset) {
284 setValues(TypeHelper<T>::convert(positive), TypeHelper<T>::convert(negative), TypeHelper<T>::convert(reset));
293 if constexpr(TypeHelper<T>::NumChannels == 1) {
294 CV_LOG_ERROR(
nullptr,
"setColorMap: Colormap can only be used with 3-channel representations");
296 colormap_ = std::make_unique<cv::ColormapTypes>(cm);
298 constexpr std::array<uchar, 3> aux1_data = {0, 128, 255};
299 cv::Mat aux1(1, 3, CV_8UC1,
const_cast<uchar *
>(aux1_data.data()));
301 cv::applyColorMap(aux1, aux3, *colormap_);
303 if constexpr(REPRESENTATION_OPTION_CHECK(Options, RepresentationOptions::IGNORE_POLARITY)) {
304 V_ON = aux3.at<cv::Vec3b>(0, 2);
305 V_RESET = aux3.at<cv::Vec3b>(0, 0);
307 V_ON = aux3.at<cv::Vec3b>(0, 2);
308 V_OFF = aux3.at<cv::Vec3b>(0, 0);
309 V_RESET = aux3.at<cv::Vec3b>(0, 1);
317 enum : uint8_t { MIN,
320 Type V_ON = TypeHelper<T>::initialize()[0];
321 Type V_OFF = TypeHelper<T>::initialize()[1];
322 Type V_RESET = TypeHelper<T>::initialize()[2];
324 TimeType timeOffset_{0};
325 std::array<TimeType, 2> tLimits_{std::numeric_limits<TimeType>::max(), std::numeric_limits<TimeType>::min()};
326 std::size_t count_{0};
327 std::unique_ptr<cv::ColormapTypes> colormap_;
329 virtual void clear_() = 0;
330 virtual void clear_(
const cv::Mat &background) = 0;
331 virtual bool insert_(
const Event_<E> &e) = 0;
338#include "openev/representations/abstract-representation.tpp"
This is an auxiliary class. This class cannot be instanced.
Definition abstract-representation.hpp:125
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:283
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:272
typename TypeHelper< T >::Type Type
Definition abstract-representation.hpp:127
TimeType midTime() const
Calculate the midpoint time between the oldest and the newest event.
Definition abstract-representation.hpp:159
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:263
void setColormap(const cv::ColormapTypes cm)
Set colormap for the representation.
Definition abstract-representation.hpp:292
std::size_t count() const
Number of events integrated in the representation.
Definition abstract-representation.hpp:142
void setValue(const bool polarity, const Type &value)
Set values for ON and OFF pixels.
Definition abstract-representation.hpp:223
TimeType duration() const
Time difference between the oldest and the newest event integrated in the representation.
Definition abstract-representation.hpp:148
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:249
void setTimeOffset(const Event_< E > &e)
Set time offset.
Definition abstract-representation.hpp:214
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:237
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:77
TimeType t
Definition types.hpp:79
This class extends std::queue to implement event queues. For more information, please refer here.
Definition queue.hpp:23
This class extends std::vector to implement event vectors. For more information, please refer here.
Definition vector.hpp:23
Basic event-based vision structures based on OpenCV components.