-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·451 lines (393 loc) · 12.5 KB
/
main.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
* main.cpp
*
* Created on: Mar 23, 2010
* Author: zuow
*/
#include "Image.h"
#include "GeometryRectify.h"
#include "GcpMatch.h"
#include "ProjectWGS84.h"
#include "Dimap.h"
#include "BaseDefine.h"
#include <sstream>
#define DEBUG_OC
//#define DEBUG_FGC
//#define GEODETIC_COORDINATE
//#define IMAGE_LD
#define PATH_DEMILE "/DPS/DEM/"
using namespace std;
// 加载GCP数据
int LoadGMData(char* szFilename, double *&pSrcX, double *&pSrcY, double *&pSrcZ,
double *&pDstX, double *&pDstY, int& iPointCount, int nZone, int nSrcRows)
{
#ifdef GEODETIC_COORDINATE
int iZone = nZone;
CProjectWGS84 project;
project.CreateProject("WGS84");
project.SetZone(iZone);
#endif
FILE* pInputFile = fopen(szFilename, "r");
printf("Load GM pairs from %s.",szFilename);
if(pInputFile != NULL)
{
char buffer[512];
memset(buffer, 0, 512*sizeof(char));
fgets(buffer, 512, pInputFile);
iPointCount = atoi(buffer);
pSrcX = new double[iPointCount]; pSrcY = new double[iPointCount]; pSrcZ = new double[iPointCount];
pDstX = new double[iPointCount]; pDstY = new double[iPointCount];
double X, Y;
for(int i = 0; i < iPointCount; i++)
{
fgets(buffer, 512, pInputFile);
sscanf( buffer, "%*s%lf%lf%lf%lf%lf", &(pSrcX[i]), &(pSrcY[i]), &(pSrcZ[i]),
&(pDstX[i]), &(pDstY[i]) );
/*
#ifdef IMAGE_LD // 左下角坐标系
pDstY[i] = nSrcRows-pDstY[i];
#endif
*/
printf("Load %d point is %lf,%lf,%lf,%lf \n",i+1,pSrcX[i], pSrcY[i], pDstX[i], pDstY[i]);
// 对GCP数据进行转换
#ifdef GEODETIC_COORDINATE
project.Geodetic2Map(pSrcY[i] * PI/180.0, pSrcX[i] * PI/180.0, &X, &Y);
printf("Transfer source is %lf,%lf,target is %lf,%lf \n",pSrcX[i],pSrcY[i],X,Y);
pSrcX[i]=X; pSrcY[i]=Y;
#endif
printf("GCP projection coordinate: %lf %lf %lf %lf %lf \n", pSrcX[i], pSrcY[i], pSrcZ[i],
pDstX[i], pDstY[i]);
}
fclose(pInputFile);
}
return 0;
}
// 进行GCP匹配 [ZuoW,2010/5/13]
int GCPMatch(char* szGCPDir, char* srcImage, char* szReGMFile, double* &pMatchGX, double* &pMatchGY, double* &pMatchGZ, double* &pMatchIX, double* &pMatchIY, int* pGCPNum)
{
int i;
HRESULT hRes;
int pointNum = *pGCPNum;
char szGcpFiles[25600]; memset(szGcpFiles, 0, 25600);
for(i=0; i<pointNum; i++)
{
char szTemp[32]; memset(szTemp, 0, 32);
sprintf(szTemp, "%s%d.tif;", szGCPDir, i);
strcat(szGcpFiles, szTemp);
}
double* pGX = new double[pointNum];
double* pGY = new double[pointNum];
double* pIX = new double[pointNum];
double* pIY = new double[pointNum];
unsigned int* pSuccess = new unsigned int[pointNum];
double dMaskWindow = 200;
CGcpMatch GcpMatch;
hRes = GcpMatch.Match(srcImage, szGcpFiles, dMaskWindow, pGX, pGY, pIX, pIY, &pointNum, pSuccess);
if(hRes==S_FALSE || pointNum<6)
{
delete [] pGX; pGX = NULL;
delete [] pGY; pGY = NULL;
delete [] pIX; pIX = NULL;
delete [] pIY; pIY = NULL;
delete [] pSuccess; pSuccess = NULL;
return -1;
}
pMatchGX = new double[pointNum]; memset(pMatchGX, 0, pointNum*sizeof(double));
pMatchGY = new double[pointNum]; memset(pMatchGY, 0, pointNum*sizeof(double));
pMatchGZ = new double[pointNum]; memset(pMatchGZ, 0, pointNum*sizeof(double));
pMatchIX = new double[pointNum]; memset(pMatchIX, 0, pointNum*sizeof(double));
pMatchIY = new double[pointNum]; memset(pMatchIY, 0, pointNum*sizeof(double));
int cpNum = 0;
for(i=0; i<pointNum; i++)
{
if(pSuccess[i])
{
pMatchGX[cpNum] = pGX[i];
pMatchGY[cpNum] = pGY[i];
pMatchIX[cpNum] = pIX[i];
pMatchIY[cpNum] = pIY[i];
cpNum++;
}
}
delete [] pGX; pGX = NULL;
delete [] pGY; pGY = NULL;
delete [] pIX; pIX = NULL;
delete [] pIY; pIY = NULL;
delete [] pSuccess; pSuccess = NULL;
CGeometryRectify CalMatchErr;
double dMatchErr=0.0f;
int nMaxErrPoint;
double dGCPMed = 4;
CalMatchErr.InitPolyTransMatrix(pMatchGX, pMatchGY, pMatchIX, pMatchIY,
cpNum, &dMatchErr, dGCPMed);
double dPointErr,dMatchIX,dMatchIY;
while(dMatchErr>=dGCPMed)
{
CalMatchErr.PolyTrans(pMatchGX[0],pMatchGY[0],&dMatchIX,&dMatchIY);
dMatchErr=sqrt((dMatchIX-pMatchIX[0])*(dMatchIX-pMatchIX[0])+(dMatchIY-pMatchIY[0])*(dMatchIY-pMatchIY[0]));
nMaxErrPoint=0;
for(int j=0;j<cpNum;j++)
{
CalMatchErr.PolyTrans(pMatchGX[j],pMatchGY[j],&dMatchIX,&dMatchIY);
dPointErr=sqrt((dMatchIX-pMatchIX[j])*(dMatchIX-pMatchIX[j])+(dMatchIY-pMatchIY[j])*(dMatchIY-pMatchIY[j]));
if(dMatchErr<dPointErr)
{
nMaxErrPoint=j;
dMatchErr=dPointErr;
}
}
cpNum--;
for(int j=nMaxErrPoint;j<cpNum;j++)
{
pMatchGX[j]=pMatchGX[j+1];
pMatchGY[j]=pMatchGY[j+1];
pMatchIX[j]=pMatchIX[j+1];
pMatchIY[j]=pMatchIY[j+1];
}
if(cpNum<6)
{
delete [] pMatchGX; pMatchGX = NULL;
delete [] pMatchGY; pMatchGY = NULL;
delete [] pMatchGZ; pMatchGZ = NULL;
delete [] pMatchIX; pMatchIX = NULL;
delete [] pMatchIY; pMatchIY = NULL;
return -1;
}
CalMatchErr.InitPolyTransMatrix( pMatchGX,pMatchGY,pMatchIX,pMatchIY,cpNum, &dMatchErr, 3);
}
// 获取高程
CProjectWGS84 project;
project.CreateProject("WGS84");
project.SetZoneStep(6);
long nZone = 50;
project.SetZone(nZone);
project.SetFalseEasting(500000);
project.SetFalseNorthing(0);
float px=0.0, py=0.0;
double latitude,longitude;
char sdemFilename[256];
CImage image;
int iBPB = 1;
for(i = 0; i < cpNum; i++)
{
project.Map2Geodetic(pMatchGX[i], pMatchGY[i], &latitude, &longitude);
latitude = latitude * 180.0 / PI;
longitude = longitude * 180.0 / PI;
strcpy(sdemFilename,"");
sprintf(sdemFilename,"%sZ_%d_%d.TIF",PATH_DEMILE,(int)((longitude+180+4.99999999)/5),
(int)((60-latitude+4.99999999)/5));
if(access(sdemFilename,00)==-1)
{
pMatchGZ[i]=-99999;
}
else
{
image.Open(sdemFilename,modeRead);
image.World2Image(latitude, longitude, &px, &py);
image.GetBPB(&iBPB);
double xStart,yStart,CellSize;
image.GetGrdInfo(&xStart,&yStart,&CellSize);
BYTE *pZValues = new BYTE[iBPB];
image.GetGrayF(px, py, 0, pZValues, 0);
pMatchGZ[i] = *((short *)pZValues);
if(pMatchGZ[i]==-32768)
pMatchGZ[i]=-99999;
image.Close();
// Clear
if(pZValues!=NULL)
{
delete []pZValues;
pZValues = NULL;
}
}
// 转换成经纬度
pMatchGY[i] = longitude;
pMatchGX[i] = latitude;
}
// 输出GCP到文件[ZuoW,2010/5/14]
FILE *fp = fopen(szReGMFile, "wt");
if(fp!=NULL)
{
fprintf(fp, "%d\n", cpNum);
for(i=0;i<cpNum;i++)
{
fprintf(fp,"%04d\t%25.6lf%25.6lf%25.6lf%25.6lf%25.6lf\n",
i+1,pMatchGY[i], pMatchGX[i], pMatchGZ[i], pMatchIX[i],pMatchIY[i]);
fflush(fp);
}
fclose(fp);
}
*pGCPNum = cpNum;
return 0;
}
int Transfer(char* szCheckPoint, char* szSrcImg, char* szOutputFile)
{
// 打开源影像
int nSrcRow, nSrcCol;
double dLBX, dLBY, dCell;
double *pIX, *pIY, *pSrcX, *pSrcY;
CImage srcImage;
if(srcImage.Open(szSrcImg, modeRead)!=S_OK)
{
printf("Open source image failed!\n");
return S_FALSE;
}
srcImage.GetRows(&nSrcRow);
srcImage.GetCols(&nSrcCol);
srcImage.GetGrdInfo(&dLBX, &dLBY, &dCell);
srcImage.Close();
FILE* pInputFile = fopen(szCheckPoint, "r");
FILE* pOutputFile = fopen(szOutputFile, "w+");
if(pInputFile!=NULL && pOutputFile!=NULL)
{
char buffer[512];
memset(buffer, 0, 512*sizeof(char));
fgets(buffer, 512, pInputFile);
int iPointCount = atoi(buffer);
pIX = new double[iPointCount]; pIY = new double[iPointCount];
pSrcX = new double[iPointCount]; pSrcY = new double[iPointCount];
for(int i = 0; i < iPointCount; i++)
{
double dX = 0.; double dY = 0.;
fgets(buffer, 512, pInputFile);
sscanf( buffer, "%*s%lf%lf%lf%lf", &(pIX[i]), &(pIY[i]), &(pSrcX[i]), &(pSrcY[i]));
printf("Load %d point is %lf,%lf,%lf,%lf \n",i+1,pIX[i], pIY[i], pSrcX[i], pSrcY[i]);
// 获取纠正影像坐标
dX = dLBX + pIX[i]*dCell;
#ifdef IMAGE_LD
dY = dLBY + (nSrcRow-pIY[i])*dCell;
#else
dY = dLBY + pIY[i]*dCell;
#endif
printf("Check GCP coordinate: %lf %lf %lf %lf \n", dX, dY, pSrcX[i], pSrcY[i]);
double offX = (pSrcX[i]-dX)/dCell; double offY = (pSrcY[i]-dY)/dCell;
fprintf(pOutputFile, "%d, %d, %d, %16.3f, %16.3f, %16.3f, %16.3f, %5.3f, %5.3f \n",
i+1, (int)(pIX[i]), (int)(pIY[i]), dX, dY, pSrcX[i], pSrcY[i], offX, offY);
}
fclose(pInputFile);
fclose(pOutputFile);
}
return 0;
}
#ifdef DEBUG
int main()
{
HRESULT hRes;
string inputpath = "/dps/workdir/CCD2/LEVEL3/374/";
string inputfile = "/dps/workdir/CCD2/LEVEL3/374/GC_report.txt";
int nMaskWindow = 30;
int nGcpNum = 630;
std::string strGCPFiles;
std::stringstream ioStream;
for( int num = 0;num<nGcpNum; num++)
{
ioStream<<inputpath<<"TestData/"<<num<<".tif;";
}
strGCPFiles = ioStream.str();
double* pGX = new double[nGcpNum];
double* pGY = new double[nGcpNum];
double* pIX = new double[nGcpNum];
double* pIY = new double[nGcpNum];
BOOL* pbSucceeded = new BOOL[nGcpNum];
memset(pbSucceeded, 0, sizeof(BOOL) * nGcpNum);
CGcpMatch GcpMatch;
hRes = GcpMatch.Match((char*)(inputfile.c_str()), (char*)(strGCPFiles.c_str()), nMaskWindow,
pGX, pGY, pIX, pIY, &nGcpNum, pbSucceeded);
/*
// GCP data
double* pSrcX=NULL; double* pSrcY=NULL; double* pSrcZ=NULL;
double* pDstX=NULL; double* pDstY=NULL;
int iPointCount = 0;
int ret = 0;
char szWorkDir[128]; memset(szWorkDir, 0, 128);
char szGMFile[256]; memset(szGMFile, 0, 256);
char szDEMFile[256]; memset(szDEMFile, 0, 256);
char szInputFile[256]; memset(szInputFile, 0, 256);
char szOutputFile[256]; memset(szOutputFile, 0, 256);
char szGCPDir[256]; memset(szGCPDir, 0, 256);
char szReGMFile[256]; memset(szReGMFile, 0, 256);
strcpy(szWorkDir, "/dps/workdir/CCD2/LEVEL3/376/");
strcpy(szGMFile, szWorkDir); strcat(szGMFile, "GCP_Pair.txt");
strcpy(szDEMFile, szWorkDir); strcat(szDEMFile, "TestOC.dem");
strcpy(szGCPDir, szWorkDir); strcat(szGCPDir, "TestData/");
strcpy(szReGMFile, szWorkDir); strcat(szReGMFile, "ReGM_gcp_pair.txt");
#ifdef DEBUG_FGC
strcpy(szInputFile, szWorkDir); strcat(szInputFile, "GC_report_4.txt");
strcpy(szOutputFile, szWorkDir); strcat(szOutputFile, "FGC_img_data_4.tif");
char szTemp[256];
strcpy(szTemp, szWorkDir); strcat(szTemp, "FGC_temp.tif");
// strcpy(szInputFile, "/dps/workdir/CCD2/LEVEL3/376/transfer/376_check_0517_auto.txt");
// strcpy(szOutputFile, "/dps/workdir/CCD2/LEVEL3/376/transfer/376level4_Result1.tif");
// strcpy(szTemp, "/dps/workdir/CCD2/LEVEL3/376/transfer/376_check.txt");
// Transfer(szInputFile, szOutputFile, szTemp);
#endif
#ifdef DEBUG_OC
char strApAuxdata[128]; char strApEphData[128]; char strOpAttData[128];
strcpy(szInputFile, szWorkDir); strcat(szInputFile, "OCTemp_4.tif");
strcpy(szOutputFile, szWorkDir); strcat(szOutputFile, "OCOutputFile_4.tif");
strcpy(strApAuxdata, szWorkDir); strcat(strApAuxdata, "AP_aux_data.txt");
strcpy(strApEphData, szWorkDir); strcat(strApEphData, "AP_eph_data.txt");
strcpy(strOpAttData, szWorkDir); strcat(strOpAttData, "OP_att_data.txt");
#define CAMERA_FILENAME "/DPS/Config/OPP/OC/Camera.ini"
double* pMatchBandParam;
#endif
// 打开源影像
int nSrcRow, nSrcCol, nZone=50;
double dLBX, dLBY, dRTX, dRTY, dCell;
// 打开源影像
CImage srcImage;
if(srcImage.Open(szInputFile, modeRead)!=S_OK)
{
printf("Open source image failed!\n");
return S_FALSE;
}
srcImage.GetRows(&nSrcRow);
srcImage.GetCols(&nSrcCol);
srcImage.GetGrdInfo(&dLBX, &dLBY, &dCell);
// 解析输入配置文件并读取输入数据
ret = LoadGMData(szGMFile, pSrcX, pSrcY, pSrcZ, pDstX, pDstY, iPointCount, nZone, nSrcRow);
if(ret!=0)
{
printf("Load GCP data failed!\n");
return -1;
}
// 进行影像校正
CGeometryRectify GeometryRectify;
#ifdef DEBUG_FGC
unsigned int iFGCMode = 5;
unsigned int iResample = 1;
dLBX = dLBY = dRTX = dRTY = 0.0;
dCell = 30.0;
// hRes = GeometryRectify.ImageTinRectify(szInputFile, szTemp,
// pDstX, pDstY, pSrcX, pSrcY, iPointCount,
// (UINT)iFGCMode, (UINT)iResample,
// dLBX, dLBY, dRTX, dRTY, dCell);
hRes = GeometryRectify.ImageTinRectify(szInputFile, szOutputFile, szDEMFile,
pDstX, pDstY, pSrcX, pSrcY, pSrcZ, iPointCount,
iFGCMode, iResample,
dLBX, dLBY, dRTX, dRTY, dCell);
delete [] pSrcX; pSrcX = NULL;
delete [] pSrcY; pSrcY = NULL;
delete [] pDstX; pDstX = NULL;
delete [] pDstY; pDstY = NULL;
#endif
#ifdef DEBUG_OC
hRes = GeometryRectify.reOrthoRectifyHJCCD(pDstX, pDstY, pSrcX, pSrcY, pSrcZ, iPointCount,
strApAuxdata, strApEphData, strOpAttData, CAMERA_FILENAME,
szDEMFile, szInputFile, szOutputFile,
pMatchBandParam, (UINT)2, (UINT)1);
#endif
if(hRes!=S_OK)
{
printf("Image rectify failed!\n");
return -1;
}
else
{
printf("Output params:LB/RT X/Y and cellsize: %f, %f, %f, %f, %f\n",
dLBX, dLBY, dRTX, dRTY, dCell);
}
*/
return 0;
}
#endif