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 <Eigen/Core>
33 
34 #include <Color/ColorParameterSet.h>
35 #include <ObjectFinder/ObjectFinderStereo.h>
36 #include <Visualizer/OpenGLVisualizer.h>
37 
38 // global forward declarations
39 class CByteImage;
40 class CGLContext;
41 
42 namespace visionx
43 {
44  static const std::size_t SegmentableBitmapWidth = 64;
45  static const std::size_t SegmentableBitmapSize =
46  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  // properties of SegmentableTemplateRecognition
76  {
77  static inline Eigen::Vector3f
78  stringToVector3f(std::string propertyValue)
79  {
80  Eigen::Vector3f vec;
81  sscanf(propertyValue.c_str(),
82  "%f, %f, %f",
83  &vec.data()[0],
84  &vec.data()[1],
85  &vec.data()[2]);
86  return vec;
87  }
88 
89  public:
92  {
93  defineOptionalProperty<std::string>(
94  "ColorParameterFile",
95  "VisionX/examples/cebit-colors.txt",
96  "The color parameter file configures the colors used for segmentable recognition "
97  "(usually colors.txt)");
98  defineOptionalProperty<int>(
99  "MinPixelsPerRegion",
100  6000,
101  "Minimum number of pixels per region for detecting a uniformly colored object");
102  defineOptionalProperty<float>(
103  "MaxEpipolarDistance",
104  12,
105  "Maximum epipolar line distance allowed for a valid 3D recognition result");
106 
107  defineOptionalProperty<std::string>("TemplatePath", "VisionX/templates");
108 
109 
111  &stringToVector3f;
112 
113  defineOptionalProperty<Eigen::Vector3f>("MinPoint",
114  Eigen::Vector3f(-3000.0f, -3000.0f, 100.0f),
115  "min point for valid result bounding box")
116  .setFactory(f);
117  defineOptionalProperty<Eigen::Vector3f>("MaxPoint",
118  Eigen::Vector3f(3000.0f, 3000.0f, 3500.0f),
119  "max point for valid result bounding box")
120  .setFactory(f);
121 
122  defineOptionalProperty<float>("TemplateMatchThreshold", 0.7f, "");
123  defineOptionalProperty<float>("SizeRatioThreshold", 0.7f, "");
124  defineOptionalProperty<float>("CorrelationThreshold", 0.7f, "");
125 
126  defineOptionalProperty<bool>("SingleInstance", true, "");
127  }
128  };
129 
130  /**
131  * SegmentableTemplateRecognition uses CSegmentableRecognition from IVT to recognize and localize single-colored objects based on their color and shape.
132  * The object data is read from PriorKnowledge and CommonStorage via MemoryX.
133  * The object localization is invoked automatically by the working memory if the object has been requested there.
134  *
135  */
137  {
138  public:
139  /**
140  * @see PropertyUser::createPropertyDefinitions()
141  */
144  {
147  }
148 
149  void
150  reportStereoCalibrationChanged(const StereoCalibration& stereoCalibration,
151  bool x,
152  const std::string& referenceName,
153  const Ice::Current& c = Ice::emptyCurrent) override
154  {
156  stereoCalibration, x, referenceName, c);
157 
158  initRecognizer();
159  }
160 
161  /**
162  * @see Component::getDefaultName()
163  */
164  std::string
165  getDefaultName() const override
166  {
167  return "SegmentableTemplateRecognition";
168  }
169 
172 
173  protected:
174  /**
175  * @see ObjectLocalizerProcessor::onInitObjectLocalizerProcessor()
176  */
177  void onInitObjectLocalizerProcessor() override;
178 
179  /**
180  * Initializes the CSegmentableRecognition
181  *
182  * @see ObjectLocalizerProcessor::onConnectObjectLocalizerProcessor()
183  */
184  void
186  {
187 
188  debugObserver = getTopic<armarx::DebugObserverInterfacePrx>(
189  getProperty<std::string>("DebugObserverName").getValue());
190  }
191 
192  /**
193  * @see ObjectLocalizerProcessor::onExitObjectLocalizerProcessor()
194  */
195  void onExitObjectLocalizerProcessor() override;
196 
197  /**
198  * Initializes segmentable recognition
199  *
200  * @return success
201  */
202  bool initRecognizer() override;
203 
204  /**
205  * Add object class to segmentable object recognition.
206  *
207  * @param objectClassEntity entity containing all information available for the object class
208  * @param fileManager GridFileManager required to read files associated to prior knowledge from the database.
209  *
210  * @return success of adding this entity to the TexturedObjectDatabase
211  */
212  bool addObjectClass(const memoryx::EntityPtr& objectClassEntity,
213  const memoryx::GridFileManagerPtr& fileManager) override;
214 
215  /**
216  * localizes segmentable object instances
217  *
218  * @param objectClassNames names of the class to localize
219  * @param cameraImages the two input images
220  * @param resultImages the two result images. are provided if result images are enabled.
221  *
222  * @return list of object instances
223  */
224  memoryx::ObjectLocalizationResultList
225  localizeObjectClasses(const std::vector<std::string>& objectClassNames,
226  CByteImage** cameraImages,
227  armarx::MetaInfoSizeBasePtr imageMetaInfo,
228  CByteImage** resultImages) override;
229 
230  private:
231  void visualizeResults(const Object3DList& objectList, CByteImage** resultImages);
232 
233  float calculateRecognitionCertainty(const std::string& objectClassName,
234  const Object3DEntry& entry);
235 
236  private:
237  std::shared_ptr<CGLContext> contextGL;
238  std::shared_ptr<COpenGLVisualizer> m_pOpenGLVisualizer;
239  std::shared_ptr<CObjectFinderStereo> m_pObjectFinderStereo;
240 
241  // params
242  int paramMinPixelsPerRegion = 200;
243  float paramTemplateMatchThreshold = 0.7f;
244  float paramSizeRatioThreshold = 0.7f;
245  float paramCorrelationThreshold = 0.7f;
246  bool paramSingleInstance = true;
247 
248  // settings
249  std::string templatePath;
250  float minPixelsPerRegion;
251  float maxEpipolarDistance;
252  CColorParameterSet colorParameters;
253  // Object name => template data
254  std::map<std::string, SegmentableTemplateEntry> database;
255 
256  Vec3d validResultBoundingBoxMin, validResultBoundingBoxMax;
257 
258 
259  std::map<std::string, int> seq;
260 
261 
262  armarx::DebugObserverInterfacePrx debugObserver;
263  };
264 } // namespace visionx
visionx::SegmentableTemplateRecognition::onInitObjectLocalizerProcessor
void onInitObjectLocalizerProcessor() override
Definition: SegmentableTemplateRecognition.cpp:298
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:474
visionx::SegmentableTemplate::bitmap
std::uint32_t bitmap[SegmentableBitmapUInt32Size]
Definition: SegmentableTemplateRecognition.h:55
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
DebugObserver.h
GfxTL::Vec3d
VectorXD< 3, double > Vec3d
Definition: VectorXD.h:737
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::SegmentableTemplateRecognition::~SegmentableTemplateRecognition
~SegmentableTemplateRecognition() override
Definition: SegmentableTemplateRecognition.cpp:293
visionx::SegmentableTemplateRecognition::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: SegmentableTemplateRecognition.h:143
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:136
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:345
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
visionx::SegmentableTemplateHeader
Definition: SegmentableTemplateRecognition.h:58
visionx::SegmentableTemplateRecognition::onExitObjectLocalizerProcessor
void onExitObjectLocalizerProcessor() override
Definition: SegmentableTemplateRecognition.cpp:306
visionx::SegmentableTemplateRecognition::SegmentableTemplateRecognition
SegmentableTemplateRecognition()
Definition: SegmentableTemplateRecognition.cpp:289
visionx::SegmentableTemplateHeader::templates
SegmentableTemplate templates[1]
Definition: SegmentableTemplateRecognition.h:63
visionx::ObjectLocalizerProcessor::imageMetaInfo
armarx::MetaInfoSizeBasePtr imageMetaInfo
Definition: ObjectLocalizerProcessor.h:432
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:429
visionx::SegmentableTemplate::rotationX
float rotationX
Definition: SegmentableTemplateRecognition.h:52
visionx::SegmentableTemplateRecognitionPropertyDefinitions::SegmentableTemplateRecognitionPropertyDefinitions
SegmentableTemplateRecognitionPropertyDefinitions(std::string prefix)
Definition: SegmentableTemplateRecognition.h:90
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
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:150
memoryx::GridFileManagerPtr
std::shared_ptr< GridFileManager > GridFileManagerPtr
Definition: AbstractEntityWrapper.h:33
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:463
visionx::SegmentableTemplateRecognition::getDefaultName
std::string getDefaultName() const override
Definition: SegmentableTemplateRecognition.h:165
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:185
visionx::SegmentableTemplateRecognition::initRecognizer
bool initRecognizer() override
Initializes segmentable recognition.
Definition: SegmentableTemplateRecognition.cpp:331
visionx::SegmentableTemplateEntry
Definition: SegmentableTemplateRecognition.h:66
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
visionx::SegmentableTemplateEntry::color
ObjectColor color
Definition: SegmentableTemplateRecognition.h:70
visionx::SegmentableTemplateRecognitionPropertyDefinitions
Definition: SegmentableTemplateRecognition.h:74
armarx::PropertyDefinition::PropertyFactoryFunction
std::function< PropertyType(std::string)> PropertyFactoryFunction
Definition: PropertyDefinition.h:123
visionx::SegmentableTemplate::rotationY
float rotationY
Definition: SegmentableTemplateRecognition.h:53
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
visionx::ObjectLocalizerProcessor
ObjectLocalizerProcessor.
Definition: ObjectLocalizerProcessor.h:128