ObjectLocalizerProcessor.h
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 Kai Welke (welke at kit dot edu)
20 * @date 2013
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
25#pragma once
26
28
29// VisionX
32
33// VisionXInterface
34#include <VisionX/interface/components/ObjectLocalizerInterfaces.h>
35#include <VisionX/interface/units/ObjectRecognitionUnit.h>
36
37// MemoryX
40#include <MemoryX/interface/components/PriorKnowledgeInterface.h>
41
42// IVT
43#include <string>
44
45#include <boost/ptr_container/ptr_vector.hpp>
46
47#include <Eigen/Core>
48
49#include <Calibration/StereoCalibration.h>
50#include <Structs/ObjectDefinitions.h>
51
52
53// forward declarations
54class CByteImage;
55class CTexturedRecognition;
56
57namespace memoryx
58{
60 using MultivariateNormalDistributionPtr = IceInternal::Handle<MultivariateNormalDistribution>;
61} // namespace memoryx
62
63namespace visionx
64{
65
66 static Eigen::Vector3i
67 extractColorValue(std::string propertyValue)
68 {
69 int number = (int)strtol(&propertyValue.c_str()[1], NULL, 16);
70
71 int r = number >> 16;
72 int g = number >> 8 & 0xFF;
73 int b = number & 0xFF;
74
75 return Eigen::Vector3i(r, g, b);
76 }
77
80 {
81 public:
84 {
86 "PriorKnowledgeProxyName", "PriorKnowledge", "name of prior memory proxy");
88 "DataBaseObjectCollectionName",
89 "memdb.Prior_Objects",
90 "name of collection from database to use for object classes");
92 "ImageProviderName", "ImageProvider", "name of the image provider to use");
94 "AgentName", "Name of the agent for which the sensor values are provided");
95 defineOptionalProperty<std::string>("CalibrationUpdateTopicName",
96 "StereoCalibrationInterface",
97 "Topic name of the stereo calibration provider");
98 defineOptionalProperty<float>("2DLocalizationNoise",
99 4.0,
100 "2D localization noise of object recognition. Used in "
101 "order to calculate the 3D world localization noise.");
102 defineOptionalProperty<bool>("EnableResultImages",
103 true,
104 "Enable the calculation of resultimages which are then "
105 "provided using an ImageProvider");
106 defineOptionalProperty<bool>("useResultImageMask",
107 false,
108 "Use a color mask for the result images if set. Otherwise "
109 "the camera images are used");
111 &extractColorValue;
113 "colorMask",
114 extractColorValue("#FFFFFF"),
115 "image color that should be used as alpha channel")
116 .setFactory(f);
117
118 defineOptionalProperty<std::string>("DebugObserverName",
119 "DebugObserver",
120 "Name of the topic the DebugObserver listens on");
121 }
122 };
123
124 /**
125 * ObjectLocalizerProcessor
126 *
127 */
129 virtual public ImageProcessor,
130 virtual public ObjectLocalizerImageInterface
131 {
133
134 public:
136
137 /**
138 * @see PropertyUser::createPropertyDefinitions()
139 */
146
147 /**
148 * Called from framework. Sets up the required proxies and properties from config file.
149 * Calls the onInitObjectLocalizerProcessor() hook.
150 *
151 * @see Component::onInitComponent()
152 */
153 void onInitImageProcessor() override;
154
155 /**
156 * Called from framework. Establishes the database connection, retrieves image provider settings and stereo calibration, initializes visualization
157 * if enabled and reads the object classes from the MemoryX database.
158 *
159 * Calls the onConnectObjectLocalizerProcessor() hook.
160 *
161 * @see Component::onConnectComponent()
162 */
163 void onConnectImageProcessor() override;
164
165 /**
166 * Called from framework.
167 *
168 * Calls the onDisconnectObjectLocalizerProcessor() hook.
169 *
170 * @see Component::onExitComponent()
171 */
172 void
174 {
175 // Prevent deadlock: Cancel job before stopping the other threads
176 job.exit();
179 }
180
181 /**
182 * Called from framework.
183 *
184 * Calls the onExitObjectLocalizerProcessor() hook.
185 *
186 * @see Component::onExitComponent()
187 */
188 void
193
194 /**
195 * The process method is inherited from ImageProcessor. The process method is called by the framework
196 * in a separate thread and. The loading of the database and the triggering of recognition is performed
197 * in this thread.
198 *
199 * @see ImageProcessor::process()
200 */
201 void process() override;
202
203 /**
204 * The process method is inherited from the ObjectLocalizationProcessorInterface and is called by the WorkingMemoryUpdater.
205 * It should be considered final. Implement ObjectLocalizerProcessor::localizeObjectClass(const std::string& objecClassName, CByteImage** cameraImages, CByteIamge** resultImages)
206 * in your subclass instead.
207 */
208 memoryx::ObjectLocalizationResultList
209 localizeObjectClasses(const memoryx::ObjectClassNameList& objectClassNames,
210 const Ice::Current& c = Ice::emptyCurrent) override;
211
212 protected:
213 /**
214 * ObjectLocalizerProcessor interface: The localizeObjectClass method needs to be implemented by any ObjectLocalizer.
215 * Based on the object class name and the camera images it generates a list of localization results which correspond
216 * to found instances of objects belonging to that class.
217 *
218 * This method is called by an @ObjectLocalizerProcessorJob.
219 *
220 * @param objectClassNames names of the class to localize
221 * @param cameraImages the two input images
222 * @param resultImages the two result images. are provided if result images are enabled.
223 *
224 * @return list of object instances
225 */
226 virtual memoryx::ObjectLocalizationResultList
227 localizeObjectClasses(const std::vector<std::string>& objectClassNames,
228 CByteImage** cameraImages,
229 armarx::MetaInfoSizeBasePtr imageMetaInfo,
230 CByteImage** resultImages) = 0;
231
232 /**
233 * ObjectLocalizerProcessor interface: The initRecognizer method needs to be implemented by any ObjectLocalizer.
234 * the method is called in the same thread as addObjectClass and localizeObjectClass. Initialize your
235 * recognition method here.
236 *
237 * @return success of adding this entity to the recognition method
238 */
239 virtual bool initRecognizer() = 0;
240
241 /**
242 * ObjectLocalizerProcessor interface: The addObjectClass method needs to be implemented by any ObjectLocalizer.
243 * Add object class to localizer. Called by the ObjectLocalizerProcessor for each object class in prior knowledge, that
244 * has the RecognitionMethod attribute corresponding to the default name of this component (see Component::getDefaultName())
245 *
246 * @param objectClassEntity entity containing all information available for the object class
247 * @param fileManager GridFileManager required to read files associated to prior knowledgge from the database. Usually accessed via an AbstractEntityWrapper.
248 *
249 * @return success of adding this entity to the recognition method
250 */
251 virtual bool addObjectClass(const memoryx::EntityPtr& objectClassEntity,
252 const memoryx::GridFileManagerPtr& fileManager) = 0;
253
254 /**
255 * ObjectLocalizerProcessor interface: subclass hook
256 *
257 * @see ObjectLocalizerProcessor::onInitImageProcessor()
258 */
259 virtual void
263
264 /**
265 * ObjectLocalizerProcessor interface: subclass hook
266 *
267 * @see ObjectLocalizerProcessor::onConnectImageProcessor()
268 */
269 virtual void
273
274 /**
275 * ObjectLocalizerProcessor interface: subclass hook
276 *
277 * @see ObjectLocalizerProcessor::onDisconnectImageProcessor()
278 */
279 virtual void
283
284 /**
285 * ObjectLocalizerProcessor interface: subclass hook
286 *
287 * @see ObjectLocalizerProcessor::onExitImageProcessor()
288 */
289 virtual void
293
294 /**
295 * Calculate 3D uncertainty from two 2d points in left and right camera. Uses the
296 * property "2DLocalizationNoise" for estimating the 3D noise. Uses unscented
297 * transform through epipolar geometry to derive an approximation
298 * The reference frame is always the left camera.
299 *
300 * @param left_point point in left camera image
301 * @param right_point point in right camera image
302 * @return 3D localization uncertainty
303 */
305 calculateLocalizationUncertainty(Vec2d left_point, Vec2d right_point);
306
307 /**
308 * Calculate localization uncertainty for a 3d point. First projects the point
309 * to the camera images and then applies calculateLocalizationUncertainty(Vec2d left_point, Vec2d right_point).
310 * The reference frame is always the left camera.
311 *
312 * @param position 3d position of the point
313 * @return 3D localization uncertainty
314 */
316 calculateLocalizationUncertainty(const Eigen::Vector3f& position);
317
318 void
319 reportStereoCalibrationChanged(const StereoCalibration& stereoCalibration,
320 bool imagesAreUndistorted,
321 const std::string& referenceFrame,
322 const Ice::Current& c = Ice::emptyCurrent) override
323 {
324 this->stereoCalibration.reset(visionx::tools::convert(stereoCalibration));
325 this->imagesAreUndistorted = imagesAreUndistorted;
326 this->referenceFrameName = referenceFrame;
327 }
328
329 /**
330 * Retrieve whether result images are enabled
331 *
332 * @return result images enables
333 */
334 bool
336 {
337 return resultImagesEnabled;
338 }
339
340 /**
341 * Retrieve whether result images are enabled
342 *
343 * @return result images enables
344 */
345 std::string
347 {
348 return referenceFrameName;
349 }
350
351 /**
352 * Retrieve format of input images
353 *
354 * @return iamge format
355 */
356 ImageFormatInfo
358 {
359 return imageFormat;
360 }
361
362 /**
363 * Retrieve whether images are undistorted
364 *
365 * @return whether images are undistorted
366 */
367 bool
369 {
370 return imagesAreUndistorted;
371 }
372
373 bool
375 {
376 return useResultImageMask;
377 }
378
379 Eigen::Vector3i
381 {
382 return colorMask;
383 }
384
385 /**
386 * Retrieve stereo calibration corresponding to image provider
387 *
388 * @return stereo calibration
389 */
390 CStereoCalibration*
392 {
393 return stereoCalibration.get();
394 }
395
396 private:
397 // read object classes from prior memory
398 void initObjectClasses();
399 // setup image objects for a specific size
400 void setupImages(int width, int height);
401
402
403 Eigen::Vector3i colorMask;
404 bool useResultImageMask;
405 bool resultImagesEnabled;
406
407 // proxies
408 ImageProviderInterfacePrx imageProviderPrx;
409 memoryx::PriorKnowledgeInterfacePrx priorKnowledgePrx;
410 memoryx::PersistentObjectClassSegmentBasePrx classesSegmentPrx;
411 memoryx::CommonStorageInterfacePrx databasePrx;
412
413 memoryx::GridFileManagerPtr fileManager;
414
415
416 // stereo information
417 std::unique_ptr<CStereoCalibration> stereoCalibration;
418 bool imagesAreUndistorted;
419 ImageFormatInfo imageFormat;
420
421 protected:
422 // settings
423 std::string imageProviderName;
426
427 // images
428 CByteImage cameraImagesData[2];
429 CByteImage* cameraImages[2];
430 boost::ptr_vector<boost::nullable<CByteImage>> resultImagesData;
431 CByteImage** resultImages;
432 armarx::MetaInfoSizeBasePtr imageMetaInfo;
434
435
436 private:
437 // noise
438 Eigen::Vector2d imageNoiseLeft;
439 Eigen::Vector2d imageNoiseRight;
440
441 // threading
443
444 public:
445 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
446 };
447
448} // namespace visionx
constexpr T c
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineOptionalProperty(const std::string &name, PropertyType defaultValue, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
PropertyDefinition< PropertyType > & defineRequiredProperty(const std::string &name, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
std::function< PropertyType(std::string)> PropertyFactoryFunction
The MultivariateNormalDistribution class.
ImageProcessorPropertyDefinitions(std::string prefix)
The ImageProcessor class provides an interface for access to ImageProviders via Ice and shared memory...
void onDisconnectComponent() override
virtual bool initRecognizer()=0
ObjectLocalizerProcessor interface: The initRecognizer method needs to be implemented by any ObjectLo...
virtual memoryx::ObjectLocalizationResultList localizeObjectClasses(const std::vector< std::string > &objectClassNames, CByteImage **cameraImages, armarx::MetaInfoSizeBasePtr imageMetaInfo, CByteImage **resultImages)=0
ObjectLocalizerProcessor interface: The localizeObjectClass method needs to be implemented by any Obj...
void onConnectImageProcessor() override
Called from framework.
ImageFormatInfo getImageFormat() const
Retrieve format of input images.
std::string getReferenceFrameName() const
Retrieve whether result images are enabled.
void onDisconnectComponent() override
Called from framework.
void onExitImageProcessor() override
Called from framework.
virtual void onExitObjectLocalizerProcessor()
ObjectLocalizerProcessor interface: subclass hook.
virtual void onConnectObjectLocalizerProcessor()
ObjectLocalizerProcessor interface: subclass hook.
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void process() override
The process method is inherited from ImageProcessor.
bool getImagesAreUndistorted() const
Retrieve whether images are undistorted.
virtual void onDisconnectObjectLocalizerProcessor()
ObjectLocalizerProcessor interface: subclass hook.
memoryx::ObjectLocalizationResultList localizeObjectClasses(const memoryx::ObjectClassNameList &objectClassNames, const Ice::Current &c=Ice::emptyCurrent) override
The process method is inherited from the ObjectLocalizationProcessorInterface and is called by the Wo...
bool getResultImagesEnabled() const
Retrieve whether result images are enabled.
void onInitImageProcessor() override
Called from framework.
boost::ptr_vector< boost::nullable< CByteImage > > resultImagesData
virtual void onInitObjectLocalizerProcessor()
ObjectLocalizerProcessor interface: subclass hook.
virtual bool addObjectClass(const memoryx::EntityPtr &objectClassEntity, const memoryx::GridFileManagerPtr &fileManager)=0
ObjectLocalizerProcessor interface: The addObjectClass method needs to be implemented by any ObjectLo...
void reportStereoCalibrationChanged(const StereoCalibration &stereoCalibration, bool imagesAreUndistorted, const std::string &referenceFrame, const Ice::Current &c=Ice::emptyCurrent) override
memoryx::MultivariateNormalDistributionPtr calculateLocalizationUncertainty(Vec2d left_point, Vec2d right_point)
Calculate 3D uncertainty from two 2d points in left and right camera.
armarx::MetaInfoSizeBasePtr imageMetaInfo
CStereoCalibration * getStereoCalibration() const
Retrieve stereo calibration corresponding to image provider.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
VirtualRobot headers.
IceInternal::Handle< MultivariateNormalDistribution > MultivariateNormalDistributionPtr
IceInternal::Handle< Entity > EntityPtr
Typedef of EntityPtr as IceInternal::Handle<Entity> for convenience.
Definition Entity.h:45
std::shared_ptr< GridFileManager > GridFileManagerPtr
CByteImage::ImageType convert(const ImageType visionxImageType)
Converts a VisionX image type into an image type of IVT's ByteImage.
ArmarX headers.