Image.h
Go to the documentation of this file.
1 #pragma once
2 
5 
6 #include <VisionX/libraries/armem/vision/images/core/aron/ImageDepth.aron.generated.h>
7 #include <VisionX/libraries/armem/vision/images/core/aron/ImageRGB.aron.generated.h>
9 
10 #include "aron_conversions.h"
11 
12 // IVT
13 #include <Image/ByteImage.h>
14 
16 {
17  class Builder;
18 }
19 
20 namespace visionx::armem_images
21 {
22  namespace detail
23  {
24  void checkNotNull(void* ptr, const char* what);
25  }
26 
27  /**
28  * @brief A class mediating between the ImageProvider/Processor APIs
29  * (based on IVT CByteImages or raw pixel buffers) and the ARON image API
30  * (based on cv::Mat).
31  *
32  * This is the non-template base class of `Image`.
33  */
34  class ImageBase
35  {
36  public:
38  virtual ~ImageBase();
39 
40 
41  virtual void resetImage(int rows, int cols) = 0;
42 
43  virtual cv::Mat& getImage() = 0;
44  virtual const cv::Mat& getImage() const = 0;
45 
46  virtual std::string printFormat() const = 0;
47 
48 
49  // IMAGE PROCESSOR TO MEMORY
50 
51  virtual bool usePixels(CByteImage** inputImages);
52  virtual bool usePixels(CByteImage* inputImage) = 0;
53  virtual bool usePixels(void** inputPixelBuffers);
54  virtual bool usePixels(void* inputPixelBuffers) = 0;
55 
56  virtual armarx::aron::data::DictPtr toAron() const = 0;
57 
58 
59  // MEMORY TO IMAGE PROVIDER
60  void addQuery(armarx::armem::client::query::Builder& queryBuilder) const;
61 
62  virtual bool updateAronImage(const armarx::armem::wm::Memory& memory) = 0;
63  virtual CByteImage toCByteImage() const = 0;
64 
65 
66  static bool areSameSize(const CByteImage& lhs, const CByteImage& rhs);
67  static bool areSameSize(const cv::Mat& lhs, const cv::Mat& rhs);
68  static bool areSameSize(const cv::Mat& lhs, const CByteImage& rhs);
69  static bool areSameSize(const CByteImage& lhs, const cv::Mat& rhs);
70 
71 
72  public:
74  size_t imageIndex = 0;
75 
76  bool updated = false;
77  };
78 
79  /**
80  * @brief A class template implementing the interface defined by
81  * `ImageBase` for a specific aron-generated class.
82  *
83  * It is assumed that `AronImageT` has an `image` member of the type
84  * `cv::Mat`, which is generated when using the `Image` tag in ARON XML.
85  */
86  template <class AronImageT>
87  class Image : public ImageBase
88  {
89  public:
90  Image(const armarx::armem::MemoryID& instanceID, size_t imageIndex) :
91  ImageBase(instanceID, imageIndex)
92  {
93  aronImage.resetHard();
94  }
95 
96  cv::Mat&
97  getImage() override
98  {
99  return aronImage.image;
100  }
101 
102  const cv::Mat&
103  getImage() const override
104  {
105  return aronImage.image;
106  }
107 
108  std::string
109  printFormat() const override
110  {
111  std::stringstream ss;
112  ss << aronImage.image.cols << "x" << aronImage.image.rows << " pixels "
113  << " x " << aronImage.image.channels() << " channels/pixel";
114  return ss.str();
115  }
116 
117  /// Initialize the image matrix header with the correct size (and type) but
118  /// without allocating data.
119  void
120  resetImage(int rows, int cols) override
121  {
122  cv::Mat& mat = aronImage.image;
123  switch (mat.type())
124  {
125  case CV_8UC3: // Rgb24
126  /* The matrix will be reset with an external pixel buffer.
127  * We still need to provide a buffer for the moment, but nullptr
128  * is not allowed. In any case, accessing it would be illegal,
129  * and detecting whether or not the matrix owns memory is not
130  * done by checking the passed buffer pointer (but by the
131  * mat's `u` member).
132  */
133  mat = cv::Mat(rows,
134  cols,
135  mat.type(),
136  reinterpret_cast<void*>(1)); // nullptr is not allowed
137  break;
138 
139  case CV_32FC1: // Depth32
140  // We will convert the data, so the mat needs its own buffer.
141  mat.create(rows, cols, mat.type());
142  break;
143  }
144  }
145 
146  bool
147  usePixels(CByteImage* inputImage) override
148  {
149  if (inputImage == nullptr)
150  {
151  return false;
152  }
153  detail::checkNotNull(inputImage, "Image::usePixels(CByteImage* inputImage)");
154  {
155  to_aron(aronImage, *inputImage);
156  }
157  return true;
158  }
159 
160  bool
161  usePixels(void* inputPixelBuffer) override
162  {
163  if (inputPixelBuffer == nullptr)
164  {
165  return false;
166  }
167  detail::checkNotNull(inputPixelBuffer, "Image::usePixels(void* inputPixelBuffer)");
168  {
169  to_aron(aronImage, inputPixelBuffer);
170  }
171  return true;
172  }
173 
175  toAron() const override
176  {
177  return aronImage.toAron();
178  }
179 
180  bool
182  {
183  if (const armarx::armem::wm::EntitySnapshot* snapshot =
184  memory.findLatestSnapshot(instanceID))
185  {
186  if (snapshot->time() > instanceID.timestamp)
187  {
188  if (const armarx::armem::wm::EntityInstance* instance =
189  snapshot->findInstance(instanceID.instanceIndex))
190  {
191  instanceID.timestamp = snapshot->time();
192  aronImage.fromAron(instance->data());
193  updated = true;
194  }
195  }
196  }
197  return updated;
198  }
199 
200  CByteImage
201  toCByteImage() const override
202  {
203  CByteImage img;
204  from_aron(aronImage, img);
205  return img;
206  }
207 
208 
209  public:
210  AronImageT aronImage;
211  };
212 
213 
216 
217 } // namespace visionx::armem_images
visionx::armem_images::ImageBase::updated
bool updated
Definition: Image.h:76
visionx::armem_images::ImageBase::~ImageBase
virtual ~ImageBase()
Definition: Image.cpp:30
visionx::armem_images::from_aron
void from_aron(const AronImageT &dto, CByteImage &bo)
Definition: aron_conversions.h:75
visionx::armem_images::ImageBase::addQuery
void addQuery(armarx::armem::client::query::Builder &queryBuilder) const
Definition: Image.cpp:47
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
MemoryID.h
detail
Definition: OpenCVUtil.cpp:127
visionx::armem_images::Image::toCByteImage
CByteImage toCByteImage() const override
Definition: Image.h:201
visionx::armem_images::ImageBase::updateAronImage
virtual bool updateAronImage(const armarx::armem::wm::Memory &memory)=0
visionx::armem_images::ImageBase::printFormat
virtual std::string printFormat() const =0
visionx::armem_images::Image::toAron
armarx::aron::data::DictPtr toAron() const override
Definition: Image.h:175
visionx::armem_images::ImageBase
A class mediating between the ImageProvider/Processor APIs (based on IVT CByteImages or raw pixel buf...
Definition: Image.h:34
visionx::armem_images::ImageBase::imageIndex
size_t imageIndex
Definition: Image.h:74
aron_conversions.h
visionx::armem_images::ImageBase::instanceID
armarx::armem::MemoryID instanceID
Definition: Image.h:73
visionx::armem_images
Definition: ImageReader.cpp:29
visionx::armem_images::Image::resetImage
void resetImage(int rows, int cols) override
Initialize the image matrix header with the correct size (and type) but without allocating data.
Definition: Image.h:120
visionx::armem_images::ImageBase::toCByteImage
virtual CByteImage toCByteImage() const =0
armarx::armem::client::query
Definition: forward_declarations.h:7
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
visionx::armem_images::Image::printFormat
std::string printFormat() const override
Definition: Image.h:109
armarx::armem::wm::Memory
Client-side working memory.
Definition: memory_definitions.h:133
visionx::armem_images::Image::updateAronImage
bool updateAronImage(const armarx::armem::wm::Memory &memory) override
Definition: Image.h:181
visionx::armem_images::to_aron
void to_aron(AronImageT &dto, void *&bo)
Definition: aron_conversions.h:41
armarx::armem::wm::EntitySnapshot
Client-side working memory entity snapshot.
Definition: memory_definitions.h:80
visionx::armem_images::Image::getImage
cv::Mat & getImage() override
Definition: Image.h:97
visionx::armem_images::detail::checkNotNull
void checkNotNull(void *ptr, const char *what)
Definition: Image.cpp:80
visionx::armem_images::ImageBase::toAron
virtual armarx::aron::data::DictPtr toAron() const =0
visionx::armem_images::Image::usePixels
bool usePixels(CByteImage *inputImage) override
Definition: Image.h:147
memory_definitions.h
visionx::armem_images::Image::usePixels
bool usePixels(void *inputPixelBuffer) override
Definition: Image.h:161
visionx::armem_images::ImageBase::getImage
virtual cv::Mat & getImage()=0
visionx::armem_images::ImageBase::areSameSize
static bool areSameSize(const CByteImage &lhs, const CByteImage &rhs)
Definition: Image.cpp:56
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
visionx::armem_images::Image::getImage
const cv::Mat & getImage() const override
Definition: Image.h:103
OpenCVUtil.h
visionx::armem_images::Image
A class template implementing the interface defined by ImageBase for a specific aron-generated class.
Definition: forward_declarations.h:33
armarx::armem::client::query::Builder
The query::Builder class provides a fluent-style specification of hierarchical queries.
Definition: Builder.h:22
armarx::armem::base::detail::GetFindSnapshotMixin::findLatestSnapshot
const auto * findLatestSnapshot() const
Find the latest entity snapshot.
Definition: lookup_mixins.h:304
visionx::armem_images::ImageBase::resetImage
virtual void resetImage(int rows, int cols)=0
visionx::armem_images::Image::aronImage
AronImageT aronImage
Definition: Image.h:210
visionx::armem_images::ImageBase::ImageBase
ImageBase(const armarx::armem::MemoryID &instanceID, size_t imageIndex)
Definition: Image.cpp:24
visionx::armem_images::ImageBase::usePixels
virtual bool usePixels(CByteImage **inputImages)
Definition: Image.cpp:35
visionx::armem_images::Image::Image
Image(const armarx::armem::MemoryID &instanceID, size_t imageIndex)
Definition: Image.h:90