Commit c30dd3e6d042a18dccdcc2538354c83a7b0205ba

Authored by David Mayerich
1 parent 9d77bbd9

added vector normalization to stim::envi

@@ -353,7 +353,7 @@ public: @@ -353,7 +353,7 @@ public:
353 /// @param outname is the name of the output file used to store the resulting baseline-corrected data. 353 /// @param outname is the name of the output file used to store the resulting baseline-corrected data.
354 /// @param w is the label specifying the band that the hyperspectral image will be normalized to. 354 /// @param w is the label specifying the band that the hyperspectral image will be normalized to.
355 /// @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. 355 /// @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.
356 - bool normalize(std::string outname, double w, unsigned char* mask = NULL, bool PROGRESS = false) 356 + bool ratio(std::string outname, double w, unsigned char* mask = NULL, bool PROGRESS = false)
357 { 357 {
358 unsigned long long B = Z(); //calculate the number of bands 358 unsigned long long B = Z(); //calculate the number of bands
359 unsigned long long ZX = Z() * X(); 359 unsigned long long ZX = Z() * X();
@@ -395,6 +395,8 @@ public: @@ -395,6 +395,8 @@ public:
395 target.close(); 395 target.close();
396 return true; 396 return true;
397 } 397 }
  398 +
  399 + void normalize(std::string outfile, unsigned char* mask = NULL, bool PROGRESS = false){}
398 400
399 /// Convert the current BIL file to a BSQ file with the specified file name. 401 /// Convert the current BIL file to a BSQ file with the specified file name.
400 402
@@ -320,7 +320,7 @@ public: @@ -320,7 +320,7 @@ public:
320 /// @param outname is the name of the output file used to store the resulting baseline-corrected data. 320 /// @param outname is the name of the output file used to store the resulting baseline-corrected data.
321 /// @param w is the label specifying the band that the hyperspectral image will be normalized to. 321 /// @param w is the label specifying the band that the hyperspectral image will be normalized to.
322 /// @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. 322 /// @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.
323 - bool normalize(std::string outname, double w, unsigned char* mask = NULL, bool PROGRESS = false) 323 + bool ratio(std::string outname, double w, unsigned char* mask = NULL, bool PROGRESS = false)
324 { 324 {
325 std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file 325 std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file
326 std::string headername = outname + ".hdr"; //the header file name 326 std::string headername = outname + ".hdr"; //the header file name
@@ -349,6 +349,8 @@ public: @@ -349,6 +349,8 @@ public:
349 target.close(); 349 target.close();
350 return true; 350 return true;
351 } 351 }
  352 +
  353 + void normalize(std::string outfile, unsigned char* mask = NULL, bool PROGRESS = false){}
352 354
353 355
354 /// Convert the current BIP file to a BIL file with the specified file name. 356 /// Convert the current BIP file to a BIL file with the specified file name.
@@ -286,7 +286,7 @@ public: @@ -286,7 +286,7 @@ public:
286 /// @param outname is the name of the output file used to store the resulting baseline-corrected data. 286 /// @param outname is the name of the output file used to store the resulting baseline-corrected data.
287 /// @param w is the label specifying the band that the hyperspectral image will be normalized to. 287 /// @param w is the label specifying the band that the hyperspectral image will be normalized to.
288 /// @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. 288 /// @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.
289 - bool normalize(std::string outname, double w, unsigned char* mask = NULL, bool PROGRESS = false) 289 + bool ratio(std::string outname, double w, unsigned char* mask = NULL, bool PROGRESS = false)
290 { 290 {
291 unsigned long long B = Z(); //calculate the number of bands 291 unsigned long long B = Z(); //calculate the number of bands
292 unsigned long long XY = X() * Y(); //calculate the number of pixels in a band 292 unsigned long long XY = X() * Y(); //calculate the number of pixels in a band
@@ -328,6 +328,47 @@ public: @@ -328,6 +328,47 @@ public:
328 return true; 328 return true;
329 } 329 }
330 330
  331 + void normalize(std::string outfile, unsigned char* mask = NULL, bool PROGRESS = false){
  332 + size_t B = Z(); //calculate the number of bands
  333 + size_t XY = X() * Y(); //calculate the number of pixels in a band
  334 + size_t XYb = XY * sizeof(T); //calculate the size of a band in bytes
  335 +
  336 + std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file
  337 + file.seekg(0, std::ios::beg); //move the pointer to the current file to the beginning
  338 +
  339 + T* len = (T*)malloc(XYb); //allocate space to store the vector length
  340 + memset(len, 0, XYb); //initialize the vector length to zero (0)
  341 +
  342 + T* band = (T*) malloc(XYb); //allocate space to store a band image
  343 +
  344 + for(size_t b = 0; b < B; b++){
  345 + file.read((char*)band, XYb);
  346 + for(size_t xy = 0; xy < XY; xy++){
  347 + if(mask == NULL || mask[xy]){
  348 + len[xy] += pow(band[xy], 2); //sum the squared value for each pixel value in the band
  349 + }
  350 + }
  351 + if(PROGRESS) progress = (double) (b+1) / (double)B * 50;
  352 + }
  353 + for(size_t xy = 0; xy < XY; xy++){ //for each pixel, calculate the square root
  354 + if(mask == NULL || mask[xy]){
  355 + len[xy] += pow(band[xy], 2); //sum the squared value for each pixel value in the band
  356 + }
  357 + }
  358 + file.seekg(0, std::ios::beg); //move the pointer to the current file to the beginning
  359 + for(size_t b = 0; b < B; b++){
  360 + file.read((char*)band, XYb);
  361 + for(size_t xy = 0; xy < XY; xy++){
  362 + if(mask == NULL || mask[xy]){
  363 + band[xy] /= len[xy]; //divide the band by the vector length
  364 + }
  365 + }
  366 + out.write((char*)band, XYb);
  367 + if(PROGRESS) progress = (double) (b+1) / (double)B * 50 + 50;
  368 + }
  369 +
  370 + }
  371 +
331 /// Convert the current BSQ file to a BIL file with the specified file name. 372 /// Convert the current BSQ file to a BIL file with the specified file name.
332 373
333 /// @param outname is the name of the output BIL file to be saved to disk. 374 /// @param outname is the name of the output BIL file to be saved to disk.
@@ -214,31 +214,31 @@ public: @@ -214,31 +214,31 @@ public:
214 /// @param outfile is the name of the normalized file to be output 214 /// @param outfile is the name of the normalized file to be output
215 /// @param band is the band label to be output 215 /// @param band is the band label to be output
216 /// @param threshold is a threshold value specified such that normalization will only be done to values in the band > threshold (preventing division by small numbers) 216 /// @param threshold is a threshold value specified such that normalization will only be done to values in the band > threshold (preventing division by small numbers)
217 - bool normalize(std::string outfile, double band, unsigned char* mask = NULL, bool PROGRESS = false){ 217 + bool ratio(std::string outfile, double band, unsigned char* mask = NULL, bool PROGRESS = false){
218 header.save(outfile + ".hdr"); 218 header.save(outfile + ".hdr");
219 if(header.interleave == envi_header::BSQ){ //if the infile is bsq file 219 if(header.interleave == envi_header::BSQ){ //if the infile is bsq file
220 if(header.data_type ==envi_header::float32) 220 if(header.data_type ==envi_header::float32)
221 - return ((bsq<float>*)file)->normalize(outfile, band, mask, PROGRESS); 221 + return ((bsq<float>*)file)->ratio(outfile, band, mask, PROGRESS);
222 else if(header.data_type == envi_header::float64) 222 else if(header.data_type == envi_header::float64)
223 - return ((bsq<double>*)file)->normalize(outfile,band, mask, PROGRESS); 223 + return ((bsq<double>*)file)->ratio(outfile,band, mask, PROGRESS);
224 else 224 else
225 std::cout<<"ERROR: unidentified data type"<<std::endl; 225 std::cout<<"ERROR: unidentified data type"<<std::endl;
226 } 226 }
227 227
228 else if(header.interleave == envi_header::BIL){ //if the infile is bil file 228 else if(header.interleave == envi_header::BIL){ //if the infile is bil file
229 if(header.data_type ==envi_header::float32) 229 if(header.data_type ==envi_header::float32)
230 - return ((bil<float>*)file)->normalize(outfile, band, mask, PROGRESS); 230 + return ((bil<float>*)file)->ratio(outfile, band, mask, PROGRESS);
231 else if(header.data_type == envi_header::float64) 231 else if(header.data_type == envi_header::float64)
232 - return ((bil<double>*)file)->normalize(outfile,band, mask, PROGRESS); 232 + return ((bil<double>*)file)->ratio(outfile,band, mask, PROGRESS);
233 else 233 else
234 std::cout<<"ERROR: unidentified data type"<<std::endl; 234 std::cout<<"ERROR: unidentified data type"<<std::endl;
235 } 235 }
236 236
237 else if(header.interleave == envi_header::BIP){ //if the infile is bip file 237 else if(header.interleave == envi_header::BIP){ //if the infile is bip file
238 if(header.data_type ==envi_header::float32) 238 if(header.data_type ==envi_header::float32)
239 - return ((bip<float>*)file)->normalize(outfile, band, mask, PROGRESS); 239 + return ((bip<float>*)file)->ratio(outfile, band, mask, PROGRESS);
240 else if(header.data_type == envi_header::float64) 240 else if(header.data_type == envi_header::float64)
241 - return ((bip<double>*)file)->normalize(outfile,band, mask, PROGRESS); 241 + return ((bip<double>*)file)->ratio(outfile,band, mask, PROGRESS);
242 else 242 else
243 std::cout<<"ERROR: unidentified data type"<<std::endl; 243 std::cout<<"ERROR: unidentified data type"<<std::endl;
244 } 244 }
@@ -250,6 +250,42 @@ public: @@ -250,6 +250,42 @@ public:
250 return false; 250 return false;
251 } 251 }
252 252
  253 + /// Perform vector normalization on the ENVI image
  254 + void normalize(std::string outfile, unsigned char* mask = NULL, bool PROGRESS = false){
  255 + header.save(outfile + ".hdr");
  256 + if(header.interleave == envi_header::BSQ){ //if the infile is bsq file
  257 + if(header.data_type ==envi_header::float32)
  258 + ((bsq<float>*)file)->normalize(outfile, mask, PROGRESS);
  259 + else if(header.data_type == envi_header::float64)
  260 + ((bsq<double>*)file)->normalize(outfile, mask, PROGRESS);
  261 + else
  262 + std::cout<<"ERROR: unidentified data type"<<std::endl;
  263 + }
  264 +
  265 + else if(header.interleave == envi_header::BIL){ //if the infile is bil file
  266 + if(header.data_type ==envi_header::float32)
  267 + ((bil<float>*)file)->normalize(outfile, mask, PROGRESS);
  268 + else if(header.data_type == envi_header::float64)
  269 + ((bil<double>*)file)->normalize(outfile, mask, PROGRESS);
  270 + else
  271 + std::cout<<"ERROR: unidentified data type"<<std::endl;
  272 + }
  273 +
  274 + else if(header.interleave == envi_header::BIP){ //if the infile is bip file
  275 + if(header.data_type ==envi_header::float32)
  276 + ((bip<float>*)file)->normalize(outfile, mask, PROGRESS);
  277 + else if(header.data_type == envi_header::float64)
  278 + ((bip<double>*)file)->normalize(outfile, mask, PROGRESS);
  279 + else
  280 + std::cout<<"ERROR: unidentified data type"<<std::endl;
  281 + }
  282 +
  283 + else{
  284 + std::cout<<"ERROR: unidentified file type"<<std::endl;
  285 + exit(1);
  286 + }
  287 + }
  288 +
253 /// Performs piecewise linear baseline correction of a hyperspectral file/ 289 /// Performs piecewise linear baseline correction of a hyperspectral file/
254 290
255 /// @param outfile is the file name for the baseline corrected output 291 /// @param outfile is the file name for the baseline corrected output
stim/visualization/colormap.h
@@ -313,10 +313,6 @@ static void cpu2cpu(T* cpuSource, unsigned char* cpuDest, unsigned long long nVa @@ -313,10 +313,6 @@ static void cpu2cpu(T* cpuSource, unsigned char* cpuDest, unsigned long long nVa
313 minVal = cpuSource[i]; 313 minVal = cpuSource[i];
314 } 314 }
315 315
316 - //if(positive)  
317 - // cpu2cpu(cpuSource, cpuDest, nVals, (T)0, maxVal, cm);  
318 - //else  
319 - // cpu2cpu(cpuSource, cpuDest, nVals, -maxVal, maxVal, cm);  
320 cpu2cpu(cpuSource, cpuDest, nVals, minVal, maxVal, cm); 316 cpu2cpu(cpuSource, cpuDest, nVals, minVal, maxVal, cm);
321 317
322 } 318 }