@@ -7,11 +7,12 @@ pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in(new pcl::PointCloud<pcl::PointXYZ>)
7
7
vector<Eigen::Vector3f> CamCenter;
8
8
vector<vector<Eigen::Matrix3f>> R;
9
9
vector<vector<Eigen::Vector3f>> T;
10
- cv::Mat current_img;
10
+ uchar * current_img;
11
11
int current_img_width, current_img_height;
12
12
Eigen::Matrix3f current_R;
13
13
Eigen::Vector3f current_T;
14
14
void texture_color (float x, float y, float z, uchar rgb[]);
15
+ void safe_process (char * cmdLine, DWORD max_time);
15
16
class _declspec (dllexport) CCloudOptimization
16
17
{
17
18
public:
@@ -30,6 +31,11 @@ class _declspec(dllexport) CCloudOptimization
30
31
};
31
32
32
33
inline void reverse (float &x) { x = -x;}
34
+ inline void reverse_normal (pcl::PointNormal &input) {
35
+ reverse (input.normal_x );
36
+ reverse (input.normal_y );
37
+ reverse (input.normal_z );
38
+ }
33
39
34
40
void CCloudOptimization::Init (int sor_meank, double sor_stdThres, int outrem_neighbor, double outrem_radius, double mls_radius, CManageData *ImageData, bool isdelete_)
35
41
{
@@ -50,7 +56,6 @@ void CCloudOptimization::Init(int sor_meank, double sor_stdThres, int outrem_nei
50
56
system (cmd);
51
57
}
52
58
53
-
54
59
void CCloudOptimization::InsertPoint (cv::Mat p)
55
60
{
56
61
cloud_in->push_back (pcl::PointXYZ (p.ptr <double >(0 )[0 ], p.ptr <double >(1 )[0 ], p.ptr <double >(2 )[0 ]));
@@ -80,6 +85,7 @@ void CCloudOptimization::filter(int idx)
80
85
sor.setStddevMulThresh (m_sor_stdThres);// face 0.3
81
86
sor.filter (*cloud_filtered);
82
87
printf (" Initial points: %d\n " ,cloud_in->points .size ());
88
+
83
89
/*
84
90
pcl::RadiusOutlierRemoval<pcl::PointXYZ> outrem;
85
91
// build the filter
@@ -88,7 +94,7 @@ void CCloudOptimization::filter(int idx)
88
94
outrem.setMinNeighborsInRadius (m_outrem_neighbor);
89
95
// apply filter
90
96
outrem.filter (*cloud_filtered);*/
91
-
97
+
92
98
std::cerr << " Cloud after filtering: " << std::endl;
93
99
std::cerr << *cloud_filtered;
94
100
@@ -104,71 +110,48 @@ void CCloudOptimization::filter(int idx)
104
110
pcl::PointCloud<pcl::PointNormal>::Ptr cloud_normal (new pcl::PointCloud<pcl::PointNormal>);
105
111
pcl::concatenateFields (*current_cloud_normals, *cloud_filtered, *cloud_normal);
106
112
113
+
107
114
#pragma omp parallel for
108
115
for (int i=0 ; i<cloud_normal->size (); i++)
109
116
{
110
117
Eigen::Vector3f current_camdir = CamCenter[idx] - cloud_normal->points [i].getVector3fMap ();
111
118
Eigen::Vector3f current_direction_normed = cloud_normal->points [i].getNormalVector3fMap ();
112
119
if (current_direction_normed.dot (current_camdir) < 0 )
113
- {
114
- reverse (cloud_normal->points [i].normal_x );
115
- reverse (cloud_normal->points [i].normal_y );
116
- reverse (cloud_normal->points [i].normal_z );
117
- }
120
+ reverse_normal (cloud_normal->points [i]);
118
121
}
119
-
120
-
122
+
121
123
*cloud_normals += *cloud_normal;
122
124
char name[MAX_PATH];
123
125
pcl::io::savePLYFileBinary (" tmp\\ cloud_filter.ply" , *cloud_normal);
124
126
system (" mesh.bat" );
125
127
current_img_width = m_ImageData->cam [idx][0 ].image .cols ;
126
128
current_img_height = m_ImageData->cam [idx][0 ].image .rows ;
129
+
127
130
MyPlyIo myPlyIo (texture_color, ' b' );
128
131
sprintf (name, " tmp\\ color_%d_0.ply" , idx);
129
- current_img = m_ImageData->cam [idx][0 ].image ;
132
+ current_img = m_ImageData->cam [idx][0 ].image . data ;
130
133
current_R = R[idx][0 ];
131
134
current_T = T[idx][0 ];
135
+
132
136
if (myPlyIo.ReadAndWrite (" tmp\\ mesh_trimmer.ply" , name))
133
137
std::cout << " fail\n " ;
134
138
sprintf (name, " tmp\\ color_%d_1.ply" , idx);
135
- current_img = m_ImageData->cam [idx][1 ].image ;
139
+ current_img = m_ImageData->cam [idx][1 ].image . data ;
136
140
current_R = R[idx][1 ];
137
141
current_T = T[idx][1 ];
138
142
if (myPlyIo.ReadAndWrite (" tmp\\ mesh_trimmer.ply" , name))
139
143
std::cout << " fail\n " ;
144
+
140
145
cloud_in->clear ();
146
+
141
147
}
142
148
143
149
void CCloudOptimization::run ()
144
150
{
145
- // Eigen::Vector4f centroid;
146
- // pcl::compute3DCentroid (*cloud,centroid);
147
- // Eigen::Vector3f m_ObjectCenter = Eigen::Vector3f(centroid[0]-50, centroid[1], centroid[2]);
148
-
149
- // for (int i=0; i<m_ImageData->m_CampairNum; i++)
150
- // {
151
- // boost::shared_ptr<std::vector<int> > indicesptr_temp (new std::vector<int> (cam_indices[i]));
152
- // pcl::ExtractIndices<pcl::PointXYZ> extract;
153
- // extract.setInputCloud (cloud);
154
- // extract.setIndices (indicesptr_temp);
155
- // extract.setNegative (false);
156
- // extract.filter (*cloud_in);
157
- // char name[100];
158
- // sprintf(name, "cloud_filter%d.ply", i+10);
159
- // pcl::io::savePLYFileBinary(name, *cloud_in);
160
- // }
161
-
162
151
boost::shared_ptr<std::vector<int > > indicesptr (new vector<int > );
163
152
if (isdelete)
164
153
{
165
154
// TODO:首先计算法向,假设法向正确,用法向和点到相机方向的夹角来判断所属相机
166
- // pcl::io::savePLYFileBinary("tmp\\bigcloud_normal.ply", *cloud_normals);
167
- // pcl::PLYWriter w;
168
- // w.write<pcl::PointNormal>("tmp\\bigcloud_normal.ply", *cloud_normals, false, false);
169
- // system("gen_normals.bat");
170
- // pcl::io::loadPLYFile("tmp\\bigcloud.ply", *cloud_normals);
171
- // cout<<"read succeed"<<endl;
172
155
std::cerr << " Cloud before deleting: " << std::endl;
173
156
std::cerr << *cloud_normals;
174
157
vector<pcl::PointNormal,Eigen::aligned_allocator<pcl::PointNormal>> &points = cloud_normals->points ;
@@ -390,57 +373,18 @@ void CCloudOptimization::run()
390
373
{
391
374
if (cloud_ms_normals->points [i].getNormalVector3fMap ().dot (
392
375
cloud_normals->points [(*indicesptr)[idx_in_orig[i]]].getNormalVector3fMap ()) < 0 )
393
- {
394
- reverse (cloud_ms_normals->points [i].normal_x );
395
- reverse (cloud_ms_normals->points [i].normal_y );
396
- reverse (cloud_ms_normals->points [i].normal_z );
397
- }
376
+ reverse_normal (cloud_ms_normals->points [i]);
398
377
}
399
378
}else {
400
379
#pragma omp parallel for
401
380
for (int i=0 ; i<idx_in_orig.size (); i++)
402
381
{
403
382
if (cloud_ms_normals->points [i].getNormalVector3fMap ().dot (
404
383
cloud_normals->points [idx_in_orig[i]].getNormalVector3fMap ()) < 0 )
405
- {
406
- reverse (cloud_ms_normals->points [i].normal_x );
407
- reverse (cloud_ms_normals->points [i].normal_y );
408
- reverse (cloud_ms_normals->points [i].normal_z );
409
- }
384
+ reverse_normal (cloud_ms_normals->points [i]);
410
385
}
411
386
}
412
-
413
- /*
414
- Eigen::Vector4f centroid;
415
- pcl::compute3DCentroid (*cloud_ms_normals,centroid);
416
- Eigen::Vector3f m_ObjectCenter = Eigen::Vector3f(centroid[0]-50, centroid[1], centroid[2]);
417
-
418
- #pragma omp parallel for
419
- for (int i=0; i<cloud_ms_normals->size(); i++)
420
- {
421
- Eigen::Vector3f current_point = cloud_ms_normals->points[i].getVector3fMap();
422
- Eigen::Vector3f current_direction_normed = m_ObjectCenter-current_point;
423
- current_direction_normed /= current_direction_normed.norm();
424
- float min_value = FLT_MAX;
425
- Eigen::Vector3f best_camdir;
426
- for (int j=0; j<m_ImageData->m_CameraNum; j++)
427
- {
428
- Eigen::Vector3f current_camdir = CamCenter[j]-current_point;
429
- float current_value = current_direction_normed.dot(current_camdir)/current_camdir.norm();
430
- if (current_value < min_value)
431
- {
432
- min_value = current_value;
433
- best_camdir = current_camdir;
434
- }
435
- }
436
- if (cloud_ms_normals->points[i].getNormalVector3fMap().dot(best_camdir) < 0)
437
- {
438
- reverse(cloud_ms_normals->points[i].normal_x);
439
- reverse(cloud_ms_normals->points[i].normal_y);
440
- reverse(cloud_ms_normals->points[i].normal_z);
441
- }
442
- }
443
- */
387
+ cloud_normals->clear ();
444
388
445
389
pcl::io::savePLYFileBinary (" tmp\\ bigcloud.ply" , *cloud_ms_normals);
446
390
// system("gen_normals.bat");
@@ -450,7 +394,7 @@ void CCloudOptimization::run()
450
394
// texture mapping
451
395
char cmd[MAX_PATH];
452
396
sprintf (cmd, " TextureStitcher.exe --in tmp\\ bigmesh.ply --scans scans.txt --out %s --useKD --verbose" , m_ImageData->outfilename .c_str ());
453
- system (cmd);
397
+ safe_process (cmd, 2e4 );
454
398
}
455
399
456
400
void texture_color (float x, float y, float z, uchar rgb[])
@@ -470,8 +414,27 @@ void texture_color(float x, float y, float z, uchar rgb[])
470
414
rgb[2 ] = 127 ;
471
415
return ;
472
416
}
473
- current_x *= 3 ;
474
- rgb[2 ] = current_img.ptr <uchar>(current_y)[current_x];
475
- rgb[1 ] = current_img.ptr <uchar>(current_y)[current_x+1 ];
476
- rgb[0 ] = current_img.ptr <uchar>(current_y)[current_x+2 ];
477
- }
417
+ uchar *base_ptr = current_img + (current_img_width * current_y + current_x) * 3 ;
418
+ rgb[2 ] = base_ptr[0 ];
419
+ rgb[1 ] = base_ptr[1 ];
420
+ rgb[0 ] = base_ptr[2 ];
421
+ }
422
+
423
+ void safe_process (char * cmdLine, DWORD max_time) {
424
+ TCHAR szCmdLine[MAX_PATH];
425
+ #ifdef UNICODE
426
+ MultiByteToWideChar (CP_ACP, 0 , cmdLine, -1 , szCmdLine, MAX_PATH);
427
+ #else
428
+ strcpy (szCmdLine, cmdLine);
429
+ #endif
430
+ STARTUPINFO si;
431
+ PROCESS_INFORMATION pi;
432
+ ZeroMemory (&si, sizeof (si));
433
+ si.cb = sizeof (si);
434
+ ZeroMemory (&pi, sizeof (pi));
435
+ if (CreateProcess (NULL , szCmdLine, 0 , 0 , FALSE , 0 , 0 , 0 , &si, &pi)) {
436
+ CloseHandle (pi.hThread );
437
+ WaitForSingleObject (pi.hProcess , max_time);
438
+ CloseHandle (pi.hProcess );
439
+ }
440
+ }
0 commit comments