-
Notifications
You must be signed in to change notification settings - Fork 0
/
supervoxel_clustering.cpp
286 lines (216 loc) · 9.89 KB
/
supervoxel_clustering.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <pcl/console/parse.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/segmentation/supervoxel_clustering.h>
#include <pcl/point_cloud.h>
#include <pcl/octree/octree_search.h>
#include <pcl/point_cloud.h>
#include <pcl/octree/octree_search.h>
#include <iostream>
#include <vector>
#include <ctime>
#include <iostream>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
//VTK include needed for drawing graph lines
#include <vtkPolyLine.h>
// Types
typedef pcl::PointXYZRGBA PointT;
typedef pcl::PointCloud<PointT> PointCloudT;
typedef pcl::PointNormal PointNT;
typedef pcl::PointCloud<PointNT> PointNCloudT;
typedef pcl::PointXYZL PointLT;
typedef pcl::PointCloud<PointLT> PointLCloudT;
void addSupervoxelConnectionsToViewer (PointT &supervoxel_center,
PointCloudT &adjacent_supervoxel_centers,
std::string supervoxel_name,
pcl::visualization::PCLVisualizer::Ptr & viewer);
pcl::visualization::PCLVisualizer::Ptr simpleVis (pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr cloud)
{
// --------------------------------------------
// -----Open 3D viewer and add point cloud-----
// --------------------------------------------
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
viewer->addPointCloud<pcl::PointXYZRGBA> (cloud, "sample cloud");
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
viewer->addCoordinateSystem (1.0);
viewer->initCameraParameters ();
return (viewer);
}
# include <iostream>
# include <dirent.h>
using namespace std;
void directory_contents ( char * directory_path )
{
DIR *dh;
struct dirent * contents;
if ( !dh )
{
cout << "The given directory is not found";
return;
}
int count = 3;
while ( ( contents = readdir ( dh ) ) != NULL && count>2)
{
string name = contents->d_name;
cout << name << endl;
}
closedir ( dh );
}
int main (int argc, char ** argv)
{
if (argc < 2)
{
pcl::console::print_error ("Syntax is: %s <pcd-file> \n "
"--NT Dsables the single cloud transform \n"
"-v <voxel resolution>\n-s <seed resolution>\n"
"-c <color weight> \n-z <spatial weight> \n"
"-n <normal_weight>\n", argv[0]);
return (1);
}
std::string toStore;
std::string toStorePCD;
std::string name;
DIR *dh;
struct dirent * contents;
char* directory_path=getcwd(NULL, 0);
dh = opendir ( directory_path );
cout << directory_path;
if ( !dh )
{
cout << "The given directory is not found";
}
std::string str(directory_path);
std::string name3 = "/labels.instances.colored.normals.pcd";
string allFile;
bool disable_transform = pcl::console::find_switch (argc, argv, "--NT");
float voxel_resolution = 0.008f;
bool voxel_res_specified = pcl::console::find_switch (argc, argv, "-v");
if (voxel_res_specified)
pcl::console::parse (argc, argv, "-v", voxel_resolution);
float seed_resolution = 0.1f;
bool seed_res_specified = pcl::console::find_switch (argc, argv, "-s");
if (seed_res_specified)
pcl::console::parse (argc, argv, "-s", seed_resolution);
float color_importance = 0.2f;
if (pcl::console::find_switch (argc, argv, "-c"))
pcl::console::parse (argc, argv, "-c", color_importance);
float spatial_importance = 0.4f;
if (pcl::console::find_switch (argc, argv, "-z"))
pcl::console::parse (argc, argv, "-z", spatial_importance);
float normal_importance = 1.0f;
if (pcl::console::find_switch (argc, argv, "-n"))
pcl::console::parse (argc, argv, "-n", normal_importance);
while ( ( contents = readdir ( dh ) ) != NULL)
{
string name = contents->d_name;
allFile = directory_path + '/' + name + name3;
toStore = directory_path + '/'+name + "/supervoxel_adj.txt";
toStorePCD = directory_path +'/'+ name + "/supervoxel.pcd";
ofstream outfile(toStore);
PointCloudT::Ptr cloud (new PointCloudT);
pcl::console::print_highlight ("Loading point cloud...\n");
ifstream ifile;
ifile.open(allFile);
if (ifile)
{
pcl::io::loadPCDFile<PointT> (allFile, *cloud);
////////////////////////////// //////////////////////////////
////// This is how to use supervoxels
////////////////////////////// //////////////////////////////
pcl::SupervoxelClustering<PointT> super (voxel_resolution, seed_resolution);
if (disable_transform)
super.setUseSingleCameraTransform (false);
super.setInputCloud (cloud);
//cout << " TEST WIDTH OF IMPORT " << cloud->width;
super.setColorImportance (color_importance);
super.setSpatialImportance (spatial_importance);
super.setNormalImportance (normal_importance);
std::map <std::uint32_t, pcl::Supervoxel<PointT>::Ptr > supervoxel_clusters;
pcl::console::print_highlight ("Extracting supervoxels!\n");
super.extract (supervoxel_clusters);
cout << toStorePCD << endl;
PointLCloudT::Ptr labeled_cloud = super.getLabeledCloud();
pcl::io::savePCDFileASCII(toStorePCD, *labeled_cloud);
pcl::console::print_info ("Found %d supervoxels\n", supervoxel_clusters.size ());
PointLCloudT::Ptr voxel_labeled_cloud = super.getLabeledVoxelCloud();
//pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
//viewer->setBackgroundColor (0, 0, 0);
//viewer->addPointCloud (labeled_cloud);
PointCloudT::Ptr voxel_centroid_cloud = super.getVoxelCentroidCloud ();
PointLCloudT::Ptr labeled_voxel_cloud = super.getLabeledVoxelCloud ();
PointNCloudT::Ptr sv_normal_cloud = super.makeSupervoxelNormalCloud (supervoxel_clusters);
pcl::console::print_highlight ("Getting supervoxel adjacency\n");
std::multimap<std::uint32_t, std::uint32_t> supervoxel_adjacency;
super.getSupervoxelAdjacency (supervoxel_adjacency);
int number = 0;
int m ;
//To make a graph of the supervoxel adjacency, we need to iterate through the supervoxel adjacency multimap
for (auto label_itr = supervoxel_adjacency.cbegin (); label_itr != supervoxel_adjacency.cend (); )
{
std::uint32_t supervoxel_label = label_itr->first;
pcl::Supervoxel<PointT>::Ptr supervoxel = supervoxel_clusters.at (supervoxel_label);
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>);
cloud = supervoxel->voxels_;
number = number + cloud->width;
std::ostringstream name;
name << label_itr->first << ".pcd";
PointCloudT adjacent_supervoxel_centers;
for (auto adjacent_itr = supervoxel_adjacency.equal_range (supervoxel_label).first; adjacent_itr!=supervoxel_adjacency.equal_range (supervoxel_label).second; ++adjacent_itr)
{
pcl::Supervoxel<PointT>::Ptr neighbor_supervoxel = supervoxel_clusters.at (adjacent_itr->second);
adjacent_supervoxel_centers.push_back (neighbor_supervoxel->centroid_);
outfile << adjacent_itr->first << "\t" << adjacent_itr->second << "\n";
}
//Now we make a name for this polygon
std::stringstream ss;
ss << "supervoxel_" << supervoxel_label;
label_itr = supervoxel_adjacency.upper_bound (supervoxel_label);
m = supervoxel_label;
}
outfile.close();
pcl::PointCloud<pcl::PointXYZ>::Ptr Changedcloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile<pcl::PointXYZ>(allFile, *Changedcloud);
//pcl::octree::OctreePointCloudAdjacency<pcl::PointXYZ > deneme(voxel_resolution);
//deneme.setInputCloud(Changedcloud);
//deneme.addPointsFromInputCloud();
//pcl::octree::OctreePointCloudAdjacencyContainer<pcl::PointXYZ> *leaf_container;
//leaf_container = deneme.getLeafContainerAtPoint(Changedcloud->points[1000]);
//pcl::IndicesPtr indexVectorXX(new std::vector <int>);
//leaf_container->getPointIndices(*indexVectorXX);
}
}
return (0);
}
void
addSupervoxelConnectionsToViewer (PointT &supervoxel_center,
PointCloudT &adjacent_supervoxel_centers,
std::string supervoxel_name,
pcl::visualization::PCLVisualizer::Ptr & viewer)
{
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New ();
vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New ();
vtkSmartPointer<vtkPolyLine> polyLine = vtkSmartPointer<vtkPolyLine>::New ();
//Iterate through all adjacent points, and add a center point to adjacent point pair
int i = 0;
for (auto adjacent_itr = adjacent_supervoxel_centers.begin (); adjacent_itr != adjacent_supervoxel_centers.end (); ++adjacent_itr)
{
points->InsertNextPoint (supervoxel_center.data);
points->InsertNextPoint (adjacent_itr->data);
}
// Create a polydata to store everything in
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New ();
// Add the points to the dataset
polyData->SetPoints (points);
polyLine->GetPointIds ()->SetNumberOfIds(points->GetNumberOfPoints ());
//cout << "number of points " << points->GetNumberOfPoints ();
for(unsigned int i = 0; i < points->GetNumberOfPoints (); i++)
polyLine->GetPointIds ()->SetId (i,i);
cells->InsertNextCell (polyLine);
// Add the lines to the dataset
polyData->SetLines (cells);
viewer->addModelFromPolyData (polyData,supervoxel_name);
}