Commit bfe9c6db5bec258b7992a0c9bc63f1d3ff547904

Authored by heziqi
1 parent 1582ce07

added feature_matrix in envi class

Showing 4 changed files with 130 additions and 3 deletions   Show diff stats
envi/bil.h
... ... @@ -164,13 +164,25 @@ public:
164 164 file.seekg((y * R[0] * R[2] + x) * sizeof(T), std::ios::beg);
165 165  
166 166 for(unsigned i = 0; i < R[2]; i++)
167   - { //point to the certain sample and line
  167 + {
  168 + //point to the certain sample and line
168 169 file.read((char *)(p + i), sizeof(T));
169 170 file.seekg(jump, std::ios::cur);
170 171 }
171 172  
172 173 return true;
173 174 }
  175 +
  176 + //save one pixel into memory
  177 + bool pixel(T * p, unsigned n){
  178 +
  179 + //calculate the corresponding x, y
  180 + unsigned int x = n % R[0];
  181 + unsigned int y = n / R[0];
  182 +
  183 + //get the pixel
  184 + return spectrum(p, x, y);
  185 + }
174 186  
175 187 //given a Y ,return a XZ slice
176 188 bool getY(T * p, unsigned y)
... ...
envi/bip.h
... ... @@ -261,6 +261,20 @@ public:
261 261  
262 262 return true;
263 263 }
  264 +
  265 + //save one pixel into memory
  266 + bool pixel(T * p, unsigned n){
  267 +
  268 + unsigned bandnum = R[0] * R[1]; //calculate numbers in one band
  269 + if ( n >= bandnum){ //make sure the pixel number is right
  270 + std::cout<<"ERROR: sample or line out of range"<<std::endl;
  271 + return false;
  272 + }
  273 +
  274 + file.seekg(n * R[2] * sizeof(T), std::ios::beg); //point to the certain pixel
  275 + file.read((char *)p, sizeof(T) * R[2]);
  276 + return true;
  277 + }
264 278  
265 279 //given a Y ,return a ZX slice
266 280 bool getY(T * p, unsigned y)
... ...
envi/bsq.h
... ... @@ -118,7 +118,7 @@ public:
118 118 }
119 119  
120 120 file.seekg((x + y * R[0]) * sizeof(T), std::ios::beg); //point to the certain sample and line
121   - for (i = 0; i < R[3]; i++)
  121 + for (i = 0; i < R[2]; i++)
122 122 {
123 123 file.read((char *)(p + i), sizeof(T));
124 124 file.seekg((R[1] * R[0] - 1) * sizeof(T), std::ios::cur); //go to the next band
... ... @@ -127,6 +127,25 @@ public:
127 127 return true;
128 128 }
129 129  
  130 + //save one pixel into memory
  131 + bool pixel(T * p, unsigned n){
  132 +
  133 + unsigned bandnum = R[0] * R[1]; //calculate numbers in one band
  134 + if ( n >= bandnum){ //make sure the pixel number is right
  135 + std::cout<<"ERROR: sample or line out of range"<<std::endl;
  136 + return false;
  137 + }
  138 +
  139 + file.seekg(n * sizeof(T), std::ios::beg); //point to the certain pixel
  140 + for (unsigned i = 0; i < R[2]; i++)
  141 + {
  142 + file.read((char *)(p + i), sizeof(T));
  143 + file.seekg((bandnum - 1) * sizeof(T), std::ios::cur); //go to the next band
  144 + }
  145 +
  146 + return true;
  147 + }
  148 +
130 149 //baseline correction and save it into file
131 150  
132 151 bool baseline(std::string outname, std::vector<double> wls )
... ...
envi/envi.h
... ... @@ -5,6 +5,8 @@
5 5 #include "../envi/bsq.h"
6 6 #include "../envi/bip.h"
7 7 #include "../envi/bil.h"
  8 +#include <iostream>
  9 +#include "../../CImg/CImg.h"
8 10  
9 11 namespace stim{
10 12  
... ... @@ -524,6 +526,40 @@ public:
524 526 }
525 527 return false;
526 528 }
  529 + //get one pixel from binary file
  530 + bool pixel(void * p, unsigned n){
  531 + if(header.interleave == envi_header::BSQ){
  532 + if(header.data_type ==envi_header::float32)
  533 + return ((bsq<float>*)file)->pixel((float*)p, n);
  534 + else if(header.data_type == envi_header::float64)
  535 + return ((bsq<double>*)file)->pixel((double*)p, n);
  536 + else{
  537 + std::cout<<"ERROR: unidentified data type"<<std::endl;
  538 + exit(1);
  539 + }
  540 + }
  541 + else if(header.interleave == envi_header::BIL){
  542 + if(header.data_type ==envi_header::float32)
  543 + return ((bil<float>*)file)->pixel((float*)p, n);
  544 + else if(header.data_type == envi_header::float64)
  545 + return ((bil<double>*)file)->pixel((double*)p, n);
  546 + else{
  547 + std::cout<<"ERROR: unidentified data type"<<std::endl;
  548 + exit(1);
  549 + }
  550 + }
  551 + else if(header.interleave == envi_header::BIP){
  552 + if(header.data_type ==envi_header::float32)
  553 + return ((bip<float>*)file)->pixel((float*)p, n);
  554 + else if(header.data_type == envi_header::float64)
  555 + return ((bip<double>*)file)->pixel((double*)p, n);
  556 + else{
  557 + std::cout<<"ERROR: unidentified data type"<<std::endl;
  558 + exit(1);
  559 + }
  560 + }
  561 + return false;
  562 + }
527 563  
528 564 bool save_header(std::string filename){
529 565  
... ... @@ -551,8 +587,54 @@ public:
551 587 }
552 588 return false;
553 589 }
  590 + /*
  591 + //load mask from maskfile and save it into memory
  592 + bool load_mask(unsigned char * mask, std::string maskname){
  593 + //open the mask file
  594 + cimg_library::CImg<unsigned char> mask_image(maskname.c_str(),true);
  595 + //save mask file into memory
  596 + for (unsigned i = 0; i < header.lines * header.samples; i++)
  597 + mask[i] = mask_image.at(i); //how to copy value from cimg class?
  598 + //close mask file
  599 + mask_image.clear();
  600 + return true;
  601 + }*/
554 602  
555   -
  603 + //p:start positon; N: number of pixels saved in X;
  604 + bool feature_matrix(void * X, std::string maskname, unsigned char * mask, unsigned start, unsigned N)
  605 + {
  606 + cimg_library::CImg<unsigned char> mask_image(maskname.c_str());
  607 + //save mask file into memory
  608 + mask = mask_image.data();
  609 + //save pixels in X as floating numbers
  610 + float * p = (float *)malloc(header.bands * sizeof(float));
  611 +
  612 + //use mask to save valid pixel in X
  613 + unsigned bandnum = header.samples * header.lines; //calculate pixel numbers in a band
  614 + unsigned count = 0; //for counting use
  615 + unsigned j = 0; //memory the pointer location in X
  616 +
  617 + for(unsigned i = start; i < bandnum; i++){
  618 + if(mask[i] != 0){
  619 + pixel(p, i);
  620 + //copy p to X
  621 + for(unsigned k = 0; k < header.bands; k++){
  622 + ((float *)X)[j] = p[k];
  623 + j++;
  624 + }
  625 + count++;
  626 + if(count == N)
  627 + break;
  628 + }
  629 + else
  630 + continue;
  631 + }
  632 + if(count < N){
  633 + std::cout << "number of valid pixels in the mask : " << count <<"is less than N: "<< N;
  634 + exit(1);
  635 + }
  636 + return true;
  637 + }
556 638  
557 639  
558 640  
... ...