Apollo  v5.5.0
Open source self driving car software
filter.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2019 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 /*
18 Copyright (C) 2006 Pedro Felzenszwalb
19 This program is free software; you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation; either version 2 of the License, or
22 (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 */
31 /* simple filters */
32 #pragma once
33 #include <algorithm>
34 #include <cmath>
35 #include <vector>
36 #include "modules/perception/lidar/segmentation/ncut/common/graph_felzenszwalb/convolve.h"
37 #include "modules/perception/lidar/segmentation/ncut/common/graph_felzenszwalb/image.h"
38 #include "modules/perception/lidar/segmentation/ncut/common/graph_felzenszwalb/imconv.h"
39 #include "modules/perception/lidar/segmentation/ncut/common/graph_felzenszwalb/misc.h"
40 
41 namespace apollo {
42 namespace perception {
43 namespace lidar {
44 const double WIDTH = 4.0;
45 /* normalize mask so it integrates to one */
46 void normalize(std::vector<float> *mask_input) {
47  std::vector<float> &mask = *mask_input;
48  int len = mask.size();
49  float sum = 0;
50  for (int i = 1; i < len; i++) {
51  sum += fabs(mask[i]);
52  }
53  sum = 2 * sum + fabs(mask[0]);
54  for (int i = 0; i < len; i++) {
55  mask[i] /= sum;
56  }
57 }
58 /* make filters */
59 #define MAKE_FILTER(name, fun) \
60  std::vector<float> make_##name(float sigma) { \
61  sigma = std::max(sigma, 0.01F); \
62  int len = static_cast<int>(ceil(sigma * WIDTH)) + 1; \
63  std::vector<float> mask(len); \
64  for (int i = 0; i < len; i++) { \
65  mask[i] = fun; \
66  } \
67  return mask; \
68  }
69 MAKE_FILTER(fgauss, exp(-0.5 * square(i / sigma)));
70 /* convolve image with gaussian filter */
71 Image<float> *smooth(Image<float> *src, float sigma) {
72  std::vector<float> mask = make_fgauss(sigma);
73  normalize(mask);
74  Image<float> *tmp = new Image<float>(src->height(), src->width(), false);
75  Image<float> *dst = new Image<float>(src->width(), src->height(), false);
76  convolve_even(src, tmp, mask);
77  convolve_even(tmp, dst, mask);
78  delete tmp;
79  return dst;
80 }
81 /* convolve image with gaussian filter */
82 Image<float> *smooth(Image<uchar> *src, float sigma) {
83  Image<float> *tmp = image_uchar2float(src);
84  Image<float> *dst = smooth(tmp, sigma);
85  delete tmp;
86  return dst;
87 }
88 /* compute laplacian */
90  int width = src->width();
91  int height = src->height();
92  Image<float> *dst = new Image<float>(width, height);
93  for (int y = 1; y < height - 1; y++) {
94  for (int x = 1; x < width - 1; x++) {
95  float d2x =
96  imRef(src, x - 1, y) + imRef(src, x + 1, y) - 2 * imRef(src, x, y);
97  float d2y =
98  imRef(src, x, y - 1) + imRef(src, x, y + 1) - 2 * imRef(src, x, y);
99  imRef(dst, x, y) = d2x + d2y;
100  }
101  }
102  return dst;
103 }
104 } // namespace lidar
105 } // namespace perception
106 } // namespace apollo
Image< float > * laplacian(Image< float > *src)
Definition: filter.h:89
int height() const
Definition: image.h:55
Definition: blob.h:72
int width() const
Definition: image.h:52
#define imRef(im, x, y)
Definition: image.h:68
void convolve_even(Image< float > *src, Image< float > *dst, const std::vector< float > &mask)
Definition: convolve.h:41
Definition: image.h:40
Image< float > * smooth(Image< float > *src, float sigma)
Definition: filter.h:71
Image< float > * image_uchar2float(Image< uchar > *input)
Definition: imconv.h:70
MAKE_FILTER(fgauss, exp(-0.5 *square(i/sigma)))
const double WIDTH
Definition: filter.h:44
T square(const T &x)
Definition: misc.h:56
void normalize(std::vector< float > *mask_input)
Definition: filter.h:46