6#ifndef OPENEV_REPRESENTATIONS_ABSTRACT_REPRESENTATION_HPP
7#define OPENEV_REPRESENTATIONS_ABSTRACT_REPRESENTATION_HPP
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>
28template <
typename T, std::
size_t N>
38enum RepresentationOptions : uint8_t {
40 IGNORE_POLARITY = 0b00000001,
41 ONLY_IF_POSITIVE = 0b00000010,
42 ONLY_IF_NEGATIVE = 0b00000100
44constexpr bool REPRESENTATION_OPTION_CHECK(
const uint8_t a,
const RepresentationOptions b) {
45 return static_cast<bool>(a &
static_cast<uint8_t
>(b));
49template <
typename T,
typename =
void>
55struct DataTypeTrait<T, std::void_t<typename T::value_type>> {
56 using type =
typename T::value_type;
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;
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)};
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};
79 return TypeArray{Type(255, 0, 0), Type(0, 0, 255), Type(0, 0, 0)};
84 static constexpr Type repeat(
const double &value) {
85 if constexpr(NumChannels == 1) {
86 return static_cast<Type
>(value);
89 for(
int i = 0; i < NumChannels; i++) {
96 static constexpr Type convert(
const cv::viz::Color &color) {
97 if constexpr(NumChannels == 1) {
101 for(
int i = 0; i < NumChannels; i++) {
108 static cv::viz::Color convert(
const Type &noncolor) {
109 if constexpr(NumChannels == 1) {
110 return cv::viz::Color(noncolor);
113 for(
int i = 0; i < NumChannels; i++) {
114 ret[i] = noncolor[i];
125template <
typename T, const RepresentationOptions Options = RepresentationOptions::NONE,
typename E =
int>
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.");
131 using Type =
typename TypeHelper<T>::Type;
146 [[nodiscard]] inline std::
size_t count()
const {
return count_; }
152 [[nodiscard]]
inline TimeType
firstTimestamp()
const {
return count_ == 0 ? 0 : tLimits_[MIN]; }
158 [[nodiscard]]
inline TimeType
lastTimestamp()
const {
return count_ == 0 ? 0 : tLimits_[MAX]; }
171 void clear(
const cv::Mat &background,
const cv::Point &origin = {0, 0});
186 template <std::
size_t N>
258 inline void setColor(
const bool polarity,
const cv::viz::Color &color) {
259 setValue(polarity, TypeHelper<T>::convert(color));
267 inline void setColor(
const cv::viz::Color &color) {
268 setValue(TypeHelper<T>::convert(color));
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));
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);
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()));
294 cv::applyColorMap(aux1, aux3, *colormap_);
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);
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);
309 enum : uint8_t { MIN,
312 Type V_ON = TypeHelper<T>::initialize()[0];
313 Type V_OFF = TypeHelper<T>::initialize()[1];
314 Type V_RESET = TypeHelper<T>::initialize()[2];
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_;
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;
332#include "openev/representations/abstract-representation.tpp"
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.