-
Notifications
You must be signed in to change notification settings - Fork 27
/
main_test_seg.cpp
382 lines (330 loc) · 12.6 KB
/
main_test_seg.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* File: newmain.cpp
* Author: chi
*
* Created on April 4, 2015, 11:20 AM
*/
#include "src/Greedy/utility.h"
#include <eigen3/Eigen/src/Geometry/Quaternion.h>
#include <opencv2/highgui/highgui.hpp>
std::string link_mesh_name("data/link_uniform.obj");
std::string node_mesh_name("data/node_uniform.obj");
ModelT LoadMesh(std::string filename, std::string label);
void showGT(const std::vector<ModelT> &model_set, const std::vector<poseT> &poses, pcl::visualization::PCLVisualizer::Ptr viewer);
//int MeshOn2D(const std::vector<ModelT> &model_set, const std::vector<poseT> &poses, cv::Mat &map2d, float fx = FOCAL_X, float fy = FOCAL_Y);
float segAcc(const std::vector<ModelT> &model_set, const std::vector<poseT> &poses, pcl::PointCloud<PointT>::Ptr cloud)
{
pcl::PointCloud<PointT>::Ptr link_cloud(new pcl::PointCloud<PointT>());
pcl::PointCloud<PointT>::Ptr node_cloud(new pcl::PointCloud<PointT>());
for( int i = 0 ; i < model_set.size() ; i++ )
{
uint32_t cur_label;
pcl::PointCloud<PointT>::Ptr cur_cloud(new pcl::PointCloud<PointT>());
if( model_set[i].model_label == "link" )
{
cur_label = 1;
cur_cloud = link_cloud;
}
else if( model_set[i].model_label == "node" )
{
cur_label = 2;
cur_cloud = node_cloud;
}
pcl::copyPointCloud(*model_set[i].model_cloud, *cur_cloud);
for( pcl::PointCloud<PointT>::iterator it = cur_cloud->begin() ; it < cur_cloud->end() ; it++ )
it->rgba = cur_label;
}
Eigen::Quaternionf calibrate_rot(Eigen::AngleAxisf(M_PI/2, Eigen::Vector3f (1, 0, 0)));
pcl::PointCloud<PointT>::Ptr all_cloud(new pcl::PointCloud<PointT>());
for(std::vector<poseT>::const_iterator it = poses.begin() ; it < poses.end() ; it++)
{
for( int i = 0 ; i < model_set.size() ; i++ )
{
if(model_set[i].model_label == it->model_name )
{
pcl::PointCloud<PointT>::Ptr cur_cloud(new pcl::PointCloud<PointT>());
if( it->model_name == "link" )
pcl::copyPointCloud(*link_cloud, *cur_cloud);
else if( it->model_name == "node" )
pcl::copyPointCloud(*node_cloud, *cur_cloud);
pcl::transformPointCloud(*cur_cloud, *cur_cloud, it->shift, it->rotation*calibrate_rot);
all_cloud->insert(all_cloud->end(), cur_cloud->begin(), cur_cloud->end());
}
}
}
pcl::search::KdTree<PointT> tree;
tree.setInputCloud (all_cloud);
uint8_t tmp_color = 255;
uint32_t red = tmp_color << 16;
uint32_t blue = tmp_color;
int pos_count = 0;
float T = 0.02*0.02;
for ( pcl::PointCloud<PointT>::iterator it = cloud->begin() ; it < cloud->end() ; it++ )
{
std::vector<int> indices (1);
std::vector<float> sqr_distances (1);
int nres = tree.nearestKSearch(*it, 1, indices, sqr_distances);
if( it->rgba > 255 )
it->rgba = 1;
else if( it->rgba > 0 )
it->rgba = 2;
else
it->rgba = 0;
if ( nres == 1 && sqr_distances[0] < T )
{
if(it->rgba == all_cloud->at(indices[0]).rgba)
pos_count++;
}
else if( it->rgba == 0 || sqr_distances[0] > T )
pos_count++;
if( nres == 1 && sqr_distances[0] < T )
{
if( all_cloud->at(indices[0]).rgba == 1 )
it->rgba = red;
else if( all_cloud->at(indices[0]).rgba == 2 )
it->rgba = blue;
}
else
it->rgba = 0;
}
return (pos_count +0.0) / cloud->size();
}
int main(int argc, char** argv)
{
std::string seg_path("data/ln_joint/");
std::string gt_path("final_joint_gt/");
std::string out_path("");
//pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer());
//viewer->initCameraParameters();
//viewer->addCoordinateSystem(0.1);
//viewer->setCameraPosition(0, 0, 0.1, 0, 0, 1, 0, -1, 0);
int c1 = 0, c2 = -1;
pcl::console::parse_argument(argc, argv, "--gt", gt_path);
pcl::console::parse_argument(argc, argv, "--eg", seg_path);
pcl::console::parse_argument(argc, argv, "--o", out_path);
pcl::console::parse_argument(argc, argv, "--c1", c1);
pcl::console::parse_argument(argc, argv, "--c2", c2);
if( out_path.empty() == false && exists_dir(out_path) == false )
boost::filesystem::create_directories(out_path);
float acc = 0;
int acc_count = 0;
for(int i = c1 ; i <= c2 ; i++ )
{
std::stringstream ss;
ss << i;
std::string pcd_file(seg_path + "seg_" + ss.str() + ".pcd");
std::string link_pose_file(gt_path + "link_gt_" + ss.str() + ".csv");
std::string node_pose_file(gt_path + "node_gt_" + ss.str() + ".csv");
std::cerr << "Loading... " << pcd_file << std::endl;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pcl::PointCloud<PointT>::Ptr cloud(new pcl::PointCloud<PointT>());
pcl::io::loadPCDFile(pcd_file, *cloud);
if( cloud->empty() == true )
break;
acc_count++;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::vector<poseT> all_poses;
readCSV(link_pose_file, "link", all_poses);
readCSV(node_pose_file, "node", all_poses);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//read meshes
ModelT link_mesh = LoadMesh(link_mesh_name, "link");
ModelT node_mesh = LoadMesh(node_mesh_name, "node");
std::vector<ModelT> mesh_set;
mesh_set.push_back(link_mesh);
mesh_set.push_back(node_mesh);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//viewer->removePointCloud("cloud");
//viewer->addPointCloud(cloud, "cloud");
//viewer->spin();
float cur_acc = segAcc(mesh_set, all_poses, cloud);
std::cerr << "P: " << cur_acc << std::endl;
acc += cur_acc;
if( out_path.empty() == false )
pcl::io::savePCDFile(out_path + "seg_" + ss.str() + ".pcd", *cloud, true);
//viewer->removePointCloud("cloud");
//viewer->addPointCloud(cloud, "cloud");
//viewer->spin();
/*
cv::Mat gt_label, seg_label, uv;
int gt_num = MeshOn2D(mesh_set, all_poses, gt_label);
int seg_num = segProj(cloud, seg_label, uv);
int true_pos = overlapGT(gt_label, seg_label, uv);
std::cerr << "P: " << (true_pos+0.0) / seg_num << std::endl;
acc += (true_pos+0.0) / seg_num;
//*
showLabel(gt_label, "GT");
showLabel(seg_label, "Seg");
//*/
}
std::cerr << "Seg-Acc: " << acc / acc_count << std::endl;
/*************************************************************************************************************************/
return 0;
}
void showGT(const std::vector<ModelT> &model_set, const std::vector<poseT> &poses, pcl::visualization::PCLVisualizer::Ptr viewer)
{
std::vector<pcl::PointCloud<myPointXYZ>::Ptr> rec;
for( std::vector<ModelT>::const_iterator it = model_set.begin() ; it < model_set.end() ; it++ )
{
pcl::PointCloud<myPointXYZ>::Ptr cur_cloud(new pcl::PointCloud<myPointXYZ>());
pcl::fromPCLPointCloud2(it->model_mesh->cloud, *cur_cloud);
rec.push_back(cur_cloud);
}
int count = 0;
Eigen::Quaternionf calibrate_rot(Eigen::AngleAxisf(M_PI/2, Eigen::Vector3f (1, 0, 0)));
for(std::vector<poseT>::const_iterator it = poses.begin() ; it < poses.end() ; it++, count++ )
{
for( int i = 0 ; i < model_set.size() ; i++ )
{
if(model_set[i].model_label == it->model_name )
{
pcl::PointCloud<myPointXYZ>::Ptr cur_cloud(new pcl::PointCloud<myPointXYZ>());
//pcl::transformPointCloud(*rec[i], *cur_cloud, Eigen::Vector3f (0, 0, 0), calibrate_rot);
pcl::transformPointCloud(*rec[i], *cur_cloud, it->shift, it->rotation*calibrate_rot);
std::stringstream ss;
ss << count;
viewer->addPolygonMesh<myPointXYZ>(cur_cloud, model_set[i].model_mesh->polygons, it->model_name+"_"+ss.str());
if( it->model_name == "link" )
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1.0, 0.55, 0.05, it->model_name+"_"+ss.str());
else if( it->model_name == "node" )
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0.05, 0.55, 1.0, it->model_name+"_"+ss.str());
break;
}
}
}
}
/*
int MeshOn2D(const std::vector<ModelT> &model_set, const std::vector<poseT> &poses, cv::Mat &map2d, float fx, float fy)
{
int img_h = 480;
int img_w = 640;
cv::Mat elev_map = cv::Mat::ones(img_h, img_w, CV_32FC1)*1000;
map2d = cv::Mat::zeros(img_h, img_w, CV_32SC1);
int count = 0;
Eigen::Quaternionf calibrate_rot(Eigen::AngleAxisf(M_PI/2, Eigen::Vector3f (1, 0, 0)));
for(std::vector<poseT>::const_iterator it = poses.begin() ; it < poses.end() ; it++)
{
for( int i = 0 ; i < model_set.size() ; i++ )
{
if(model_set[i].model_label == it->model_name )
{
pcl::PointCloud<myPointXYZ>::Ptr cur_cloud(new pcl::PointCloud<myPointXYZ>());
pcl::transformPointCloud(*model_set[i].model_cloud, *cur_cloud, Eigen::Vector3f (0, 0, 0), calibrate_rot);
pcl::transformPointCloud(*cur_cloud, *cur_cloud, it->shift, it->rotation);
int cur_label;
if( it->model_name == "link" )
cur_label = 1;
else if( it->model_name == "node" )
cur_label = 2;
size_t num = cur_cloud->size();
for( size_t i = 0 ; i < num ; i++ )
{
myPointXYZ *pt_ptr = &cur_cloud->at(i);
int img_x = round(pt_ptr->x / pt_ptr->z * fx + CENTER_X);
int img_y = round(pt_ptr->y / pt_ptr->z * fy + CENTER_Y);
if( img_x < 0 ) img_x = 0;
if( img_y < 0 ) img_y = 0;
if( img_x >= img_w ) img_x = img_w-1;
if( img_y >= img_h ) img_y = img_h-1;
if( pt_ptr->z < elev_map.at<float>(img_y, img_x) )
{
if( map2d.at<int>(img_y, img_x) <= 0 )
count++;
map2d.at<int>(img_y, img_x) = cur_label;
elev_map.at<float>(img_y, img_x) = pt_ptr->z;
}
}
}
}
}
return count;
}
int segProj(const pcl::PointCloud<PointT>::Ptr cloud, cv::Mat &map2d, cv::Mat &uv, float fx = FOCAL_X, float fy = FOCAL_Y)
{
size_t num = cloud->size();
int img_h = 480;
int img_w = 640;
map2d = cv::Mat::ones(img_h, img_w, CV_32SC1)*-1;
uv = cv::Mat::zeros(num, 2, CV_32SC1);
int *ptr_uv = (int *)uv.data;
cv::Mat link_count = cv::Mat::zeros(img_h, img_w, CV_32SC1);
cv::Mat node_count = cv::Mat::zeros(img_h, img_w, CV_32SC1);
cv::Mat back_count = cv::Mat::zeros(img_h, img_w, CV_32SC1);
int count = 0;
for( size_t i = 0 ; i < num ; i++ )
{
PointT *pt_ptr = &cloud->at(i);
int img_x = round(pt_ptr->x / pt_ptr->z * fx + CENTER_X);
int img_y = round(pt_ptr->y / pt_ptr->z * fy + CENTER_Y);
if( img_x < 0 ) img_x = 0;
if( img_y < 0 ) img_y = 0;
if( img_x >= img_w ) img_x = img_w-1;
if( img_y >= img_h ) img_y = img_h-1;
if( map2d.at<int>(img_y, img_x) < 0 )
{
*ptr_uv = img_y; ptr_uv++;
*ptr_uv = img_x; ptr_uv++;
count++;
}
if( pt_ptr->rgba > 255 )
link_count.at<int>(img_y, img_x)++;
else if( pt_ptr->rgba > 0 )
node_count.at<int>(img_y, img_x)++;
else
back_count.at<int>(img_y, img_x)++;
int link_num = link_count.at<int>(img_y, img_x);
int node_num = node_count.at<int>(img_y, img_x);
int back_num = back_count.at<int>(img_y, img_x);
if( link_num > node_num && link_num > back_num )
map2d.at<int>(img_y, img_x) = 1;
else if( node_num > back_num )
map2d.at<int>(img_y, img_x) = 2;
else
map2d.at<int>(img_y, img_x) = 0;
}
return count;
}
int overlapGT(const cv::Mat >_label, const cv::Mat &seg_label, const cv::Mat &uv)
{
int num = uv.rows;
int count = 0;
cv::Mat diff = cv::Mat::zeros(480, 640, CV_8UC1);
for(int i = 0 ; i < num ; i++ )
{
int img_y = uv.at<int>(i, 0);
int img_x = uv.at<int>(i, 1);
if( gt_label.at<int>(img_y, img_x) == seg_label.at<int>(img_y, img_x) )
count++;
if( gt_label.at<int>(img_y, img_x) == 0 && seg_label.at<int>(img_y, img_x) > 0)
count++;
//diff.at<uchar>(img_y, img_x) = 255;
}
//cv::imshow("DIFF", diff);
//cv::waitKey();
return count;
}
void showLabel(cv::Mat label_map, std::string name)
{
cv::Mat show_gt = cv::Mat::zeros(label_map.rows, label_map.cols, CV_8UC3);
for(int r = 0 ; r < label_map.rows; r++ ){
for(int c = 0 ; c < label_map.cols; c++ )
{
if(label_map.at<int>(r, c) == 1 )
{
show_gt.at<uchar>(r, c*3+2) = 255;
show_gt.at<uchar>(r, c*3+1) = 0;
show_gt.at<uchar>(r, c*3) = 0;
}
else if(label_map.at<int>(r, c) == 2 )
{
show_gt.at<uchar>(r, c*3+2) = 0;
show_gt.at<uchar>(r, c*3+1) = 0;
show_gt.at<uchar>(r, c*3) = 255;
}
}
}
cv::imshow(name.c_str(), show_gt);
cv::waitKey();
}
*/