Point Cloud Library (PCL)  1.12.1-dev
color_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/recognition/point_types.h>
47 
48 #include <algorithm>
49 #include <cstddef>
50 #include <list>
51 #include <vector>
52 
53 namespace pcl
54 {
55 
56  // --------------------------------------------------------------------------
57 
58  template <typename PointInT>
60  : public QuantizableModality, public PCLBase<PointInT>
61  {
62  protected:
64 
65  struct Candidate
66  {
67  float distance;
68 
69  unsigned char bin_index;
70 
71  std::size_t x;
72  std::size_t y;
73 
74  bool
75  operator< (const Candidate & rhs)
76  {
77  return (distance > rhs.distance);
78  }
79  };
80 
81  public:
83 
84  ColorModality ();
85 
86  virtual ~ColorModality ();
87 
88  inline QuantizedMap &
90  {
91  return (filtered_quantized_colors_);
92  }
93 
94  inline QuantizedMap &
96  {
97  return (spreaded_filtered_quantized_colors_);
98  }
99 
100  void
101  extractFeatures (const MaskMap & mask, std::size_t nr_features, std::size_t modalityIndex,
102  std::vector<QuantizedMultiModFeature> & features) const;
103 
104  /** \brief Provide a pointer to the input dataset (overwrites the PCLBase::setInputCloud method)
105  * \param cloud the const boost shared pointer to a PointCloud message
106  */
107  virtual void
108  setInputCloud (const typename PointCloudIn::ConstPtr & cloud)
109  {
110  input_ = cloud;
111  }
112 
113  virtual void
114  processInputData ();
115 
116  protected:
117 
118  void
119  quantizeColors ();
120 
121  void
123 
124  static inline int
125  quantizeColorOnRGBExtrema (const float r,
126  const float g,
127  const float b);
128 
129  void
130  computeDistanceMap (const MaskMap & input, DistanceMap & output) const;
131 
132  private:
133  float feature_distance_threshold_;
134 
135  pcl::QuantizedMap quantized_colors_;
136  pcl::QuantizedMap filtered_quantized_colors_;
137  pcl::QuantizedMap spreaded_filtered_quantized_colors_;
138 
139  };
140 
141 }
142 
143 //////////////////////////////////////////////////////////////////////////////////////////////
144 template <typename PointInT>
146  : feature_distance_threshold_ (1.0f), quantized_colors_ (), filtered_quantized_colors_ (), spreaded_filtered_quantized_colors_ ()
147 {
148 }
149 
150 //////////////////////////////////////////////////////////////////////////////////////////////
151 template <typename PointInT>
153 {
154 }
155 
156 //////////////////////////////////////////////////////////////////////////////////////////////
157 template <typename PointInT>
158 void
160 {
161  // quantize gradients
162  quantizeColors ();
163 
164  // filter quantized gradients to get only dominants one + thresholding
165  filterQuantizedColors ();
166 
167  // spread filtered quantized gradients
168  //spreadFilteredQunatizedColorGradients ();
169  const int spreading_size = 8;
170  pcl::QuantizedMap::spreadQuantizedMap (filtered_quantized_colors_,
171  spreaded_filtered_quantized_colors_, spreading_size);
172 }
173 
174 //////////////////////////////////////////////////////////////////////////////////////////////
175 template <typename PointInT>
177  const std::size_t nr_features,
178  const std::size_t modality_index,
179  std::vector<QuantizedMultiModFeature> & features) const
180 {
181  const std::size_t width = mask.getWidth ();
182  const std::size_t height = mask.getHeight ();
183 
184  MaskMap mask_maps[8];
185  for (std::size_t map_index = 0; map_index < 8; ++map_index)
186  mask_maps[map_index].resize (width, height);
187 
188  unsigned char map[255]{};
189 
190  map[0x1<<0] = 0;
191  map[0x1<<1] = 1;
192  map[0x1<<2] = 2;
193  map[0x1<<3] = 3;
194  map[0x1<<4] = 4;
195  map[0x1<<5] = 5;
196  map[0x1<<6] = 6;
197  map[0x1<<7] = 7;
198 
199  QuantizedMap distance_map_indices (width, height);
200  //memset (distance_map_indices.data, 0, sizeof (distance_map_indices.data[0])*width*height);
201 
202  for (std::size_t row_index = 0; row_index < height; ++row_index)
203  {
204  for (std::size_t col_index = 0; col_index < width; ++col_index)
205  {
206  if (mask (col_index, row_index) != 0)
207  {
208  //const unsigned char quantized_value = quantized_surface_normals_ (row_index, col_index);
209  const unsigned char quantized_value = filtered_quantized_colors_ (col_index, row_index);
210 
211  if (quantized_value == 0)
212  continue;
213  const int dist_map_index = map[quantized_value];
214 
215  distance_map_indices (col_index, row_index) = dist_map_index;
216  //distance_maps[dist_map_index].at<unsigned char>(row_index, col_index) = 255;
217  mask_maps[dist_map_index] (col_index, row_index) = 255;
218  }
219  }
220  }
221 
222  DistanceMap distance_maps[8];
223  for (int map_index = 0; map_index < 8; ++map_index)
224  computeDistanceMap (mask_maps[map_index], distance_maps[map_index]);
225 
226  std::list<Candidate> list1;
227  std::list<Candidate> list2;
228 
229  float weights[8] = {0,0,0,0,0,0,0,0};
230 
231  const std::size_t off = 4;
232  for (std::size_t row_index = off; row_index < height-off; ++row_index)
233  {
234  for (std::size_t col_index = off; col_index < width-off; ++col_index)
235  {
236  if (mask (col_index, row_index) != 0)
237  {
238  //const unsigned char quantized_value = quantized_surface_normals_ (row_index, col_index);
239  const unsigned char quantized_value = filtered_quantized_colors_ (col_index, row_index);
240 
241  //const float nx = surface_normals_ (col_index, row_index).normal_x;
242  //const float ny = surface_normals_ (col_index, row_index).normal_y;
243  //const float nz = surface_normals_ (col_index, row_index).normal_z;
244 
245  if (quantized_value != 0)
246  {
247  const int distance_map_index = map[quantized_value];
248 
249  //const float distance = distance_maps[distance_map_index].at<float> (row_index, col_index);
250  const float distance = distance_maps[distance_map_index] (col_index, row_index);
251 
252  if (distance >= feature_distance_threshold_)
253  {
254  Candidate candidate;
255 
256  candidate.distance = distance;
257  candidate.x = col_index;
258  candidate.y = row_index;
259  candidate.bin_index = distance_map_index;
260 
261  list1.push_back (candidate);
262 
263  ++weights[distance_map_index];
264  }
265  }
266  }
267  }
268  }
269 
270  for (typename std::list<Candidate>::iterator iter = list1.begin (); iter != list1.end (); ++iter)
271  iter->distance *= 1.0f / weights[iter->bin_index];
272 
273  list1.sort ();
274 
275  if (list1.size () <= nr_features)
276  {
277  features.reserve (list1.size ());
278  for (typename std::list<Candidate>::iterator iter = list1.begin (); iter != list1.end (); ++iter)
279  {
280  QuantizedMultiModFeature feature;
281 
282  feature.x = static_cast<int> (iter->x);
283  feature.y = static_cast<int> (iter->y);
284  feature.modality_index = modality_index;
285  feature.quantized_value = filtered_quantized_colors_ (iter->x, iter->y);
286 
287  features.push_back (feature);
288  }
289 
290  return;
291  }
292 
293  int distance = static_cast<int> (list1.size () / nr_features + 1); // ??? @todo:!:!:!:!:!:!
294  while (list2.size () != nr_features)
295  {
296  const int sqr_distance = distance*distance;
297  for (typename std::list<Candidate>::iterator iter1 = list1.begin (); iter1 != list1.end (); ++iter1)
298  {
299  bool candidate_accepted = true;
300 
301  for (typename std::list<Candidate>::iterator iter2 = list2.begin (); iter2 != list2.end (); ++iter2)
302  {
303  const int dx = static_cast<int> (iter1->x) - static_cast<int> (iter2->x);
304  const int dy = static_cast<int> (iter1->y) - static_cast<int> (iter2->y);
305  const int tmp_distance = dx*dx + dy*dy;
306 
307  if (tmp_distance < sqr_distance)
308  {
309  candidate_accepted = false;
310  break;
311  }
312  }
313 
314  if (candidate_accepted)
315  list2.push_back (*iter1);
316 
317  if (list2.size () == nr_features) break;
318  }
319  --distance;
320  }
321 
322  for (typename std::list<Candidate>::iterator iter2 = list2.begin (); iter2 != list2.end (); ++iter2)
323  {
324  QuantizedMultiModFeature feature;
325 
326  feature.x = static_cast<int> (iter2->x);
327  feature.y = static_cast<int> (iter2->y);
328  feature.modality_index = modality_index;
329  feature.quantized_value = filtered_quantized_colors_ (iter2->x, iter2->y);
330 
331  features.push_back (feature);
332  }
333 }
334 
335 //////////////////////////////////////////////////////////////////////////////////////////////
336 template <typename PointInT>
337 void
339 {
340  const std::size_t width = input_->width;
341  const std::size_t height = input_->height;
342 
343  quantized_colors_.resize (width, height);
344 
345  for (std::size_t row_index = 0; row_index < height; ++row_index)
346  {
347  for (std::size_t col_index = 0; col_index < width; ++col_index)
348  {
349  const float r = static_cast<float> ((*input_) (col_index, row_index).r);
350  const float g = static_cast<float> ((*input_) (col_index, row_index).g);
351  const float b = static_cast<float> ((*input_) (col_index, row_index).b);
352 
353  quantized_colors_ (col_index, row_index) = quantizeColorOnRGBExtrema (r, g, b);
354  }
355  }
356 }
357 
358 //////////////////////////////////////////////////////////////////////////////////////////////
359 template <typename PointInT>
360 void
362 {
363  const std::size_t width = input_->width;
364  const std::size_t height = input_->height;
365 
366  filtered_quantized_colors_.resize (width, height);
367 
368  // filter data
369  for (std::size_t row_index = 1; row_index < height-1; ++row_index)
370  {
371  for (std::size_t col_index = 1; col_index < width-1; ++col_index)
372  {
373  unsigned char histogram[8] = {0,0,0,0,0,0,0,0};
374 
375  {
376  const unsigned char * data_ptr = quantized_colors_.getData () + (row_index-1)*width+col_index-1;
377  assert (0 <= data_ptr[0] && data_ptr[0] < 9 &&
378  0 <= data_ptr[1] && data_ptr[1] < 9 &&
379  0 <= data_ptr[2] && data_ptr[2] < 9);
380  ++histogram[data_ptr[0]];
381  ++histogram[data_ptr[1]];
382  ++histogram[data_ptr[2]];
383  }
384  {
385  const unsigned char * data_ptr = quantized_colors_.getData () + row_index*width+col_index-1;
386  assert (0 <= data_ptr[0] && data_ptr[0] < 9 &&
387  0 <= data_ptr[1] && data_ptr[1] < 9 &&
388  0 <= data_ptr[2] && data_ptr[2] < 9);
389  ++histogram[data_ptr[0]];
390  ++histogram[data_ptr[1]];
391  ++histogram[data_ptr[2]];
392  }
393  {
394  const unsigned char * data_ptr = quantized_colors_.getData () + (row_index+1)*width+col_index-1;
395  assert (0 <= data_ptr[0] && data_ptr[0] < 9 &&
396  0 <= data_ptr[1] && data_ptr[1] < 9 &&
397  0 <= data_ptr[2] && data_ptr[2] < 9);
398  ++histogram[data_ptr[0]];
399  ++histogram[data_ptr[1]];
400  ++histogram[data_ptr[2]];
401  }
402 
403  unsigned char max_hist_value = 0;
404  int max_hist_index = -1;
405 
406  // for (int i = 0; i < 8; ++i)
407  // {
408  // if (max_hist_value < histogram[i+1])
409  // {
410  // max_hist_index = i;
411  // max_hist_value = histogram[i+1]
412  // }
413  // }
414  // Unrolled for performance optimization:
415  if (max_hist_value < histogram[0]) {max_hist_index = 0; max_hist_value = histogram[0];}
416  if (max_hist_value < histogram[1]) {max_hist_index = 1; max_hist_value = histogram[1];}
417  if (max_hist_value < histogram[2]) {max_hist_index = 2; max_hist_value = histogram[2];}
418  if (max_hist_value < histogram[3]) {max_hist_index = 3; max_hist_value = histogram[3];}
419  if (max_hist_value < histogram[4]) {max_hist_index = 4; max_hist_value = histogram[4];}
420  if (max_hist_value < histogram[5]) {max_hist_index = 5; max_hist_value = histogram[5];}
421  if (max_hist_value < histogram[6]) {max_hist_index = 6; max_hist_value = histogram[6];}
422  if (max_hist_value < histogram[7]) {max_hist_index = 7; max_hist_value = histogram[7];}
423 
424  //if (max_hist_index != -1 && max_hist_value >= 5)
425  filtered_quantized_colors_ (col_index, row_index) = 0x1 << max_hist_index;
426  //else
427  // filtered_quantized_color_gradients_ (col_index, row_index) = 0;
428 
429  }
430  }
431 }
432 
433 //////////////////////////////////////////////////////////////////////////////////////////////
434 template <typename PointInT>
435 int
437  const float g,
438  const float b)
439 {
440  const float r_inv = 255.0f-r;
441  const float g_inv = 255.0f-g;
442  const float b_inv = 255.0f-b;
443 
444  const float dist_0 = (r*r + g*g + b*b)*2.0f;
445  const float dist_1 = r*r + g*g + b_inv*b_inv;
446  const float dist_2 = r*r + g_inv*g_inv+ b*b;
447  const float dist_3 = r*r + g_inv*g_inv + b_inv*b_inv;
448  const float dist_4 = r_inv*r_inv + g*g + b*b;
449  const float dist_5 = r_inv*r_inv + g*g + b_inv*b_inv;
450  const float dist_6 = r_inv*r_inv + g_inv*g_inv+ b*b;
451  const float dist_7 = (r_inv*r_inv + g_inv*g_inv + b_inv*b_inv)*1.5f;
452 
453  const float min_dist = std::min (std::min (std::min (dist_0, dist_1), std::min (dist_2, dist_3)), std::min (std::min (dist_4, dist_5), std::min (dist_6, dist_7)));
454 
455  if (min_dist == dist_0)
456  {
457  return 0;
458  }
459  if (min_dist == dist_1)
460  {
461  return 1;
462  }
463  if (min_dist == dist_2)
464  {
465  return 2;
466  }
467  if (min_dist == dist_3)
468  {
469  return 3;
470  }
471  if (min_dist == dist_4)
472  {
473  return 4;
474  }
475  if (min_dist == dist_5)
476  {
477  return 5;
478  }
479  if (min_dist == dist_6)
480  {
481  return 6;
482  }
483  return 7;
484 }
485 
486 //////////////////////////////////////////////////////////////////////////////////////////////
487 template <typename PointInT> void
489  DistanceMap & output) const
490 {
491  const std::size_t width = input.getWidth ();
492  const std::size_t height = input.getHeight ();
493 
494  output.resize (width, height);
495 
496  // compute distance map
497  //float *distance_map = new float[input_->size ()];
498  const unsigned char * mask_map = input.getData ();
499  float * distance_map = output.getData ();
500  for (std::size_t index = 0; index < width*height; ++index)
501  {
502  if (mask_map[index] == 0)
503  distance_map[index] = 0.0f;
504  else
505  distance_map[index] = static_cast<float> (width + height);
506  }
507 
508  // first pass
509  float * previous_row = distance_map;
510  float * current_row = previous_row + width;
511  for (std::size_t ri = 1; ri < height; ++ri)
512  {
513  for (std::size_t ci = 1; ci < width; ++ci)
514  {
515  const float up_left = previous_row [ci - 1] + 1.4f; //distance_map[(ri-1)*input_->width + ci-1] + 1.4f;
516  const float up = previous_row [ci] + 1.0f; //distance_map[(ri-1)*input_->width + ci] + 1.0f;
517  const float up_right = previous_row [ci + 1] + 1.4f; //distance_map[(ri-1)*input_->width + ci+1] + 1.4f;
518  const float left = current_row [ci - 1] + 1.0f; //distance_map[ri*input_->width + ci-1] + 1.0f;
519  const float center = current_row [ci]; //distance_map[ri*input_->width + ci];
520 
521  const float min_value = std::min (std::min (up_left, up), std::min (left, up_right));
522 
523  if (min_value < center)
524  current_row[ci] = min_value; //distance_map[ri * input_->width + ci] = min_value;
525  }
526  previous_row = current_row;
527  current_row += width;
528  }
529 
530  // second pass
531  float * next_row = distance_map + width * (height - 1);
532  current_row = next_row - width;
533  for (int ri = static_cast<int> (height)-2; ri >= 0; --ri)
534  {
535  for (int ci = static_cast<int> (width)-2; ci >= 0; --ci)
536  {
537  const float lower_left = next_row [ci - 1] + 1.4f; //distance_map[(ri+1)*input_->width + ci-1] + 1.4f;
538  const float lower = next_row [ci] + 1.0f; //distance_map[(ri+1)*input_->width + ci] + 1.0f;
539  const float lower_right = next_row [ci + 1] + 1.4f; //distance_map[(ri+1)*input_->width + ci+1] + 1.4f;
540  const float right = current_row [ci + 1] + 1.0f; //distance_map[ri*input_->width + ci+1] + 1.0f;
541  const float center = current_row [ci]; //distance_map[ri*input_->width + ci];
542 
543  const float min_value = std::min (std::min (lower_left, lower), std::min (right, lower_right));
544 
545  if (min_value < center)
546  current_row[ci] = min_value; //distance_map[ri*input_->width + ci] = min_value;
547  }
548  next_row = current_row;
549  current_row -= width;
550  }
551 }
void computeDistanceMap(const MaskMap &input, DistanceMap &output) const
virtual void setInputCloud(const typename PointCloudIn::ConstPtr &cloud)
Provide a pointer to the input dataset (overwrites the PCLBase::setInputCloud method)
QuantizedMap & getSpreadedQuantizedMap()
Returns a reference to the internally computed spread quantized map.
virtual ~ColorModality()
static int quantizeColorOnRGBExtrema(const float r, const float g, const float b)
QuantizedMap & getQuantizedMap()
Returns a reference to the internally computed quantized map.
virtual void processInputData()
void extractFeatures(const MaskMap &mask, std::size_t nr_features, std::size_t modalityIndex, std::vector< QuantizedMultiModFeature > &features) const
Extracts features from this modality within the specified mask.
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
std::size_t getWidth() const
Definition: mask_map.h:57
unsigned char * getData()
Definition: mask_map.h:69
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
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)
Defines all the PCL implemented PointT point type structures.
float distance(const PointT &p1, const PointT &p2)
Definition: geometry.h:60
bool operator<(const Candidate &rhs)
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.