e8eb202f
David Mayerich
added a new ENVI ...
|
1
2
3
4
|
#ifndef STIM_BIP_H
#define STIM_BIP_H
#include "../envi/envi_header.h"
|
6aa04ba2
David Mayerich
interleave types
|
5
|
#include "../envi/bil.h"
|
88c3e636
heziqi
Ziqi added big.h
|
6
7
8
9
|
#include "../envi/binary.h"
#include <cstring>
#include <utility>
|
8a86bd56
David Mayerich
changed rts names...
|
10
|
namespace stim{
|
88c3e636
heziqi
Ziqi added big.h
|
11
|
|
a23c4132
David Mayerich
Doxygen comments ...
|
12
13
14
15
16
17
18
19
|
/**
The BIP class represents a 3-dimensional binary file stored using band interleaved by pixel (BIP) image encoding. The binary file is stored
such that Z-X "frames" are stored sequentially to form an image stack along the y-axis. When accessing the data sequentially on disk,
the dimensions read, from fastest to slowest, are Z, X, Y.
This class is optimized for data streaming, and therefore supports extremely large (terabyte-scale) files. Data is loaded from disk
on request. Functions used to access data are written to support efficient reading.
*/
|
88c3e636
heziqi
Ziqi added big.h
|
20
21
22
23
24
25
|
template <typename T>
class bip: public binary<T> {
protected:
|
6708cc25
heziqi
Ziqi added envi c...
|
26
27
28
|
std::vector<double> w; //band wavelength
unsigned int offset; //header offset
|
88c3e636
heziqi
Ziqi added big.h
|
29
|
|
724ec347
David Mayerich
simplified the EN...
|
30
31
32
33
34
35
36
37
38
39
|
unsigned long X(){
return R[1];
}
unsigned long Y(){
return R[2];
}
unsigned long Z(){
return R[0];
}
|
88c3e636
heziqi
Ziqi added big.h
|
40
41
42
43
|
public:
using binary<T>::open;
using binary<T>::file;
|
6708cc25
heziqi
Ziqi added envi c...
|
44
|
using binary<T>::R;
|
724ec347
David Mayerich
simplified the EN...
|
45
|
using binary<T>::read_line_12;
|
88c3e636
heziqi
Ziqi added big.h
|
46
|
|
a23c4132
David Mayerich
Doxygen comments ...
|
47
48
49
50
51
52
53
54
|
/// Open a data file for reading using the class interface.
/// @param filename is the name of the binary file on disk
/// @param X is the number of samples along dimension 1
/// @param Y is the number of samples (lines) along dimension 2
/// @param B is the number of samples (bands) along dimension 3
/// @param header_offset is the number of bytes (if any) in the binary header
/// @param wavelengths is an optional STL vector of size B specifying a numerical label for each band
|
6708cc25
heziqi
Ziqi added envi c...
|
55
|
bool open(std::string filename, unsigned int X, unsigned int Y, unsigned int B, unsigned int header_offset, std::vector<double> wavelengths){
|
88c3e636
heziqi
Ziqi added big.h
|
56
|
|
6708cc25
heziqi
Ziqi added envi c...
|
57
58
59
60
|
//copy the wavelengths to the BSQ file structure
w = wavelengths;
//copy the offset to the structure
offset = header_offset;
|
88c3e636
heziqi
Ziqi added big.h
|
61
|
|
724ec347
David Mayerich
simplified the EN...
|
62
|
return open(filename, vec<unsigned int>(B, X, Y), header_offset);
|
88c3e636
heziqi
Ziqi added big.h
|
63
64
65
|
}
|
a23c4132
David Mayerich
Doxygen comments ...
|
66
67
68
69
|
/// Retrieve a single band (based on index) and stores it in pre-allocated memory.
/// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size.
/// @param page <= B is the integer number of the band to be copied.
|
88c3e636
heziqi
Ziqi added big.h
|
70
|
bool band_index( T * p, unsigned int page){
|
724ec347
David Mayerich
simplified the EN...
|
71
|
return binary<T>::read_plane_0(p, page);
|
88c3e636
heziqi
Ziqi added big.h
|
72
73
|
}
|
a23c4132
David Mayerich
Doxygen comments ...
|
74
75
76
77
|
/// Retrieve a single band (by numerical label) and stores it in pre-allocated memory.
/// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size.
/// @param wavelength is a floating point value (usually a wavelength in spectral data) used as a label for the band to be copied.
|
e933c14e
David Mayerich
cleaned up the code
|
78
|
bool band( T * p, double wavelength){
|
88c3e636
heziqi
Ziqi added big.h
|
79
|
|
ee4dea28
David Mayerich
fixed errors in c...
|
80
81
82
83
|
//if there are no wavelengths in the BSQ file
if(w.size() == 0)
return band_index(p, (unsigned int)wavelength);
|
724ec347
David Mayerich
simplified the EN...
|
84
|
unsigned int XY = X() * Y(); //calculate the number of pixels in a band
|
88c3e636
heziqi
Ziqi added big.h
|
85
86
|
unsigned page=0; //bands around the wavelength
|
517876d6
heziqi
metrics finished ...
|
87
|
|
88c3e636
heziqi
Ziqi added big.h
|
88
89
90
91
|
//get the bands numbers around the wavelength
//if wavelength is smaller than the first one in header file
|
6708cc25
heziqi
Ziqi added envi c...
|
92
|
if ( w[page] > wavelength ){
|
88c3e636
heziqi
Ziqi added big.h
|
93
94
95
96
|
band_index(p, page);
return true;
}
|
6708cc25
heziqi
Ziqi added envi c...
|
97
|
while( w[page] < wavelength )
|
88c3e636
heziqi
Ziqi added big.h
|
98
99
100
|
{
page++;
//if wavelength is larger than the last wavelength in header file
|
724ec347
David Mayerich
simplified the EN...
|
101
102
|
if (page == Z()) {
band_index(p, Z()-1);
|
88c3e636
heziqi
Ziqi added big.h
|
103
104
105
|
return true;
}
}
|
6708cc25
heziqi
Ziqi added envi c...
|
106
|
if ( wavelength < w[page] )
|
88c3e636
heziqi
Ziqi added big.h
|
107
|
{
|
517876d6
heziqi
metrics finished ...
|
108
109
|
T * p1;
T * p2;
|
88c3e636
heziqi
Ziqi added big.h
|
110
111
112
113
114
|
p1=(T*)malloc( XY * sizeof(T)); //memory allocation
p2=(T*)malloc( XY * sizeof(T));
band_index(p1, page - 1);
band_index(p2, page );
for(unsigned i=0; i < XY; i++){
|
6708cc25
heziqi
Ziqi added envi c...
|
115
|
double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]);
|
88c3e636
heziqi
Ziqi added big.h
|
116
117
|
p[i] = (p2[i] - p1[i]) * r + p1[i];
}
|
517876d6
heziqi
metrics finished ...
|
118
119
|
free(p1);
free(p2);
|
88c3e636
heziqi
Ziqi added big.h
|
120
121
122
123
124
125
|
}
else //if the wavelength is equal to a wavelength in header file
{
band_index(p, page);
}
|
88c3e636
heziqi
Ziqi added big.h
|
126
127
|
return true;
}
|
724ec347
David Mayerich
simplified the EN...
|
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
/// Retrieve a single spectrum (Z-axis line) at a given (x, y) location and stores it in pre-allocated memory.
/// @param p is a pointer to pre-allocated memory at least B * sizeof(T) in size.
/// @param x is the x-coordinate (dimension 1) of the spectrum.
/// @param y is the y-coordinate (dimension 2) of the spectrum.
bool spectrum(T * p, unsigned x, unsigned y){
return read_line_12(p, x, y); //read a line in the binary YZ plane (dimension order for BIP is ZXY)
}
/// Retrieves a band of x values from a given xz plane.
/// @param p is a pointer to pre-allocated memory at least X * sizeof(T) in size
/// @param c is a pointer to an existing XZ plane (size X*Z*sizeof(T))
/// @param wavelength is the wavelength of X values to retrieve
bool read_x_from_xz(T* p, T* c, double wavelength)
|
11f177d5
heziqi
Ziqi added functi...
|
144
|
{
|
724ec347
David Mayerich
simplified the EN...
|
145
|
unsigned int B = Z();
|
11f177d5
heziqi
Ziqi added functi...
|
146
147
|
unsigned page=0; //samples around the wavelength
|
517876d6
heziqi
metrics finished ...
|
148
|
|
11f177d5
heziqi
Ziqi added functi...
|
149
150
151
152
|
//get the bands numbers around the wavelength
//if wavelength is smaller than the first one in header file
|
6708cc25
heziqi
Ziqi added envi c...
|
153
|
if ( w[page] > wavelength ){
|
724ec347
David Mayerich
simplified the EN...
|
154
|
for(unsigned j = 0; j < X(); j++)
|
11f177d5
heziqi
Ziqi added functi...
|
155
156
157
158
159
160
|
{
p[j] = c[j * B];
}
return true;
}
|
6708cc25
heziqi
Ziqi added envi c...
|
161
|
while( w[page] < wavelength )
|
11f177d5
heziqi
Ziqi added functi...
|
162
163
164
165
|
{
page++;
//if wavelength is larger than the last wavelength in header file
if (page == B) {
|
724ec347
David Mayerich
simplified the EN...
|
166
|
for(unsigned j = 0; j < X(); j++)
|
11f177d5
heziqi
Ziqi added functi...
|
167
168
169
170
171
172
|
{
p[j] = c[(j + 1) * B - 1];
}
return true;
}
}
|
6708cc25
heziqi
Ziqi added envi c...
|
173
|
if ( wavelength < w[page] )
|
11f177d5
heziqi
Ziqi added functi...
|
174
|
{
|
517876d6
heziqi
metrics finished ...
|
175
176
|
T * p1;
T * p2;
|
724ec347
David Mayerich
simplified the EN...
|
177
178
|
p1=(T*)malloc( X() * sizeof(T)); //memory allocation
p2=(T*)malloc( X() * sizeof(T));
|
11f177d5
heziqi
Ziqi added functi...
|
179
|
//band_index(p1, page - 1);
|
724ec347
David Mayerich
simplified the EN...
|
180
|
for(unsigned j = 0; j < X(); j++)
|
11f177d5
heziqi
Ziqi added functi...
|
181
182
183
184
|
{
p1[j] = c[j * B + page - 1];
}
//band_index(p2, page );
|
724ec347
David Mayerich
simplified the EN...
|
185
|
for(unsigned j = 0; j < X(); j++)
|
11f177d5
heziqi
Ziqi added functi...
|
186
187
188
189
|
{
p2[j] = c[j * B + page];
}
|
724ec347
David Mayerich
simplified the EN...
|
190
|
for(unsigned i=0; i < X(); i++){
|
6708cc25
heziqi
Ziqi added envi c...
|
191
|
double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]);
|
11f177d5
heziqi
Ziqi added functi...
|
192
193
|
p[i] = (p2[i] - p1[i]) * r + p1[i];
}
|
517876d6
heziqi
metrics finished ...
|
194
195
|
free(p1);
free(p2);
|
11f177d5
heziqi
Ziqi added functi...
|
196
197
198
199
|
}
else //if the wavelength is equal to a wavelength in header file
{
//band_index(p, page);
|
724ec347
David Mayerich
simplified the EN...
|
200
|
for(unsigned j = 0; j < X(); j++)
|
11f177d5
heziqi
Ziqi added functi...
|
201
202
203
204
205
206
207
|
{
p[j] = c[j * B + page];
}
}
return true;
}
|
bfe9c6db
heziqi
added feature_mat...
|
208
|
|
a23c4132
David Mayerich
Doxygen comments ...
|
209
210
211
212
|
/// Retrieve a single pixel and stores it in pre-allocated memory.
/// @param p is a pointer to pre-allocated memory at least sizeof(T) in size.
/// @param n is an integer index to the pixel using linear array indexing.
|
bfe9c6db
heziqi
added feature_mat...
|
213
214
|
bool pixel(T * p, unsigned n){
|
724ec347
David Mayerich
simplified the EN...
|
215
|
unsigned bandnum = X() * Y(); //calculate numbers in one band
|
bfe9c6db
heziqi
added feature_mat...
|
216
217
218
219
220
|
if ( n >= bandnum){ //make sure the pixel number is right
std::cout<<"ERROR: sample or line out of range"<<std::endl;
return false;
}
|
724ec347
David Mayerich
simplified the EN...
|
221
222
223
|
file.seekg(n * Z() * sizeof(T), std::ios::beg); //point to the certain pixel
file.read((char *)p, sizeof(T) * Z());
return true;
|
bfe9c6db
heziqi
added feature_mat...
|
224
|
}
|
11f177d5
heziqi
Ziqi added functi...
|
225
226
|
//given a Y ,return a ZX slice
|
724ec347
David Mayerich
simplified the EN...
|
227
228
|
bool read_plane_y(T * p, unsigned y){
return binary<T>::read_plane_2(p, y);
|
11f177d5
heziqi
Ziqi added functi...
|
229
230
|
}
|
a23c4132
David Mayerich
Doxygen comments ...
|
231
232
233
234
|
/// Perform baseline correction given a list of baseline points and stores the result in a new BSQ file.
/// @param outname is the name of the output file used to store the resulting baseline-corrected data.
/// @param wls is the list of baseline points based on band labels.
|
11f177d5
heziqi
Ziqi added functi...
|
235
236
|
bool baseline(std::string outname, std::vector<double> wls){
|
88c3e636
heziqi
Ziqi added big.h
|
237
238
239
240
241
242
|
unsigned N = wls.size(); //get the number of baseline points
std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file
std::string headername = outname + ".hdr"; //the header file name
//simplify image resolution
|
724ec347
David Mayerich
simplified the EN...
|
243
|
unsigned int ZX = Z() * X(); //calculate the number of points in a Y slice
|
11f177d5
heziqi
Ziqi added functi...
|
244
|
unsigned int L = ZX * sizeof(T); //calculate the number of bytes of a Y slice
|
724ec347
David Mayerich
simplified the EN...
|
245
|
unsigned int B = Z();
|
88c3e636
heziqi
Ziqi added big.h
|
246
|
|
11f177d5
heziqi
Ziqi added functi...
|
247
248
|
T* c; //pointer to the current Y slice
c = (T*)malloc(L); //memory allocation
|
88c3e636
heziqi
Ziqi added big.h
|
249
|
|
11f177d5
heziqi
Ziqi added functi...
|
250
251
|
T* a; //pointer to the two YZ lines surrounding the current YZ line
T* b;
|
88c3e636
heziqi
Ziqi added big.h
|
252
|
|
724ec347
David Mayerich
simplified the EN...
|
253
254
|
a = (T*)malloc(X() * sizeof(T));
b = (T*)malloc(X() * sizeof(T));
|
88c3e636
heziqi
Ziqi added big.h
|
255
|
|
88c3e636
heziqi
Ziqi added big.h
|
256
257
258
|
double ai, bi; //stores the two baseline points wavelength surrounding the current band
double ci; //stores the current band's wavelength
|
f6169dea
heziqi
Ziqi completed bi...
|
259
|
unsigned control;
|
88c3e636
heziqi
Ziqi added big.h
|
260
|
|
88c3e636
heziqi
Ziqi added big.h
|
261
262
263
264
|
if (a == NULL || b == NULL || c == NULL){
std::cout<<"ERROR: error allocating memory";
exit(1);
}
|
11f177d5
heziqi
Ziqi added functi...
|
265
266
|
// loop start correct every y slice
|
724ec347
David Mayerich
simplified the EN...
|
267
|
for (unsigned k =0; k < Y(); k++)
|
11f177d5
heziqi
Ziqi added functi...
|
268
269
|
{
//get the current y slice
|
724ec347
David Mayerich
simplified the EN...
|
270
|
read_plane_y(c, k);
|
11f177d5
heziqi
Ziqi added functi...
|
271
272
|
//initialize lownum, highnum, low, high
|
f6169dea
heziqi
Ziqi completed bi...
|
273
|
control=0;
|
6708cc25
heziqi
Ziqi added envi c...
|
274
|
ai = w[0];
|
11f177d5
heziqi
Ziqi added functi...
|
275
|
//if no baseline point is specified at band 0,
|
88c3e636
heziqi
Ziqi added big.h
|
276
|
//set the baseline point at band 0 to 0
|
6708cc25
heziqi
Ziqi added envi c...
|
277
|
if(wls[0] != w[0]){
|
11f177d5
heziqi
Ziqi added functi...
|
278
|
bi = wls[control];
|
724ec347
David Mayerich
simplified the EN...
|
279
|
memset(a, (char)0, X() * sizeof(T) );
|
11f177d5
heziqi
Ziqi added functi...
|
280
281
282
283
|
}
//else get the low band
else{
control++;
|
724ec347
David Mayerich
simplified the EN...
|
284
|
read_x_from_xz(a, c, ai);
|
11f177d5
heziqi
Ziqi added functi...
|
285
286
287
|
bi = wls[control];
}
//get the high band
|
724ec347
David Mayerich
simplified the EN...
|
288
|
read_x_from_xz(b, c, bi);
|
11f177d5
heziqi
Ziqi added functi...
|
289
290
291
292
|
//correct every YZ line
for(unsigned cii = 0; cii < B; cii++){
|
11f177d5
heziqi
Ziqi added functi...
|
293
|
//update baseline points, if necessary
|
6708cc25
heziqi
Ziqi added envi c...
|
294
|
if( w[cii] >= bi && cii != B - 1) {
|
11f177d5
heziqi
Ziqi added functi...
|
295
296
297
298
299
300
301
302
303
|
//if the high band is now on the last BL point?
if (control != N-1) {
control++; //increment the index
std::swap(a, b); //swap the baseline band pointers
ai = bi;
bi = wls[control];
|
724ec347
David Mayerich
simplified the EN...
|
304
|
read_x_from_xz(b, c, bi);
|
11f177d5
heziqi
Ziqi added functi...
|
305
306
307
|
}
//if the last BL point on the last band of the file?
|
6708cc25
heziqi
Ziqi added envi c...
|
308
|
else if ( wls[control] < w[B - 1]) {
|
11f177d5
heziqi
Ziqi added functi...
|
309
310
311
|
std::swap(a, b); //swap the baseline band pointers
|
724ec347
David Mayerich
simplified the EN...
|
312
|
memset(b, (char)0, X() * sizeof(T) ); //clear the high band
|
11f177d5
heziqi
Ziqi added functi...
|
313
314
|
ai = bi;
|
6708cc25
heziqi
Ziqi added envi c...
|
315
|
bi = w[B - 1];
|
11f177d5
heziqi
Ziqi added functi...
|
316
|
}
|
88c3e636
heziqi
Ziqi added big.h
|
317
|
}
|
88c3e636
heziqi
Ziqi added big.h
|
318
|
|
6708cc25
heziqi
Ziqi added envi c...
|
319
|
ci = w[cii];
|
11f177d5
heziqi
Ziqi added functi...
|
320
321
|
//perform the baseline correction
|
724ec347
David Mayerich
simplified the EN...
|
322
|
for(unsigned i=0; i < X(); i++)
|
11f177d5
heziqi
Ziqi added functi...
|
323
324
|
{
double r = (double) (ci - ai) / (double) (bi - ai);
|
92e4cf05
heziqi
include file fixed
|
325
|
c[i * B + cii] =(T) ( c[i * B + cii] - (b[i] - a[i]) * r - a[i] );
|
11f177d5
heziqi
Ziqi added functi...
|
326
327
328
|
}
}//loop for YZ line end
|
11f177d5
heziqi
Ziqi added functi...
|
329
330
|
target.write(reinterpret_cast<const char*>(c), L); //write the corrected data into destination
}//loop for Y slice end
|
88c3e636
heziqi
Ziqi added big.h
|
331
|
|
6708cc25
heziqi
Ziqi added envi c...
|
332
|
|
88c3e636
heziqi
Ziqi added big.h
|
333
334
335
336
337
|
free(a);
free(b);
free(c);
target.close();
|
11f177d5
heziqi
Ziqi added functi...
|
338
339
|
return true;
|
88c3e636
heziqi
Ziqi added big.h
|
340
|
}
|
11f177d5
heziqi
Ziqi added functi...
|
341
|
|
a23c4132
David Mayerich
Doxygen comments ...
|
342
343
344
345
346
|
/// Normalize all spectra based on the value of a single band, storing the result in a new BSQ file.
/// @param outname is the name of the output file used to store the resulting baseline-corrected data.
/// @param w is the label specifying the band that the hyperspectral image will be normalized to.
/// @param t is a threshold specified such that a spectrum with a value at w less than t is set to zero. Setting this threshold allows the user to limit division by extremely small numbers.
|
93503ec6
David Mayerich
allowed masking d...
|
347
|
bool normalize(std::string outname, double w, double t = 0.0)
|
88c3e636
heziqi
Ziqi added big.h
|
348
|
{
|
724ec347
David Mayerich
simplified the EN...
|
349
350
351
|
unsigned int B = Z(); //calculate the number of bands
unsigned int ZX = Z() * X();
unsigned int XY = X() * Y(); //calculate the number of pixels in a band
|
11f177d5
heziqi
Ziqi added functi...
|
352
353
|
unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band
unsigned int L = ZX * sizeof(T);
|
88c3e636
heziqi
Ziqi added big.h
|
354
355
356
357
|
std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file
std::string headername = outname + ".hdr"; //the header file name
|
11f177d5
heziqi
Ziqi added functi...
|
358
359
|
T * c; //pointer to the current ZX slice
T * b; //pointer to the standard band
|
88c3e636
heziqi
Ziqi added big.h
|
360
361
|
b = (T*)malloc( S ); //memory allocation
|
11f177d5
heziqi
Ziqi added functi...
|
362
|
c = (T*)malloc( L );
|
88c3e636
heziqi
Ziqi added big.h
|
363
|
|
e933c14e
David Mayerich
cleaned up the code
|
364
|
band(b, w); //get the certain band into memory
|
88c3e636
heziqi
Ziqi added big.h
|
365
|
|
724ec347
David Mayerich
simplified the EN...
|
366
|
for(unsigned j = 0; j < Y(); j++)
|
88c3e636
heziqi
Ziqi added big.h
|
367
|
{
|
724ec347
David Mayerich
simplified the EN...
|
368
369
370
|
read_plane_y(c, j);
unsigned jX = j * X(); //to avoid calculating it many times
for(unsigned i = 0; i < X(); i++)
|
88c3e636
heziqi
Ziqi added big.h
|
371
|
{
|
f6169dea
heziqi
Ziqi completed bi...
|
372
|
unsigned iB = i * B;
|
11f177d5
heziqi
Ziqi added functi...
|
373
374
|
for(unsigned m = 0; m < B; m++)
{
|
93503ec6
David Mayerich
allowed masking d...
|
375
376
377
378
|
if( b[i+jX] < t )
c[m + iB] = (T)0.0;
else
c[m + iB] = c[m + iB] / b[i + jX]; //perform normalization
|
11f177d5
heziqi
Ziqi added functi...
|
379
|
}
|
88c3e636
heziqi
Ziqi added big.h
|
380
|
}
|
11f177d5
heziqi
Ziqi added functi...
|
381
|
target.write(reinterpret_cast<const char*>(c), L); //write normalized data into destination
|
88c3e636
heziqi
Ziqi added big.h
|
382
|
}
|
6708cc25
heziqi
Ziqi added envi c...
|
383
|
|
88c3e636
heziqi
Ziqi added big.h
|
384
385
386
387
388
389
390
|
free(b);
free(c);
target.close();
return true;
}
|
a23c4132
David Mayerich
Doxygen comments ...
|
391
392
393
|
/// Convert the current BIP file to a BSQ file with the specified file name.
/// @param outname is the name of the output BSQ file to be saved to disk.
|
11f177d5
heziqi
Ziqi added functi...
|
394
|
bool bsq(std::string outname)
|
f6169dea
heziqi
Ziqi completed bi...
|
395
396
397
398
399
400
|
{
std::string temp = outname + "_temp";
std::string headtemp = temp + ".hdr";
//first creat a temporary bil file and convert bip file to bil file
bil(temp);
|
8a86bd56
David Mayerich
changed rts names...
|
401
|
stim::bil<T> n;
|
724ec347
David Mayerich
simplified the EN...
|
402
|
if(n.open(temp, X(), Y(), Z(), offset, w)==false){ //open infile
|
f6169dea
heziqi
Ziqi completed bi...
|
403
404
405
406
407
408
409
410
411
412
413
|
std::cout<<"ERROR: unable to open input file"<<std::endl;
exit(1);
}
//then convert bil file to bsq file
n.bsq(outname);
n.close();
remove(temp.c_str());
remove(headtemp.c_str());
return true;
}
|
a23c4132
David Mayerich
Doxygen comments ...
|
414
415
416
|
/// Convert the current BIP file to a BIL file with the specified file name.
/// @param outname is the name of the output BIL file to be saved to disk.
|
f6169dea
heziqi
Ziqi completed bi...
|
417
|
bool bil(std::string outname)
|
11f177d5
heziqi
Ziqi added functi...
|
418
|
{
|
724ec347
David Mayerich
simplified the EN...
|
419
|
unsigned int S = X() * Z() * sizeof(T); //calculate the number of bytes in a ZX slice
|
11f177d5
heziqi
Ziqi added functi...
|
420
421
422
423
|
std::ofstream target(outname.c_str(), std::ios::binary);
std::string headername = outname + ".hdr";
|
f6169dea
heziqi
Ziqi completed bi...
|
424
|
T * p; //pointer to the current ZX slice for bip file
|
11f177d5
heziqi
Ziqi added functi...
|
425
|
p = (T*)malloc(S);
|
f6169dea
heziqi
Ziqi completed bi...
|
426
427
|
T * q; //pointer to the current XZ slice for bil file
q = (T*)malloc(S);
|
11f177d5
heziqi
Ziqi added functi...
|
428
|
|
724ec347
David Mayerich
simplified the EN...
|
429
|
for ( unsigned i = 0; i < Y(); i++)
|
11f177d5
heziqi
Ziqi added functi...
|
430
|
{
|
724ec347
David Mayerich
simplified the EN...
|
431
432
|
read_plane_y(p, i);
for ( unsigned k = 0; k < Z(); k++)
|
f6169dea
heziqi
Ziqi completed bi...
|
433
|
{
|
724ec347
David Mayerich
simplified the EN...
|
434
435
436
|
unsigned ks = k * X();
for ( unsigned j = 0; j < X(); j++)
q[ks + j] = p[k + j * Z()];
|
f6169dea
heziqi
Ziqi completed bi...
|
437
438
|
}
target.write(reinterpret_cast<const char*>(q), S); //write a band data into target file
|
11f177d5
heziqi
Ziqi added functi...
|
439
|
}
|
f6169dea
heziqi
Ziqi completed bi...
|
440
|
|
11f177d5
heziqi
Ziqi added functi...
|
441
442
|
free(p);
|
f6169dea
heziqi
Ziqi completed bi...
|
443
|
free(q);
|
11f177d5
heziqi
Ziqi added functi...
|
444
445
446
|
target.close();
return true;
}
|
88c3e636
heziqi
Ziqi added big.h
|
447
|
|
a23c4132
David Mayerich
Doxygen comments ...
|
448
449
450
451
452
453
454
455
|
/// Return a baseline corrected band given two adjacent baseline points and their bands. The result is stored in a pre-allocated array.
/// @param lb is the label value for the left baseline point
/// @param rb is the label value for the right baseline point
/// @param lp is a pointer to an array holding the band image for the left baseline point
/// @param rp is a pointer to an array holding the band image for the right baseline point
/// @param wavelength is the label value for the requested baseline-corrected band
/// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size.
|
20c212c0
heziqi
Ziqi added functi...
|
456
457
|
bool baseline_band(double lb, double rb, T* lp, T* rp, double wavelength, T* result){
|
724ec347
David Mayerich
simplified the EN...
|
458
|
unsigned XY = X() * Y();
|
20c212c0
heziqi
Ziqi added functi...
|
459
460
461
462
463
464
465
466
467
|
band(result, wavelength); //get band
//perform the baseline correction
double r = (double) (wavelength - lb) / (double) (rb - lb);
for(unsigned i=0; i < XY; i++){
result[i] =(T) (result[i] - (rp[i] - lp[i]) * r - lp[i] );
}
return true;
}
|
517876d6
heziqi
metrics finished ...
|
468
|
|
a23c4132
David Mayerich
Doxygen comments ...
|
469
470
471
472
473
474
|
/// Return a baseline corrected band given two adjacent baseline points. The result is stored in a pre-allocated array.
/// @param lb is the label value for the left baseline point
/// @param rb is the label value for the right baseline point
/// @param bandwavelength is the label value for the desired baseline-corrected band
/// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size.
|
70407ea9
heziqi
Ziqi completed he...
|
475
|
bool height(double lb, double rb, double bandwavelength, T* result){
|
20c212c0
heziqi
Ziqi added functi...
|
476
477
478
|
T* lp;
T* rp;
|
724ec347
David Mayerich
simplified the EN...
|
479
|
unsigned XY = X() * Y();
|
70407ea9
heziqi
Ziqi completed he...
|
480
481
482
|
unsigned S = XY * sizeof(T);
lp = (T*) malloc(S); //memory allocation
rp = (T*) malloc(S);
|
20c212c0
heziqi
Ziqi added functi...
|
483
|
|
70407ea9
heziqi
Ziqi completed he...
|
484
|
band(lp, lb);
|
20c212c0
heziqi
Ziqi added functi...
|
485
486
487
488
|
band(rp, rb);
baseline_band(lb, rb, lp, rp, bandwavelength, result);
|
517876d6
heziqi
metrics finished ...
|
489
490
|
free(lp);
free(rp);
|
20c212c0
heziqi
Ziqi added functi...
|
491
492
493
|
return true;
}
|
c359422d
heziqi
Ziqi added functi...
|
494
|
|
a23c4132
David Mayerich
Doxygen comments ...
|
495
496
497
498
499
500
501
|
/// Calculate the area under the spectrum between two specified points and stores the result in a pre-allocated array.
/// @param lb is the label value for the left baseline point
/// @param rb is the label value for the right baseline point
/// @param lab is the label value for the left bound (start of the integration)
/// @param rab is the label value for the right bound (end of the integration)
/// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
|
70407ea9
heziqi
Ziqi completed he...
|
502
|
bool area(double lb, double rb, double lab, double rab, T* result){
|
20c212c0
heziqi
Ziqi added functi...
|
503
504
505
506
507
|
T* lp; //left band pointer
T* rp; //right band pointer
T* cur; //current band 1
T* cur2; //current band 2
|
20c212c0
heziqi
Ziqi added functi...
|
508
|
|
724ec347
David Mayerich
simplified the EN...
|
509
|
unsigned XY = X() * Y();
|
20c212c0
heziqi
Ziqi added functi...
|
510
511
512
513
514
515
|
unsigned S = XY * sizeof(T);
lp = (T*) malloc(S); //memory allocation
rp = (T*) malloc(S);
cur = (T*) malloc(S);
cur2 = (T*) malloc(S);
|
20c212c0
heziqi
Ziqi added functi...
|
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
memset(result, (char)0, S);
//find the wavelenght position in the whole band
unsigned int n = w.size();
unsigned int ai = 0; //left bound position
unsigned int bi = n - 1; //right bound position
//to make sure the left and the right bound are in the bandwidth
if (lb < w[0] || rb < w[0] || lb > w[n-1] || rb >w[n-1]){
std::cout<<"ERROR: left bound or right bound out of bandwidth"<<std::endl;
exit(1);
}
//to make sure rigth bound is bigger than left bound
else if(lb > rb){
std::cout<<"ERROR: right bound should be bigger than left bound"<<std::endl;
exit(1);
}
//get the position of lb and rb
|
70407ea9
heziqi
Ziqi completed he...
|
538
|
while (lab >= w[ai]){
|
20c212c0
heziqi
Ziqi added functi...
|
539
540
|
ai++;
}
|
70407ea9
heziqi
Ziqi completed he...
|
541
|
while (rab <= w[bi]){
|
20c212c0
heziqi
Ziqi added functi...
|
542
543
544
545
546
547
548
|
bi--;
}
band(lp, lb);
band(rp, rb);
//calculate the beginning and the ending part
|
70407ea9
heziqi
Ziqi completed he...
|
549
550
|
baseline_band(lb, rb, lp, rp, rab, cur2); //ending part
baseline_band(lb, rb, lp, rp, w[bi], cur);
|
20c212c0
heziqi
Ziqi added functi...
|
551
|
for(unsigned j = 0; j < XY; j++){
|
70407ea9
heziqi
Ziqi completed he...
|
552
|
result[j] += (rab - w[bi]) * (cur[j] + cur2[j]) / 2.0;
|
20c212c0
heziqi
Ziqi added functi...
|
553
|
}
|
70407ea9
heziqi
Ziqi completed he...
|
554
555
|
baseline_band(lb, rb, lp, rp, lab, cur2); //beginnning part
baseline_band(lb, rb, lp, rp, w[ai], cur);
|
20c212c0
heziqi
Ziqi added functi...
|
556
|
for(unsigned j = 0; j < XY; j++){
|
70407ea9
heziqi
Ziqi completed he...
|
557
|
result[j] += (w[ai] - lab) * (cur[j] + cur2[j]) / 2.0;
|
20c212c0
heziqi
Ziqi added functi...
|
558
|
}
|
517876d6
heziqi
metrics finished ...
|
559
|
|
20c212c0
heziqi
Ziqi added functi...
|
560
561
562
563
564
565
566
567
568
569
570
|
//calculate the area
ai++;
for(unsigned i = ai; i <= bi ;i++)
{
baseline_band(lb, rb, lp, rp, w[ai], cur2);
for(unsigned j = 0; j < XY; j++)
{
result[j] += (w[ai] - w[ai-1]) * (cur[j] + cur2[j]) / 2.0;
}
std::swap(cur,cur2); //swap the band pointers
}
|
70407ea9
heziqi
Ziqi completed he...
|
571
|
|
517876d6
heziqi
metrics finished ...
|
572
573
574
575
|
free(lp);
free(rp);
free(cur);
free(cur2);
|
20c212c0
heziqi
Ziqi added functi...
|
576
577
578
|
return true;
}
|
a23c4132
David Mayerich
Doxygen comments ...
|
579
580
581
582
583
584
585
586
587
|
/// Compute the ratio of two baseline-corrected peaks. The result is stored in a pre-allocated array.
/// @param lb1 is the label value for the left baseline point for the first peak (numerator)
/// @param rb1 is the label value for the right baseline point for the first peak (numerator)
/// @param pos1 is the label value for the first peak (numerator) position
/// @param lb2 is the label value for the left baseline point for the second peak (denominator)
/// @param rb2 is the label value for the right baseline point for the second peak (denominator)
/// @param pos2 is the label value for the second peak (denominator) position
/// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
|
70407ea9
heziqi
Ziqi completed he...
|
588
589
|
bool ph_to_ph(double lb1, double rb1, double pos1, double lb2, double rb2, double pos2, T * result){
|
724ec347
David Mayerich
simplified the EN...
|
590
591
|
T* p1 = (T*)malloc(X() * Y() * sizeof(T));
T* p2 = (T*)malloc(X() * Y() * sizeof(T));
|
70407ea9
heziqi
Ziqi completed he...
|
592
593
594
595
596
|
//get the two peak band
height(lb1, rb1, pos1, p1);
height(lb2, rb2, pos2, p2);
//calculate the ratio in result
|
724ec347
David Mayerich
simplified the EN...
|
597
|
for(unsigned i = 0; i < X() * Y(); i++){
|
70407ea9
heziqi
Ziqi completed he...
|
598
599
600
601
602
|
if(p1[i] == 0 && p2[i] ==0)
result[i] = 1;
else
result[i] = p1[i] / p2[i];
}
|
517876d6
heziqi
metrics finished ...
|
603
604
605
|
free(p1);
free(p2);
|
70407ea9
heziqi
Ziqi completed he...
|
606
607
608
|
return true;
}
|
a23c4132
David Mayerich
Doxygen comments ...
|
609
610
611
612
613
614
615
616
617
|
/// Compute the ratio between a peak area and peak height.
/// @param lb1 is the label value for the left baseline point for the first peak (numerator)
/// @param rb1 is the label value for the right baseline point for the first peak (numerator)
/// @param pos1 is the label value for the first peak (numerator) position
/// @param lb2 is the label value for the left baseline point for the second peak (denominator)
/// @param rb2 is the label value for the right baseline point for the second peak (denominator)
/// @param pos2 is the label value for the second peak (denominator) position
/// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
|
70407ea9
heziqi
Ziqi completed he...
|
618
619
620
|
bool pa_to_ph(double lb1, double rb1, double lab1, double rab1,
double lb2, double rb2, double pos, T* result){
|
724ec347
David Mayerich
simplified the EN...
|
621
622
|
T* p1 = (T*)malloc(X() * Y() * sizeof(T));
T* p2 = (T*)malloc(X() * Y() * sizeof(T));
|
70407ea9
heziqi
Ziqi completed he...
|
623
624
625
626
627
|
//get the area and the peak band
area(lb1, rb1, lab1, rab1, p1);
height(lb2, rb2, pos, p2);
//calculate the ratio in result
|
724ec347
David Mayerich
simplified the EN...
|
628
|
for(unsigned i = 0; i < X() * Y(); i++){
|
70407ea9
heziqi
Ziqi completed he...
|
629
630
631
632
633
|
if(p1[i] == 0 && p2[i] ==0)
result[i] = 1;
else
result[i] = p1[i] / p2[i];
}
|
517876d6
heziqi
metrics finished ...
|
634
635
636
|
free(p1);
free(p2);
|
70407ea9
heziqi
Ziqi completed he...
|
637
638
639
|
return true;
}
|
a23c4132
David Mayerich
Doxygen comments ...
|
640
641
642
643
644
645
646
647
648
649
650
|
/// Compute the ratio between two peak areas.
/// @param lb1 is the label value for the left baseline point for the first peak (numerator)
/// @param rb1 is the label value for the right baseline point for the first peak (numerator)
/// @param lab1 is the label value for the left bound (start of the integration) of the first peak (numerator)
/// @param rab1 is the label value for the right bound (end of the integration) of the first peak (numerator)
/// @param lb2 is the label value for the left baseline point for the second peak (denominator)
/// @param rb2 is the label value for the right baseline point for the second peak (denominator)
/// @param lab2 is the label value for the left bound (start of the integration) of the second peak (denominator)
/// @param rab2 is the label value for the right bound (end of the integration) of the second peak (denominator)
/// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
|
70407ea9
heziqi
Ziqi completed he...
|
651
652
653
|
bool pa_to_pa(double lb1, double rb1, double lab1, double rab1,
double lb2, double rb2, double lab2, double rab2, T* result){
|
724ec347
David Mayerich
simplified the EN...
|
654
655
|
T* p1 = (T*)malloc(X() * Y() * sizeof(T));
T* p2 = (T*)malloc(X() * Y() * sizeof(T));
|
70407ea9
heziqi
Ziqi completed he...
|
656
657
658
659
660
|
//get the area and the peak band
area(lb1, rb1, lab1, rab1, p1);
area(lb2, rb2, lab2, rab2, p2);
//calculate the ratio in result
|
724ec347
David Mayerich
simplified the EN...
|
661
|
for(unsigned i = 0; i < X() * Y(); i++){
|
70407ea9
heziqi
Ziqi completed he...
|
662
663
664
665
666
|
if(p1[i] == 0 && p2[i] ==0)
result[i] = 1;
else
result[i] = p1[i] / p2[i];
}
|
517876d6
heziqi
metrics finished ...
|
667
668
669
|
free(p1);
free(p2);
|
70407ea9
heziqi
Ziqi completed he...
|
670
671
|
return true;
}
|
517876d6
heziqi
metrics finished ...
|
672
|
|
a23c4132
David Mayerich
Doxygen comments ...
|
673
674
675
676
677
678
679
|
/// Compute the definite integral of a baseline corrected peak.
/// @param lb is the label value for the left baseline point
/// @param rb is the label value for the right baseline point
/// @param lab is the label for the start of the definite integral
/// @param rab is the label for the end of the definite integral
/// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
|
517876d6
heziqi
metrics finished ...
|
680
681
682
683
684
685
|
bool x_area(double lb, double rb, double lab, double rab, T* result){
T* lp; //left band pointer
T* rp; //right band pointer
T* cur; //current band 1
T* cur2; //current band 2
|
724ec347
David Mayerich
simplified the EN...
|
686
|
unsigned XY = X() * Y();
|
517876d6
heziqi
metrics finished ...
|
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
|
unsigned S = XY * sizeof(T);
lp = (T*) malloc(S); //memory allocation
rp = (T*) malloc(S);
cur = (T*) malloc(S);
cur2 = (T*) malloc(S);
memset(result, (char)0, S);
//find the wavelenght position in the whole band
unsigned int n = w.size();
unsigned int ai = 0; //left bound position
unsigned int bi = n - 1; //right bound position
//to make sure the left and the right bound are in the bandwidth
if (lb < w[0] || rb < w[0] || lb > w[n-1] || rb >w[n-1]){
std::cout<<"ERROR: left bound or right bound out of bandwidth"<<std::endl;
exit(1);
}
//to make sure rigth bound is bigger than left bound
else if(lb > rb){
std::cout<<"ERROR: right bound should be bigger than left bound"<<std::endl;
exit(1);
}
//get the position of lb and rb
while (lab >= w[ai]){
ai++;
}
while (rab <= w[bi]){
bi--;
}
band(lp, lb);
band(rp, rb);
//calculate the beginning and the ending part
baseline_band(lb, rb, lp, rp, rab, cur2); //ending part
baseline_band(lb, rb, lp, rp, w[bi], cur);
for(unsigned j = 0; j < XY; j++){
result[j] += (rab - w[bi]) * (rab + w[bi]) * (cur[j] + cur2[j]) / 4.0;
}
baseline_band(lb, rb, lp, rp, lab, cur2); //beginnning part
baseline_band(lb, rb, lp, rp, w[ai], cur);
for(unsigned j = 0; j < XY; j++){
result[j] += (w[ai] - lab) * (w[ai] + lab) * (cur[j] + cur2[j]) / 4.0;
}
//calculate f(x) times x
ai++;
for(unsigned i = ai; i <= bi ;i++)
{
baseline_band(lb, rb, lp, rp, w[ai], cur2);
for(unsigned j = 0; j < XY; j++)
{
result[j] += (w[ai] - w[ai-1]) * (w[ai] + w[ai-1]) * (cur[j] + cur2[j]) / 4.0;
}
std::swap(cur,cur2); //swap the band pointers
}
free(lp);
free(rp);
free(cur);
free(cur2);
return true;
}
|
a23c4132
David Mayerich
Doxygen comments ...
|
754
755
756
757
758
759
760
|
/// Compute the centroid of a baseline corrected peak.
/// @param lb is the label value for the left baseline point
/// @param rb is the label value for the right baseline point
/// @param lab is the label for the start of the peak
/// @param rab is the label for the end of the peak
/// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
|
517876d6
heziqi
metrics finished ...
|
761
|
bool cpoint(double lb, double rb, double lab, double rab, T* result){
|
724ec347
David Mayerich
simplified the EN...
|
762
763
|
T* p1 = (T*)malloc(X() * Y() * sizeof(T));
T* p2 = (T*)malloc(X() * Y() * sizeof(T));
|
517876d6
heziqi
metrics finished ...
|
764
765
766
767
768
|
//get the area and the peak band
x_area(lb, rb, lab, rab, p1);
area(lb, rb, lab, rab, p2);
//calculate the ratio in result
|
724ec347
David Mayerich
simplified the EN...
|
769
|
for(unsigned i = 0; i < X() * Y(); i++){
|
517876d6
heziqi
metrics finished ...
|
770
771
772
773
774
775
776
777
778
779
780
|
if(p1[i] == 0 && p2[i] ==0)
result[i] = 1;
else
result[i] = p1[i] / p2[i];
}
free(p1);
free(p2);
return true;
}
|
a23c4132
David Mayerich
Doxygen comments ...
|
781
782
783
784
785
786
787
|
/// Create a mask based on a given band and threshold value.
/// All pixels in the
/// specified band greater than the threshold are true and all pixels less than the threshold are false.
/// @param mask_band is the band used to specify the mask
/// @param threshold is the threshold used to determine if the mask value is true or false
/// @param p is a pointer to a pre-allocated array at least X * Y in size
|
f4c5d71b
David Mayerich
started working w...
|
788
|
bool build_mask(double mask_band, double threshold, unsigned char* p){
|
4a6f666c
heziqi
Added mask method
|
789
|
|
724ec347
David Mayerich
simplified the EN...
|
790
|
T* temp = (T*)malloc(X() * Y() * sizeof(T)); //allocate memory for the certain band
|
4a6f666c
heziqi
Added mask method
|
791
792
|
band(temp, mask_band);
|
724ec347
David Mayerich
simplified the EN...
|
793
|
for (unsigned int i = 0; i < X() * Y();i++) {
|
4a6f666c
heziqi
Added mask method
|
794
795
796
797
798
799
|
if (temp[i] < threshold)
p[i] = 0;
else
p[i] = 255;
}
|
517876d6
heziqi
metrics finished ...
|
800
|
free(temp);
|
4a6f666c
heziqi
Added mask method
|
801
802
803
804
|
return true;
}
|
a23c4132
David Mayerich
Doxygen comments ...
|
805
806
807
808
|
/// Apply a mask file to the BSQ image, setting all values outside the mask to zero.
/// @param outfile is the name of the masked output file
/// @param p is a pointer to memory of size X * Y, where p(i) = 0 for pixels that will be set to zero.
|
740f8cd2
heziqi
added apply_mask
|
809
810
811
812
|
bool apply_mask(std::string outfile, unsigned char* p){
std::ofstream target(outfile.c_str(), std::ios::binary);
|
724ec347
David Mayerich
simplified the EN...
|
813
|
unsigned ZX = Z() * X(); //calculate the number of values in a page (XZ in BIP)
|
1a55a328
David Mayerich
Added comments fo...
|
814
|
unsigned L = ZX * sizeof(T); //calculate the number of bytes in a page
|
740f8cd2
heziqi
added apply_mask
|
815
|
|
1a55a328
David Mayerich
Added comments fo...
|
816
|
T * temp = (T*)malloc(L); //allocate space for that page
|
740f8cd2
heziqi
added apply_mask
|
817
|
|
724ec347
David Mayerich
simplified the EN...
|
818
|
for (unsigned i = 0; i < Y(); i++) //for each page (Y in BIP)
|
740f8cd2
heziqi
added apply_mask
|
819
|
{
|
724ec347
David Mayerich
simplified the EN...
|
820
821
|
read_plane_y(temp, i); //load that page (it's pointed to by temp)
for ( unsigned j = 0; j < X(); j++) //for each X value
|
740f8cd2
heziqi
added apply_mask
|
822
|
{
|
724ec347
David Mayerich
simplified the EN...
|
823
|
for (unsigned k = 0; k < Z(); k++) //for each B value (band)
|
740f8cd2
heziqi
added apply_mask
|
824
|
{
|
724ec347
David Mayerich
simplified the EN...
|
825
826
|
if (p[i * X() + j] == 0) //if the mask value is zero
temp[j * Z() + k] = 0; //set the pixel value to zero
|
1a55a328
David Mayerich
Added comments fo...
|
827
|
else //otherwise just continue
|
740f8cd2
heziqi
added apply_mask
|
828
829
830
|
continue;
}
}
|
1a55a328
David Mayerich
Added comments fo...
|
831
|
target.write(reinterpret_cast<const char*>(temp), L); //write the edited band data into target file
|
740f8cd2
heziqi
added apply_mask
|
832
|
}
|
1a55a328
David Mayerich
Added comments fo...
|
833
834
835
|
target.close(); //close the target file
free(temp); //free allocated memory
return true; //return true
|
740f8cd2
heziqi
added apply_mask
|
836
837
|
}
|
e843658b
Brad Deutsch
Previous push did...
|
838
|
|
2fcab4f0
David Mayerich
added unsifting f...
|
839
840
|
/// Saves to disk only those spectra corresponding to mask values != 0
bool sift(std::string outfile, unsigned char* p){
|
724ec347
David Mayerich
simplified the EN...
|
841
|
// Assume X() = X, Y() = Y, Z() = Z.
|
e843658b
Brad Deutsch
Previous push did...
|
842
843
844
|
std::ofstream target(outfile.c_str(), std::ios::binary);
//for loading pages:
|
724ec347
David Mayerich
simplified the EN...
|
845
|
unsigned ZX = X() * Z(); //calculate the number of values in an XZ page on disk
|
e843658b
Brad Deutsch
Previous push did...
|
846
847
848
849
|
unsigned L = ZX * sizeof(T); //calculate the size of the page (in bytes)
T * temp = (T*)malloc(L); //allocate memory for a temporary page
//for saving spectra:
|
724ec347
David Mayerich
simplified the EN...
|
850
|
unsigned LZ = Z() * sizeof(T); //calculate the size of the spectrum (in bytes)
|
e843658b
Brad Deutsch
Previous push did...
|
851
|
T * tempZ = (T*)malloc(LZ); //allocate memory for a temporary spectrum
|
724ec347
David Mayerich
simplified the EN...
|
852
|
spectrum(tempZ, X() - 1, Y() - 1); //creates a dummy spectrum by taking the last spectrum in the image
|
e843658b
Brad Deutsch
Previous push did...
|
853
|
|
724ec347
David Mayerich
simplified the EN...
|
854
|
for (unsigned i = 0; i < Y(); i++) //Select a page by choosing Y coordinate, Y()
|
e843658b
Brad Deutsch
Previous push did...
|
855
|
{
|
724ec347
David Mayerich
simplified the EN...
|
856
857
|
read_plane_y(temp, i); //retrieve an ZX page, store in "temp"
for (unsigned j = 0; j < X(); j++) //Select a pixel by choosing X coordinate in the page, X()
|
e843658b
Brad Deutsch
Previous push did...
|
858
|
{
|
724ec347
David Mayerich
simplified the EN...
|
859
|
if (p[j * X() + i] != 0) //if the mask != 0 at that XY pixel
|
e843658b
Brad Deutsch
Previous push did...
|
860
|
{
|
724ec347
David Mayerich
simplified the EN...
|
861
|
for (unsigned k = 0; k < Z(); k++) //Select a voxel by choosing Z coordinate at the pixel
|
e843658b
Brad Deutsch
Previous push did...
|
862
|
{
|
724ec347
David Mayerich
simplified the EN...
|
863
|
tempZ[k] = temp[i*Z() + k]; //Pass the correct spectral value from XZ page into the spectrum to be saved.
|
e843658b
Brad Deutsch
Previous push did...
|
864
865
866
867
868
869
870
871
872
873
874
875
876
|
}
target.write(reinterpret_cast<const char*>(tempZ), LZ); //write that spectrum to disk. Size is L2.
}
else
continue;
}
}
target.close();
free(temp);
return true;
}
|
a23c4132
David Mayerich
Doxygen comments ...
|
877
878
|
/// @param p is a pointer to memory of size X * Y * sizeof(T) that will store the band averages.
|
a43c4fe1
heziqi
Added crop in env...
|
879
|
bool band_avg(T* p){
|
724ec347
David Mayerich
simplified the EN...
|
880
|
unsigned long long XY = X() * Y();
|
a43c4fe1
heziqi
Added crop in env...
|
881
|
//get every pixel and calculate average value
|
724ec347
David Mayerich
simplified the EN...
|
882
|
T* temp = (T*)malloc(sizeof(T) * Z());
|
a43c4fe1
heziqi
Added crop in env...
|
883
884
885
886
887
|
T sum;
for (unsigned i = 0; i < XY; i++){
pixel(temp, i);
//calculate the sum value of every value
sum = 0; //initialize sum value
|
724ec347
David Mayerich
simplified the EN...
|
888
889
|
for (unsigned j = 0; j < Z(); j++){
sum += temp[j]/(T)Z();
|
a43c4fe1
heziqi
Added crop in env...
|
890
891
892
893
894
895
896
|
}
p[i] = sum;
}
free(temp);
return true;
}
|
a23c4132
David Mayerich
Doxygen comments ...
|
897
898
899
900
|
/// Calculate the mean value for all masked (or valid) pixels in a band and returns the average spectrum
/// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum
/// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location
|
a43c4fe1
heziqi
Added crop in env...
|
901
|
bool avg_band(T*p, unsigned char* mask){
|
724ec347
David Mayerich
simplified the EN...
|
902
903
|
unsigned long long XY = X() * Y();
T* temp = (T*)malloc(sizeof(T) * Z());
|
a43c4fe1
heziqi
Added crop in env...
|
904
|
//Iinitialize
|
724ec347
David Mayerich
simplified the EN...
|
905
|
for (unsigned j = 0; j < Z(); j++){
|
a43c4fe1
heziqi
Added crop in env...
|
906
907
908
909
910
911
912
913
914
915
916
917
918
|
p[j] = 0;
}
//calculate vaild number in a band
unsigned count = 0;
for (unsigned j = 0; j < XY; j++){
if (mask[j] != 0){
count++;
}
}
//calculate average number of a band
for (unsigned i = 0; i < XY; i++){
if (mask[i] != 0){
pixel(temp, i);
|
724ec347
David Mayerich
simplified the EN...
|
919
|
for (unsigned j = 0; j < Z(); j++){
|
a43c4fe1
heziqi
Added crop in env...
|
920
921
922
923
924
925
926
|
p[j] += temp[j] / (T)count;
}
}
}
free(temp);
return true;
}
|
a23c4132
David Mayerich
Doxygen comments ...
|
927
928
929
930
931
932
|
/// Calculate the covariance matrix for all masked pixels in the image.
/// @param co is a pointer to pre-allocated memory of size [B * B] that stores the resulting covariance matrix
/// @param avg is a pointer to memory of size B that stores the average spectrum
/// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location
|
a43c4fe1
heziqi
Added crop in env...
|
933
934
|
bool co_matrix(T* co, T* avg, unsigned char *mask){
//memory allocation
|
724ec347
David Mayerich
simplified the EN...
|
935
936
|
unsigned long long xy = X() * Y();
unsigned int B = Z();
|
a43c4fe1
heziqi
Added crop in env...
|
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
|
T* temp = (T*)malloc(sizeof(T) * B);
//count vaild pixels in a band
unsigned count = 0;
for (unsigned j = 0; j < xy; j++){
if (mask[j] != 0){
count++;
}
}
//initialize correlation matrix
for (unsigned i = 0; i < B; i++){
for (unsigned k = 0; k < B; k++){
co[i * B + k] = 0;
}
}
//calculate correlation coefficient matrix
for (unsigned j = 0; j < xy; j++){
if (mask[j] != 0){
pixel(temp, j);
for (unsigned i = 0; i < B; i++){
for (unsigned k = i; k < B; k++){
co[i * B + k] += (temp[i] - avg[i]) * (temp[k] - avg[k]) / count;
}
}
}
}
//because correlation matrix is symmetric
for (unsigned i = 0; i < B; i++){
for (unsigned k = i + 1; k < B; k++){
co[k * B + i] = co[i * B + k];
}
}
free(temp);
return true;
}
|
0df38ff3
heziqi
fixed head detached
|
973
|
|
a23c4132
David Mayerich
Doxygen comments ...
|
974
975
976
977
978
979
980
|
/// Crop a region of the image and save it to a new file.
/// @param outfile is the file name for the new cropped image
/// @param x0 is the lower-left x pixel coordinate to be included in the cropped image
/// @param y0 is the lower-left y pixel coordinate to be included in the cropped image
/// @param x1 is the upper-right x pixel coordinate to be included in the cropped image
/// @param y1 is the upper-right y pixel coordinate to be included in the cropped image
|
a43c4fe1
heziqi
Added crop in env...
|
981
982
983
984
985
|
bool crop(std::string outfile, unsigned x0, unsigned y0, unsigned x1, unsigned y1){
//calculate the new number of samples and lines
unsigned long long sam = x1 - x0; //samples
unsigned long long lin = y1 - y0; //lines
|
724ec347
David Mayerich
simplified the EN...
|
986
|
unsigned long long L = Z() * sizeof(T);
|
a43c4fe1
heziqi
Added crop in env...
|
987
988
989
990
|
//get specified band and save
T* temp = (T*)malloc(L);
std::ofstream out(outfile.c_str(), std::ios::binary);
//get start
|
724ec347
David Mayerich
simplified the EN...
|
991
|
unsigned long long sp = y0 * X() + x0; //start pixel
|
a43c4fe1
heziqi
Added crop in env...
|
992
993
994
995
|
for (unsigned i = 0; i < lin; i++)
{
for (unsigned j = 0; j < sam; j++)
{
|
724ec347
David Mayerich
simplified the EN...
|
996
|
pixel(temp, sp + j + i * X());
|
a43c4fe1
heziqi
Added crop in env...
|
997
998
999
1000
1001
1002
1003
|
out.write(reinterpret_cast<const char*>(temp), L); //write slice data into target file
}
}
free(temp);
return true;
}
|
0df38ff3
heziqi
fixed head detached
|
1004
|
|
a23c4132
David Mayerich
Doxygen comments ...
|
1005
|
/// Close the file.
|
f6169dea
heziqi
Ziqi completed bi...
|
1006
1007
1008
1009
1010
|
bool close(){
file.close();
return true;
}
|
88c3e636
heziqi
Ziqi added big.h
|
1011
|
};
|
6aa04ba2
David Mayerich
interleave types
|
1012
|
}
|
e8eb202f
David Mayerich
added a new ENVI ...
|
1013
1014
|
#endif
|