Commit 31394e2a6855d431fdca2fa469d90293fa8af0e8

Authored by heziqi
1 parent 64c582bb

Ziqi added method: baseline in bsq.h

Showing 1 changed file with 105 additions and 8 deletions   Show diff stats
envi/bsq.h
... ... @@ -21,27 +21,25 @@ public:
21 21 return false;
22 22 }
23 23  
24   - binary::open(filename, (header.samples, header.lines, header.bands), header.header_offset);
  24 + binary::open(filename, vec<unsigned int>(header.samples, header.lines, header.bands), header.header_offset);
25 25 return true;
26 26  
27 27 }
28 28  
29 29 //save one band of the file into the memory, and return the pointer
30   - bool saveBand( T * p, unsigned int page){
  30 + bool getBand( T * p, unsigned int page){
31 31  
32 32 if (page >= header.bands){ //make sure the bank number is right
33 33 std::cout<<"ERROR: page out of range"<<std::endl;
34 34 return false;
35 35 }
36 36  
37   - file.seekg(header.lines * header.samples * page * sizeof(T), ios::beg); //write into memory from the binary file
38   - file.read((char *)p, header.lines * header.samples * sizeof(T));
39   -
  37 + getSlice(p, 2, page);
40 38 return true;
41 39 }
42 40  
43 41 //save one pixel of the file into the memory, and return the pointer
44   - bool saveSpectrum(T * p, unsigned x, unsigned y){
  42 + bool getSpectrum(T * p, unsigned x, unsigned y){
45 43  
46 44 unsigned int i;
47 45  
... ... @@ -50,15 +48,114 @@ public:
50 48 return false;
51 49 }
52 50  
53   - file.seekg((x + y * header.samples) * sizeof(T), ios::beg); //point to the certain sample and line
  51 + file.seekg((x + y * header.samples) * sizeof(T), std::ios::beg); //point to the certain sample and line
54 52 for (i = 0; i < header.bands; i++)
55 53 {
56 54 file.read((char *)(p + i), sizeof(T));
57   - file.seekg((header.lines * header.samples - 1) * sizeof(T), ios::cur); //go to the next band
  55 + file.seekg((header.lines * header.samples - 1) * sizeof(T), std::ios::cur); //go to the next band
58 56 }
59 57  
60 58 return true;
61 59 }
62 60  
  61 + //baseline correction and save it into file
  62 + bool baseline(std::string outname, std::vector<unsigned> bands )
  63 + {
  64 + unsigned N = bands.size(); //to get points number;
  65 +
  66 + ofstream bitext(outname.c_str(), std::ios::binary); //target binary file
  67 +
  68 + unsigned i, j, k;
  69 + unsigned lownum, highnum;
  70 + unsigned control=0;
  71 +
  72 + T * low;
  73 + T * high;
  74 + T * current;
  75 + T * temp;
  76 +
  77 + low=(T *)malloc(header.lines * header.samples * sizeof(T)); //memory allocation
  78 + high=(T *)malloc(header.lines * header.samples * sizeof(T));
  79 + current=(T *)malloc(header.lines * header.samples * sizeof(T));
  80 +
  81 + if (low == NULL || high == NULL || current == NULL){
  82 + cout<<"memory allocation failure";
  83 + return false;
  84 + }
  85 +
  86 + //perform baseline correction
  87 +
  88 + //initialize lownum, highnum, low, high
  89 + lownum=0;
  90 + if(bands[0] != 0){
  91 + highnum = bands[control];
  92 + memset(low, 0.0, sizeof(T) * header.lines * header.samples);
  93 + }
  94 + else{
  95 + control += 1;
  96 + getBand(low, lownum);
  97 + highnum = bands[control];
  98 + }
  99 + getBand(high, highnum);
  100 +
  101 + //correct every band
  102 + for(k = 0; k < header.bands; k++)
  103 + {
  104 + cout<<k<<endl; //progress bar, delete it afterwards
  105 + if( k == highnum && k != header.bands-1) {
  106 + //if the high band is now on the last BL point?
  107 + if (control != N-1) {
  108 +
  109 + control += 1;
  110 +
  111 + temp=low;
  112 + low = high;
  113 + high=temp;
  114 +
  115 + lownum=highnum;
  116 + highnum = bands[control];
  117 + getBand(high, highnum);
  118 +
  119 + }
  120 + //if the last BL point on the last band of the file?
  121 + else if ( bands[control] != header.bands-1) {
  122 +
  123 + temp = low;
  124 + low = high;
  125 + high = temp;
  126 +
  127 + memset(high, 0.0, sizeof(T) * header.lines * header.samples);
  128 +
  129 + lownum = highnum;
  130 + highnum = header.bands-1;
  131 + }
  132 + }
  133 + getBand(current, k);
  134 +
  135 + for(j=0; j<header.lines; j++)
  136 + {
  137 + for(i=0; i<header.samples; i++)
  138 + {
  139 + //straight line correction
  140 + current[j * header.samples + i] = current[j * header.samples + i] - (
  141 + ( high[j * header.samples + i] - low[j * header.samples + i] ) * (T)(k - lownum) / (T)(highnum - lownum)
  142 + ) - low[j * header.samples + i];
  143 + }
  144 + }
  145 +
  146 + bitext.write(reinterpret_cast<const char*>(current), sizeof(T) * header.lines * header.samples); //write the corrected data into destination
  147 +
  148 + }
  149 +
  150 + free(low);
  151 + free(high);
  152 + free(current);
  153 + bitext.close();
  154 + return true;
  155 + }
  156 +
  157 +
63 158 };
64 159 }
  160 +
  161 +
65 162 \ No newline at end of file
... ...