-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaircraftDetection.h
278 lines (229 loc) · 9.27 KB
/
aircraftDetection.h
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
#include <algorithm>
#include <numeric>
#include "baseIO.h"
#include "SLSearch.h"
#include "genShpIMG.h"
#ifndef AIRCRAFTDETECTION_Header_AD
#define AIRCRAFTDETECTION_Header_AD
class GenTypes
{
public:
GenTypes(bool image = false, bool shp = true, bool Sobel = false, bool Lap = false, bool SL = false, bool resultQA = false) :
Sobel(Sobel), Lap(Lap), SL(SL), resultQA(resultQA), shp(shp), image(image) {};
bool genSobel()const;
bool genLap()const;
bool genSL()const;
bool genresultQA()const;
bool genshp()const;
bool genimage()const;
void setSobel(bool status = true);
void setLap(bool status = true);
void setSL(bool status = true);
void setresultQA(bool status = true);
void setshp(bool status = true);
void setimage(bool status = true);
~GenTypes();
private:
bool Sobel;
bool Lap;
bool SL;
bool resultQA;
bool shp;
bool image;
};
class HandleInput
{
public:
HandleInput(GenTypes *genTypes, std::string inPathStr138 = "", std::string inPathStr21 = "", std::string outPathStr = "")
: genTypes(genTypes), inPathStr138(""), inPathStr21(""), outPathStr("") {}
int parseArguments(int argc, char *argv[]);
std::string inPath138()const;
std::string inPath21()const;
std::string inPathVapor()const;
std::string outPath()const;
void usage()const;
~HandleInput();
private:
GenTypes *genTypes;
std::string inPathStr138;
std::string inPathStr21;
std::string inPathStrVapor;
std::string outPathStr;
};
template<typename T>
void centerEdgeVerify(std::vector<int> &finalAircraftsIndex, const int rows, const int cols, const OLIData<T>& dataInfo, BYTE *finalAircraft)
{
int topPad = 5;
int col, row;
int count = finalAircraftsIndex.size();
for (int tmpNum = 0; tmpNum < count; ++tmpNum)
{
std::vector<int> totalRef;
if (finalAircraftsIndex[tmpNum] == 0) continue;
col = finalAircraftsIndex[tmpNum] % cols;
row = floor(finalAircraftsIndex[tmpNum] / float(cols));
// get 10*10 pixels around the center point
for (int tmpRow = row - topPad; tmpRow < row + topPad && tmpRow >= 0; ++tmpRow)
{
for (int tmpCol = col - topPad; tmpCol < col + topPad && tmpCol >= 0; ++tmpCol)
{
if ((tmpRow == row - 1) || (tmpRow == row) || (tmpRow == row + 1) ||
(tmpCol == col - 1) || (tmpCol == col) || (tmpCol == col + 1)) continue;
totalRef.push_back(dataInfo.GetValue()[tmpRow*cols + tmpCol]);
}
}
double sum = std::accumulate(std::begin(totalRef), std::end(totalRef), 0.0);
double mean = sum / totalRef.size();
if (dataInfo.GetValue()[row*cols + col] - mean < 200.0)
{
finalAircraftsIndex[tmpNum] = 0;
finalAircraft[row*cols + col] = 0;
}
// Cloud Shadow Judgment Only for South America
// If there is a pixel in the four fields of the maximum value that is less
// than the mean value in the range of 10*10, it is considered as a small cloud spot.
std::vector<double> centerLatLon = dataInfo.GetCenterLatLon();
if( !((centerLatLon[0] >= -66 && centerLatLon[0] <= 0) && (centerLatLon[1] >= -100 && centerLatLon[1] <= -20)) ||
((centerLatLon[0] >= -33 && centerLatLon[0] <= -5) && (centerLatLon[1] >= 0 && centerLatLon[1] <= 52)) ||
((centerLatLon[0] >= 0 && centerLatLon[0] <= 6) && (centerLatLon[1] >= -61 && centerLatLon[1] <= -47))
)
{
// The ratio of the four points of the central point to the mean of the 10*10 pixels > 0.02 is considered valid
// left and right up and down
int avaiableCount = 0;
if ((static_cast<double>(dataInfo.GetValue()[row*cols + col - 1]) / mean) > 1.025) ++avaiableCount;
if ((static_cast<double>(dataInfo.GetValue()[row*cols + col + 1]) / mean) > 1.025) ++avaiableCount;
if ((static_cast<double>(dataInfo.GetValue()[(row - 1)*cols + col]) / mean) > 1.025) ++avaiableCount;
if ((static_cast<double>(dataInfo.GetValue()[(row + 1)*cols + col]) / mean) > 1.025) ++avaiableCount;
if (avaiableCount <= 1)
{
finalAircraftsIndex[tmpNum] = 0;
finalAircraft[row*cols + col] = 0;
}
avaiableCount = 0;
if (dataInfo.GetValue()[row*cols + col - 1] < mean || dataInfo.GetValue()[row*cols + col + 1] < mean ||
dataInfo.GetValue()[(row - 1)*cols + col] < mean || dataInfo.GetValue()[(row + 1)*cols + col] < mean)
{
finalAircraftsIndex[tmpNum] = 0;
finalAircraft[row*cols + col] = 0;
}
}
}
}
// 2.1 micro-meter channel to enhance and remove false alarm caused by low vapor content or high altitude
template<typename T>
void enhanceBy21Channel(std::vector<int> &finalAircraftsIndex, const int rows, const int cols, const OLIData<T>& dataInfo, BYTE *finalAircraft)
{
int topPad = 7;
int col, row;
int count = finalAircraftsIndex.size();
for (int tmpNum = 0; tmpNum < count; ++tmpNum)
{
std::vector<int> totalRef, totalRefBak;
col = finalAircraftsIndex[tmpNum] % cols;
row = floor(finalAircraftsIndex[tmpNum] / float(cols));
// get 10*10 pixels around the center point
for (int tmpRow = row - topPad; tmpRow < row + topPad && tmpRow >= 0; ++tmpRow)
{
for (int tmpCol = col - topPad; tmpCol < col + topPad && tmpCol >= 0; ++tmpCol)
{
totalRef.push_back( dataInfo.GetValue()[tmpRow*cols + tmpCol] );
}
}
// histogram
std::sort(totalRef.begin(), totalRef.end());
totalRefBak = totalRef;
totalRef.erase(std::unique(totalRef.begin(), totalRef.end()), totalRef.end());
std::vector<double> countRef;
double pixelsCount = static_cast<double>(totalRefBak.size());
int threshold = 0;
for each (int ref in totalRef)
{
countRef.push_back(std::count(totalRefBak.begin(), totalRefBak.end(), ref));
// Get the value of position 0.95
if (static_cast<double>(std::accumulate(countRef.begin(), countRef.end(), 0)) / pixelsCount > 0.95)
{
threshold = ref;
break;
}
}
//int tmpPos = static_cast<int>(totalRef.size() * 0.9);
//if (tmpPos == 0) continue;
if (dataInfo.GetValue()[row*cols + col] > threshold)
{
//std::cout << row << " " << col << std::endl;
finalAircraftsIndex[tmpNum] = 0;
finalAircraft[row*cols + col] = 0;
}
}
}
template<typename T>
double getCenterPointVapor(const std::string& inPathVapor, const OLIData<T> &dataInfo138)
{
// LC81210362017120LGN00_B9
// C:\\Users\\xialang2012\\Desktop\\aircraftDetection\\vapor/2017_04_12_00.tif"
// C:\\Users\\xialang2012\\Desktop\\aircraftDetection\\vapor
std::string inVaporFile = inPathVapor + "/" + dataInfo138.GetDateYear() + "_" + dataInfo138.GetDateMonth() + "_" + dataInfo138.GetDateDay() + "_06.tif";
#ifdef WIN32
if ((_access(inVaporFile.c_str(), 0)) == -1)
{
return -1;
}
#elif linux
if ((access(inVaporFile.c_str(), 0)) == -1)
{
return -1;
}
#endif
std::vector<double> vapor;
BandBase<short> dataInfoVapor(inVaporFile);
dataInfoVapor.openBandData();
double *adfGeoTransform = dataInfoVapor.GetGT();
// center point
double dProjX = dataInfo138.GetCenterLatLon()[1];
double dProjY = dataInfo138.GetCenterLatLon()[0];
double dTemp = adfGeoTransform[1] * adfGeoTransform[5] - adfGeoTransform[2] * adfGeoTransform[4];
double dCol = 0.0, dRow = 0.0;
dCol = (adfGeoTransform[5] * (dProjX - adfGeoTransform[0]) - adfGeoTransform[2] * (dProjY - adfGeoTransform[3])) / dTemp + 0.5;
dRow = (adfGeoTransform[1] * (dProjY - adfGeoTransform[3]) - adfGeoTransform[4] * (dProjX - adfGeoTransform[0])) / dTemp + 0.5;
int col = int(dCol);
int row = int(dRow);
if (col >= 0 && col < dataInfoVapor.GetWidth() && row >0 && row < dataInfoVapor.GetHeigh())
{
if (dataInfoVapor.GetValue()[dataInfoVapor.GetWidth() * row + col] < 30000)
{
vapor.push_back(5.0);
}
else
{
vapor.push_back((dataInfoVapor.GetValue()[dataInfoVapor.GetWidth() * row + col] * 0.0013314611669646014 - 43.67868070046324)*0.1);
}
}
// corner point
for (int i = 0; i < dataInfo138.getCornerLatLon().size(); i = i + 2)
{
dProjX = dataInfo138.getCornerLatLon()[i + 1];
dProjY = dataInfo138.getCornerLatLon()[i];
double dTemp = adfGeoTransform[1] * adfGeoTransform[5] - adfGeoTransform[2] * adfGeoTransform[4];
double dCol = 0.0, dRow = 0.0;
dCol = (adfGeoTransform[5] * (dProjX - adfGeoTransform[0]) - adfGeoTransform[2] * (dProjY - adfGeoTransform[3])) / dTemp + 0.5;
dRow = (adfGeoTransform[1] * (dProjY - adfGeoTransform[3]) - adfGeoTransform[4] * (dProjX - adfGeoTransform[0])) / dTemp + 0.5;
int col = int(dCol);
int row = int(dRow);
if (col >= 0 && col < dataInfoVapor.GetWidth() && row >0 && row < dataInfoVapor.GetHeigh())
{
vapor.push_back((dataInfoVapor.GetValue()[dataInfoVapor.GetWidth() * row + col] * 0.0013314611669646014 + 43.67868070046324)*0.1);
}
}
if (vapor.size() == 0) return -1;
return getMin<double>(vapor);
}
// get center point of the aircraft
void getCenterPoint(const UINT16* bandCirrus, const BYTE* resultQA, const int &cols, const int &rows, BYTE *finalAircraft, std::vector<int> &finalAircraftsIndex);
// process one file to find aircraft in it
int processOneSwath(std::ofstream &outf, const std::string &inFile, CreateLineShp& cLineShp, HandleShpFile & shpHandle, FilePathTools &pathTool, const GenTypes &genTypes, const std::string &outPath, const std::string &inPath21 = "", const std::string &inPathVapor = "");
// find 8 pixels
int validate8Pixels(const FLOAT32 *resultLap, const int &col, const int &row, const int &cols, const FLOAT32 &thresholdLap);
// find candinate aircraft
int findCandidateAircraft(const UINT16 *bandRef, const FLOAT32 *resultLap, const int &padLeft, const int &padTop, const FLOAT32 &thresholdLap, const int& cols, const int& rows, BYTE *resultQA);
#endif