-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrbnn.cpp
195 lines (162 loc) · 4.92 KB
/
rbnn.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <pcl/point_cloud.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <ctime>
#include <string>
#include <map>
#include <Windows.h>
using namespace std;
#pragma region [configuration]
string directory = "C:\\Users\\km\\Desktop\\MAG\\FloatingObjectFilter\\data";
string file_name = "459_100.pcd";
string result_prefix = "result";
vector<double> radius_values {};
#pragma endregion
#pragma region [auxiliary]
int ReadPointCloudFile(pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud, const string& filename) {
srand(time(NULL));
if (pcl::io::loadPCDFile<pcl::PointXYZ>(filename, *cloud) == -1) //* load the file
{
PCL_ERROR("Couldn't read file test_pcd.pcd \n");
return (-1);
}
std::cout << "Loaded "
<< cloud->width * cloud->height
<< " data points from test_pcd.pcd with the following fields: "
<< std::endl;
return 0;
}
void mergeClusters(vector<int>& cluster_indices, int idx1, int idx2) {
for (int i = 0; i < cluster_indices.size(); i++) {
if (cluster_indices[i] == idx1) {
cluster_indices[i] = idx2;
}
}
}
// return the cluster that each point belongs to
vector<int> rbnn(pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud, double radius, int min_members) {
vector<int> cluster_indices = vector<int>(cloud->points.size(), -1);
pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;
kdtree.setInputCloud(cloud);
int current_cluster = 0;
for (int i = 0; i < cloud->points.size(); i++) {
if (i % 100000 == 0) {
cout << i << endl;
}
// 1. skip this point if it's already within a cluster
if (cluster_indices[i] != -1)
continue;
// 2. find nearest neigbours for current point
std::vector<int> pointIdxRadiusSearch;
std::vector<float> pointRadiusSquaredDistance;
if (kdtree.radiusSearch(cloud->points[i], radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0)
{
// 3. merge or assign new clusters
for (size_t j = 0; j < pointIdxRadiusSearch.size(); ++j) {
int oc = cluster_indices[i]; // original point's cluster
int nc = cluster_indices[pointIdxRadiusSearch[j]]; // neighbor point's cluster
if (oc != -1 && nc != -1) {
if (oc != nc)
mergeClusters(cluster_indices, oc, nc);
}
else {
if (nc != -1) {
cluster_indices[i] = nc;
}
else {
if (oc != -1) {
cluster_indices[pointIdxRadiusSearch[j]] = oc;
}
}
}
}
}
// 4. if there is still no cluster, create a new one and assign all neighbors to it
if (cluster_indices[i] == -1) {
current_cluster++;
cluster_indices[i] = current_cluster;
for (int j = 0; j < pointIdxRadiusSearch.size(); j++) {
cluster_indices[pointIdxRadiusSearch[j]] = current_cluster;
}
}
}
// 5. delete the clusters that are too small
// not implemented
// 6. return cluster_indices
return cluster_indices;
}
int most_frequent_value(vector<int> values) {
map<int, int> histcounts;
for (int i = 0; i < values.size(); i++) {
if (histcounts.find(values[i]) == histcounts.end()) {
histcounts[values[i]] = 1;
}
else {
histcounts[values[i]] += 1;
}
}
int max = 0, maxi;
vector<pair<int, int>> tr(histcounts.begin(), histcounts.end());
for (auto& val : tr) {
cout << val.first << " : " << val.second << endl;
if (val.second > max) {
max = val.second;
maxi = val.first;
}
}
return maxi;
}
#pragma endregion
int main (int argc, char** argv)
{
if (argc >= 3) {
directory = argv[1];
file_name = argv[2];
result_prefix = argv[3];
for (int i = 4; i < argc; i++) {
radius_values.push_back(atof(argv[i]));
}
}
cout << "begin reading PC file" << endl;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
ReadPointCloudFile(cloud, directory + "\\" + file_name);
// foreach rbnn radius value
stringstream result_storage;
for (double radius_value : radius_values) {
cout << "processing radius value " << radius_value << endl;
// perform the rbnn
vector<int> cluster_indices = rbnn(cloud, radius_value, 1);
cout << "RBNN FINISHED" << endl;
cout << endl;
// find the maximal cluster (representative of the floor segment)
int max_clust = most_frequent_value(cluster_indices);
cout << "MAXIMUM CLUSTER: " << max_clust << endl;
// all other points are classified as floating objects
int cntr_yes = 0, cntr_no = 0;
result_storage << radius_value << " ";
for (int i = 0; i < cluster_indices.size(); i++) {
if (cluster_indices[i] == max_clust) {
result_storage << -1 << " ";
cntr_no++;
}
else {
result_storage << cluster_indices[i] << " ";
cntr_yes++;
}
}
result_storage << endl;
cout << "POSITIVES: " << cntr_yes << " NEGATIVES: " << cntr_no << endl;
}
// write the results
ofstream outfile;
outfile.open(directory + "\\" + result_prefix + file_name, ios::out);
outfile << result_storage.str();
outfile.close();
cout << "results written to disc. Press any key to exit..." << endl;
return 0;
}