-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.cpp
More file actions
403 lines (371 loc) · 11.8 KB
/
Copy pathfile.cpp
File metadata and controls
403 lines (371 loc) · 11.8 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "stdafx.h"
#include "file.h"
// 读取特征点输入文件
void readfilePoints(char * filename, Mat& points, Mat& imageX, Mat& imageY)
{
FILE * fp;
errno_t error;
error = fopen_s(&fp, filename, "r");
if(error != 0){
cout << "无法打开文件 " << filename << endl;
exit(1);
}
char str[SHRT_MAX];
const char* delim = " ";
char * ctx;
char * token;
int i=0, j=0;
///逐行读取
// fgets是从指定的流 stream 读取一行,并把它存储在 str 所指向的字符串内
while( fgets(str, LONG_MAX, fp) != NULL )
{
token = strtok_s( str, delim, &ctx); //按照空格分割
while( token != NULL )
{
// atof分割的字符串并将其分配给corrpoint
// atof:将字符串转化成double
if(j==0)
points.at<float>(i,0) = (float)atof(token);
else if(j==1)
points.at<float>(i,1) = (float)atof(token);
else if(j==2)
points.at<float>(i,2) = (float)atof(token);
else if(j%2==1 && j<(3+FRAMETOTAL*2))
imageX.at<float>(i,(j-3)/2) = (float)atof(token);
else if(j%2==0 && j<(3+FRAMETOTAL*2))
imageY.at<float>(i,(j-4)/2) = (float)atof(token);
token = strtok_s(NULL, delim, &ctx);
j++;
}
i++;
j=0;
}
fclose( fp );
}
////////////////////////////
// CorrPoint構造体初期化
//
void initCorrPoint(CorrPoint * corrpoint)
{
for(int i=0;i<KPtotal;i++)
{
corrpoint[i].x0 = (float *)malloc(sizeof(float)*FRAMETOTAL);
corrpoint[i].y0 = (float *)malloc(sizeof(float)*FRAMETOTAL);
}
}
////////////////////////////
// CorrPoint構造体free
//
void freeCorrPoint(CorrPoint * corrpoint)
{
for(int i=0;i<KPtotal;i++)
{
free(corrpoint[i].x0);
free(corrpoint[i].y0);
}
}
/////////////////////////////
//特徴点入力ファイル読み込み
//
void readfileCorr(char * filename, CorrPoint * corrpoint)
{
///---ファイル読み込み---
FILE * fp;
errno_t error;
error = fopen_s(&fp, filename, "r");
if(error != 0){
cout << "ファイルが開けません " << filename << endl;
exit(1);
}
char str[SHRT_MAX];
char* delim = new char(1);
*delim = ' ';
char * ctx;
char * token;
int i=0, j=0;
///---1行づつ読み込んでいく---
while( fgets(str, LONG_MAX, fp) != NULL ){
token = strtok_s( str, delim, &ctx); //半角スペースで分割
while( token != NULL ){
//分割した文字列をatofしてcorrpointに代入していく
if(j==0) corrpoint[i].x = (float)atof(token);
else if(j==1) corrpoint[i].y = (float)atof(token);
else if(j==2) corrpoint[i].z = (float)atof(token);
else if(j%2==1 && j<(3+FRAMETOTAL*2)) corrpoint[i].x0[(j-3)/2] = (float)atof(token);
else if(j%2==0 && j<(3+FRAMETOTAL*2)) corrpoint[i].y0[(j-4)/2] = (float)atof(token);
//else //何もしない
token = strtok_s(NULL, delim, &ctx); //2回目以降
j++;
}
i++;
j=0;
}
fclose( fp );
}
///////////////////////////////
// ファイル読み捨てて行数取得
//
int readfileLine(char * filename)
{
int total = 0;
FILE * fp;
errno_t error;
error = fopen_s(&fp, filename, "r");
if(error != 0){
cout << "ファイルが開けません " << filename << endl;
exit(1);
}
char s[SHRT_MAX];
while( fgets( s, SHRT_MAX, fp ) != NULL ){
total++;
}
return total;
}
/////////////////////////////
//並進・回転ファイル読み込み
//
void readfileRT(char * filename, Mat * rt)
{
// Open File Storage
cv::FileStorage cvfs(filename,CV_STORAGE_READ);
cv::FileNode node(cvfs.fs, NULL); // Get Top Node
cv::FileNode fn = node["mat_rt"]; //ノードを指定
for(int i=0;i<FRAMETOTAL;i++){
cv::read(fn[i], rt[i]);
}
}
/////////////////////////////////
//カメラ内部行列ファイル読み込み
//
void readfileCamera(char * filename, Mat * cameraA)
{
// Open File Storage
cv::FileStorage cvfs(filename,CV_STORAGE_READ);
cv::FileNode node(cvfs.fs, NULL); // Get Top Node
cv::FileNode fn = node["intrinsic"]; //ノードを指定
cv::read(fn, *cameraA);
}
///////////////////////////
// paraAファイル書き込み
//
void writefile(char * filename, float * paraA, WriteType type , float min, float max, Mat * rt, CorrPoint * corrpoint)
{
if(type==0)//XYZ
{
///---ファイル読み込み---
FILE * fp;
errno_t error;
error = fopen_s(&fp, filename, "w");
if(error != 0){
cout << "ファイルが開けません " << filename << endl;
exit(1);
}
//色つきかどうか
if(min == -1. && max == -1.){
for(int i=0;i<KPtotal;i++){
fprintf(fp,"%f ",paraA[i*3+1]);
fprintf(fp,"%f ",paraA[i*3+2]);
fprintf(fp,"%f",paraA[i*3+3]);
fprintf(fp,"\n");
}
}else{
int white = 255*256*256 + 255*256 + 255;
int arrowNum = 20;
//カメラ位置表示するかどうか
if(rt != NULL){
fprintf(fp,"# .PCD v.7 - Point Cloud Data file format\nVERSION .7\nFIELDS x y z rgb\nSIZE 4 4 4 4\nTYPE F F F F\nCOUNT 1 1 1 1\nWIDTH %d\nHEIGHT 1\nVIEWPOINT 0 0 0 1 0 0 0\nPOINTS %d\nDATA ascii\n", KPtotal+FRAMETOTAL*arrowNum, KPtotal+FRAMETOTAL*arrowNum);
for(int i=0;i<FRAMETOTAL;i++){
/*
Mat camera = Mat::eye(4,4,CV_32F);
Mat temp = Mat::eye(4,4,CV_32F);
for(int j=0;j<i+1;j++){
Mat r = rt[j].operator()(Range(0,3),Range(0,3));
Mat t = rt[j].operator()(Range(3,4),Range(0,3));
temp.at<float>(0,0) = r.at<float>(0,0);
temp.at<float>(0,1) = r.at<float>(0,1);
temp.at<float>(0,2) = r.at<float>(0,2);
temp.at<float>(1,0) = r.at<float>(1,0);
temp.at<float>(1,1) = r.at<float>(1,1);
temp.at<float>(1,2) = r.at<float>(1,2);
temp.at<float>(2,0) = r.at<float>(2,0);
temp.at<float>(2,1) = r.at<float>(2,1);
temp.at<float>(2,2) = r.at<float>(2,2);
temp.operator()(Range(0,3),Range(3,4)) = t.t();
camera = camera * temp;
}
//cout << camera << endl;
for(int j=0;j<=arrowNum;j++){
fprintf(fp,"%f ",camera.at<float>(3,0)+(float)(arrowNum-j)/arrowNum*camera.at<float>(2,0)*10);
fprintf(fp,"%f ",camera.at<float>(3,1)+(float)(arrowNum-j)/arrowNum*camera.at<float>(2,1)*10);
fprintf(fp,"%f ",camera.at<float>(3,2)+(float)(arrowNum-j)/arrowNum*camera.at<float>(2,2)*10);
fprintf(fp,"%d",white);
fprintf(fp,"\n");
}
*/
for(int j=0;j<=arrowNum;j++){
fprintf(fp,"%f ",rt[i].at<float>(3,0)+(float)(arrowNum-j)/arrowNum*rt[i].at<float>(0,2)*10);
fprintf(fp,"%f ",rt[i].at<float>(3,1)+(float)(arrowNum-j)/arrowNum*rt[i].at<float>(1,2)*10);
fprintf(fp,"%f ",rt[i].at<float>(3,2)+(float)(arrowNum-j)/arrowNum*rt[i].at<float>(2,2)*10);
fprintf(fp,"%d",white-40*i);
fprintf(fp,"\n");
}
}
}else{
fprintf(fp,"# .PCD v.7 - Point Cloud Data file format\nVERSION .7\nFIELDS x y z rgb\nSIZE 4 4 4 4\nTYPE F F F F\nCOUNT 1 1 1 1\nWIDTH %d\nHEIGHT 1\nVIEWPOINT 0 0 0 1 0 0 0\nPOINTS %d\nDATA ascii\n", KPtotal, KPtotal);
}
int red = 255*256*256;
if(corrpoint == NULL){
for(int i=0;i<KPtotal;i++){
fprintf(fp,"%f ",paraA[i*3+1]);
fprintf(fp,"%f ",paraA[i*3+2]);
fprintf(fp,"%f ",paraA[i*3+3]);
fprintf(fp,"%d",(int)(red + 256*(int)(255*(1.-(paraA[i*3+3]-min)/(max-min)))));
fprintf(fp,"\n");
}
}else{
for(int i=0;i<KPtotal;i++){
fprintf(fp,"%f ",paraA[i*3+1]);
fprintf(fp,"%f ",paraA[i*3+2]);
fprintf(fp,"%f ",paraA[i*3+3]);
int frame=0;
for(int j=0;j<FRAMETOTAL;j++){
if(corrpoint[i].x0[j] != -999.){
frame = j;
break;
}
}
fprintf(fp,"%d",(int)(red + 256*(int)(255*((float)frame/(FRAMETOTAL-1)))));
fprintf(fp,"\n");
}
}
}
fclose(fp);
}
}
///////////////////
//Mat書き出し
//
void writefileMat(char * filename, Mat * mat)
{
// Open File Storage
cv::FileStorage cvfs(filename,CV_STORAGE_WRITE);
//cv::WriteStructContext ws(cvfs, "mat", CV_NODE_SEQ); // create node
for(int i=0; i<FRAMETOTAL; i++){
cv::write(cvfs,"",mat[i]);
}
}
///////////////////
//Mat書き出し
//
void writefileDistort(char * filename, Mat Distort)
{
FILE * fp;
errno_t error;
error = fopen_s(&fp, filename, "w");
if(error != 0){
cout << "ファイルが開けません " << filename << endl;
exit(1);
}
fprintf(fp,"K1 K2 K3 P1 P2\n\n");
for(int i=0;i<FRAMETOTAL;i++){
float K1 = Distort.at<float>(0,i);
float K2 = Distort.at<float>(1,i);
float P1 = Distort.at<float>(2,i);
float P2 = Distort.at<float>(3,i);
float K3 = Distort.at<float>(4,i);
fprintf(fp,"フレーム%d\n",i);
fprintf(fp,"%e\t%e\t%e\t%e\t%e\n\n",K1,K2,K3,P1,P2);
}
fclose(fp);
}
///////////////////////////
// 3次元点群書き込み
//
void writefilePoints(char * filename, Mat& points, int pointTotal)
{
///---ファイル読み込み---
FILE * fp;
errno_t error;
error = fopen_s(&fp, filename, "w");
if(error != 0){
cout << "ファイルが開けません " << filename << endl;
exit(1);
}
for(int i=0;i<pointTotal;i++){
fprintf(fp,"%f,",points.at<float>(i,0));
fprintf(fp,"%f,",points.at<float>(i,1));
fprintf(fp,"%f",points.at<float>(i,2));
fprintf(fp,"\n");
}
// */
fclose(fp);
}
///////////////////////////
// 3次元点群書き込み
//
void writefilePoints(char * filename, Mat& points, int pointTotal, int frameTotal, float min, float max, Mat * R, Mat * T)
{
///---ファイル読み込み---
FILE * fp;
errno_t error;
error = fopen_s(&fp, filename, "w");
if(error != 0){
cout << "ファイルが開けません " << filename << endl;
exit(1);
}
int red = 255*256*256;
int green = 255*256*256 + 255*256;
int white = 255*256*256 + 255*256 + 255;
int arrowNum = 20;
/*
if(R != NULL){
///カメラ位置表示する
fprintf(fp,"# .PCD v.7 - Point Cloud Data file format\nVERSION .7\nFIELDS x y z rgb\nSIZE 4 4 4 4\nTYPE F F F F\nCOUNT 1 1 1 1\nWIDTH %d\nHEIGHT 1\nVIEWPOINT 0 0 0 1 0 0 0\nPOINTS %d\nDATA ascii\n", pointTotal+frameTotal*(arrowNum+1), pointTotal+frameTotal*(arrowNum+1));
for(int i=0;i<frameTotal;i++){
for(int j=0;j<=arrowNum;j++){
fprintf(fp,"%f ",T[i].at<float>(0,0)+(float)(arrowNum-j)/arrowNum*R[i].at<float>(0,2)*5);
fprintf(fp,"%f ",T[i].at<float>(1,0)+(float)(arrowNum-j)/arrowNum*R[i].at<float>(1,2)*5);
fprintf(fp,"%f ",T[i].at<float>(2,0)+(float)(arrowNum-j)/arrowNum*R[i].at<float>(2,2)*5);
fprintf(fp,"%d",white-40*i);
fprintf(fp,"\n");
}
}
}else{
///カメラ位置表示しない
fprintf(fp,"# .PCD v.7 - Point Cloud Data file format\nVERSION .7\nFIELDS x y z rgb\nSIZE 4 4 4 4\nTYPE F F F F\nCOUNT 1 1 1 1\nWIDTH %d\nHEIGHT 1\nVIEWPOINT 0 0 0 1 0 0 0\nPOINTS %d\nDATA ascii\n", pointTotal, pointTotal);
}
for(int i=0;i<pointTotal;i++){
fprintf(fp,"%f ",points.at<float>(i,0));
fprintf(fp,"%f ",points.at<float>(i,1));
fprintf(fp,"%f ",points.at<float>(i,2));
fprintf(fp,"%d",(int)(red + 256*(int)(255*(1.-(points.at<float>(i,2)-min)/(max-min)))));
fprintf(fp,"\n");
}
// */
///*
if(R != NULL){
///カメラ位置表示する
fprintf(fp,"# .PCD v.7 - Point Cloud Data file format\nVERSION .7\nFIELDS x y z rgb\nSIZE 4 4 4 4\nTYPE F F F F\nCOUNT 1 1 1 1\nWIDTH %d\nHEIGHT 1\nVIEWPOINT 0 0 0 1 0 0 0\nPOINTS %d\nDATA ascii\n", pointTotal+frameTotal*(arrowNum+1), pointTotal+frameTotal*(arrowNum+1));
for(int i=0;i<frameTotal;i++){
for(int j=0;j<=arrowNum;j++){
fprintf(fp,"%f ",T[i].at<float>(0,0)+(float)(arrowNum-j)/arrowNum*R[i].at<float>(0,2)*5);
fprintf(fp,"%f ",T[i].at<float>(1,0)+(float)(arrowNum-j)/arrowNum*R[i].at<float>(1,2)*5);
fprintf(fp,"%f ",T[i].at<float>(2,0)+(float)(arrowNum-j)/arrowNum*R[i].at<float>(2,2)*5);
fprintf(fp,"%d",white);
fprintf(fp,"\n");
}
}
}else{
///カメラ位置表示しない
fprintf(fp,"# .PCD v.7 - Point Cloud Data file format\nVERSION .7\nFIELDS x y z rgb\nSIZE 4 4 4 4\nTYPE F F F F\nCOUNT 1 1 1 1\nWIDTH %d\nHEIGHT 1\nVIEWPOINT 0 0 0 1 0 0 0\nPOINTS %d\nDATA ascii\n", pointTotal, pointTotal);
}
for(int i=0;i<pointTotal;i++){
fprintf(fp,"%f ",points.at<float>(i,0));
fprintf(fp,"%f ",points.at<float>(i,1));
fprintf(fp,"%f ",points.at<float>(i,2));
fprintf(fp,"%d",white);
fprintf(fp,"\n");
}
// */
fclose(fp);
}