-
Notifications
You must be signed in to change notification settings - Fork 11
/
laser_filters.cpp
118 lines (103 loc) · 2.77 KB
/
laser_filters.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "laser_filters.h"
Laser_Filters::Laser_Filters()
{
;
}
Laser_Filters::~Laser_Filters()
{
;
}
bool Laser_Filters::median_filter(std::vector<float>& laser_scans_angle_in, std::vector<float>& laser_scans_dist_in, std::vector<float>& laser_scans_angle_out, std::vector<float>& laser_scans_dist_out)
{
laser_scans_dist_out = laser_scans_dist_in;
laser_scans_angle_out = laser_scans_angle_in;
//
int median_filter_radius = 2;
for(int i = 0; i < laser_scans_dist_in.size(); i++)
{
if((i < median_filter_radius) || (i >= laser_scans_dist_in.size()-median_filter_radius))
{
laser_scans_dist_out[i] = laser_scans_dist_in[i];
}
else
{
float median = find_median(i, laser_scans_dist_in, median_filter_radius);
if(median == -1)
{
laser_scans_dist_out[i] = 0;
return 0;
}
else
{
laser_scans_dist_out[i] = median;
}
}
}
return 1;
}
float Laser_Filters::find_median(int index, std::vector<float>& laser_scans_dist_in, int radius)
{
int i = 0;
int j = 0;
int l = 0;
int m = radius*2;
// check the boundary of laser_scans_dist_in
if((index - radius < 0) || (index + radius >= laser_scans_dist_in.size()))
{
return -1;
}
// copy data from laser_scans_dist_in
float* laser_scans_dist_local = new float[radius*2+1];
for(i = -radius; i <= radius; i++)
{
laser_scans_dist_local[i+radius] = laser_scans_dist_in[index+i];
}
// find the median
float x;
while(l < m)
{
x = laser_scans_dist_local[radius];
i = l;
j = m;
do
{
while(laser_scans_dist_local[i] < x) i++;
while(x < laser_scans_dist_local[j]) j--;
if(i<=j)
{
// swap
float temp = laser_scans_dist_local[i];
laser_scans_dist_local[i] = laser_scans_dist_local[j];
laser_scans_dist_local[j] = temp;
i++;
j--;
}
} while (i <= j);
if(j < radius) l = i;
if(radius < i) m = j;
}
// debug - make sure the returned variable is median
/*
for(i = -radius; i <= radius; i++)
{
if(i > 0)
{
if(laser_scans_dist_local[radius] > laser_scans_dist_local[radius+i])
{
ROS_ERROR("*** [laser_filters] find incorrect median ***");
}
}
else if(i < 0)
{
if(laser_scans_dist_local[radius] < laser_scans_dist_local[radius+i])
{
ROS_ERROR("*** [laser_filters] find incorrect median ***");
}
}
}
*/
// delete and return
float ret_val = laser_scans_dist_local[radius];
delete [] laser_scans_dist_local;
return ret_val;
}