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
54 class CByteImage;
55 class CTexturedRecognition;
56 
57 namespace memoryx
58 {
61 } // namespace memoryx
62 
63 namespace 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  {
85  defineOptionalProperty<std::string>(
86  "PriorKnowledgeProxyName", "PriorKnowledge", "name of prior memory proxy");
87  defineOptionalProperty<std::string>(
88  "DataBaseObjectCollectionName",
89  "memdb.Prior_Objects",
90  "name of collection from database to use for object classes");
91  defineOptionalProperty<std::string>(
92  "ImageProviderName", "ImageProvider", "name of the image provider to use");
93  defineRequiredProperty<std::string>(
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;
112  defineOptionalProperty<Eigen::Vector3i>(
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  */
142  {
145  }
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
190  {
192  }
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
261  {
262  }
263 
264  /**
265  * ObjectLocalizerProcessor interface: subclass hook
266  *
267  * @see ObjectLocalizerProcessor::onConnectImageProcessor()
268  */
269  virtual void
271  {
272  }
273 
274  /**
275  * ObjectLocalizerProcessor interface: subclass hook
276  *
277  * @see ObjectLocalizerProcessor::onDisconnectImageProcessor()
278  */
279  virtual void
281  {
282  }
283 
284  /**
285  * ObjectLocalizerProcessor interface: subclass hook
286  *
287  * @see ObjectLocalizerProcessor::onExitImageProcessor()
288  */
289  virtual void
291  {
292  }
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
380  getColorMask() const
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;
425  std::string referenceFrameName;
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
visionx::ObjectLocalizerProcessor::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ObjectLocalizerProcessor.h:141
visionx::ObjectLocalizerProcessor::onConnectObjectLocalizerProcessor
virtual void onConnectObjectLocalizerProcessor()
ObjectLocalizerProcessor interface: subclass hook.
Definition: ObjectLocalizerProcessor.h:270
visionx::ObjectLocalizerProcessor::resultImages
CByteImage ** resultImages
Definition: ObjectLocalizerProcessor.h:431
visionx::ObjectLocalizerProcessor::reportStereoCalibrationChanged
void reportStereoCalibrationChanged(const StereoCalibration &stereoCalibration, bool imagesAreUndistorted, const std::string &referenceFrame, const Ice::Current &c=Ice::emptyCurrent) override
Definition: ObjectLocalizerProcessor.h:319
ObjectLocalizerProcessorJob.h
visionx::ObjectLocalizerProcessor::localizeObjectClasses
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...
Definition: ObjectLocalizerProcessor.cpp:192
visionx::ObjectLocalizerProcessor::referenceFrameName
std::string referenceFrameName
Definition: ObjectLocalizerProcessor.h:425
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::ObjectLocalizerProcessor::getImageFormat
ImageFormatInfo getImageFormat() const
Retrieve format of input images.
Definition: ObjectLocalizerProcessor.h:357
visionx::ImageProcessor
The ImageProcessor class provides an interface for access to ImageProviders via Ice and shared memory...
Definition: ImageProcessor.h:98
visionx::ObjectLocalizerProcessor::getResultImagesEnabled
bool getResultImagesEnabled() const
Retrieve whether result images are enabled.
Definition: ObjectLocalizerProcessor.h:335
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:345
GridFileManager.h
visionx::ObjectLocalizerProcessor::onDisconnectObjectLocalizerProcessor
virtual void onDisconnectObjectLocalizerProcessor()
ObjectLocalizerProcessor interface: subclass hook.
Definition: ObjectLocalizerProcessor.h:280
visionx::ObjectLocalizerProcessor::initRecognizer
virtual bool initRecognizer()=0
ObjectLocalizerProcessor interface: The initRecognizer method needs to be implemented by any ObjectLo...
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
visionx::ObjectLocalizerProcessorJob::exit
void exit()
Requests exit to the job.
Definition: ObjectLocalizerProcessorJob.cpp:69
visionx::ObjectLocalizerProcessorPropertyDefinitions::ObjectLocalizerProcessorPropertyDefinitions
ObjectLocalizerProcessorPropertyDefinitions(std::string prefix)
Definition: ObjectLocalizerProcessor.h:82
visionx::ObjectLocalizerProcessor::addObjectClass
virtual bool addObjectClass(const memoryx::EntityPtr &objectClassEntity, const memoryx::GridFileManagerPtr &fileManager)=0
ObjectLocalizerProcessor interface: The addObjectClass method needs to be implemented by any ObjectLo...
visionx::ObjectLocalizerProcessor::imageMetaInfo
armarx::MetaInfoSizeBasePtr imageMetaInfo
Definition: ObjectLocalizerProcessor.h:432
IceInternal::Handle
Definition: forward_declarations.h:8
visionx::tools::convert
CByteImage::ImageType convert(const ImageType visionxImageType)
Converts a VisionX image type into an image type of IVT's ByteImage.
Definition: TypeMapping.cpp:97
armarx::extractColorValue
Eigen::Vector3i extractColorValue(std::string propertyValue)
Definition: ResultImageFuser.h:51
visionx::ObjectLocalizerProcessor::process
void process() override
The process method is inherited from ImageProcessor.
Definition: ObjectLocalizerProcessor.cpp:215
visionx::ObjectLocalizerProcessor::getImagesAreUndistorted
bool getImagesAreUndistorted() const
Retrieve whether images are undistorted.
Definition: ObjectLocalizerProcessor.h:368
ImageProcessor.h
visionx::ObjectLocalizerProcessor::cameraImages
CByteImage * cameraImages[2]
Definition: ObjectLocalizerProcessor.h:429
visionx::ObjectLocalizerProcessor::isResultImageMaskEnabled
bool isResultImageMaskEnabled() const
Definition: ObjectLocalizerProcessor.h:374
visionx::ObjectLocalizerProcessor::onExitImageProcessor
void onExitImageProcessor() override
Called from framework.
Definition: ObjectLocalizerProcessor.h:189
visionx::ObjectLocalizerProcessor::priorKnowledgeProxyName
std::string priorKnowledgeProxyName
Definition: ObjectLocalizerProcessor.h:424
visionx::ObjectLocalizerProcessor::onConnectImageProcessor
void onConnectImageProcessor() override
Called from framework.
Definition: ObjectLocalizerProcessor.cpp:94
visionx::ObjectLocalizerProcessor::imageProviderName
std::string imageProviderName
Definition: ObjectLocalizerProcessor.h:423
visionx::ObjectLocalizerProcessor::onDisconnectComponent
void onDisconnectComponent() override
Called from framework.
Definition: ObjectLocalizerProcessor.h:173
visionx::ObjectLocalizerProcessor::onExitObjectLocalizerProcessor
virtual void onExitObjectLocalizerProcessor()
ObjectLocalizerProcessor interface: subclass hook.
Definition: ObjectLocalizerProcessor.h:290
visionx::ObjectLocalizerProcessor::resultImagesData
boost::ptr_vector< boost::nullable< CByteImage > > resultImagesData
Definition: ObjectLocalizerProcessor.h:430
GfxTL::Vec2d
VectorXD< 2, double > Vec2d
Definition: VectorXD.h:736
memoryx::MultivariateNormalDistributionPtr
IceInternal::Handle< MultivariateNormalDistribution > MultivariateNormalDistributionPtr
Definition: ProbabilityMeasures.h:279
visionx::ObjectLocalizerProcessor::onInitObjectLocalizerProcessor
virtual void onInitObjectLocalizerProcessor()
ObjectLocalizerProcessor interface: subclass hook.
Definition: ObjectLocalizerProcessor.h:260
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
visionx::ObjectLocalizerProcessor::getStereoCalibration
CStereoCalibration * getStereoCalibration() const
Retrieve stereo calibration corresponding to image provider.
Definition: ObjectLocalizerProcessor.h:391
visionx::ObjectLocalizerProcessorJob
ObjectLocalizerProcessorJob encapsules the object localization job.
Definition: ObjectLocalizerProcessorJob.h:43
memoryx::GridFileManagerPtr
std::shared_ptr< GridFileManager > GridFileManagerPtr
Definition: AbstractEntityWrapper.h:33
visionx::ObjectLocalizerProcessor::getReferenceFrameName
std::string getReferenceFrameName() const
Retrieve whether result images are enabled.
Definition: ObjectLocalizerProcessor.h:346
Entity.h
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::ImageProcessorPropertyDefinitions
Definition: ImageProcessor.h:61
visionx::ObjectLocalizerProcessorPropertyDefinitions
Definition: ObjectLocalizerProcessor.h:78
visionx::ObjectLocalizerProcessor::getColorMask
Eigen::Vector3i getColorMask() const
Definition: ObjectLocalizerProcessor.h:380
visionx::ObjectLocalizerProcessor::onInitImageProcessor
void onInitImageProcessor() override
Called from framework.
Definition: ObjectLocalizerProcessor.cpp:60
TypeMapping.h
visionx::ObjectLocalizerProcessor::calculateLocalizationUncertainty
memoryx::MultivariateNormalDistributionPtr calculateLocalizationUncertainty(Vec2d left_point, Vec2d right_point)
Calculate 3D uncertainty from two 2d points in left and right camera.
Definition: ObjectLocalizerProcessor.cpp:221
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
visionx::ObjectLocalizerProcessor::cameraImagesData
CByteImage cameraImagesData[2]
Definition: ObjectLocalizerProcessor.h:428
armarx::PropertyDefinition::PropertyFactoryFunction
std::function< PropertyType(std::string)> PropertyFactoryFunction
Definition: PropertyDefinition.h:123
visionx::ImageProcessor::onDisconnectComponent
void onDisconnectComponent() override
Definition: ImageProcessor.cpp:90
visionx::ObjectLocalizerProcessor::numberOfResultImages
int numberOfResultImages
Definition: ObjectLocalizerProcessor.h:433
armarx::VariantType::MultivariateNormalDistribution
const armarx::VariantTypeId MultivariateNormalDistribution
Definition: ProbabilityMeasures.h:40
visionx::ObjectLocalizerProcessor::ObjectLocalizerProcessor
ObjectLocalizerProcessor()
Definition: ObjectLocalizerProcessor.cpp:52
visionx::ObjectLocalizerProcessor
ObjectLocalizerProcessor.
Definition: ObjectLocalizerProcessor.h:128