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 #include <opencv2/highgui.hpp>
28 
30 {
31  std::string
33  {
34  return "rgbReader.";
35  }
36 
39  {
41  p.memoryName = "Vision";
42  p.coreSegmentName = "ImageRGB";
43 
44  return p;
45  }
46 
47 
48  void
49  detail::RGBImageReader::getLatestRGBImages(const std::string& providerSegmentName, std::vector<cv::Mat>& ret)
50  {
51  auto mid = armarx::armem::MemoryID();
52  mid.memoryName = properties().memoryName;
53  mid.coreSegmentName = properties().coreSegmentName;
54  mid.providerSegmentName = providerSegmentName;
55  mid.entityName = "images";
56 
57  auto res = this->memoryReader().getLatestSnapshotIn(mid);
58 
59  if (res.has_value())
60  {
61  res->forEachInstance(
63  {
64  auto data = i.dataAs<visionx::armem_images::arondto::ImageRGB>();
65  ret.push_back(data.image);
66  });
67  }
68  }
69 
70  std::vector<CByteImage*>
71  detail::RGBImageReader::getLatestRGBCByteImages(const std::string& providerSegmentName)
72  {
73  std::vector<cv::Mat> mats;
74  getLatestRGBImages(providerSegmentName, mats);
75 
76  std::vector<CByteImage*> ret;
77  ret.reserve(mats.size());
78 
79  for (const auto& m : mats)
80  {
81  ARMARX_CHECK(m.dims == 2);
82 
83  CByteImage* i;
84 
85  if (m.elemSize() == 1)
86  {
87  i = new CByteImage(m.cols, m.rows, CByteImage::ImageType::eGrayScale);
88  }
89  else if (m.elemSize() == 3)
90  {
91  i = new CByteImage(m.cols, m.rows, CByteImage::ImageType::eRGB24);
92  }
93  else
94  {
95  throw armarx::LocalException("Only grayscale and RGB images are allow for "
96  "conversion from cv::mat to cbyteimage.");
97  }
98 
99  ARMARX_CHECK(i->width == m.cols);
100  ARMARX_CHECK(i->height == m.rows);
101  ARMARX_CHECK(i->bytesPerPixel == m.elemSize());
102  std::memcpy(i->pixels, m.data, i->width * i->height * i->bytesPerPixel);
103 
104  ret.emplace_back(i);
105  }
106 
107  return ret;
108  }
109 
110  std::string
112  {
113  return "depthReader.";
114  }
115 
118  {
120  p.memoryName = "Vision";
121  p.coreSegmentName = "ImageDepth";
122 
123  return p;
124  }
125 
126  std::vector<cv::Mat>
127  detail::DepthImageReader::getLatestDepthImages(const std::string& providerSegmentName)
128  {
129  auto mid = armarx::armem::MemoryID();
130  mid.memoryName = properties().memoryName;
131  mid.coreSegmentName = properties().coreSegmentName;
132  mid.providerSegmentName = providerSegmentName;
133  mid.entityName = "images";
134 
135  auto res = this->memoryReader().getLatestSnapshotIn(mid);
136 
137  std::vector<cv::Mat> ret;
138  if (res.has_value())
139  {
140  res->forEachInstance(
142  {
143  auto data = i.dataAs<visionx::armem_images::arondto::ImageDepth>();
144  ret.push_back(data.image);
145  });
146  }
147  return ret;
148  }
149 
150  std::optional<visionx::StereoCalibration>
151  detail::CameraCalibrationReader::getStereoCameraCalibration(const std::string& providerSegmentName)
152  {
153  std::optional<visionx::StereoCalibration> stereoCalibration = std::nullopt;
154 
155  auto mid = armarx::armem::MemoryID();
156  mid.memoryName = "Vision";
157  mid.coreSegmentName = "StereoCameraCalibration";
158  mid.providerSegmentName = providerSegmentName;
159  mid.entityName = "CameraCalibration";
160 
161  auto res = this->memoryReader().getLatestSnapshotIn(mid);
162 
163  if (res.has_value())
164  {
165  std::vector<visionx::arondto::StereoCameraCalibration> calibration;
166  res->forEachInstance(
167  [&calibration](const armarx::armem::wm::EntityInstance& i)
168  {
169  auto data = i.dataAs<visionx::arondto::StereoCameraCalibration>();
170  calibration.push_back(data);
171  });
172 
173  if (calibration.size() > 0)
174  {
175  //now also convert calibration to bo object:
176  visionx::arondto::StereoCameraCalibration dto = calibration[0];
177 
178  visionx::armem::camera::fromAron(stereoCalibration.emplace(), dto);
179  }
180  }
181 
182  return stereoCalibration;
183  }
184 
185 /**
186  visionx::MonocularCalibration
187  detail::CameraCalibrationReader::getMonoCameraCalibration(const std::string& providerSegmentName)
188  {
189  std::vector<visionx::arondto::MonocularCameraCalibration> calibration;
190  auto mid = armarx::armem::MemoryID();
191  mid.memoryName = "Vision";
192  if(providerSegmentName == "AzureKinectPointCloudProvider"){
193  mid.coreSegmentName = "StereoCameraCalibration";
194  } else {
195  mid.coreSegmentName = "MonocularCameraCalibration";
196  }
197 
198  mid.providerSegmentName = providerSegmentName;
199  mid.entityName = "CameraCalibration";
200 
201  auto res = this->memoryReader().getLatestSnapshotIn(mid);
202 
203  if (res.has_value())
204  {
205  res->forEachInstance(
206  [&calibration](const armarx::armem::wm::EntityInstance& i)
207  {
208  auto data = i.dataAs<visionx::arondto::MonocularCameraCalibration>();
209  calibration.push_back(data);
210  });
211  }
212  visionx::MonocularCameraCalibration toReturn;
213  fromAron(&toReturn, &calibration[0]);
214  return toReturn;
215  }
216  **/
217 
218  std::string
220  {
221  return "cameraCalibrationReader.";
222  }
223 
226  {
228  p.memoryName = "Vision";
229  p.coreSegmentName = "StereoCameraCalibration";
230 
231  return p;
232  }
233 
234  void
236  {
237  rgbReader.registerPropertyDefinitions(def);
238  depthReader.registerPropertyDefinitions(def);
239  cameraCalibrationReader.registerPropertyDefinitions(def);
240  }
241 
242  void
244  {
245  rgbReader.connect(mns);
246  depthReader.connect(mns);
247  cameraCalibrationReader.connect(mns);
248  }
249 
250  void
251  ImageReader::getLatestRGBImages(const std::string& providerSegmentName, std::vector<cv::Mat>& ret)
252  {
253  rgbReader.getLatestRGBImages(providerSegmentName, ret);
254  }
255 
256  std::vector<CByteImage*>
257  ImageReader::getLatestRGBCByteImages(const std::string& providerSegmentName)
258  {
259  return rgbReader.getLatestRGBCByteImages(providerSegmentName);
260  }
261 
262  std::vector<cv::Mat>
263  ImageReader::getLatestDepthImages(const std::string& providerSegmentName)
264  {
265  return depthReader.getLatestDepthImages(providerSegmentName);
266  }
267 
268  std::optional<visionx::StereoCalibration>
269  ImageReader::getStereoCameraCalibration(const std::string& providerSegmentName)
270  {
271  return cameraCalibrationReader.getStereoCameraCalibration(providerSegmentName);
272  }
273 
274  /**
275  visionx::MonocularCalibration
276  ImageReader::getMonoCameraCalibration(const std::string& providerSegmentName){
277  return cameraCalibrationReader.getMonoCameraCalibration(providerSegmentName);
278  }
279  **/
280 
281 } // namespace visionx::armem_images::client
visionx::armem_images::client::detail::RGBImageReader::getLatestRGBCByteImages
std::vector< CByteImage * > getLatestRGBCByteImages(const std::string &providerSegmentName)
Definition: ImageReader.cpp:71
visionx::armem_images::client::detail::RGBImageReader::getLatestRGBImages
void getLatestRGBImages(const std::string &providerSegmentName, std::vector< cv::Mat > &ret)
Definition: ImageReader.cpp:49
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:117
visionx::armem_images::client::ImageReader::getLatestRGBImages
void getLatestRGBImages(const std::string &providerSegmentName, std::vector< cv::Mat > &ret)
Definition: ImageReader.cpp:251
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:243
visionx::armem_images::client::detail::RGBImageReader::defaultProperties
Properties defaultProperties() const final
Definition: ImageReader.cpp:38
visionx::armem_images::client::ImageReader::getStereoCameraCalibration
std::optional< visionx::StereoCalibration > getStereoCameraCalibration(const std::string &providerSegmentName)
Definition: ImageReader.cpp:269
visionx::armem_images::client::detail::RGBImageReader::propertyPrefix
std::string propertyPrefix() const final
Definition: ImageReader.cpp:32
visionx::armem_images::client::detail::DepthImageReader::getLatestDepthImages
std::vector< cv::Mat > getLatestDepthImages(const std::string &providerSegmentName)
Definition: ImageReader.cpp:127
armarx::armem::client::util::SimpleReaderBase::Properties::memoryName
std::string memoryName
Definition: SimpleReaderBase.h:45
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
visionx::armem_images::client::detail::CameraCalibrationReader::propertyPrefix
std::string propertyPrefix() const final
visionx::MonocularCalibration detail::CameraCalibrationReader::getMonoCameraCalibration(const std::st...
Definition: ImageReader.cpp:219
visionx::armem_images::client::detail::CameraCalibrationReader::getStereoCameraCalibration
std::optional< visionx::StereoCalibration > getStereoCameraCalibration(const std::string &providerSegmentName)
Definition: ImageReader.cpp:151
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::camera::fromAron
visionx::StereoCalibration fromAron(visionx::StereoCalibration &bo, visionx::arondto::StereoCameraCalibration &dto)
Definition: aron_conversions.cpp:31
visionx::armem_images::client::ImageReader::registerPropertyDefinitions
void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr &def)
Definition: ImageReader.cpp:235
visionx::armem_images::client::ImageReader::getLatestRGBCByteImages
std::vector< CByteImage * > getLatestRGBCByteImages(const std::string &providerSegmentName)
Definition: ImageReader.cpp:257
visionx::armem_images::client::detail::CameraCalibrationReader::defaultProperties
Properties defaultProperties() const final
Definition: ImageReader.cpp:225
visionx::armem_images::client::detail::DepthImageReader::propertyPrefix
std::string propertyPrefix() const final
Definition: ImageReader.cpp:111
armarx::armem::client::util::SimpleReaderBase::Properties
Definition: SimpleReaderBase.h:43
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::armem_images::client
Definition: ImageReader.cpp:29
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:69
aron_conversions.h
ImageReader.h
visionx::armem_images::client::ImageReader::getLatestDepthImages
std::vector< cv::Mat > getLatestDepthImages(const std::string &providerSegmentName)
Definition: ImageReader.cpp:263
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:29