Commit 904614c3987dfd1ad775ca77ee2d023e803a8e88

Authored by David Mayerich
1 parent 84bae1ab

added normalization for BIP files, and added get_noninterleaved to stim::image

stim/envi/bil.h
... ... @@ -386,7 +386,40 @@ public:
386 386 return true;
387 387 }
388 388  
389   - void normalize(std::string outfile, unsigned char* mask = NULL, bool PROGRESS = false){}
  389 + void normalize(std::string outfile, unsigned char* mask = NULL, bool PROGRESS = false){
  390 + std::cout<<"ERROR: algorithm not implemented"<<std::endl;
  391 + exit(1);
  392 + //This code is almost done but has to be debugged
  393 + /*std::ofstream target(outfile.c_str(), std::ios::binary); //open the target binary file
  394 +
  395 + size_t B = Z(); //calculate the number of bands
  396 + size_t L = X(); //get the number of items in a line
  397 + size_t Lb = L * sizeof(T); //calculate the number of bytes in a line
  398 + size_t XY = X() * Y(); //calculate the number of pixels in an image
  399 +
  400 + T* line = (T*) malloc(Lb); //allocate space for a line
  401 + T* len = (T*) malloc(Lb); //allocate space for the lengths in a line
  402 +
  403 + for(size_t y = 0; y < Y(); y++){ //for each line in the image
  404 + file.seekg(y * Lb, std::ios::beg); //move the pointer to the current file to the beginning
  405 + for(size_t b = 0; b < B; b++){ //for each band in this line
  406 + file.read((char*)line, Lb); //read a line of pixels
  407 + for(size_t l = 0; l < L; l++) //for each element in the line for this band
  408 + len[l] += line[l] * line[l]; //add the square of the spectral component
  409 + }
  410 + for(size_t l = 0; l < L; l++) //for each length element in the line
  411 + len[l] = sqrt(len[l]); //calculate the square root of the sum of squares
  412 +
  413 + file.seekg(y * Lb, std::ios::beg); //move the pointer to the current file to the beginning
  414 + for(size_t b = 0; b < B; b++){ //for each band in this line
  415 + file.read((char*)line, Lb); //read a line of pixels
  416 + for(size_t l = 0; l < L; l++) //for each element in the line for this band
  417 + line[l] /= len[l]; //divide each element by the length
  418 + }
  419 + target.write((char*)line, Lb); //write the normalized line to the target file
  420 + if(PROGRESS) progress = (double)(y+1) / (double)Y();
  421 + }*/
  422 + }
390 423  
391 424 /// Convert the current BIL file to a BSQ file with the specified file name.
392 425  
... ...
stim/envi/bip.h
... ... @@ -353,7 +353,34 @@ public:
353 353 return true;
354 354 }
355 355  
356   - void normalize(std::string outfile, unsigned char* mask = NULL, bool PROGRESS = false){}
  356 + void normalize(std::string outfile, unsigned char* mask = NULL, bool PROGRESS = false){
  357 +
  358 + std::ofstream target(outfile.c_str(), std::ios::binary); //open the target binary file
  359 + file.seekg(0, std::ios::beg); //move the pointer to the current file to the beginning
  360 +
  361 + size_t B = Z(); //number of spectral components
  362 + size_t XY = X() * Y(); //calculate the number of pixels
  363 + size_t Bb = B * sizeof(T); //number of bytes in a spectrum
  364 +
  365 + T* spec = (T*) malloc(Bb); //allocate space for the spectrum
  366 + T len;
  367 + for(size_t xy = 0; xy < XY; xy++){ //for each pixel
  368 + memset(spec, 0, Bb); //set the spectrum to zero
  369 + if(mask == NULL || mask[xy]){ //if the pixel is masked
  370 + len = 0; //initialize the
  371 + file.read((char*)spec, Bb); //read a spectrum
  372 + for(size_t b = 0; b < B; b++) //for each band
  373 + len += spec[b]*spec[b]; //add the square of the spectral band
  374 + len = sqrt(len); //calculate the square of the sum of squared components
  375 + for(size_t b = 0; b < B; b++) //for each band
  376 + spec[b] /= len; //divide by the length
  377 + }
  378 + else
  379 + file.seekg(Bb, std::ios::cur); //otherwise skip a spectrum
  380 + target.write((char*)spec, Bb); //output the normalized spectrum
  381 + if(PROGRESS) progress = (double)(xy + 1) / (double)XY * 100; //update the progress
  382 + }
  383 + }
357 384  
358 385  
359 386 /// Convert the current BIP file to a BIL file with the specified file name.
... ...
stim/image/image.h
... ... @@ -186,6 +186,18 @@ public:
186 186 memcpy(data, img, bytes());
187 187 }
188 188  
  189 + //copies data in the given channel order as a non-interleaved image
  190 + void get_noninterleaved(T* data){
  191 + //for each channel
  192 + for(size_t y = 0; y < Y(); y++){
  193 + for(size_t x = 0; x < X(); x++){
  194 + for(size_t c = 0; c < C(); c++){
  195 + data[c * Y() * X() + y * X() + x] = img[idx(x, y, c)];
  196 + }
  197 + }
  198 + }
  199 + }
  200 +
189 201  
190 202 image<T> channel(size_t c){
191 203  
... ...
stim/parser/arguments.h
... ... @@ -23,18 +23,50 @@ namespace stim{
23 23 //argument name
24 24 std::string name;
25 25  
26   - //description of the argument
  26 + //description of the option
27 27 std::vector<std::string> desc;
28 28  
29 29 //argument values
30 30 std::vector<std::string> vals;
31 31  
  32 + //integral and numerical flags for each argument
  33 + std::vector<bool> integral;
  34 + std::vector<bool> numerical;
  35 +
32 36 //range or example
33 37 std::string range;
34 38  
35 39 //flag is true when the argument is user-specified
36 40 bool flag;
37 41  
  42 + bool char_numerical(char c){
  43 + if( (c >= '0' && c <= '9') || c == '-' || c == '.')
  44 + return true;
  45 + }
  46 +
  47 + bool char_integral(char c){
  48 + if( (c >= '0' && c <= '9') || c == '-')
  49 + return true;
  50 + }
  51 +
  52 + /// Test if a given string contains a numerical (real) value
  53 + bool test_numerical(std::string v){
  54 + for(size_t i = 0; i < v.length(); i++){ //for each character in the string
  55 + if(!char_numerical(v[i]))
  56 + return false;
  57 + }
  58 + return true;
  59 + }
  60 +
  61 + /// Test if a given string contains an integer value
  62 + bool test_integral(std::string v){
  63 + for(size_t i = 0; i < v.length(); i++){ //for each character in the string
  64 + if(!char_integral(v[i]))
  65 + return false;
  66 + }
  67 + return true;
  68 + }
  69 +
38 70 void parse_val(const std::string &s){
39 71  
40 72 vals.clear();
... ...