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