Apollo 11.0
自动驾驶开放平台
filter.h
浏览该文件的文档.
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/*
18Copyright (C) 2006 Pedro Felzenszwalb
19This program is free software; you can redistribute it and/or modify
20it under the terms of the GNU General Public License as published by
21the Free Software Foundation; either version 2 of the License, or
22(at your option) any later version.
23This program is distributed in the hope that it will be useful,
24but WITHOUT ANY WARRANTY; without even the implied warranty of
25MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26GNU General Public License for more details.
27You should have received a copy of the GNU General Public License
28along with this program; if not, write to the Free Software
29Foundation, 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
41
42namespace apollo {
43namespace perception {
44namespace lidar {
45const double WIDTH = 4.0;
51void normalize(std::vector<float> *mask_input) {
52 std::vector<float> &mask = *mask_input;
53 int len = mask.size();
54 float sum = 0;
55 for (int i = 1; i < len; i++) {
56 sum += fabs(mask[i]);
57 }
58 sum = 2 * sum + fabs(mask[0]);
59 for (int i = 0; i < len; i++) {
60 mask[i] /= sum;
61 }
62}
67#define MAKE_FILTER(name, fun) \
68 std::vector<float> make_##name(float sigma) { \
69 sigma = std::max(sigma, 0.01F); \
70 int len = static_cast<int>(ceil(sigma * WIDTH)) + 1; \
71 std::vector<float> mask(len); \
72 for (int i = 0; i < len; i++) { \
73 mask[i] = fun; \
74 } \
75 return mask; \
76 }
77MAKE_FILTER(fgauss, exp(-0.5 * square(i / sigma)));
78
86Image<float> *smooth(Image<float> *src, float sigma) {
87 std::vector<float> mask = make_fgauss(sigma);
88 normalize(mask);
89 Image<float> *tmp = new Image<float>(src->height(), src->width(), false);
90 Image<float> *dst = new Image<float>(src->width(), src->height(), false);
91 convolve_even(src, tmp, mask);
92 convolve_even(tmp, dst, mask);
93 delete tmp;
94 return dst;
95}
103Image<float> *smooth(Image<uchar> *src, float sigma) {
105 Image<float> *dst = smooth(tmp, sigma);
106 delete tmp;
107 return dst;
108}
116 int width = src->width();
117 int height = src->height();
118 Image<float> *dst = new Image<float>(width, height);
119 for (int y = 1; y < height - 1; y++) {
120 for (int x = 1; x < width - 1; x++) {
121 float d2x = imRef(src, x - 1, y) + imRef(src, x + 1, y) - 2 * imRef(src, x, y);
122 float d2y = imRef(src, x, y - 1) + imRef(src, x, y + 1) - 2 * imRef(src, x, y);
123 imRef(dst, x, y) = d2x + d2y;
124 }
125 }
126 return dst;
127}
128} // namespace lidar
129} // namespace perception
130} // namespace apollo
int height() const
Get the height of an image
Definition image.h:73
int width() const
Get the width of an image
Definition image.h:64
#define imRef(im, x, y)
Definition image.h:88
Image< float > * image_uchar2float(Image< uchar > *input)
Transform uchar to float
Definition imconv.h:89
T square(const T &x)
get square of value x
Definition misc.h:85
void convolve_even(Image< float > *src, Image< float > *dst, const std::vector< float > &mask)
convolve src with mask.
Definition convolve.h:49
Image< float > * smooth(Image< float > *src, float sigma)
Convolve image with gaussian filter
Definition filter.h:86
Image< float > * laplacian(Image< float > *src)
Compute laplacian
Definition filter.h:115
void normalize(std::vector< float > *mask_input)
Normalize mask so it integrates to one
Definition filter.h:51
const double WIDTH
Definition filter.h:45
class register implement
Definition arena_queue.h:37
#define MAKE_FILTER(name, fun)
Make filters
Definition filter.h:67