Point Cloud Library (PCL)  1.12.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 =
110  std::exp(float(-double(iks * iks + jks * jks) / sigma_sqr));
111  sum += float(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 = float(double(iks * iks + jks * jks) / sigma_sqr);
136  kernel(j, i).intensity = (1.0f - temp) * std::exp(-temp);
137  sum += kernel(j, i).intensity;
138  }
139  }
140 
141  // Normalizing the kernel
142  for (std::size_t i = 0; i < kernel.size(); ++i)
143  kernel[i].intensity /= sum;
144 }
145 
146 template <typename PointT>
147 void
149 {
150  kernel.resize(9);
151  kernel.height = 3;
152  kernel.width = 3;
153  kernel(0, 0).intensity = -1;
154  kernel(1, 0).intensity = 0;
155  kernel(2, 0).intensity = 1;
156  kernel(0, 1).intensity = -2;
157  kernel(1, 1).intensity = 0;
158  kernel(2, 1).intensity = 2;
159  kernel(0, 2).intensity = -1;
160  kernel(1, 2).intensity = 0;
161  kernel(2, 2).intensity = 1;
162 }
163 
164 template <typename PointT>
165 void
167 {
168  kernel.resize(9);
169  kernel.height = 3;
170  kernel.width = 3;
171  kernel(0, 0).intensity = -1;
172  kernel(1, 0).intensity = 0;
173  kernel(2, 0).intensity = 1;
174  kernel(0, 1).intensity = -1;
175  kernel(1, 1).intensity = 0;
176  kernel(2, 1).intensity = 1;
177  kernel(0, 2).intensity = -1;
178  kernel(1, 2).intensity = 0;
179  kernel(2, 2).intensity = 1;
180 }
181 
182 template <typename PointT>
183 void
185 {
186  kernel.resize(4);
187  kernel.height = 2;
188  kernel.width = 2;
189  kernel(0, 0).intensity = 1;
190  kernel(1, 0).intensity = 0;
191  kernel(0, 1).intensity = 0;
192  kernel(1, 1).intensity = -1;
193 }
194 
195 template <typename PointT>
196 void
198 {
199  kernel.resize(9);
200  kernel.height = 3;
201  kernel.width = 3;
202  kernel(0, 0).intensity = -1;
203  kernel(1, 0).intensity = -2;
204  kernel(2, 0).intensity = -1;
205  kernel(0, 1).intensity = 0;
206  kernel(1, 1).intensity = 0;
207  kernel(2, 1).intensity = 0;
208  kernel(0, 2).intensity = 1;
209  kernel(1, 2).intensity = 2;
210  kernel(2, 2).intensity = 1;
211 }
212 
213 template <typename PointT>
214 void
216 {
217  kernel.resize(9);
218  kernel.height = 3;
219  kernel.width = 3;
220  kernel(0, 0).intensity = 1;
221  kernel(1, 0).intensity = 1;
222  kernel(2, 0).intensity = 1;
223  kernel(0, 1).intensity = 0;
224  kernel(1, 1).intensity = 0;
225  kernel(2, 1).intensity = 0;
226  kernel(0, 2).intensity = -1;
227  kernel(1, 2).intensity = -1;
228  kernel(2, 2).intensity = -1;
229 }
230 
231 template <typename PointT>
232 void
234 {
235  kernel.resize(4);
236  kernel.height = 2;
237  kernel.width = 2;
238  kernel(0, 0).intensity = 0;
239  kernel(1, 0).intensity = 1;
240  kernel(0, 1).intensity = -1;
241  kernel(1, 1).intensity = 0;
242 }
243 
244 template <typename PointT>
245 void
247 {
248  kernel.resize(3);
249  kernel.height = 1;
250  kernel.width = 3;
251  kernel(0, 0).intensity = -1;
252  kernel(1, 0).intensity = 0;
253  kernel(2, 0).intensity = 1;
254 }
255 
256 template <typename PointT>
257 void
259 {
260  kernel.resize(3);
261  kernel.height = 1;
262  kernel.width = 3;
263  kernel(0, 0).intensity = 0;
264  kernel(1, 0).intensity = -1;
265  kernel(2, 0).intensity = 1;
266 }
267 
268 template <typename PointT>
269 void
271 {
272  kernel.resize(3);
273  kernel.height = 1;
274  kernel.width = 3;
275  kernel(0, 0).intensity = -1;
276  kernel(1, 0).intensity = 1;
277  kernel(2, 0).intensity = 0;
278 }
279 
280 template <typename PointT>
281 void
283 {
284  kernel.resize(3);
285  kernel.height = 3;
286  kernel.width = 1;
287  kernel(0, 0).intensity = -1;
288  kernel(0, 1).intensity = 0;
289  kernel(0, 2).intensity = 1;
290 }
291 
292 template <typename PointT>
293 void
295 {
296  kernel.resize(3);
297  kernel.height = 3;
298  kernel.width = 1;
299  kernel(0, 0).intensity = 0;
300  kernel(0, 1).intensity = -1;
301  kernel(0, 2).intensity = 1;
302 }
303 
304 template <typename PointT>
305 void
307 {
308  kernel.resize(3);
309  kernel.height = 3;
310  kernel.width = 1;
311  kernel(0, 0).intensity = -1;
312  kernel(0, 1).intensity = 1;
313  kernel(0, 2).intensity = 0;
314 }
315 
316 template <typename PointT>
317 void
319 {
320  kernel_type_ = kernel_type;
321 }
322 
323 template <typename PointT>
324 void
326 {
327  kernel_size_ = kernel_size;
328 }
329 
330 template <typename PointT>
331 void
333 {
334  sigma_ = kernel_sigma;
335 }
336 
337 } // namespace pcl
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
std::size_t size() const
Definition: point_cloud.h:443
void prewittKernelY(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:215
void derivativeXBackwardKernel(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:270
void derivativeYBackwardKernel(PointCloud< PointT > &kernel)
Definition: kernel.hpp:306
void prewittKernelX(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:166
void loGKernel(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:122
void setKernelSize(int kernel_size)
Definition: kernel.hpp:325
void sobelKernelX(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:148
void setKernelSigma(float kernel_sigma)
Definition: kernel.hpp:332
void derivativeXCentralKernel(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:246
void robertsKernelY(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:233
KERNEL_ENUM
Different types of kernels available.
Definition: kernel.h:49
void setKernelType(KERNEL_ENUM kernel_type)
Definition: kernel.hpp:318
void gaussianKernel(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:96
void sobelKernelY(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:197
void fetchKernel(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:46
void derivativeXForwardKernel(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:258
void derivativeYForwardKernel(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:294
void robertsKernelX(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:184
void derivativeYCentralKernel(pcl::PointCloud< PointT > &kernel)
Definition: kernel.hpp:282