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
27#include <Eigen/Core>
28
29#include <IceUtil/Time.h>
30
31#include <opencv2/calib3d.hpp>
32#include <opencv2/core/eigen.hpp>
33#include <opencv2/highgui.hpp>
34
35#include <SimoxUtility/algorithm/string.h>
36
41#include <ArmarXCore/interface/core/SharedMemory.h>
42
44
48
49#include <Calibration/Calibration.h>
50#include <Calibration/StereoCalibration.h>
51#include <Image/IplImageAdaptor.h>
52
53namespace visionx
54{
55
60
63 {
66
67 defs->optional(
68 p.imageProviderName, "img.ImageProviderName", "Name of the image provider to use.");
69 defs->optional(
70 p.referenceFrame, "img.ReferenceFrameName", "Name of the ReferenceFrameName");
71 defs->optional(p.agentName, "img.AgentName", "Name of the agent");
72 defs->optional(p.cameraIndex, "img.cameraIndex", "Camera index. Left = 0, Right = 1, ...");
73
74 std::string pattern = "[ k3 [, k4, k5, k6 [, s1, s2, s3, s4 ] ] ]";
75 // defs->optional(p.extraDistortionCoeffs, "cam.ExtraDistortionCoeffs",
76 defs->defineOptionalProperty<std::vector<std::string>>(
77 "cam.ExtraDistortionCoeffs",
78 {},
79 "Optional extra distortion coefficients (which cannot be retrieved from the image "
80 "provider)."
81 "\nThe expected parameters are: " +
82 pattern +
83 ""
84 "\nThat is, you must specifiy 1 (k3), 4 (k3-k6), or 8 (k3-k6, s1-s4) elements."
85 "\nNote: k1, k2, p1, p2 are retrieved from the image provider.")
86 .map(pattern, {});
87
88
89 // defs->defineOptionalProperty<bool>(
90 // "visu.Enabled", p.visuEnabled.load(), "If true, visualize marker poses.");
91
92 return defs;
93 }
94
96 {
97 // addPlugin(virtualRobotReaderPlugin);
98 }
99
100 std::string
102 {
103 return "ImagePassThrough";
104 }
105
106 void
108 {
109
110 {
111 std::string propName = "cam.ExtraDistortionCoeffs";
112 std::vector<std::string> coeffsStr;
113 // getProperty(coeffsStr, "cam.ExtraDistortionCoeffs");
114
115 // p.extraDistortionCoeffs.clear();
116 // for (const auto& coeffStr : coeffsStr)
117 // {
118 // try
119 // {
120 // p.extraDistortionCoeffs.push_back(std::stof(coeffStr));
121 // }
122 // catch (const std::invalid_argument&)
123 // {
124 // ARMARX_WARNING << "Could not parse '" << coeffStr << "' as float in property "
125 // << propName << "."
126 // << "\nIgnoring extra parameters.";
127 // p.extraDistortionCoeffs.clear();
128 // break;
129 // }
130 // }
131 }
132
133 ARMARX_INFO << "Will use image provider " << QUOTED(p.imageProviderName);
134 usingProxy(p.imageProviderName);
135 }
136
137 void
139 {
140 // ARMARX_CHECK_NOT_NULL(virtualRobotReaderPlugin);
141
142 // robot = virtualRobotReaderPlugin->get().getRobot(p.agentName);
143 // ARMARX_CHECK_NOT_NULL(robot) << p.agentName;
144
145 // referenceNode = robot->getRobotNode(p.referenceFrame);
146 // ARMARX_CHECK_NOT_NULL(referenceNode) << p.referenceFrame;
147
148 ARMARX_INFO << "Connecting to image provider '" << p.imageProviderName << "'...";
149
150 visionx::ImageProviderInfo imageProviderInfo =
151 ImageProcessor::getImageProvider(p.imageProviderName);
152 StereoCalibrationInterfacePrx calibrationProvider =
153 StereoCalibrationInterfacePrx::checkedCast(imageProviderInfo.proxy);
154
155 std::unique_ptr<CStereoCalibration> stereoCalibration(
156 visionx::tools::convert(calibrationProvider->getStereoCalibration()));
157 // CCalibration::CCameraParameters ivtCameraParameters =
158 // stereoCalibration->GetLeftCalibration()->GetCameraParameters();
159
160 // this->cameraMatrix = visionx::makeCameraMatrix(ivtCameraParameters);
161 // ARMARX_VERBOSE << "Camera matrix: \n" << this->cameraMatrix;
162
163 // {
164 // // "Vector of distortion coefficients (k1,k2,p1,p2 [,k3 [,k4,k5,k6], [s1,s2,s3,s4]]) of 4, 5, 8 or 12 elements"
165 // // Source: https://docs.opencv.org/3.2.0/d9/d6a/group__aruco.html#ga7da45d2e8139504f3a532d884b4fb4ac
166
167 // std::set<int> allowedSizes = {4, 5, 8, 12};
168 // ARMARX_VERBOSE << "Got " << p.extraDistortionCoeffs.size()
169 // << " extra distortion coefficients.";
170
171 // int num = int(4 + p.extraDistortionCoeffs.size());
172 // ARMARX_CHECK_POSITIVE(allowedSizes.count(num))
173 // << "Allowed sizes: "
174 // << simox::alg::join(
175 // simox::alg::multi_to_string(allowedSizes.begin(), allowedSizes.end()), " ")
176 // << "\n num = " << num << " = 4 + " << p.extraDistortionCoeffs.size()
177 // << " = 4 + #extra coeffs";
178
179 // cv::Mat distortionParameters(num, 1, cv::DataType<float>::type);
180
181 // if (!calibrationProvider->getImagesAreUndistorted())
182 // {
183 // // k1, k2, p1, p2
184 // for (int i = 0; i < 4; ++i)
185 // {
186 // distortionParameters.at<float>(i, 0) = ivtCameraParameters.distortion[i];
187 // }
188 // // extra
189 // for (int i = 0; size_t(i) < p.extraDistortionCoeffs.size(); ++i)
190 // {
191 // distortionParameters.at<float>(4 + i, 0) =
192 // p.extraDistortionCoeffs.at(size_t(i));
193 // }
194 // }
195 // else
196 // {
197 // for (int i = 0; i < distortionParameters.rows; ++i)
198 // {
199 // distortionParameters.at<float>(i, 0) = 0;
200 // }
201 // }
202
203 // this->distortionCoeffs = distortionParameters;
204 // ARMARX_CHECK_POSITIVE(allowedSizes.count(this->distortionCoeffs.rows));
205 // }
206 // ARMARX_VERBOSE << "Distortion coefficients: \n" << this->distortionCoeffs;
207
208
209 cameraImages = new CByteImage*[2];
210
211 cameraImages[0] = tools::createByteImage(imageProviderInfo);
212 cameraImages[1] = tools::createByteImage(imageProviderInfo);
213
214 // createRemoteGuiTab();
215 // RemoteGui_startRunningTask();
216
217 // set the image format and number of images for the ImageProvider interface
218 ARMARX_INFO << "Number of images: " << imageProviderInfo.numberImages;
219 setNumberImages(imageProviderInfo.numberImages);
220 setImageFormat(imageProviderInfo.imageFormat.dimension,
221 imageProviderInfo.imageFormat.type,
222 imageProviderInfo.imageFormat.bpType);
223 }
224
225 void
227 {
228 ARMARX_VERBOSE << "Waiting for images from provider '" << p.imageProviderName << "'...";
230 {
231 armarx::MetaInfoSizeBasePtr info;
232 ImageProcessor::getImages(p.imageProviderName, cameraImages, info);
233 m_timestamp_last_image =
235
236 // CByteImage* images[2] = {resultColorImage.get(), resultDepthImage.get()};
237
238 ARMARX_VERBOSE << "Got images from provider '" << p.imageProviderName
239 << "'. Providing to working memory...";
240
241 // provide the images through the shared memory
242 ImageProvider::provideImages(cameraImages,
243 IceUtil::Time::microSeconds(info->timeProvided));
244 }
245 else
246 {
247 ARMARX_VERBOSE << "Timeout or error in wait for images.";
248 return;
249 }
250 }
251
252 void
254 {
255 // using namespace armarx::RemoteGui::Client;
256
257 // GridLayout grid;
258 // int row = 0;
259
260 // tab.markerSize.setRange(1.0, 1000.0);
261 // tab.markerSize.setSteps(1000);
262 // tab.markerSize.setDecimals(1);
263 // tab.markerSize.setValue(p.markerSize);
264
265 // tab.visuEnabled.setValue(p.visuEnabled);
266
267 // grid.add(Label("Marker size:"), {row, 0}).add(tab.markerSize, {row, 1});
268 // row++;
269 // grid.add(Label("Enable visu:"), {row, 0}).add(tab.visuEnabled, {row, 1});
270 // row++;
271
272 // VBoxLayout root = {grid, VSpacer()};
273 // RemoteGui_createTab(getName(), root, &tab);
274 }
275
276 void
278 {
279 // if (tab.markerSize.hasValueChanged())
280 // {
281 // p.markerSize = tab.markerSize.getValue();
282 // }
283 // if (tab.visuEnabled.hasValueChanged())
284 // {
285 // p.visuEnabled = tab.visuEnabled.getValue();
286 // }
287 }
288
289 std::string
291 {
292 return getStereoCalibrationProvider()->getReferenceFrame();
293 }
294
295 visionx::StereoCalibration
297 {
298 return getStereoCalibrationProvider()->getStereoCalibration();
299 }
300
301 bool
303 {
304 return getStereoCalibrationProvider()->getImagesAreUndistorted();
305 }
306
307 StereoCalibrationInterfacePrx
309 {
310 visionx::ImageProviderInfo imageProviderInfo =
311 ImageProcessor::getImageProvider(p.imageProviderName);
312 auto calibProvider = StereoCalibrationInterfacePrx::checkedCast(imageProviderInfo.proxy);
313
314
315 ARMARX_CHECK(calibProvider) << "Image provider '" << p.imageProviderName
316 << "' does not provide stereo calibration.";
317 return calibProvider;
318 }
319
320 void
321 ImagePassThrough::startCapture(::Ice::Float framesPerSecond, const ::Ice::Current& current)
322 {
323 }
324
325 void
326 ImagePassThrough::stopCapture(const ::Ice::Current& current)
327 {
328 }
329} // namespace visionx
#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
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
std::string getDefaultName() const override
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.