Point Cloud Library (PCL)  1.9.1-dev
kernel.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, 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 the copyright holder(s) 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 #ifndef PCL_2D_KERNEL_IMPL_HPP
39 #define PCL_2D_KERNEL_IMPL_HPP
40 
41 //////////////////////////////////////////////////////////////////////////////
42 template <typename PointT>
43 void
45 {
46  switch (kernel_type_) {
47  case SOBEL_X:
48  sobelKernelX(kernel);
49  break;
50  case SOBEL_Y:
51  sobelKernelY(kernel);
52  break;
53  case PREWITT_X:
54  prewittKernelX(kernel);
55  break;
56  case PREWITT_Y:
57  prewittKernelY(kernel);
58  break;
59  case ROBERTS_X:
60  robertsKernelX(kernel);
61  break;
62  case ROBERTS_Y:
63  robertsKernelY(kernel);
64  break;
65  case LOG:
66  loGKernel(kernel);
67  break;
68  case DERIVATIVE_CENTRAL_X:
69  derivativeXCentralKernel(kernel);
70  break;
71  case DERIVATIVE_FORWARD_X:
72  derivativeXForwardKernel(kernel);
73  break;
74  case DERIVATIVE_BACKWARD_X:
75  derivativeXBackwardKernel(kernel);
76  break;
77  case DERIVATIVE_CENTRAL_Y:
78  derivativeYCentralKernel(kernel);
79  break;
80  case DERIVATIVE_FORWARD_Y:
81  derivativeYForwardKernel(kernel);
82  break;
83  case DERIVATIVE_BACKWARD_Y:
84  derivativeYBackwardKernel(kernel);
85  break;
86  case GAUSSIAN:
87  gaussianKernel(kernel);
88  break;
89  }
90 }
91 
92 //////////////////////////////////////////////////////////////////////////////
93 template <typename PointT>
94 void
96 {
97  float sum = 0;
98  kernel.resize(kernel_size_ * kernel_size_);
99  kernel.height = kernel_size_;
100  kernel.width = kernel_size_;
101 
102  double sigma_sqr = 2 * sigma_ * sigma_;
103 
104  for (int i = 0; i < kernel_size_; i++) {
105  for (int j = 0; j < kernel_size_; j++) {
106  int iks = (i - kernel_size_ / 2);
107  int jks = (j - kernel_size_ / 2);
108  kernel(j, i).intensity =
109  std::exp(float(-double(iks * iks + jks * jks) / sigma_sqr));
110  sum += float(kernel(j, i).intensity);
111  }
112  }
113 
114  // Normalizing the kernel
115  for (size_t i = 0; i < kernel.size(); ++i)
116  kernel[i].intensity /= sum;
117 }
118 
119 //////////////////////////////////////////////////////////////////////////////
120 template <typename PointT>
121 void
123 {
124  float sum = 0;
125  float temp = 0;
126  kernel.resize(kernel_size_ * kernel_size_);
127  kernel.height = kernel_size_;
128  kernel.width = kernel_size_;
129 
130  double sigma_sqr = 2 * sigma_ * sigma_;
131 
132  for (int i = 0; i < kernel_size_; i++) {
133  for (int j = 0; j < kernel_size_; j++) {
134  int iks = (i - kernel_size_ / 2);
135  int jks = (j - kernel_size_ / 2);
136  temp = float(double(iks * iks + jks * jks) / sigma_sqr);
137  kernel(j, i).intensity = (1.0f - temp) * std::exp(-temp);
138  sum += kernel(j, i).intensity;
139  }
140  }
141 
142  // Normalizing the kernel
143  for (size_t i = 0; i < kernel.size(); ++i)
144  kernel[i].intensity /= sum;
145 }
146 
147 //////////////////////////////////////////////////////////////////////////////
148 template <typename PointT>
149 void
151 {
152  kernel.resize(9);
153  kernel.height = 3;
154  kernel.width = 3;
155  kernel(0, 0).intensity = -1;
156  kernel(1, 0).intensity = 0;
157  kernel(2, 0).intensity = 1;
158  kernel(0, 1).intensity = -2;
159  kernel(1, 1).intensity = 0;
160  kernel(2, 1).intensity = 2;
161  kernel(0, 2).intensity = -1;
162  kernel(1, 2).intensity = 0;
163  kernel(2, 2).intensity = 1;
164 }
165 
166 //////////////////////////////////////////////////////////////////////////////
167 template <typename PointT>
168 void
170 {
171  kernel.resize(9);
172  kernel.height = 3;
173  kernel.width = 3;
174  kernel(0, 0).intensity = -1;
175  kernel(1, 0).intensity = 0;
176  kernel(2, 0).intensity = 1;
177  kernel(0, 1).intensity = -1;
178  kernel(1, 1).intensity = 0;
179  kernel(2, 1).intensity = 1;
180  kernel(0, 2).intensity = -1;
181  kernel(1, 2).intensity = 0;
182  kernel(2, 2).intensity = 1;
183 }
184 
185 //////////////////////////////////////////////////////////////////////////////
186 template <typename PointT>
187 void
189 {
190  kernel.resize(4);
191  kernel.height = 2;
192  kernel.width = 2;
193  kernel(0, 0).intensity = 1;
194  kernel(1, 0).intensity = 0;
195  kernel(0, 1).intensity = 0;
196  kernel(1, 1).intensity = -1;
197 }
198 
199 //////////////////////////////////////////////////////////////////////////////
200 template <typename PointT>
201 void
203 {
204  kernel.resize(9);
205  kernel.height = 3;
206  kernel.width = 3;
207  kernel(0, 0).intensity = -1;
208  kernel(1, 0).intensity = -2;
209  kernel(2, 0).intensity = -1;
210  kernel(0, 1).intensity = 0;
211  kernel(1, 1).intensity = 0;
212  kernel(2, 1).intensity = 0;
213  kernel(0, 2).intensity = 1;
214  kernel(1, 2).intensity = 2;
215  kernel(2, 2).intensity = 1;
216 }
217 
218 //////////////////////////////////////////////////////////////////////////////
219 template <typename PointT>
220 void
222 {
223  kernel.resize(9);
224  kernel.height = 3;
225  kernel.width = 3;
226  kernel(0, 0).intensity = 1;
227  kernel(1, 0).intensity = 1;
228  kernel(2, 0).intensity = 1;
229  kernel(0, 1).intensity = 0;
230  kernel(1, 1).intensity = 0;
231  kernel(2, 1).intensity = 0;
232  kernel(0, 2).intensity = -1;
233  kernel(1, 2).intensity = -1;
234  kernel(2, 2).intensity = -1;
235 }
236 
237 template <typename PointT>
238 void
240 {
241  kernel.resize(4);
242  kernel.height = 2;
243  kernel.width = 2;
244  kernel(0, 0).intensity = 0;
245  kernel(1, 0).intensity = 1;
246  kernel(0, 1).intensity = -1;
247  kernel(1, 1).intensity = 0;
248 }
249 
250 //////////////////////////////////////////////////////////////////////////////
251 template <typename PointT>
252 void
254 {
255  kernel.resize(3);
256  kernel.height = 1;
257  kernel.width = 3;
258  kernel(0, 0).intensity = -1;
259  kernel(1, 0).intensity = 0;
260  kernel(2, 0).intensity = 1;
261 }
262 
263 //////////////////////////////////////////////////////////////////////////////
264 template <typename PointT>
265 void
267 {
268  kernel.resize(3);
269  kernel.height = 1;
270  kernel.width = 3;
271  kernel(0, 0).intensity = 0;
272  kernel(1, 0).intensity = -1;
273  kernel(2, 0).intensity = 1;
274 }
275 
276 //////////////////////////////////////////////////////////////////////////////
277 template <typename PointT>
278 void
280 {
281  kernel.resize(3);
282  kernel.height = 1;
283  kernel.width = 3;
284  kernel(0, 0).intensity = -1;
285  kernel(1, 0).intensity = 1;
286  kernel(2, 0).intensity = 0;
287 }
288 
289 //////////////////////////////////////////////////////////////////////////////
290 template <typename PointT>
291 void
293 {
294  kernel.resize(3);
295  kernel.height = 3;
296  kernel.width = 1;
297  kernel(0, 0).intensity = -1;
298  kernel(0, 1).intensity = 0;
299  kernel(0, 2).intensity = 1;
300 }
301 
302 //////////////////////////////////////////////////////////////////////////////
303 template <typename PointT>
304 void
306 {
307  kernel.resize(3);
308  kernel.height = 3;
309  kernel.width = 1;
310  kernel(0, 0).intensity = 0;
311  kernel(0, 1).intensity = -1;
312  kernel(0, 2).intensity = 1;
313 }
314 
315 //////////////////////////////////////////////////////////////////////////////
316 template <typename PointT>
317 void
319 {
320  kernel.resize(3);
321  kernel.height = 3;
322  kernel.width = 1;
323  kernel(0, 0).intensity = -1;
324  kernel(0, 1).intensity = 1;
325  kernel(0, 2).intensity = 0;
326 }
327 
328 //////////////////////////////////////////////////////////////////////////////
329 //////////////////////////////////////////////////////////////////////////////
330 template <typename PointT>
331 void
332 pcl::kernel<PointT>::setKernelType(KERNEL_ENUM kernel_type)
333 {
334  kernel_type_ = kernel_type;
335 }
336 
337 //////////////////////////////////////////////////////////////////////////////
338 template <typename PointT>
339 void
341 {
342  kernel_size_ = kernel_size;
343 }
344 
345 //////////////////////////////////////////////////////////////////////////////
346 template <typename PointT>
347 void
349 {
350  sigma_ = kernel_sigma;
351 }
352 
353 #endif
void setKernelType(KERNEL_ENUM kernel_type)
Definition: kernel.hpp:332
void prewittKernelY(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:221
void prewittKernelX(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:169
void robertsKernelX(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:188
void derivativeYForwardKernel(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:305
void sobelKernelX(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:150
uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:428
void derivativeYBackwardKernel(PointCloud< PointT > &kernel)
Definition: kernel.hpp:318
uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:426
void sobelKernelY(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:202
void loGKernel(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:122
void setKernelSigma(float kernel_sigma)
Definition: kernel.hpp:348
PointCloud represents the base class in PCL for storing collections of 3D points. ...
void fetchKernel(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:44
void gaussianKernel(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:95
void derivativeXForwardKernel(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:266
void derivativeYCentralKernel(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:292
void resize(size_t n)
Resize the cloud.
Definition: point_cloud.h:468
void derivativeXCentralKernel(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:253
void derivativeXBackwardKernel(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:279
void robertsKernelY(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:239
void setKernelSize(int kernel_size)
Definition: kernel.hpp:340
size_t size() const
Definition: point_cloud.h:461