ImageReader.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package VisionX::ArmarXObjects::armem_images
17  * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18  * @date 2021
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "ImageReader.h"
24 
26 {
27  std::string
29  {
30  return "rgbReader.";
31  }
32 
35  {
37  p.memoryName = "Vision";
38  p.coreSegmentName = "ImageRGB";
39 
40  return p;
41  }
42 
43  std::vector<cv::Mat>
44  detail::RGBImageReader::getLatestRGBImages(const std::string& providerSegmentName)
45  {
46  auto mid = armarx::armem::MemoryID();
47  mid.memoryName = properties().memoryName;
48  mid.coreSegmentName = properties().coreSegmentName;
49  mid.providerSegmentName = providerSegmentName;
50  mid.entityName = "images";
51 
52  auto res = this->memoryReader().getLatestSnapshotIn(mid);
53 
54  std::vector<cv::Mat> ret;
55  if (res.has_value())
56  {
57  res->forEachInstance(
59  {
60  auto data = i.dataAs<visionx::armem_images::arondto::ImageRGB>();
61  ret.push_back(data.image);
62  });
63  }
64  return ret;
65  }
66 
67  std::vector<CByteImage>
68  detail::RGBImageReader::getLatestRGBCByteImages(const std::string& providerSegmentName)
69  {
70  auto mats = getLatestRGBImages(providerSegmentName);
71 
72  std::vector<CByteImage> ret;
73 
74  for (const auto& m : mats)
75  {
76  ARMARX_CHECK(m.dims == 2);
77 
78  auto& i = ret.emplace_back();
79 
80  if (m.elemSize() == 1)
81  {
82  i.Set(m.cols, m.rows, CByteImage::ImageType::eGrayScale);
83  }
84  else if (m.elemSize() == 3)
85  {
86  i.Set(m.cols, m.rows, CByteImage::ImageType::eRGB24);
87  }
88  else
89  {
90  throw armarx::LocalException("Only grayscale and RGB images are allow for "
91  "conversion from cv::mat to cbyteimage.");
92  }
93 
94  ARMARX_CHECK(i.width == m.cols);
95  ARMARX_CHECK(i.height == m.rows);
96  ARMARX_CHECK(i.bytesPerPixel == m.elemSize());
97  std::memcpy(i.pixels, m.data, i.width * i.height * i.bytesPerPixel);
98  }
99  return ret;
100  }
101 
102  std::string
104  {
105  return "depthReader.";
106  }
107 
110  {
112  p.memoryName = "Vision";
113  p.coreSegmentName = "ImageDepth";
114 
115  return p;
116  }
117 
118  std::vector<cv::Mat>
119  detail::DepthImageReader::getLatestDepthImages(const std::string& providerSegmentName)
120  {
121  auto mid = armarx::armem::MemoryID();
122  mid.memoryName = properties().memoryName;
123  mid.coreSegmentName = properties().coreSegmentName;
124  mid.providerSegmentName = providerSegmentName;
125  mid.entityName = "images";
126 
127  auto res = this->memoryReader().getLatestSnapshotIn(mid);
128 
129  std::vector<cv::Mat> ret;
130  if (res.has_value())
131  {
132  res->forEachInstance(
134  {
135  auto data = i.dataAs<visionx::armem_images::arondto::ImageDepth>();
136  ret.push_back(data.image);
137  });
138  }
139  return ret;
140  }
141 
142  void
144  {
145  rgbReader.registerPropertyDefinitions(def);
146  depthReader.registerPropertyDefinitions(def);
147  }
148 
149  void
151  {
152  rgbReader.connect(mns);
153  depthReader.connect(mns);
154  }
155 
156  std::vector<cv::Mat>
157  ImageReader::getLatestRGBImages(const std::string& providerSegmentName)
158  {
159  return rgbReader.getLatestRGBImages(providerSegmentName);
160  }
161 
162  std::vector<CByteImage>
163  ImageReader::getLatestRGBCByteImages(const std::string& providerSegmentName)
164  {
165  return rgbReader.getLatestRGBCByteImages(providerSegmentName);
166  }
167 
168  std::vector<cv::Mat>
169  ImageReader::getLatestDepthImages(const std::string& providerSegmentName)
170  {
171  return depthReader.getLatestDepthImages(providerSegmentName);
172  }
173 
174 } // namespace visionx::armem_images::client
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
visionx::armem_images::client::detail::DepthImageReader::defaultProperties
Properties defaultProperties() const final
Definition: ImageReader.cpp:109
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
visionx::armem_images::client::ImageReader::connect
virtual void connect(armarx::armem::client::MemoryNameSystem &mns)
Definition: ImageReader.cpp:150
visionx::armem_images::client::detail::RGBImageReader::defaultProperties
Properties defaultProperties() const final
Definition: ImageReader.cpp:34
visionx::armem_images::client::detail::RGBImageReader::propertyPrefix
std::string propertyPrefix() const final
Definition: ImageReader.cpp:28
visionx::armem_images::client::detail::DepthImageReader::getLatestDepthImages
std::vector< cv::Mat > getLatestDepthImages(const std::string &providerSegmentName)
Definition: ImageReader.cpp:119
armarx::armem::client::util::SimpleReaderBase::Properties::memoryName
std::string memoryName
Definition: SimpleReaderBase.h:45
visionx::armem_images::client::ImageReader::getLatestRGBCByteImages
std::vector< CByteImage > getLatestRGBCByteImages(const std::string &providerSegmentName)
Definition: ImageReader.cpp:163
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::armem::base::EntityInstanceBase::dataAs
AronDtoT dataAs() const
Get the data converted to a generated Aron DTO class.
Definition: EntityInstanceBase.h:157
armarx::armem::client::util::SimpleReaderBase::Properties::coreSegmentName
std::string coreSegmentName
Definition: SimpleReaderBase.h:46
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
visionx::armem_images::client::ImageReader::registerPropertyDefinitions
void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr &def)
Definition: ImageReader.cpp:143
visionx::armem_images::client::detail::DepthImageReader::propertyPrefix
std::string propertyPrefix() const final
Definition: ImageReader.cpp:103
armarx::armem::client::util::SimpleReaderBase::Properties
Definition: SimpleReaderBase.h:43
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::armem_images::client
Definition: ImageReader.cpp:25
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:69
visionx::armem_images::client::ImageReader::getLatestRGBImages
std::vector< cv::Mat > getLatestRGBImages(const std::string &providerSegmentName)
Definition: ImageReader.cpp:157
visionx::armem_images::client::detail::RGBImageReader::getLatestRGBImages
std::vector< cv::Mat > getLatestRGBImages(const std::string &providerSegmentName)
Definition: ImageReader.cpp:44
ImageReader.h
visionx::armem_images::client::ImageReader::getLatestDepthImages
std::vector< cv::Mat > getLatestDepthImages(const std::string &providerSegmentName)
Definition: ImageReader.cpp:169
visionx::armem_images::client::detail::RGBImageReader::getLatestRGBCByteImages
std::vector< CByteImage > getLatestRGBCByteImages(const std::string &providerSegmentName)
Definition: ImageReader.cpp:68
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:29