SegmentableTemplateRecognition.h
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::Component
17 * @author Kai Welke <welke at kit dot edu>
18 * @copyright 2013 Humanoids Group, HIS, KIT
19 * @license http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #pragma once
24 
25 
26 // VisionX
28 
30 
31 // IVT
32 #include <Color/ColorParameterSet.h>
33 #include <Visualizer/OpenGLVisualizer.h>
34 #include <ObjectFinder/ObjectFinderStereo.h>
35 
36 #include <Eigen/Core>
37 
38 // global forward declarations
39 class CByteImage;
40 class CGLContext;
41 
42 
43 namespace visionx
44 {
45  static const std::size_t SegmentableBitmapWidth = 64;
46  static const std::size_t SegmentableBitmapSize = SegmentableBitmapWidth * SegmentableBitmapWidth;
47  // How much uint32_t do we need to store all the bits
48  static const std::size_t SegmentableBitmapUInt32Size = SegmentableBitmapSize / 32;
49 
51  {
52  float rotationX;
53  float rotationY;
54  float rotationZ;
55  std::uint32_t bitmap[SegmentableBitmapUInt32Size];
56  };
57 
59  {
60  char magic[4];
61  char name[256];
62  std::uint32_t templateCount;
64  };
65 
67  {
68  std::unique_ptr<SegmentableTemplateHeader> data;
69  std::unique_ptr<CFloatMatrix> model;
70  ObjectColor color;
71  };
72 
73 
74 
75  // properties of SegmentableTemplateRecognition
78  {
79  static inline Eigen::Vector3f stringToVector3f(std::string propertyValue)
80  {
81  Eigen::Vector3f vec;
82  sscanf(propertyValue.c_str(), "%f, %f, %f", &vec.data()[0], &vec.data()[1], &vec.data()[2]);
83  return vec;
84  }
85  public:
88  {
89  defineOptionalProperty<std::string>("ColorParameterFile", "VisionX/examples/cebit-colors.txt", "The color parameter file configures the colors used for segmentable recognition (usually colors.txt)");
90  defineOptionalProperty<int>("MinPixelsPerRegion", 6000, "Minimum number of pixels per region for detecting a uniformly colored object");
91  defineOptionalProperty<float>("MaxEpipolarDistance", 12, "Maximum epipolar line distance allowed for a valid 3D recognition result");
92 
93  defineOptionalProperty<std::string>("TemplatePath", "VisionX/templates");
94 
95 
97 
98  defineOptionalProperty<Eigen::Vector3f>("MinPoint", Eigen::Vector3f(-3000.0f, -3000.0f, 100.0f), "min point for valid result bounding box").setFactory(f);
99  defineOptionalProperty<Eigen::Vector3f>("MaxPoint", Eigen::Vector3f(3000.0f, 3000.0f, 3500.0f), "max point for valid result bounding box").setFactory(f);
100 
101  defineOptionalProperty<float>("TemplateMatchThreshold", 0.7f, "");
102  defineOptionalProperty<float>("SizeRatioThreshold", 0.7f, "");
103  defineOptionalProperty<float>("CorrelationThreshold", 0.7f, "");
104 
105  defineOptionalProperty<bool>("SingleInstance", true, "");
106  }
107  };
108 
109  /**
110  * SegmentableTemplateRecognition uses CSegmentableRecognition from IVT to recognize and localize single-colored objects based on their color and shape.
111  * The object data is read from PriorKnowledge and CommonStorage via MemoryX.
112  * The object localization is invoked automatically by the working memory if the object has been requested there.
113  *
114  */
116  virtual public ObjectLocalizerProcessor
117  {
118  public:
119  /**
120  * @see PropertyUser::createPropertyDefinitions()
121  */
123  {
125  }
126 
127  void reportStereoCalibrationChanged(const StereoCalibration& stereoCalibration, bool x, const std::string& referenceName, const Ice::Current& c = Ice::emptyCurrent) override
128  {
129  ObjectLocalizerProcessor::reportStereoCalibrationChanged(stereoCalibration, x, referenceName, c);
130 
131  initRecognizer();
132  }
133 
134  /**
135  * @see Component::getDefaultName()
136  */
137  std::string getDefaultName() const override
138  {
139  return "SegmentableTemplateRecognition";
140  }
141 
144 
145  protected:
146  /**
147  * @see ObjectLocalizerProcessor::onInitObjectLocalizerProcessor()
148  */
149  void onInitObjectLocalizerProcessor() override;
150 
151  /**
152  * Initializes the CSegmentableRecognition
153  *
154  * @see ObjectLocalizerProcessor::onConnectObjectLocalizerProcessor()
155  */
157  {
158 
159  debugObserver = getTopic<armarx::DebugObserverInterfacePrx>(getProperty<std::string>("DebugObserverName").getValue());
160  }
161 
162  /**
163  * @see ObjectLocalizerProcessor::onExitObjectLocalizerProcessor()
164  */
165  void onExitObjectLocalizerProcessor() override;
166 
167  /**
168  * Initializes segmentable recognition
169  *
170  * @return success
171  */
172  bool initRecognizer() override;
173 
174  /**
175  * Add object class to segmentable object recognition.
176  *
177  * @param objectClassEntity entity containing all information available for the object class
178  * @param fileManager GridFileManager required to read files associated to prior knowledge from the database.
179  *
180  * @return success of adding this entity to the TexturedObjectDatabase
181  */
182  bool addObjectClass(const memoryx::EntityPtr& objectClassEntity, const memoryx::GridFileManagerPtr& fileManager) override;
183 
184  /**
185  * localizes segmentable object instances
186  *
187  * @param objectClassNames names of the class to localize
188  * @param cameraImages the two input images
189  * @param resultImages the two result images. are provided if result images are enabled.
190  *
191  * @return list of object instances
192  */
193  memoryx::ObjectLocalizationResultList localizeObjectClasses(const std::vector<std::string>& objectClassNames, CByteImage** cameraImages, armarx::MetaInfoSizeBasePtr imageMetaInfo, CByteImage** resultImages) override;
194 
195  private:
196  void visualizeResults(const Object3DList& objectList, CByteImage** resultImages);
197 
198  float calculateRecognitionCertainty(const std::string& objectClassName, const Object3DEntry& entry);
199 
200  private:
201  std::shared_ptr<CGLContext> contextGL;
202  std::shared_ptr<COpenGLVisualizer> m_pOpenGLVisualizer;
203  std::shared_ptr<CObjectFinderStereo> m_pObjectFinderStereo;
204 
205  // params
206  int paramMinPixelsPerRegion = 200;
207  float paramTemplateMatchThreshold = 0.7f;
208  float paramSizeRatioThreshold = 0.7f;
209  float paramCorrelationThreshold = 0.7f;
210  bool paramSingleInstance = true;
211 
212  // settings
213  std::string templatePath;
214  float minPixelsPerRegion;
215  float maxEpipolarDistance;
216  CColorParameterSet colorParameters;
217  // Object name => template data
218  std::map<std::string, SegmentableTemplateEntry> database;
219 
220  Vec3d validResultBoundingBoxMin, validResultBoundingBoxMax;
221 
222 
223  std::map<std::string, int> seq;
224 
225 
226  armarx::DebugObserverInterfacePrx debugObserver;
227 
228  };
229 }
visionx::SegmentableTemplateRecognition::onInitObjectLocalizerProcessor
void onInitObjectLocalizerProcessor() override
Definition: SegmentableTemplateRecognition.cpp:287
visionx::SegmentableTemplateRecognition::localizeObjectClasses
memoryx::ObjectLocalizationResultList localizeObjectClasses(const std::vector< std::string > &objectClassNames, CByteImage **cameraImages, armarx::MetaInfoSizeBasePtr imageMetaInfo, CByteImage **resultImages) override
localizes segmentable object instances
Definition: SegmentableTemplateRecognition.cpp:457
visionx::SegmentableTemplate::bitmap
std::uint32_t bitmap[SegmentableBitmapUInt32Size]
Definition: SegmentableTemplateRecognition.h:55
visionx::ObjectLocalizerProcessor::resultImages
CByteImage ** resultImages
Definition: ObjectLocalizerProcessor.h:374
visionx::ObjectLocalizerProcessor::reportStereoCalibrationChanged
void reportStereoCalibrationChanged(const StereoCalibration &stereoCalibration, bool imagesAreUndistorted, const std::string &referenceFrame, const Ice::Current &c=Ice::emptyCurrent) override
Definition: ObjectLocalizerProcessor.h:272
DebugObserver.h
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::SegmentableTemplateRecognition::~SegmentableTemplateRecognition
~SegmentableTemplateRecognition() override
Definition: SegmentableTemplateRecognition.cpp:282
visionx::SegmentableTemplateRecognition::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: SegmentableTemplateRecognition.h:122
visionx::SegmentableTemplateEntry::data
std::unique_ptr< SegmentableTemplateHeader > data
Definition: SegmentableTemplateRecognition.h:68
visionx::SegmentableTemplateRecognition
SegmentableTemplateRecognition uses CSegmentableRecognition from IVT to recognize and localize single...
Definition: SegmentableTemplateRecognition.h:115
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:333
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
visionx::SegmentableTemplateHeader
Definition: SegmentableTemplateRecognition.h:58
visionx::SegmentableTemplateRecognition::onExitObjectLocalizerProcessor
void onExitObjectLocalizerProcessor() override
Definition: SegmentableTemplateRecognition.cpp:294
visionx::SegmentableTemplateRecognition::SegmentableTemplateRecognition
SegmentableTemplateRecognition()
Definition: SegmentableTemplateRecognition.cpp:278
visionx::SegmentableTemplateHeader::templates
SegmentableTemplate templates[1]
Definition: SegmentableTemplateRecognition.h:63
visionx::ObjectLocalizerProcessor::imageMetaInfo
armarx::MetaInfoSizeBasePtr imageMetaInfo
Definition: ObjectLocalizerProcessor.h:375
visionx::SegmentableTemplate::rotationZ
float rotationZ
Definition: SegmentableTemplateRecognition.h:54
visionx::SegmentableTemplateHeader::magic
char magic[4]
Definition: SegmentableTemplateRecognition.h:60
IceInternal::Handle
Definition: forward_declarations.h:8
ObjectLocalizerProcessor.h
visionx::SegmentableTemplateEntry::model
std::unique_ptr< CFloatMatrix > model
Definition: SegmentableTemplateRecognition.h:69
visionx::ObjectLocalizerProcessor::cameraImages
CByteImage * cameraImages[2]
Definition: ObjectLocalizerProcessor.h:372
GfxTL::Vec3d
VectorXD< 3, double > Vec3d
Definition: VectorXD.h:695
visionx::SegmentableTemplate::rotationX
float rotationX
Definition: SegmentableTemplateRecognition.h:52
visionx::SegmentableTemplateRecognitionPropertyDefinitions::SegmentableTemplateRecognitionPropertyDefinitions
SegmentableTemplateRecognitionPropertyDefinitions(std::string prefix)
Definition: SegmentableTemplateRecognition.h:86
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
visionx::SegmentableTemplateHeader::name
char name[256]
Definition: SegmentableTemplateRecognition.h:61
visionx::SegmentableTemplateRecognition::reportStereoCalibrationChanged
void reportStereoCalibrationChanged(const StereoCalibration &stereoCalibration, bool x, const std::string &referenceName, const Ice::Current &c=Ice::emptyCurrent) override
Definition: SegmentableTemplateRecognition.h:127
memoryx::GridFileManagerPtr
std::shared_ptr< GridFileManager > GridFileManagerPtr
Definition: AbstractEntityWrapper.h:32
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::SegmentableTemplateHeader::templateCount
std::uint32_t templateCount
Definition: SegmentableTemplateRecognition.h:62
visionx::SegmentableTemplateRecognition::addObjectClass
bool addObjectClass(const memoryx::EntityPtr &objectClassEntity, const memoryx::GridFileManagerPtr &fileManager) override
Add object class to segmentable object recognition.
Definition: SegmentableTemplateRecognition.cpp:448
visionx::SegmentableTemplateRecognition::getDefaultName
std::string getDefaultName() const override
Definition: SegmentableTemplateRecognition.h:137
visionx::ObjectLocalizerProcessorPropertyDefinitions
Definition: ObjectLocalizerProcessor.h:78
IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface >
visionx::SegmentableTemplate
Definition: SegmentableTemplateRecognition.h:50
visionx::SegmentableTemplateRecognition::onConnectObjectLocalizerProcessor
void onConnectObjectLocalizerProcessor() override
Initializes the CSegmentableRecognition.
Definition: SegmentableTemplateRecognition.h:156
visionx::SegmentableTemplateRecognition::initRecognizer
bool initRecognizer() override
Initializes segmentable recognition.
Definition: SegmentableTemplateRecognition.cpp:318
visionx::SegmentableTemplateEntry
Definition: SegmentableTemplateRecognition.h:66
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
visionx::SegmentableTemplateEntry::color
ObjectColor color
Definition: SegmentableTemplateRecognition.h:70
visionx::SegmentableTemplateRecognitionPropertyDefinitions
Definition: SegmentableTemplateRecognition.h:76
armarx::PropertyDefinition::PropertyFactoryFunction
std::function< PropertyType(std::string)> PropertyFactoryFunction
Definition: PropertyDefinition.h:114
visionx::SegmentableTemplate::rotationY
float rotationY
Definition: SegmentableTemplateRecognition.h:53
visionx::ObjectLocalizerProcessor
ObjectLocalizerProcessor.
Definition: ObjectLocalizerProcessor.h:107