ImagePassThrough.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package VisionX::Component
19 * @author David Schiebener (schiebener at kit dot edu)
20 * @date 2015
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
25#include "ImagePassThrough.h"
26
28
29#include <Eigen/Core>
30
31#include <IceUtil/Time.h>
32
33#include <opencv2/calib3d.hpp>
34#include <opencv2/core/eigen.hpp>
35#include <opencv2/highgui.hpp>
36
37#include <SimoxUtility/algorithm/string.h>
38
43#include <ArmarXCore/interface/core/SharedMemory.h>
44
46
50
51#include <Calibration/Calibration.h>
52#include <Calibration/StereoCalibration.h>
53#include <Image/IplImageAdaptor.h>
54
55namespace visionx
56{
57
62
65 {
68
69 defs->optional(
70 p.imageProviderName, "img.ImageProviderName", "Name of the image provider to use.");
71 defs->optional(
72 p.referenceFrame, "img.ReferenceFrameName", "Name of the ReferenceFrameName");
73 defs->optional(p.agentName, "img.AgentName", "Name of the agent");
74 defs->optional(p.cameraIndex, "img.cameraIndex", "Camera index. Left = 0, Right = 1, ...");
75
76 std::string pattern = "[ k3 [, k4, k5, k6 [, s1, s2, s3, s4 ] ] ]";
77 // defs->optional(p.extraDistortionCoeffs, "cam.ExtraDistortionCoeffs",
78 defs->defineOptionalProperty<std::vector<std::string>>(
79 "cam.ExtraDistortionCoeffs",
80 {},
81 "Optional extra distortion coefficients (which cannot be retrieved from the image "
82 "provider)."
83 "\nThe expected parameters are: " +
84 pattern +
85 ""
86 "\nThat is, you must specifiy 1 (k3), 4 (k3-k6), or 8 (k3-k6, s1-s4) elements."
87 "\nNote: k1, k2, p1, p2 are retrieved from the image provider.")
88 .map(pattern, {});
89
90
91 // defs->defineOptionalProperty<bool>(
92 // "visu.Enabled", p.visuEnabled.load(), "If true, visualize marker poses.");
93
94 return defs;
95 }
96
98 {
99 // addPlugin(virtualRobotReaderPlugin);
100 }
101
102 std::string
104 {
105 return "ImagePassThrough";
106 }
107
108 std::string
110 {
111 return GetDefaultName();
112 }
113
114 void
116 {
117
118 {
119 std::string propName = "cam.ExtraDistortionCoeffs";
120 std::vector<std::string> coeffsStr;
121 // getProperty(coeffsStr, "cam.ExtraDistortionCoeffs");
122
123 // p.extraDistortionCoeffs.clear();
124 // for (const auto& coeffStr : coeffsStr)
125 // {
126 // try
127 // {
128 // p.extraDistortionCoeffs.push_back(std::stof(coeffStr));
129 // }
130 // catch (const std::invalid_argument&)
131 // {
132 // ARMARX_WARNING << "Could not parse '" << coeffStr << "' as float in property "
133 // << propName << "."
134 // << "\nIgnoring extra parameters.";
135 // p.extraDistortionCoeffs.clear();
136 // break;
137 // }
138 // }
139 }
140
141 ARMARX_INFO << "Will use image provider " << QUOTED(p.imageProviderName);
142 usingProxy(p.imageProviderName);
143 }
144
145 void
147 {
148 // ARMARX_CHECK_NOT_NULL(virtualRobotReaderPlugin);
149
150 // robot = virtualRobotReaderPlugin->get().getRobot(p.agentName);
151 // ARMARX_CHECK_NOT_NULL(robot) << p.agentName;
152
153 // referenceNode = robot->getRobotNode(p.referenceFrame);
154 // ARMARX_CHECK_NOT_NULL(referenceNode) << p.referenceFrame;
155
156 ARMARX_INFO << "Connecting to image provider '" << p.imageProviderName << "'...";
157
158 visionx::ImageProviderInfo imageProviderInfo =
159 ImageProcessor::getImageProvider(p.imageProviderName);
160 StereoCalibrationInterfacePrx calibrationProvider =
161 StereoCalibrationInterfacePrx::checkedCast(imageProviderInfo.proxy);
162
163 std::unique_ptr<CStereoCalibration> stereoCalibration(
164 visionx::tools::convert(calibrationProvider->getStereoCalibration()));
165 // CCalibration::CCameraParameters ivtCameraParameters =
166 // stereoCalibration->GetLeftCalibration()->GetCameraParameters();
167
168 // this->cameraMatrix = visionx::makeCameraMatrix(ivtCameraParameters);
169 // ARMARX_VERBOSE << "Camera matrix: \n" << this->cameraMatrix;
170
171 // {
172 // // "Vector of distortion coefficients (k1,k2,p1,p2 [,k3 [,k4,k5,k6], [s1,s2,s3,s4]]) of 4, 5, 8 or 12 elements"
173 // // Source: https://docs.opencv.org/3.2.0/d9/d6a/group__aruco.html#ga7da45d2e8139504f3a532d884b4fb4ac
174
175 // std::set<int> allowedSizes = {4, 5, 8, 12};
176 // ARMARX_VERBOSE << "Got " << p.extraDistortionCoeffs.size()
177 // << " extra distortion coefficients.";
178
179 // int num = int(4 + p.extraDistortionCoeffs.size());
180 // ARMARX_CHECK_POSITIVE(allowedSizes.count(num))
181 // << "Allowed sizes: "
182 // << simox::alg::join(
183 // simox::alg::multi_to_string(allowedSizes.begin(), allowedSizes.end()), " ")
184 // << "\n num = " << num << " = 4 + " << p.extraDistortionCoeffs.size()
185 // << " = 4 + #extra coeffs";
186
187 // cv::Mat distortionParameters(num, 1, cv::DataType<float>::type);
188
189 // if (!calibrationProvider->getImagesAreUndistorted())
190 // {
191 // // k1, k2, p1, p2
192 // for (int i = 0; i < 4; ++i)
193 // {
194 // distortionParameters.at<float>(i, 0) = ivtCameraParameters.distortion[i];
195 // }
196 // // extra
197 // for (int i = 0; size_t(i) < p.extraDistortionCoeffs.size(); ++i)
198 // {
199 // distortionParameters.at<float>(4 + i, 0) =
200 // p.extraDistortionCoeffs.at(size_t(i));
201 // }
202 // }
203 // else
204 // {
205 // for (int i = 0; i < distortionParameters.rows; ++i)
206 // {
207 // distortionParameters.at<float>(i, 0) = 0;
208 // }
209 // }
210
211 // this->distortionCoeffs = distortionParameters;
212 // ARMARX_CHECK_POSITIVE(allowedSizes.count(this->distortionCoeffs.rows));
213 // }
214 // ARMARX_VERBOSE << "Distortion coefficients: \n" << this->distortionCoeffs;
215
216
217 cameraImages = new CByteImage*[2];
218
219 cameraImages[0] = tools::createByteImage(imageProviderInfo);
220 cameraImages[1] = tools::createByteImage(imageProviderInfo);
221
222 // createRemoteGuiTab();
223 // RemoteGui_startRunningTask();
224
225 // set the image format and number of images for the ImageProvider interface
226 ARMARX_INFO << "Number of images: " << imageProviderInfo.numberImages;
227 setNumberImages(imageProviderInfo.numberImages);
228 setImageFormat(imageProviderInfo.imageFormat.dimension,
229 imageProviderInfo.imageFormat.type,
230 imageProviderInfo.imageFormat.bpType);
231 }
232
233 void
235 {
236 ARMARX_VERBOSE << "Waiting for images from provider '" << p.imageProviderName << "'...";
238 {
239 armarx::MetaInfoSizeBasePtr info;
240 ImageProcessor::getImages(p.imageProviderName, cameraImages, info);
241 m_timestamp_last_image =
243
244 // CByteImage* images[2] = {resultColorImage.get(), resultDepthImage.get()};
245
246 ARMARX_VERBOSE << "Got images from provider '" << p.imageProviderName
247 << "'. Providing to working memory...";
248
249 // provide the images through the shared memory
250 ImageProvider::provideImages(cameraImages,
251 IceUtil::Time::microSeconds(info->timeProvided));
252 }
253 else
254 {
255 ARMARX_VERBOSE << "Timeout or error in wait for images.";
256 return;
257 }
258 }
259
260 void
262 {
263 // using namespace armarx::RemoteGui::Client;
264
265 // GridLayout grid;
266 // int row = 0;
267
268 // tab.markerSize.setRange(1.0, 1000.0);
269 // tab.markerSize.setSteps(1000);
270 // tab.markerSize.setDecimals(1);
271 // tab.markerSize.setValue(p.markerSize);
272
273 // tab.visuEnabled.setValue(p.visuEnabled);
274
275 // grid.add(Label("Marker size:"), {row, 0}).add(tab.markerSize, {row, 1});
276 // row++;
277 // grid.add(Label("Enable visu:"), {row, 0}).add(tab.visuEnabled, {row, 1});
278 // row++;
279
280 // VBoxLayout root = {grid, VSpacer()};
281 // RemoteGui_createTab(getName(), root, &tab);
282 }
283
284 void
286 {
287 // if (tab.markerSize.hasValueChanged())
288 // {
289 // p.markerSize = tab.markerSize.getValue();
290 // }
291 // if (tab.visuEnabled.hasValueChanged())
292 // {
293 // p.visuEnabled = tab.visuEnabled.getValue();
294 // }
295 }
296
297 std::string
299 {
300 return getStereoCalibrationProvider()->getReferenceFrame();
301 }
302
303 visionx::StereoCalibration
305 {
306 return getStereoCalibrationProvider()->getStereoCalibration();
307 }
308
309 bool
311 {
312 return getStereoCalibrationProvider()->getImagesAreUndistorted();
313 }
314
315 StereoCalibrationInterfacePrx
317 {
318 visionx::ImageProviderInfo imageProviderInfo =
319 ImageProcessor::getImageProvider(p.imageProviderName);
320 auto calibProvider = StereoCalibrationInterfacePrx::checkedCast(imageProviderInfo.proxy);
321
322
323 ARMARX_CHECK(calibProvider) << "Image provider '" << p.imageProviderName
324 << "' does not provide stereo calibration.";
325 return calibProvider;
326 }
327
328 void
329 ImagePassThrough::startCapture(::Ice::Float framesPerSecond, const ::Ice::Current& current)
330 {
331 }
332
333 void
334 ImagePassThrough::stopCapture(const ::Ice::Current& current)
335 {
336 }
337
339} // namespace visionx
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
#define QUOTED(x)
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
static Duration MicroSeconds(std::int64_t microSeconds)
Constructs a duration in microseconds.
Definition Duration.cpp:24
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Represents a point in time.
Definition DateTime.h:25
ImagePassThrough uses CTexturedRecognition of IVTRecognition in order to recognize and localize objec...
void startCapture(::Ice::Float framesPerSecond, const ::Ice::Current &current=::Ice::emptyCurrent) override
void onConnectImageProcessor() override
Implement this method in the ImageProcessor in order execute parts when the component is fully initia...
std::string getReferenceFrame(const ::Ice::Current &=::Ice::emptyCurrent) override
void stopCapture(const ::Ice::Current &current=::Ice::emptyCurrent) override
StereoCalibrationInterfacePrx getStereoCalibrationProvider()
visionx::StereoCalibration getStereoCalibration(const ::Ice::Current &=::Ice::emptyCurrent) override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void process() override
Process the vision component.
void onInitImageProcessor() override
Setup the vision component.
bool getImagesAreUndistorted(const ::Ice::Current &=::Ice::emptyCurrent) override
static std::string GetDefaultName()
std::string getDefaultName() const override
Retrieve default name of component.
ImageProcessorPropertyDefinitions(std::string prefix)
bool waitForImages(int milliseconds=1000)
Wait for new images.
ImageProviderInfo getImageProvider(std::string name, ImageType destinationImageType=eRgb, bool waitForProxy=false)
Select an ImageProvider.
int getImages(CByteImage **ppImages)
Poll images from provider.
int numberImages
Number of images.
ImageFormatInfo imageFormat
Image format struct that contains all necessary image information.
ImageProviderInterfacePrx proxy
proxy to image provider
void setImageFormat(ImageDimension imageDimension, ImageType imageType, BayerPatternType bayerPatternType=visionx::eBayerPatternRg)
Sets the image basic format data.
void provideImages(void **inputBuffers, const IceUtil::Time &imageTimestamp=IceUtil::Time())
send images raw.
void setNumberImages(int numberImages)
Sets the number of images on each capture.
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
CByteImage::ImageType convert(const ImageType visionxImageType)
Converts a VisionX image type into an image type of IVT's ByteImage.
ArmarX headers.