Point Cloud Library (PCL)  1.12.1-dev
surface_normal_modality.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #pragma once
39 
40 #include <pcl/recognition/quantizable_modality.h>
41 #include <pcl/recognition/distance_map.h>
42 
43 #include <pcl/pcl_base.h>
44 #include <pcl/point_cloud.h>
45 #include <pcl/point_types.h>
46 #include <pcl/features/linear_least_squares_normal.h>
47 
48 #include <algorithm>
49 #include <cmath>
50 #include <cstddef>
51 // #include <iostream>
52 #include <limits>
53 #include <list>
54 #include <vector>
55 
56 namespace pcl
57 {
58 
59  /** \brief Map that stores orientations.
60  * \author Stefan Holzer
61  */
63  {
64  public:
65  /** \brief Constructor. */
66  inline LINEMOD_OrientationMap () : width_ (0), height_ (0) {}
67  /** \brief Destructor. */
68  inline ~LINEMOD_OrientationMap () = default;
69 
70  /** \brief Returns the width of the modality data map. */
71  inline std::size_t
72  getWidth () const
73  {
74  return width_;
75  }
76 
77  /** \brief Returns the height of the modality data map. */
78  inline std::size_t
79  getHeight () const
80  {
81  return height_;
82  }
83 
84  /** \brief Resizes the map to the specific width and height and initializes
85  * all new elements with the specified value.
86  * \param[in] width the width of the resized map.
87  * \param[in] height the height of the resized map.
88  * \param[in] value the value all new elements will be initialized with.
89  */
90  inline void
91  resize (const std::size_t width, const std::size_t height, const float value)
92  {
93  width_ = width;
94  height_ = height;
95  map_.clear ();
96  map_.resize (width*height, value);
97  }
98 
99  /** \brief Operator to access elements of the map.
100  * \param[in] col_index the column index of the element to access.
101  * \param[in] row_index the row index of the element to access.
102  */
103  inline float &
104  operator() (const std::size_t col_index, const std::size_t row_index)
105  {
106  return map_[row_index * width_ + col_index];
107  }
108 
109  /** \brief Operator to access elements of the map.
110  * \param[in] col_index the column index of the element to access.
111  * \param[in] row_index the row index of the element to access.
112  */
113  inline const float &
114  operator() (const std::size_t col_index, const std::size_t row_index) const
115  {
116  return map_[row_index * width_ + col_index];
117  }
118 
119  private:
120  /** \brief The width of the map. */
121  std::size_t width_;
122  /** \brief The height of the map. */
123  std::size_t height_;
124  /** \brief Storage for the data of the map. */
125  std::vector<float> map_;
126 
127  };
128 
129  /** \brief Look-up-table for fast surface normal quantization.
130  * \author Stefan Holzer
131  */
133  {
134  /** \brief The range of the LUT in x-direction. */
135  int range_x;
136  /** \brief The range of the LUT in y-direction. */
137  int range_y;
138  /** \brief The range of the LUT in z-direction. */
139  int range_z;
140 
141  /** \brief The offset in x-direction. */
142  int offset_x;
143  /** \brief The offset in y-direction. */
144  int offset_y;
145  /** \brief The offset in z-direction. */
146  int offset_z;
147 
148  /** \brief The size of the LUT in x-direction. */
149  int size_x;
150  /** \brief The size of the LUT in y-direction. */
151  int size_y;
152  /** \brief The size of the LUT in z-direction. */
153  int size_z;
154 
155  /** \brief The LUT data. */
156  unsigned char * lut;
157 
158  /** \brief Constructor. */
160  range_x (-1), range_y (-1), range_z (-1),
161  offset_x (-1), offset_y (-1), offset_z (-1),
162  size_x (-1), size_y (-1), size_z (-1), lut (nullptr)
163  {}
164 
165  /** \brief Destructor. */
167  {
168  delete[] lut;
169  }
170 
171  /** \brief Initializes the LUT.
172  * \param[in] range_x_arg the range of the LUT in x-direction.
173  * \param[in] range_y_arg the range of the LUT in y-direction.
174  * \param[in] range_z_arg the range of the LUT in z-direction.
175  */
176  void
177  initializeLUT (const int range_x_arg, const int range_y_arg, const int range_z_arg)
178  {
179  range_x = range_x_arg;
180  range_y = range_y_arg;
181  range_z = range_z_arg;
182  size_x = range_x_arg+1;
183  size_y = range_y_arg+1;
184  size_z = range_z_arg+1;
185  offset_x = range_x_arg/2;
186  offset_y = range_y_arg/2;
187  offset_z = range_z_arg;
188 
189  //if (lut != NULL) free16(lut);
190  //lut = malloc16(size_x*size_y*size_z);
191 
192  delete[] lut;
193  lut = new unsigned char[size_x*size_y*size_z];
194 
195  const int nr_normals = 8;
196  pcl::PointCloud<PointXYZ>::VectorType ref_normals (nr_normals);
197 
198  const float normal0_angle = 40.0f * 3.14f / 180.0f;
199  ref_normals[0].x = std::cos (normal0_angle);
200  ref_normals[0].y = 0.0f;
201  ref_normals[0].z = -sinf (normal0_angle);
202 
203  const float inv_nr_normals = 1.0f / static_cast<float> (nr_normals);
204  for (int normal_index = 1; normal_index < nr_normals; ++normal_index)
205  {
206  const float angle = 2.0f * static_cast<float> (M_PI * normal_index * inv_nr_normals);
207 
208  ref_normals[normal_index].x = std::cos (angle) * ref_normals[0].x - sinf (angle) * ref_normals[0].y;
209  ref_normals[normal_index].y = sinf (angle) * ref_normals[0].x + std::cos (angle) * ref_normals[0].y;
210  ref_normals[normal_index].z = ref_normals[0].z;
211  }
212 
213  // normalize normals
214  for (int normal_index = 0; normal_index < nr_normals; ++normal_index)
215  {
216  const float length = std::sqrt (ref_normals[normal_index].x * ref_normals[normal_index].x +
217  ref_normals[normal_index].y * ref_normals[normal_index].y +
218  ref_normals[normal_index].z * ref_normals[normal_index].z);
219 
220  const float inv_length = 1.0f / length;
221 
222  ref_normals[normal_index].x *= inv_length;
223  ref_normals[normal_index].y *= inv_length;
224  ref_normals[normal_index].z *= inv_length;
225  }
226 
227  // set LUT
228  for (int z_index = 0; z_index < size_z; ++z_index)
229  {
230  for (int y_index = 0; y_index < size_y; ++y_index)
231  {
232  for (int x_index = 0; x_index < size_x; ++x_index)
233  {
234  PointXYZ normal (static_cast<float> (x_index - range_x/2),
235  static_cast<float> (y_index - range_y/2),
236  static_cast<float> (z_index - range_z));
237  const float length = std::sqrt (normal.x*normal.x + normal.y*normal.y + normal.z*normal.z);
238  const float inv_length = 1.0f / (length + 0.00001f);
239 
240  normal.x *= inv_length;
241  normal.y *= inv_length;
242  normal.z *= inv_length;
243 
244  float max_response = -1.0f;
245  int max_index = -1;
246 
247  for (int normal_index = 0; normal_index < nr_normals; ++normal_index)
248  {
249  const float response = normal.x * ref_normals[normal_index].x +
250  normal.y * ref_normals[normal_index].y +
251  normal.z * ref_normals[normal_index].z;
252 
253  const float abs_response = std::abs (response);
254  if (max_response < abs_response)
255  {
256  max_response = abs_response;
257  max_index = normal_index;
258  }
259 
260  lut[z_index*size_y*size_x + y_index*size_x + x_index] = static_cast<unsigned char> (0x1 << max_index);
261  }
262  }
263  }
264  }
265  }
266 
267  /** \brief Operator to access an element in the LUT.
268  * \param[in] x the x-component of the normal.
269  * \param[in] y the y-component of the normal.
270  * \param[in] z the z-component of the normal.
271  */
272  inline unsigned char
273  operator() (const float x, const float y, const float z) const
274  {
275  const std::size_t x_index = static_cast<std::size_t> (x * static_cast<float> (offset_x) + static_cast<float> (offset_x));
276  const std::size_t y_index = static_cast<std::size_t> (y * static_cast<float> (offset_y) + static_cast<float> (offset_y));
277  const std::size_t z_index = static_cast<std::size_t> (z * static_cast<float> (range_z) + static_cast<float> (range_z));
278 
279  const std::size_t index = z_index*size_y*size_x + y_index*size_x + x_index;
280 
281  return (lut[index]);
282  }
283 
284  /** \brief Operator to access an element in the LUT.
285  * \param[in] index the index of the element.
286  */
287  inline unsigned char
288  operator() (const int index) const
289  {
290  return (lut[index]);
291  }
292  };
293 
294 
295  /** \brief Modality based on surface normals.
296  * \author Stefan Holzer
297  */
298  template <typename PointInT>
299  class SurfaceNormalModality : public QuantizableModality, public PCLBase<PointInT>
300  {
301  protected:
303 
304  /** \brief Candidate for a feature (used in feature extraction methods). */
305  struct Candidate
306  {
307  /** \brief Constructor. */
308  Candidate () : distance (0.0f), bin_index (0), x (0), y (0) {}
309 
310  /** \brief Normal. */
312  /** \brief Distance to the next different quantized value. */
313  float distance;
314 
315  /** \brief Quantized value. */
316  unsigned char bin_index;
317 
318  /** \brief x-position of the feature. */
319  std::size_t x;
320  /** \brief y-position of the feature. */
321  std::size_t y;
322 
323  /** \brief Compares two candidates based on their distance to the next different quantized value.
324  * \param[in] rhs the candidate to compare with.
325  */
326  bool
327  operator< (const Candidate & rhs) const
328  {
329  return (distance > rhs.distance);
330  }
331  };
332 
333  public:
335 
336  /** \brief Constructor. */
338  /** \brief Destructor. */
340 
341  /** \brief Sets the spreading size.
342  * \param[in] spreading_size the spreading size.
343  */
344  inline void
345  setSpreadingSize (const std::size_t spreading_size)
346  {
347  spreading_size_ = spreading_size;
348  }
349 
350  /** \brief Enables/disables the use of extracting a variable number of features.
351  * \param[in] enabled specifies whether extraction of a variable number of features will be enabled/disabled.
352  */
353  inline void
354  setVariableFeatureNr (const bool enabled)
355  {
356  variable_feature_nr_ = enabled;
357  }
358 
359  /** \brief Returns the surface normals. */
362  {
363  return surface_normals_;
364  }
365 
366  /** \brief Returns the surface normals. */
367  inline const pcl::PointCloud<pcl::Normal> &
369  {
370  return surface_normals_;
371  }
372 
373  /** \brief Returns a reference to the internal quantized map. */
374  inline QuantizedMap &
375  getQuantizedMap () override
376  {
377  return (filtered_quantized_surface_normals_);
378  }
379 
380  /** \brief Returns a reference to the internal spread quantized map. */
381  inline QuantizedMap &
383  {
384  return (spreaded_quantized_surface_normals_);
385  }
386 
387  /** \brief Returns a reference to the orientation map. */
388  inline LINEMOD_OrientationMap &
390  {
391  return (surface_normal_orientations_);
392  }
393 
394  /** \brief Extracts features from this modality within the specified mask.
395  * \param[in] mask defines the areas where features are searched in.
396  * \param[in] nr_features defines the number of features to be extracted
397  * (might be less if not sufficient information is present in the modality).
398  * \param[in] modality_index the index which is stored in the extracted features.
399  * \param[out] features the destination for the extracted features.
400  */
401  void
402  extractFeatures (const MaskMap & mask, std::size_t nr_features, std::size_t modality_index,
403  std::vector<QuantizedMultiModFeature> & features) const override;
404 
405  /** \brief Extracts all possible features from the modality within the specified mask.
406  * \param[in] mask defines the areas where features are searched in.
407  * \param[in] nr_features IGNORED (TODO: remove this parameter).
408  * \param[in] modality_index the index which is stored in the extracted features.
409  * \param[out] features the destination for the extracted features.
410  */
411  void
412  extractAllFeatures (const MaskMap & mask, std::size_t nr_features, std::size_t modality_index,
413  std::vector<QuantizedMultiModFeature> & features) const override;
414 
415  /** \brief Provide a pointer to the input dataset (overwrites the PCLBase::setInputCloud method)
416  * \param[in] cloud the const boost shared pointer to a PointCloud message
417  */
418  void
419  setInputCloud (const typename PointCloudIn::ConstPtr & cloud) override
420  {
421  input_ = cloud;
422  }
423 
424  /** \brief Processes the input data (smoothing, computing gradients, quantizing, filtering, spreading). */
425  virtual void
427 
428  /** \brief Processes the input data assuming that everything up to filtering is already done/available
429  * (so only spreading is performed). */
430  virtual void
432 
433  protected:
434 
435  /** \brief Computes the surface normals from the input cloud. */
436  void
438 
439  /** \brief Computes and quantizes the surface normals. */
440  void
442 
443  /** \brief Computes and quantizes the surface normals. */
444  void
446 
447  /** \brief Quantizes the surface normals. */
448  void
450 
451  /** \brief Filters the quantized surface normals. */
452  void
454 
455  /** \brief Computes a distance map from the supplied input mask.
456  * \param[in] input the mask for which a distance map will be computed.
457  * \param[out] output the destination for the distance map.
458  */
459  void
460  computeDistanceMap (const MaskMap & input, DistanceMap & output) const;
461 
462  private:
463 
464  /** \brief Determines whether variable numbers of features are extracted or not. */
465  bool variable_feature_nr_;
466 
467  /** \brief The feature distance threshold. */
468  float feature_distance_threshold_;
469  /** \brief Minimum distance of a feature to a border. */
470  float min_distance_to_border_;
471 
472  /** \brief Look-up-table for quantizing surface normals. */
473  QuantizedNormalLookUpTable normal_lookup_;
474 
475  /** \brief The spreading size. */
476  std::size_t spreading_size_;
477 
478  /** \brief Point cloud holding the computed surface normals. */
479  pcl::PointCloud<pcl::Normal> surface_normals_;
480  /** \brief Quantized surface normals. */
481  pcl::QuantizedMap quantized_surface_normals_;
482  /** \brief Filtered quantized surface normals. */
483  pcl::QuantizedMap filtered_quantized_surface_normals_;
484  /** \brief Spread quantized surface normals. */
485  pcl::QuantizedMap spreaded_quantized_surface_normals_;
486 
487  /** \brief Map containing surface normal orientations. */
488  pcl::LINEMOD_OrientationMap surface_normal_orientations_;
489 
490  };
491 
492 }
493 
494 //////////////////////////////////////////////////////////////////////////////////////////////
495 template <typename PointInT>
498  : variable_feature_nr_ (false)
499  , feature_distance_threshold_ (2.0f)
500  , min_distance_to_border_ (2.0f)
501  , spreading_size_ (8)
502 {
503 }
504 
505 //////////////////////////////////////////////////////////////////////////////////////////////
506 template <typename PointInT>
508 
509 //////////////////////////////////////////////////////////////////////////////////////////////
510 template <typename PointInT> void
512 {
513  // compute surface normals
514  //computeSurfaceNormals ();
515 
516  // quantize surface normals
517  //quantizeSurfaceNormals ();
518 
519  computeAndQuantizeSurfaceNormals2 ();
520 
521  // filter quantized surface normals
522  filterQuantizedSurfaceNormals ();
523 
524  // spread quantized surface normals
525  pcl::QuantizedMap::spreadQuantizedMap (filtered_quantized_surface_normals_,
526  spreaded_quantized_surface_normals_,
527  spreading_size_);
528 }
529 
530 //////////////////////////////////////////////////////////////////////////////////////////////
531 template <typename PointInT> void
533 {
534  // spread quantized surface normals
535  pcl::QuantizedMap::spreadQuantizedMap (filtered_quantized_surface_normals_,
536  spreaded_quantized_surface_normals_,
537  spreading_size_);
538 }
539 
540 //////////////////////////////////////////////////////////////////////////////////////////////
541 template <typename PointInT> void
543 {
544  // compute surface normals
546  ne.setMaxDepthChangeFactor(0.05f);
547  ne.setNormalSmoothingSize(5.0f);
548  ne.setDepthDependentSmoothing(false);
549  ne.setInputCloud (input_);
550  ne.compute (surface_normals_);
551 }
552 
553 //////////////////////////////////////////////////////////////////////////////////////////////
554 template <typename PointInT> void
556 {
557  // compute surface normals
558  //pcl::LinearLeastSquaresNormalEstimation<PointInT, pcl::Normal> ne;
559  //ne.setMaxDepthChangeFactor(0.05f);
560  //ne.setNormalSmoothingSize(5.0f);
561  //ne.setDepthDependentSmoothing(false);
562  //ne.setInputCloud (input_);
563  //ne.compute (surface_normals_);
564 
565 
566  const float bad_point = std::numeric_limits<float>::quiet_NaN ();
567 
568  const int width = input_->width;
569  const int height = input_->height;
570 
571  surface_normals_.resize (width*height);
572  surface_normals_.width = width;
573  surface_normals_.height = height;
574  surface_normals_.is_dense = false;
575 
576  quantized_surface_normals_.resize (width, height);
577 
578  // we compute the normals as follows:
579  // ----------------------------------
580  //
581  // for the depth-gradient you can make the following first-order Taylor approximation:
582  // D(x + dx) - D(x) = dx^T \Delta D + h.o.t.
583  //
584  // build linear system by stacking up equation for 8 neighbor points:
585  // Y = X \Delta D
586  //
587  // => \Delta D = (X^T X)^{-1} X^T Y
588  // => \Delta D = (A)^{-1} b
589 
590  for (int y = 0; y < height; ++y)
591  {
592  for (int x = 0; x < width; ++x)
593  {
594  const int index = y * width + x;
595 
596  const float px = (*input_)[index].x;
597  const float py = (*input_)[index].y;
598  const float pz = (*input_)[index].z;
599 
600  if (std::isnan(px) || pz > 2.0f)
601  {
602  surface_normals_[index].normal_x = bad_point;
603  surface_normals_[index].normal_y = bad_point;
604  surface_normals_[index].normal_z = bad_point;
605  surface_normals_[index].curvature = bad_point;
606 
607  quantized_surface_normals_ (x, y) = 0;
608 
609  continue;
610  }
611 
612  const int smoothingSizeInt = 5;
613 
614  float matA0 = 0.0f;
615  float matA1 = 0.0f;
616  float matA3 = 0.0f;
617 
618  float vecb0 = 0.0f;
619  float vecb1 = 0.0f;
620 
621  for (int v = y - smoothingSizeInt; v <= y + smoothingSizeInt; v += smoothingSizeInt)
622  {
623  for (int u = x - smoothingSizeInt; u <= x + smoothingSizeInt; u += smoothingSizeInt)
624  {
625  if (u < 0 || u >= width || v < 0 || v >= height) continue;
626 
627  const std::size_t index2 = v * width + u;
628 
629  const float qx = (*input_)[index2].x;
630  const float qy = (*input_)[index2].y;
631  const float qz = (*input_)[index2].z;
632 
633  if (std::isnan(qx)) continue;
634 
635  const float delta = qz - pz;
636  const float i = qx - px;
637  const float j = qy - py;
638 
639  const float f = std::abs(delta) < 0.05f ? 1.0f : 0.0f;
640 
641  matA0 += f * i * i;
642  matA1 += f * i * j;
643  matA3 += f * j * j;
644  vecb0 += f * i * delta;
645  vecb1 += f * j * delta;
646  }
647  }
648 
649  const float det = matA0 * matA3 - matA1 * matA1;
650  const float ddx = matA3 * vecb0 - matA1 * vecb1;
651  const float ddy = -matA1 * vecb0 + matA0 * vecb1;
652 
653  const float nx = ddx;
654  const float ny = ddy;
655  const float nz = -det * pz;
656 
657  const float length = nx * nx + ny * ny + nz * nz;
658 
659  if (length <= 0.0f)
660  {
661  surface_normals_[index].normal_x = bad_point;
662  surface_normals_[index].normal_y = bad_point;
663  surface_normals_[index].normal_z = bad_point;
664  surface_normals_[index].curvature = bad_point;
665 
666  quantized_surface_normals_ (x, y) = 0;
667  }
668  else
669  {
670  const float normInv = 1.0f / std::sqrt (length);
671 
672  const float normal_x = nx * normInv;
673  const float normal_y = ny * normInv;
674  const float normal_z = nz * normInv;
675 
676  surface_normals_[index].normal_x = normal_x;
677  surface_normals_[index].normal_y = normal_y;
678  surface_normals_[index].normal_z = normal_z;
679  surface_normals_[index].curvature = bad_point;
680 
681  float angle = 11.25f + std::atan2 (normal_y, normal_x)*180.0f/3.14f;
682 
683  if (angle < 0.0f) angle += 360.0f;
684  if (angle >= 360.0f) angle -= 360.0f;
685 
686  int bin_index = static_cast<int> (angle*8.0f/360.0f) & 7;
687 
688  quantized_surface_normals_ (x, y) = static_cast<unsigned char> (bin_index);
689  }
690  }
691  }
692 }
693 
694 
695 //////////////////////////////////////////////////////////////////////////////////////////////
696 // Contains GRANULARITY and NORMAL_LUT
697 //#include "normal_lut.i"
698 
699 static void accumBilateral(long delta, long i, long j, long * A, long * b, int threshold)
700 {
701  long f = std::abs(delta) < threshold ? 1 : 0;
702 
703  const long fi = f * i;
704  const long fj = f * j;
705 
706  A[0] += fi * i;
707  A[1] += fi * j;
708  A[3] += fj * j;
709  b[0] += fi * delta;
710  b[1] += fj * delta;
711 }
712 
713 /**
714  * \brief Compute quantized normal image from depth image.
715  *
716  * Implements section 2.6 "Extension to Dense Depth Sensors."
717  *
718  * \todo Should also need camera model, or at least focal lengths? Replace distance_threshold with mask?
719  */
720 template <typename PointInT> void
722 {
723  const int width = input_->width;
724  const int height = input_->height;
725 
726  unsigned short * lp_depth = new unsigned short[width*height];
727  unsigned char * lp_normals = new unsigned char[width*height];
728  memset (lp_normals, 0, width*height);
729 
730  surface_normal_orientations_.resize (width, height, 0.0f);
731 
732  for (int row_index = 0; row_index < height; ++row_index)
733  {
734  for (int col_index = 0; col_index < width; ++col_index)
735  {
736  const float value = (*input_)[row_index*width + col_index].z;
737  if (std::isfinite (value))
738  {
739  lp_depth[row_index*width + col_index] = static_cast<unsigned short> (value * 1000.0f);
740  }
741  else
742  {
743  lp_depth[row_index*width + col_index] = 0;
744  }
745  }
746  }
747 
748  const int l_W = width;
749  const int l_H = height;
750 
751  const int l_r = 5; // used to be 7
752  //const int l_offset0 = -l_r - l_r * l_W;
753  //const int l_offset1 = 0 - l_r * l_W;
754  //const int l_offset2 = +l_r - l_r * l_W;
755  //const int l_offset3 = -l_r;
756  //const int l_offset4 = +l_r;
757  //const int l_offset5 = -l_r + l_r * l_W;
758  //const int l_offset6 = 0 + l_r * l_W;
759  //const int l_offset7 = +l_r + l_r * l_W;
760 
761  const int offsets_i[] = {-l_r, 0, l_r, -l_r, l_r, -l_r, 0, l_r};
762  const int offsets_j[] = {-l_r, -l_r, -l_r, 0, 0, l_r, l_r, l_r};
763  const int offsets[] = { offsets_i[0] + offsets_j[0] * l_W
764  , offsets_i[1] + offsets_j[1] * l_W
765  , offsets_i[2] + offsets_j[2] * l_W
766  , offsets_i[3] + offsets_j[3] * l_W
767  , offsets_i[4] + offsets_j[4] * l_W
768  , offsets_i[5] + offsets_j[5] * l_W
769  , offsets_i[6] + offsets_j[6] * l_W
770  , offsets_i[7] + offsets_j[7] * l_W };
771 
772 
773  //const int l_offsetx = GRANULARITY / 2;
774  //const int l_offsety = GRANULARITY / 2;
775 
776  const int difference_threshold = 50;
777  const int distance_threshold = 2000;
778 
779  //const double scale = 1000.0;
780  //const double difference_threshold = 0.05 * scale;
781  //const double distance_threshold = 2.0 * scale;
782 
783  for (int l_y = l_r; l_y < l_H - l_r - 1; ++l_y)
784  {
785  unsigned short * lp_line = lp_depth + (l_y * l_W + l_r);
786  unsigned char * lp_norm = lp_normals + (l_y * l_W + l_r);
787 
788  for (int l_x = l_r; l_x < l_W - l_r - 1; ++l_x)
789  {
790  long l_d = lp_line[0];
791  //float l_d = (*input_)[(l_y * l_W + l_r) + l_x].z;
792  //float px = (*input_)[(l_y * l_W + l_r) + l_x].x;
793  //float py = (*input_)[(l_y * l_W + l_r) + l_x].y;
794 
795  if (l_d < distance_threshold)
796  {
797  // accum
798  long l_A[4]; l_A[0] = l_A[1] = l_A[2] = l_A[3] = 0;
799  long l_b[2]; l_b[0] = l_b[1] = 0;
800  //double l_A[4]; l_A[0] = l_A[1] = l_A[2] = l_A[3] = 0;
801  //double l_b[2]; l_b[0] = l_b[1] = 0;
802 
803  accumBilateral(lp_line[offsets[0]] - l_d, offsets_i[0], offsets_j[0], l_A, l_b, difference_threshold);
804  accumBilateral(lp_line[offsets[1]] - l_d, offsets_i[1], offsets_j[1], l_A, l_b, difference_threshold);
805  accumBilateral(lp_line[offsets[2]] - l_d, offsets_i[2], offsets_j[2], l_A, l_b, difference_threshold);
806  accumBilateral(lp_line[offsets[3]] - l_d, offsets_i[3], offsets_j[3], l_A, l_b, difference_threshold);
807  accumBilateral(lp_line[offsets[4]] - l_d, offsets_i[4], offsets_j[4], l_A, l_b, difference_threshold);
808  accumBilateral(lp_line[offsets[5]] - l_d, offsets_i[5], offsets_j[5], l_A, l_b, difference_threshold);
809  accumBilateral(lp_line[offsets[6]] - l_d, offsets_i[6], offsets_j[6], l_A, l_b, difference_threshold);
810  accumBilateral(lp_line[offsets[7]] - l_d, offsets_i[7], offsets_j[7], l_A, l_b, difference_threshold);
811 
812  //for (std::size_t index = 0; index < 8; ++index)
813  //{
814  // //accumBilateral(lp_line[offsets[index]] - l_d, offsets_i[index], offsets_j[index], l_A, l_b, difference_threshold);
815 
816  // //const long delta = lp_line[offsets[index]] - l_d;
817  // //const long i = offsets_i[index];
818  // //const long j = offsets_j[index];
819  // //long * A = l_A;
820  // //long * b = l_b;
821  // //const int threshold = difference_threshold;
822 
823  // //const long f = std::abs(delta) < threshold ? 1 : 0;
824 
825  // //const long fi = f * i;
826  // //const long fj = f * j;
827 
828  // //A[0] += fi * i;
829  // //A[1] += fi * j;
830  // //A[3] += fj * j;
831  // //b[0] += fi * delta;
832  // //b[1] += fj * delta;
833 
834 
835  // const double delta = 1000.0f * ((*input_)[(l_y * l_W + l_r) + l_x + offsets[index]].z - l_d);
836  // const double i = offsets_i[index];
837  // const double j = offsets_j[index];
838  // //const float i = (*input_)[(l_y * l_W + l_r) + l_x + offsets[index]].x - px;//offsets_i[index];
839  // //const float j = (*input_)[(l_y * l_W + l_r) + l_x + offsets[index]].y - py;//offsets_j[index];
840  // double * A = l_A;
841  // double * b = l_b;
842  // const double threshold = difference_threshold;
843 
844  // const double f = std::fabs(delta) < threshold ? 1.0f : 0.0f;
845 
846  // const double fi = f * i;
847  // const double fj = f * j;
848 
849  // A[0] += fi * i;
850  // A[1] += fi * j;
851  // A[3] += fj * j;
852  // b[0] += fi * delta;
853  // b[1] += fj * delta;
854  //}
855 
856  //long f = std::abs(delta) < threshold ? 1 : 0;
857 
858  //const long fi = f * i;
859  //const long fj = f * j;
860 
861  //A[0] += fi * i;
862  //A[1] += fi * j;
863  //A[3] += fj * j;
864  //b[0] += fi * delta;
865  //b[1] += fj * delta;
866 
867 
868  // solve
869  long l_det = l_A[0] * l_A[3] - l_A[1] * l_A[1];
870  long l_ddx = l_A[3] * l_b[0] - l_A[1] * l_b[1];
871  long l_ddy = -l_A[1] * l_b[0] + l_A[0] * l_b[1];
872 
873  /// @todo Magic number 1150 is focal length? This is something like
874  /// f in SXGA mode, but in VGA is more like 530.
875  float l_nx = static_cast<float>(1150 * l_ddx);
876  float l_ny = static_cast<float>(1150 * l_ddy);
877  float l_nz = static_cast<float>(-l_det * l_d);
878 
879  //// solve
880  //double l_det = l_A[0] * l_A[3] - l_A[1] * l_A[1];
881  //double l_ddx = l_A[3] * l_b[0] - l_A[1] * l_b[1];
882  //double l_ddy = -l_A[1] * l_b[0] + l_A[0] * l_b[1];
883 
884  ///// @todo Magic number 1150 is focal length? This is something like
885  ///// f in SXGA mode, but in VGA is more like 530.
886  //const double dummy_focal_length = 1150.0f;
887  //double l_nx = l_ddx * dummy_focal_length;
888  //double l_ny = l_ddy * dummy_focal_length;
889  //double l_nz = -l_det * l_d;
890 
891  float l_sqrt = std::sqrt (l_nx * l_nx + l_ny * l_ny + l_nz * l_nz);
892 
893  if (l_sqrt > 0)
894  {
895  float l_norminv = 1.0f / (l_sqrt);
896 
897  l_nx *= l_norminv;
898  l_ny *= l_norminv;
899  l_nz *= l_norminv;
900 
901  float angle = 22.5f + std::atan2 (l_ny, l_nx) * 180.0f / 3.14f;
902 
903  if (angle < 0.0f) angle += 360.0f;
904  if (angle >= 360.0f) angle -= 360.0f;
905 
906  int bin_index = static_cast<int> (angle*8.0f/360.0f) & 7;
907 
908  surface_normal_orientations_ (l_x, l_y) = angle;
909 
910  //*lp_norm = std::abs(l_nz)*255;
911 
912  //int l_val1 = static_cast<int>(l_nx * l_offsetx + l_offsetx);
913  //int l_val2 = static_cast<int>(l_ny * l_offsety + l_offsety);
914  //int l_val3 = static_cast<int>(l_nz * GRANULARITY + GRANULARITY);
915 
916  //*lp_norm = NORMAL_LUT[l_val3][l_val2][l_val1];
917  *lp_norm = static_cast<unsigned char> (0x1 << bin_index);
918  }
919  else
920  {
921  *lp_norm = 0; // Discard shadows from depth sensor
922  }
923  }
924  else
925  {
926  *lp_norm = 0; //out of depth
927  }
928  ++lp_line;
929  ++lp_norm;
930  }
931  }
932  /*cvSmooth(m_dep[0], m_dep[0], CV_MEDIAN, 5, 5);*/
933 
934  unsigned char map[255];
935  memset(map, 0, 255);
936 
937  map[0x1<<0] = 0;
938  map[0x1<<1] = 1;
939  map[0x1<<2] = 2;
940  map[0x1<<3] = 3;
941  map[0x1<<4] = 4;
942  map[0x1<<5] = 5;
943  map[0x1<<6] = 6;
944  map[0x1<<7] = 7;
945 
946  quantized_surface_normals_.resize (width, height);
947  for (int row_index = 0; row_index < height; ++row_index)
948  {
949  for (int col_index = 0; col_index < width; ++col_index)
950  {
951  quantized_surface_normals_ (col_index, row_index) = map[lp_normals[row_index*width + col_index]];
952  }
953  }
954 
955  delete[] lp_depth;
956  delete[] lp_normals;
957 }
958 
959 
960 //////////////////////////////////////////////////////////////////////////////////////////////
961 template <typename PointInT> void
963  const std::size_t nr_features,
964  const std::size_t modality_index,
965  std::vector<QuantizedMultiModFeature> & features) const
966 {
967  const std::size_t width = mask.getWidth ();
968  const std::size_t height = mask.getHeight ();
969 
970  //cv::Mat maskImage(height, width, CV_8U, mask.mask);
971  //cv::erode(maskImage, maskImage
972 
973  // create distance maps for every quantization value
974  //cv::Mat distance_maps[8];
975  //for (int map_index = 0; map_index < 8; ++map_index)
976  //{
977  // distance_maps[map_index] = ::cv::Mat::zeros(height, width, CV_8U);
978  //}
979 
980  MaskMap mask_maps[8];
981  for (auto &mask_map : mask_maps)
982  mask_map.resize (width, height);
983 
984  unsigned char map[255];
985  memset(map, 0, 255);
986 
987  map[0x1<<0] = 0;
988  map[0x1<<1] = 1;
989  map[0x1<<2] = 2;
990  map[0x1<<3] = 3;
991  map[0x1<<4] = 4;
992  map[0x1<<5] = 5;
993  map[0x1<<6] = 6;
994  map[0x1<<7] = 7;
995 
996  QuantizedMap distance_map_indices (width, height);
997  //memset (distance_map_indices.data, 0, sizeof (distance_map_indices.data[0])*width*height);
998 
999  for (std::size_t row_index = 0; row_index < height; ++row_index)
1000  {
1001  for (std::size_t col_index = 0; col_index < width; ++col_index)
1002  {
1003  if (mask (col_index, row_index) != 0)
1004  {
1005  //const unsigned char quantized_value = quantized_surface_normals_ (row_index, col_index);
1006  const unsigned char quantized_value = filtered_quantized_surface_normals_ (col_index, row_index);
1007 
1008  if (quantized_value == 0)
1009  continue;
1010  const int dist_map_index = map[quantized_value];
1011 
1012  distance_map_indices (col_index, row_index) = static_cast<unsigned char> (dist_map_index);
1013  //distance_maps[dist_map_index].at<unsigned char>(row_index, col_index) = 255;
1014  mask_maps[dist_map_index] (col_index, row_index) = 255;
1015  }
1016  }
1017  }
1018 
1019  DistanceMap distance_maps[8];
1020  for (int map_index = 0; map_index < 8; ++map_index)
1021  computeDistanceMap (mask_maps[map_index], distance_maps[map_index]);
1022 
1023  DistanceMap mask_distance_maps;
1024  computeDistanceMap (mask, mask_distance_maps);
1025 
1026  std::list<Candidate> list1;
1027  std::list<Candidate> list2;
1028 
1029  float weights[8] = {0,0,0,0,0,0,0,0};
1030 
1031  const std::size_t off = 4;
1032  for (std::size_t row_index = off; row_index < height-off; ++row_index)
1033  {
1034  for (std::size_t col_index = off; col_index < width-off; ++col_index)
1035  {
1036  if (mask (col_index, row_index) != 0)
1037  {
1038  //const unsigned char quantized_value = quantized_surface_normals_ (row_index, col_index);
1039  const unsigned char quantized_value = filtered_quantized_surface_normals_ (col_index, row_index);
1040 
1041  //const float nx = surface_normals_ (col_index, row_index).normal_x;
1042  //const float ny = surface_normals_ (col_index, row_index).normal_y;
1043  //const float nz = surface_normals_ (col_index, row_index).normal_z;
1044 
1045  if (quantized_value != 0)// && !(std::isnan (nx) || std::isnan (ny) || std::isnan (nz)))
1046  {
1047  const int distance_map_index = map[quantized_value];
1048 
1049  //const float distance = distance_maps[distance_map_index].at<float> (row_index, col_index);
1050  const float distance = distance_maps[distance_map_index] (col_index, row_index);
1051  const float distance_to_border = mask_distance_maps (col_index, row_index);
1052 
1053  if (distance >= feature_distance_threshold_ && distance_to_border >= min_distance_to_border_)
1054  {
1055  Candidate candidate;
1056 
1057  candidate.distance = distance;
1058  candidate.x = col_index;
1059  candidate.y = row_index;
1060  candidate.bin_index = static_cast<unsigned char> (distance_map_index);
1061 
1062  list1.push_back (candidate);
1063 
1064  ++weights[distance_map_index];
1065  }
1066  }
1067  }
1068  }
1069  }
1070 
1071  for (typename std::list<Candidate>::iterator iter = list1.begin (); iter != list1.end (); ++iter)
1072  iter->distance *= 1.0f / weights[iter->bin_index];
1073 
1074  list1.sort ();
1075 
1076  if (variable_feature_nr_)
1077  {
1078  int distance = static_cast<int> (list1.size ());
1079  bool feature_selection_finished = false;
1080  while (!feature_selection_finished)
1081  {
1082  const int sqr_distance = distance*distance;
1083  for (typename std::list<Candidate>::iterator iter1 = list1.begin (); iter1 != list1.end (); ++iter1)
1084  {
1085  bool candidate_accepted = true;
1086  for (typename std::list<Candidate>::iterator iter2 = list2.begin (); iter2 != list2.end (); ++iter2)
1087  {
1088  const int dx = static_cast<int> (iter1->x) - static_cast<int> (iter2->x);
1089  const int dy = static_cast<int> (iter1->y) - static_cast<int> (iter2->y);
1090  const int tmp_distance = dx*dx + dy*dy;
1091 
1092  if (tmp_distance < sqr_distance)
1093  {
1094  candidate_accepted = false;
1095  break;
1096  }
1097  }
1098 
1099 
1100  float min_min_sqr_distance = std::numeric_limits<float>::max ();
1101  float max_min_sqr_distance = 0;
1102  for (typename std::list<Candidate>::iterator iter2 = list2.begin (); iter2 != list2.end (); ++iter2)
1103  {
1104  float min_sqr_distance = std::numeric_limits<float>::max ();
1105  for (typename std::list<Candidate>::iterator iter3 = list2.begin (); iter3 != list2.end (); ++iter3)
1106  {
1107  if (iter2 == iter3)
1108  continue;
1109 
1110  const float dx = static_cast<float> (iter2->x) - static_cast<float> (iter3->x);
1111  const float dy = static_cast<float> (iter2->y) - static_cast<float> (iter3->y);
1112 
1113  const float sqr_distance = dx*dx + dy*dy;
1114 
1115  if (sqr_distance < min_sqr_distance)
1116  {
1117  min_sqr_distance = sqr_distance;
1118  }
1119 
1120  //std::cerr << min_sqr_distance;
1121  }
1122  //std::cerr << std::endl;
1123 
1124  // check current feature
1125  {
1126  const float dx = static_cast<float> (iter2->x) - static_cast<float> (iter1->x);
1127  const float dy = static_cast<float> (iter2->y) - static_cast<float> (iter1->y);
1128 
1129  const float sqr_distance = dx*dx + dy*dy;
1130 
1131  if (sqr_distance < min_sqr_distance)
1132  {
1133  min_sqr_distance = sqr_distance;
1134  }
1135  }
1136 
1137  if (min_sqr_distance < min_min_sqr_distance)
1138  min_min_sqr_distance = min_sqr_distance;
1139  if (min_sqr_distance > max_min_sqr_distance)
1140  max_min_sqr_distance = min_sqr_distance;
1141 
1142  //std::cerr << min_sqr_distance << ", " << min_min_sqr_distance << ", " << max_min_sqr_distance << std::endl;
1143  }
1144 
1145  if (candidate_accepted)
1146  {
1147  //std::cerr << "feature_index: " << list2.size () << std::endl;
1148  //std::cerr << "min_min_sqr_distance: " << min_min_sqr_distance << std::endl;
1149  //std::cerr << "max_min_sqr_distance: " << max_min_sqr_distance << std::endl;
1150 
1151  if (min_min_sqr_distance < 50)
1152  {
1153  feature_selection_finished = true;
1154  break;
1155  }
1156 
1157  list2.push_back (*iter1);
1158  }
1159 
1160  //if (list2.size () == nr_features)
1161  //{
1162  // feature_selection_finished = true;
1163  // break;
1164  //}
1165  }
1166  --distance;
1167  }
1168  }
1169  else
1170  {
1171  if (list1.size () <= nr_features)
1172  {
1173  features.reserve (list1.size ());
1174  for (typename std::list<Candidate>::iterator iter = list1.begin (); iter != list1.end (); ++iter)
1175  {
1176  QuantizedMultiModFeature feature;
1177 
1178  feature.x = static_cast<int> (iter->x);
1179  feature.y = static_cast<int> (iter->y);
1180  feature.modality_index = modality_index;
1181  feature.quantized_value = filtered_quantized_surface_normals_ (iter->x, iter->y);
1182 
1183  features.push_back (feature);
1184  }
1185 
1186  return;
1187  }
1188 
1189  int distance = static_cast<int> (list1.size () / nr_features + 1); // ??? @todo:!:!:!:!:!:!
1190  while (list2.size () != nr_features)
1191  {
1192  const int sqr_distance = distance*distance;
1193  for (typename std::list<Candidate>::iterator iter1 = list1.begin (); iter1 != list1.end (); ++iter1)
1194  {
1195  bool candidate_accepted = true;
1196 
1197  for (typename std::list<Candidate>::iterator iter2 = list2.begin (); iter2 != list2.end (); ++iter2)
1198  {
1199  const int dx = static_cast<int> (iter1->x) - static_cast<int> (iter2->x);
1200  const int dy = static_cast<int> (iter1->y) - static_cast<int> (iter2->y);
1201  const int tmp_distance = dx*dx + dy*dy;
1202 
1203  if (tmp_distance < sqr_distance)
1204  {
1205  candidate_accepted = false;
1206  break;
1207  }
1208  }
1209 
1210  if (candidate_accepted)
1211  list2.push_back (*iter1);
1212 
1213  if (list2.size () == nr_features) break;
1214  }
1215  --distance;
1216  }
1217  }
1218 
1219  for (typename std::list<Candidate>::iterator iter2 = list2.begin (); iter2 != list2.end (); ++iter2)
1220  {
1221  QuantizedMultiModFeature feature;
1222 
1223  feature.x = static_cast<int> (iter2->x);
1224  feature.y = static_cast<int> (iter2->y);
1225  feature.modality_index = modality_index;
1226  feature.quantized_value = filtered_quantized_surface_normals_ (iter2->x, iter2->y);
1227 
1228  features.push_back (feature);
1229  }
1230 }
1231 
1232 //////////////////////////////////////////////////////////////////////////////////////////////
1233 template <typename PointInT> void
1235  const MaskMap & mask, const std::size_t, const std::size_t modality_index,
1236  std::vector<QuantizedMultiModFeature> & features) const
1237 {
1238  const std::size_t width = mask.getWidth ();
1239  const std::size_t height = mask.getHeight ();
1240 
1241  //cv::Mat maskImage(height, width, CV_8U, mask.mask);
1242  //cv::erode(maskImage, maskImage
1243 
1244  // create distance maps for every quantization value
1245  //cv::Mat distance_maps[8];
1246  //for (int map_index = 0; map_index < 8; ++map_index)
1247  //{
1248  // distance_maps[map_index] = ::cv::Mat::zeros(height, width, CV_8U);
1249  //}
1250 
1251  MaskMap mask_maps[8];
1252  for (auto &mask_map : mask_maps)
1253  mask_map.resize (width, height);
1254 
1255  unsigned char map[255];
1256  memset(map, 0, 255);
1257 
1258  map[0x1<<0] = 0;
1259  map[0x1<<1] = 1;
1260  map[0x1<<2] = 2;
1261  map[0x1<<3] = 3;
1262  map[0x1<<4] = 4;
1263  map[0x1<<5] = 5;
1264  map[0x1<<6] = 6;
1265  map[0x1<<7] = 7;
1266 
1267  QuantizedMap distance_map_indices (width, height);
1268  //memset (distance_map_indices.data, 0, sizeof (distance_map_indices.data[0])*width*height);
1269 
1270  for (std::size_t row_index = 0; row_index < height; ++row_index)
1271  {
1272  for (std::size_t col_index = 0; col_index < width; ++col_index)
1273  {
1274  if (mask (col_index, row_index) != 0)
1275  {
1276  //const unsigned char quantized_value = quantized_surface_normals_ (row_index, col_index);
1277  const unsigned char quantized_value = filtered_quantized_surface_normals_ (col_index, row_index);
1278 
1279  if (quantized_value == 0)
1280  continue;
1281  const int dist_map_index = map[quantized_value];
1282 
1283  distance_map_indices (col_index, row_index) = static_cast<unsigned char> (dist_map_index);
1284  //distance_maps[dist_map_index].at<unsigned char>(row_index, col_index) = 255;
1285  mask_maps[dist_map_index] (col_index, row_index) = 255;
1286  }
1287  }
1288  }
1289 
1290  DistanceMap distance_maps[8];
1291  for (int map_index = 0; map_index < 8; ++map_index)
1292  computeDistanceMap (mask_maps[map_index], distance_maps[map_index]);
1293 
1294  DistanceMap mask_distance_maps;
1295  computeDistanceMap (mask, mask_distance_maps);
1296 
1297  std::list<Candidate> list1;
1298  std::list<Candidate> list2;
1299 
1300  float weights[8] = {0,0,0,0,0,0,0,0};
1301 
1302  const std::size_t off = 4;
1303  for (std::size_t row_index = off; row_index < height-off; ++row_index)
1304  {
1305  for (std::size_t col_index = off; col_index < width-off; ++col_index)
1306  {
1307  if (mask (col_index, row_index) != 0)
1308  {
1309  //const unsigned char quantized_value = quantized_surface_normals_ (row_index, col_index);
1310  const unsigned char quantized_value = filtered_quantized_surface_normals_ (col_index, row_index);
1311 
1312  //const float nx = surface_normals_ (col_index, row_index).normal_x;
1313  //const float ny = surface_normals_ (col_index, row_index).normal_y;
1314  //const float nz = surface_normals_ (col_index, row_index).normal_z;
1315 
1316  if (quantized_value != 0)// && !(std::isnan (nx) || std::isnan (ny) || std::isnan (nz)))
1317  {
1318  const int distance_map_index = map[quantized_value];
1319 
1320  //const float distance = distance_maps[distance_map_index].at<float> (row_index, col_index);
1321  const float distance = distance_maps[distance_map_index] (col_index, row_index);
1322  const float distance_to_border = mask_distance_maps (col_index, row_index);
1323 
1324  if (distance >= feature_distance_threshold_ && distance_to_border >= min_distance_to_border_)
1325  {
1326  Candidate candidate;
1327 
1328  candidate.distance = distance;
1329  candidate.x = col_index;
1330  candidate.y = row_index;
1331  candidate.bin_index = static_cast<unsigned char> (distance_map_index);
1332 
1333  list1.push_back (candidate);
1334 
1335  ++weights[distance_map_index];
1336  }
1337  }
1338  }
1339  }
1340  }
1341 
1342  for (typename std::list<Candidate>::iterator iter = list1.begin (); iter != list1.end (); ++iter)
1343  iter->distance *= 1.0f / weights[iter->bin_index];
1344 
1345  list1.sort ();
1346 
1347  features.reserve (list1.size ());
1348  for (typename std::list<Candidate>::iterator iter = list1.begin (); iter != list1.end (); ++iter)
1349  {
1350  QuantizedMultiModFeature feature;
1351 
1352  feature.x = static_cast<int> (iter->x);
1353  feature.y = static_cast<int> (iter->y);
1354  feature.modality_index = modality_index;
1355  feature.quantized_value = filtered_quantized_surface_normals_ (iter->x, iter->y);
1356 
1357  features.push_back (feature);
1358  }
1359 }
1360 
1361 //////////////////////////////////////////////////////////////////////////////////////////////
1362 template <typename PointInT> void
1364 {
1365  const std::size_t width = input_->width;
1366  const std::size_t height = input_->height;
1367 
1368  quantized_surface_normals_.resize (width, height);
1369 
1370  for (std::size_t row_index = 0; row_index < height; ++row_index)
1371  {
1372  for (std::size_t col_index = 0; col_index < width; ++col_index)
1373  {
1374  const float normal_x = surface_normals_ (col_index, row_index).normal_x;
1375  const float normal_y = surface_normals_ (col_index, row_index).normal_y;
1376  const float normal_z = surface_normals_ (col_index, row_index).normal_z;
1377 
1378  if (std::isnan(normal_x) || std::isnan(normal_y) || std::isnan(normal_z) || normal_z > 0)
1379  {
1380  quantized_surface_normals_ (col_index, row_index) = 0;
1381  continue;
1382  }
1383 
1384  //quantized_surface_normals_.data[row_index*width+col_index] =
1385  // normal_lookup_(normal_x, normal_y, normal_z);
1386 
1387  float angle = 11.25f + std::atan2 (normal_y, normal_x)*180.0f/3.14f;
1388 
1389  if (angle < 0.0f) angle += 360.0f;
1390  if (angle >= 360.0f) angle -= 360.0f;
1391 
1392  int bin_index = static_cast<int> (angle*8.0f/360.0f);
1393 
1394  //quantized_surface_normals_.data[row_index*width+col_index] = 0x1 << bin_index;
1395  quantized_surface_normals_ (col_index, row_index) = static_cast<unsigned char> (bin_index);
1396  }
1397  }
1398 
1399  return;
1400 }
1401 
1402 //////////////////////////////////////////////////////////////////////////////////////////////
1403 template <typename PointInT> void
1405 {
1406  const int width = input_->width;
1407  const int height = input_->height;
1408 
1409  filtered_quantized_surface_normals_.resize (width, height);
1410 
1411  //for (int row_index = 2; row_index < height-2; ++row_index)
1412  //{
1413  // for (int col_index = 2; col_index < width-2; ++col_index)
1414  // {
1415  // std::list<unsigned char> values;
1416  // values.reserve (25);
1417 
1418  // unsigned char * dataPtr = quantized_surface_normals_.getData () + (row_index-2)*width+col_index-2;
1419  // values.push_back (dataPtr[0]);
1420  // values.push_back (dataPtr[1]);
1421  // values.push_back (dataPtr[2]);
1422  // values.push_back (dataPtr[3]);
1423  // values.push_back (dataPtr[4]);
1424  // dataPtr += width;
1425  // values.push_back (dataPtr[0]);
1426  // values.push_back (dataPtr[1]);
1427  // values.push_back (dataPtr[2]);
1428  // values.push_back (dataPtr[3]);
1429  // values.push_back (dataPtr[4]);
1430  // dataPtr += width;
1431  // values.push_back (dataPtr[0]);
1432  // values.push_back (dataPtr[1]);
1433  // values.push_back (dataPtr[2]);
1434  // values.push_back (dataPtr[3]);
1435  // values.push_back (dataPtr[4]);
1436  // dataPtr += width;
1437  // values.push_back (dataPtr[0]);
1438  // values.push_back (dataPtr[1]);
1439  // values.push_back (dataPtr[2]);
1440  // values.push_back (dataPtr[3]);
1441  // values.push_back (dataPtr[4]);
1442  // dataPtr += width;
1443  // values.push_back (dataPtr[0]);
1444  // values.push_back (dataPtr[1]);
1445  // values.push_back (dataPtr[2]);
1446  // values.push_back (dataPtr[3]);
1447  // values.push_back (dataPtr[4]);
1448 
1449  // values.sort ();
1450 
1451  // filtered_quantized_surface_normals_ (col_index, row_index) = values[12];
1452  // }
1453  //}
1454 
1455 
1456  //for (int row_index = 2; row_index < height-2; ++row_index)
1457  //{
1458  // for (int col_index = 2; col_index < width-2; ++col_index)
1459  // {
1460  // filtered_quantized_surface_normals_ (col_index, row_index) = static_cast<unsigned char> (0x1 << (quantized_surface_normals_ (col_index, row_index) - 1));
1461  // }
1462  //}
1463 
1464 
1465  // filter data
1466  for (int row_index = 2; row_index < height-2; ++row_index)
1467  {
1468  for (int col_index = 2; col_index < width-2; ++col_index)
1469  {
1470  unsigned char histogram[9] = {0,0,0,0,0,0,0,0,0};
1471 
1472  //{
1473  // unsigned char * dataPtr = quantized_surface_normals_.getData () + (row_index-1)*width+col_index-1;
1474  // ++histogram[dataPtr[0]];
1475  // ++histogram[dataPtr[1]];
1476  // ++histogram[dataPtr[2]];
1477  //}
1478  //{
1479  // unsigned char * dataPtr = quantized_surface_normals_.getData () + row_index*width+col_index-1;
1480  // ++histogram[dataPtr[0]];
1481  // ++histogram[dataPtr[1]];
1482  // ++histogram[dataPtr[2]];
1483  //}
1484  //{
1485  // unsigned char * dataPtr = quantized_surface_normals_.getData () + (row_index+1)*width+col_index-1;
1486  // ++histogram[dataPtr[0]];
1487  // ++histogram[dataPtr[1]];
1488  // ++histogram[dataPtr[2]];
1489  //}
1490 
1491  {
1492  unsigned char * dataPtr = quantized_surface_normals_.getData () + (row_index-2)*width+col_index-2;
1493  ++histogram[dataPtr[0]];
1494  ++histogram[dataPtr[1]];
1495  ++histogram[dataPtr[2]];
1496  ++histogram[dataPtr[3]];
1497  ++histogram[dataPtr[4]];
1498  }
1499  {
1500  unsigned char * dataPtr = quantized_surface_normals_.getData () + (row_index-1)*width+col_index-2;
1501  ++histogram[dataPtr[0]];
1502  ++histogram[dataPtr[1]];
1503  ++histogram[dataPtr[2]];
1504  ++histogram[dataPtr[3]];
1505  ++histogram[dataPtr[4]];
1506  }
1507  {
1508  unsigned char * dataPtr = quantized_surface_normals_.getData () + (row_index)*width+col_index-2;
1509  ++histogram[dataPtr[0]];
1510  ++histogram[dataPtr[1]];
1511  ++histogram[dataPtr[2]];
1512  ++histogram[dataPtr[3]];
1513  ++histogram[dataPtr[4]];
1514  }
1515  {
1516  unsigned char * dataPtr = quantized_surface_normals_.getData () + (row_index+1)*width+col_index-2;
1517  ++histogram[dataPtr[0]];
1518  ++histogram[dataPtr[1]];
1519  ++histogram[dataPtr[2]];
1520  ++histogram[dataPtr[3]];
1521  ++histogram[dataPtr[4]];
1522  }
1523  {
1524  unsigned char * dataPtr = quantized_surface_normals_.getData () + (row_index+2)*width+col_index-2;
1525  ++histogram[dataPtr[0]];
1526  ++histogram[dataPtr[1]];
1527  ++histogram[dataPtr[2]];
1528  ++histogram[dataPtr[3]];
1529  ++histogram[dataPtr[4]];
1530  }
1531 
1532 
1533  unsigned char max_hist_value = 0;
1534  int max_hist_index = -1;
1535 
1536  if (max_hist_value < histogram[1]) {max_hist_index = 0; max_hist_value = histogram[1];}
1537  if (max_hist_value < histogram[2]) {max_hist_index = 1; max_hist_value = histogram[2];}
1538  if (max_hist_value < histogram[3]) {max_hist_index = 2; max_hist_value = histogram[3];}
1539  if (max_hist_value < histogram[4]) {max_hist_index = 3; max_hist_value = histogram[4];}
1540  if (max_hist_value < histogram[5]) {max_hist_index = 4; max_hist_value = histogram[5];}
1541  if (max_hist_value < histogram[6]) {max_hist_index = 5; max_hist_value = histogram[6];}
1542  if (max_hist_value < histogram[7]) {max_hist_index = 6; max_hist_value = histogram[7];}
1543  if (max_hist_value < histogram[8]) {max_hist_index = 7; max_hist_value = histogram[8];}
1544 
1545  if (max_hist_index != -1 && max_hist_value >= 1)
1546  {
1547  filtered_quantized_surface_normals_ (col_index, row_index) = static_cast<unsigned char> (0x1 << max_hist_index);
1548  }
1549  else
1550  {
1551  filtered_quantized_surface_normals_ (col_index, row_index) = 0;
1552  }
1553 
1554  //filtered_quantized_color_gradients_.data[row_index*width+col_index] = quantized_color_gradients_.data[row_index*width+col_index];
1555  }
1556  }
1557 
1558 
1559 
1560  //cv::Mat data1(quantized_surface_normals_.height, quantized_surface_normals_.width, CV_8U, quantized_surface_normals_.data);
1561  //cv::Mat data2(filtered_quantized_surface_normals_.height, filtered_quantized_surface_normals_.width, CV_8U, filtered_quantized_surface_normals_.data);
1562 
1563  //cv::medianBlur(data1, data2, 3);
1564 
1565  //for (int row_index = 0; row_index < height; ++row_index)
1566  //{
1567  // for (int col_index = 0; col_index < width; ++col_index)
1568  // {
1569  // filtered_quantized_surface_normals_.data[row_index*width+col_index] = 0x1 << filtered_quantized_surface_normals_.data[row_index*width+col_index];
1570  // }
1571  //}
1572 }
1573 
1574 //////////////////////////////////////////////////////////////////////////////////////////////
1575 template <typename PointInT> void
1577 {
1578  const std::size_t width = input.getWidth ();
1579  const std::size_t height = input.getHeight ();
1580 
1581  output.resize (width, height);
1582 
1583  // compute distance map
1584  //float *distance_map = new float[input_->size ()];
1585  const unsigned char * mask_map = input.getData ();
1586  float * distance_map = output.getData ();
1587  for (std::size_t index = 0; index < width*height; ++index)
1588  {
1589  if (mask_map[index] == 0)
1590  distance_map[index] = 0.0f;
1591  else
1592  distance_map[index] = static_cast<float> (width + height);
1593  }
1594 
1595  // first pass
1596  float * previous_row = distance_map;
1597  float * current_row = previous_row + width;
1598  for (std::size_t ri = 1; ri < height; ++ri)
1599  {
1600  for (std::size_t ci = 1; ci < width; ++ci)
1601  {
1602  const float up_left = previous_row [ci - 1] + 1.4f; //distance_map[(ri-1)*input_->width + ci-1] + 1.4f;
1603  const float up = previous_row [ci] + 1.0f; //distance_map[(ri-1)*input_->width + ci] + 1.0f;
1604  const float up_right = previous_row [ci + 1] + 1.4f; //distance_map[(ri-1)*input_->width + ci+1] + 1.4f;
1605  const float left = current_row [ci - 1] + 1.0f; //distance_map[ri*input_->width + ci-1] + 1.0f;
1606  const float center = current_row [ci]; //distance_map[ri*input_->width + ci];
1607 
1608  const float min_value = std::min (std::min (up_left, up), std::min (left, up_right));
1609 
1610  if (min_value < center)
1611  current_row[ci] = min_value; //distance_map[ri * input_->width + ci] = min_value;
1612  }
1613  previous_row = current_row;
1614  current_row += width;
1615  }
1616 
1617  // second pass
1618  float * next_row = distance_map + width * (height - 1);
1619  current_row = next_row - width;
1620  for (int ri = static_cast<int> (height)-2; ri >= 0; --ri)
1621  {
1622  for (int ci = static_cast<int> (width)-2; ci >= 0; --ci)
1623  {
1624  const float lower_left = next_row [ci - 1] + 1.4f; //distance_map[(ri+1)*input_->width + ci-1] + 1.4f;
1625  const float lower = next_row [ci] + 1.0f; //distance_map[(ri+1)*input_->width + ci] + 1.0f;
1626  const float lower_right = next_row [ci + 1] + 1.4f; //distance_map[(ri+1)*input_->width + ci+1] + 1.4f;
1627  const float right = current_row [ci + 1] + 1.0f; //distance_map[ri*input_->width + ci+1] + 1.0f;
1628  const float center = current_row [ci]; //distance_map[ri*input_->width + ci];
1629 
1630  const float min_value = std::min (std::min (lower_left, lower), std::min (right, lower_right));
1631 
1632  if (min_value < center)
1633  current_row[ci] = min_value; //distance_map[ri*input_->width + ci] = min_value;
1634  }
1635  next_row = current_row;
1636  current_row -= width;
1637  }
1638 }
Represents a distance map obtained from a distance transformation.
Definition: distance_map.h:47
float * getData()
Returns a pointer to the beginning of map.
Definition: distance_map.h:70
void resize(const std::size_t width, const std::size_t height)
Resizes the map to the specified size.
Definition: distance_map.h:80
void compute(PointCloudOut &output)
Base method for feature estimation for all points given in <setInputCloud (), setIndices ()> using th...
Definition: feature.hpp:194
Surface normal estimation on dense data using a least-squares estimation based on a first-order Taylo...
void setNormalSmoothingSize(float normal_smoothing_size)
Set the normal smoothing size.
void setInputCloud(const typename PointCloudIn::ConstPtr &cloud) override
Provide a pointer to the input dataset (overwrites the PCLBase::setInputCloud method)
void setMaxDepthChangeFactor(float max_depth_change_factor)
The depth change threshold for computing object borders.
void setDepthDependentSmoothing(bool use_depth_dependent_smoothing)
Set whether to use depth depending smoothing or not.
std::size_t getWidth() const
Definition: mask_map.h:57
unsigned char * getData()
Definition: mask_map.h:69
void resize(std::size_t width, std::size_t height)
std::size_t getHeight() const
Definition: mask_map.h:63
PCL base class.
Definition: pcl_base.h:70
PointCloudConstPtr input_
The input point cloud dataset.
Definition: pcl_base.h:147
std::vector< PointT, Eigen::aligned_allocator< PointT > > VectorType
Definition: point_cloud.h:411
shared_ptr< const PointCloud< PointInT > > ConstPtr
Definition: point_cloud.h:414
Interface for a quantizable modality.
static void spreadQuantizedMap(const QuantizedMap &input_map, QuantizedMap &output_map, std::size_t spreading_size)
Modality based on surface normals.
void computeSurfaceNormals()
Computes the surface normals from the input cloud.
void computeAndQuantizeSurfaceNormals()
Computes and quantizes the surface normals.
void setSpreadingSize(const std::size_t spreading_size)
Sets the spreading size.
const pcl::PointCloud< pcl::Normal > & getSurfaceNormals() const
Returns the surface normals.
void computeAndQuantizeSurfaceNormals2()
Computes and quantizes the surface normals.
virtual void processInputData()
Processes the input data (smoothing, computing gradients, quantizing, filtering, spreading).
~SurfaceNormalModality() override
Destructor.
virtual void processInputDataFromFiltered()
Processes the input data assuming that everything up to filtering is already done/available (so only ...
QuantizedMap & getSpreadedQuantizedMap() override
Returns a reference to the internal spread quantized map.
void quantizeSurfaceNormals()
Quantizes the surface normals.
void setInputCloud(const typename PointCloudIn::ConstPtr &cloud) override
Provide a pointer to the input dataset (overwrites the PCLBase::setInputCloud method)
QuantizedMap & getQuantizedMap() override
Returns a reference to the internal quantized map.
void extractAllFeatures(const MaskMap &mask, std::size_t nr_features, std::size_t modality_index, std::vector< QuantizedMultiModFeature > &features) const override
Extracts all possible features from the modality within the specified mask.
void computeDistanceMap(const MaskMap &input, DistanceMap &output) const
Computes a distance map from the supplied input mask.
pcl::PointCloud< pcl::Normal > & getSurfaceNormals()
Returns the surface normals.
LINEMOD_OrientationMap & getOrientationMap()
Returns a reference to the orientation map.
void setVariableFeatureNr(const bool enabled)
Enables/disables the use of extracting a variable number of features.
void filterQuantizedSurfaceNormals()
Filters the quantized surface normals.
void extractFeatures(const MaskMap &mask, std::size_t nr_features, std::size_t modality_index, std::vector< QuantizedMultiModFeature > &features) const override
Extracts features from this modality within the specified mask.
Defines all the PCL implemented PointT point type structures.
float distance(const PointT &p1, const PointT &p2)
Definition: geometry.h:60
#define PCL_EXPORTS
Definition: pcl_macros.h:323
#define M_PI
Definition: pcl_macros.h:201
Map that stores orientations.
std::size_t getWidth() const
Returns the width of the modality data map.
std::size_t getHeight() const
Returns the height of the modality data map.
~LINEMOD_OrientationMap()=default
Destructor.
void resize(const std::size_t width, const std::size_t height, const float value)
Resizes the map to the specific width and height and initializes all new elements with the specified ...
A point structure representing normal coordinates and the surface curvature estimate.
A point structure representing Euclidean xyz coordinates.
Feature that defines a position and quantized value in a specific modality.
std::size_t modality_index
the index of the corresponding modality.
unsigned char quantized_value
the quantized value attached to the feature.
Look-up-table for fast surface normal quantization.
int size_y
The size of the LUT in y-direction.
void initializeLUT(const int range_x_arg, const int range_y_arg, const int range_z_arg)
Initializes the LUT.
int size_x
The size of the LUT in x-direction.
unsigned char operator()(const float x, const float y, const float z) const
Operator to access an element in the LUT.
int range_y
The range of the LUT in y-direction.
int offset_x
The offset in x-direction.
unsigned char * lut
The LUT data.
int offset_z
The offset in z-direction.
int range_z
The range of the LUT in z-direction.
int size_z
The size of the LUT in z-direction.
int range_x
The range of the LUT in x-direction.
int offset_y
The offset in y-direction.
Candidate for a feature (used in feature extraction methods).
float distance
Distance to the next different quantized value.
std::size_t x
x-position of the feature.
std::size_t y
y-position of the feature.
bool operator<(const Candidate &rhs) const
Compares two candidates based on their distance to the next different quantized value.