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
25#include <opencv2/highgui.hpp>
26
28
30{
31 std::string
33 {
34 return "rgbReader.";
35 }
36
46
47 void
48 detail::RGBImageReader::getLatestRGBImages(const std::string& providerSegmentName,
49 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_EQUAL(i->width, m.cols);
100 ARMARX_CHECK_EQUAL(i->height, m.rows);
101 ARMARX_CHECK_EQUAL(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(
141 [&ret](const armarx::armem::wm::EntityInstance& i)
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>
152 const std::string& providerSegmentName)
153 {
154 std::optional<visionx::StereoCalibration> stereoCalibration = std::nullopt;
155
156 auto mid = armarx::armem::MemoryID();
157 mid.memoryName = "Vision";
158 mid.coreSegmentName = "StereoCameraCalibration";
159 mid.providerSegmentName = providerSegmentName;
160 mid.entityName = "CameraCalibration";
161
162 auto res = this->memoryReader().getLatestSnapshotIn(mid);
163
164 if (res.has_value())
165 {
166 std::vector<visionx::arondto::StereoCameraCalibration> calibration;
167 res->forEachInstance(
168 [&calibration](const armarx::armem::wm::EntityInstance& i)
169 {
170 auto data = i.dataAs<visionx::arondto::StereoCameraCalibration>();
171 calibration.push_back(data);
172 });
173
174 if (calibration.size() > 0)
175 {
176 //now also convert calibration to bo object:
177 visionx::arondto::StereoCameraCalibration dto = calibration[0];
178
179 visionx::armem::camera::fromAron(stereoCalibration.emplace(), dto);
180 }
181 }
182
183 return stereoCalibration;
184 }
185
186 /**
187 visionx::MonocularCalibration
188 detail::CameraCalibrationReader::getMonoCameraCalibration(const std::string& providerSegmentName)
189 {
190 std::vector<visionx::arondto::MonocularCameraCalibration> calibration;
191 auto mid = armarx::armem::MemoryID();
192 mid.memoryName = "Vision";
193 if(providerSegmentName == "AzureKinectPointCloudProvider"){
194 mid.coreSegmentName = "StereoCameraCalibration";
195 } else {
196 mid.coreSegmentName = "MonocularCameraCalibration";
197 }
198
199 mid.providerSegmentName = providerSegmentName;
200 mid.entityName = "CameraCalibration";
201
202 auto res = this->memoryReader().getLatestSnapshotIn(mid);
203
204 if (res.has_value())
205 {
206 res->forEachInstance(
207 [&calibration](const armarx::armem::wm::EntityInstance& i)
208 {
209 auto data = i.dataAs<visionx::arondto::MonocularCameraCalibration>();
210 calibration.push_back(data);
211 });
212 }
213 visionx::MonocularCameraCalibration toReturn;
214 fromAron(&toReturn, &calibration[0]);
215 return toReturn;
216 }
217 **/
218
219 std::string
221 {
222 return "cameraCalibrationReader.";
223 }
224
227 {
229 p.memoryName = "Vision";
230 p.coreSegmentName = "StereoCameraCalibration";
231
232 return p;
233 }
234
235 void
237 {
238 rgbReader.registerPropertyDefinitions(def);
239 depthReader.registerPropertyDefinitions(def);
240 cameraCalibrationReader.registerPropertyDefinitions(def);
241 }
242
243 void
245 {
246 rgbReader.connect(mns);
247 depthReader.connect(mns);
248 cameraCalibrationReader.connect(mns);
249 }
250
251 void
252 ImageReader::getLatestRGBImages(const std::string& providerSegmentName,
253 std::vector<cv::Mat>& ret)
254 {
255 rgbReader.getLatestRGBImages(providerSegmentName, ret);
256 }
257
258 std::vector<CByteImage*>
259 ImageReader::getLatestRGBCByteImages(const std::string& providerSegmentName)
260 {
261 return rgbReader.getLatestRGBCByteImages(providerSegmentName);
262 }
263
264 std::vector<cv::Mat>
265 ImageReader::getLatestDepthImages(const std::string& providerSegmentName)
266 {
267 return depthReader.getLatestDepthImages(providerSegmentName);
268 }
269
270 std::optional<visionx::StereoCalibration>
271 ImageReader::getStereoCameraCalibration(const std::string& providerSegmentName)
272 {
273 return cameraCalibrationReader.getStereoCameraCalibration(providerSegmentName);
274 }
275
276 /**
277 visionx::MonocularCalibration
278 ImageReader::getMonoCameraCalibration(const std::string& providerSegmentName){
279 return cameraCalibrationReader.getMonoCameraCalibration(providerSegmentName);
280 }
281 **/
282
283} // namespace visionx::armem_images::client
uint8_t data[1]
AronDtoT dataAs() const
Get the data converted to a generated Aron DTO class.
The memory name system (MNS) client.
const armem::client::Reader & memoryReader() const
Client-side working entity instance.
void getLatestRGBImages(const std::string &providerSegmentName, std::vector< cv::Mat > &ret)
std::vector< CByteImage * > getLatestRGBCByteImages(const std::string &providerSegmentName)
virtual void connect(armarx::armem::client::MemoryNameSystem &mns)
std::vector< cv::Mat > getLatestDepthImages(const std::string &providerSegmentName)
std::optional< visionx::StereoCalibration > getStereoCameraCalibration(const std::string &providerSegmentName)
void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr &def)
std::optional< visionx::StereoCalibration > getStereoCameraCalibration(const std::string &providerSegmentName)
std::string propertyPrefix() const final
visionx::MonocularCalibration detail::CameraCalibrationReader::getMonoCameraCalibration(const std::st...
std::vector< cv::Mat > getLatestDepthImages(const std::string &providerSegmentName)
void getLatestRGBImages(const std::string &providerSegmentName, std::vector< cv::Mat > &ret)
std::vector< CByteImage * > getLatestRGBCByteImages(const std::string &providerSegmentName)
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
visionx::StereoCalibration fromAron(visionx::StereoCalibration &bo, visionx::arondto::StereoCameraCalibration &dto)