diff --git a/envi/bil.h b/envi/bil.h index 47ad258..16edb2c 100644 --- a/envi/bil.h +++ b/envi/bil.h @@ -164,13 +164,25 @@ public: file.seekg((y * R[0] * R[2] + x) * sizeof(T), std::ios::beg); for(unsigned i = 0; i < R[2]; i++) - { //point to the certain sample and line + { + //point to the certain sample and line file.read((char *)(p + i), sizeof(T)); file.seekg(jump, std::ios::cur); } return true; } + + //save one pixel into memory + bool pixel(T * p, unsigned n){ + + //calculate the corresponding x, y + unsigned int x = n % R[0]; + unsigned int y = n / R[0]; + + //get the pixel + return spectrum(p, x, y); + } //given a Y ,return a XZ slice bool getY(T * p, unsigned y) diff --git a/envi/bip.h b/envi/bip.h index f929e20..c1e9b61 100644 --- a/envi/bip.h +++ b/envi/bip.h @@ -261,6 +261,20 @@ public: return true; } + + //save one pixel into memory + bool pixel(T * p, unsigned n){ + + unsigned bandnum = R[0] * R[1]; //calculate numbers in one band + if ( n >= bandnum){ //make sure the pixel number is right + std::cout<<"ERROR: sample or line out of range"<= bandnum){ //make sure the pixel number is right + std::cout<<"ERROR: sample or line out of range"< wls ) diff --git a/envi/envi.h b/envi/envi.h index 8dc7977..8d1eb07 100644 --- a/envi/envi.h +++ b/envi/envi.h @@ -5,6 +5,8 @@ #include "../envi/bsq.h" #include "../envi/bip.h" #include "../envi/bil.h" +#include +#include "../../CImg/CImg.h" namespace stim{ @@ -524,6 +526,40 @@ public: } return false; } + //get one pixel from binary file + bool pixel(void * p, unsigned n){ + if(header.interleave == envi_header::BSQ){ + if(header.data_type ==envi_header::float32) + return ((bsq*)file)->pixel((float*)p, n); + else if(header.data_type == envi_header::float64) + return ((bsq*)file)->pixel((double*)p, n); + else{ + std::cout<<"ERROR: unidentified data type"<*)file)->pixel((float*)p, n); + else if(header.data_type == envi_header::float64) + return ((bil*)file)->pixel((double*)p, n); + else{ + std::cout<<"ERROR: unidentified data type"<*)file)->pixel((float*)p, n); + else if(header.data_type == envi_header::float64) + return ((bip*)file)->pixel((double*)p, n); + else{ + std::cout<<"ERROR: unidentified data type"< mask_image(maskname.c_str(),true); + //save mask file into memory + for (unsigned i = 0; i < header.lines * header.samples; i++) + mask[i] = mask_image.at(i); //how to copy value from cimg class? + //close mask file + mask_image.clear(); + return true; + }*/ - + //p:start positon; N: number of pixels saved in X; + bool feature_matrix(void * X, std::string maskname, unsigned char * mask, unsigned start, unsigned N) + { + cimg_library::CImg mask_image(maskname.c_str()); + //save mask file into memory + mask = mask_image.data(); + //save pixels in X as floating numbers + float * p = (float *)malloc(header.bands * sizeof(float)); + + //use mask to save valid pixel in X + unsigned bandnum = header.samples * header.lines; //calculate pixel numbers in a band + unsigned count = 0; //for counting use + unsigned j = 0; //memory the pointer location in X + + for(unsigned i = start; i < bandnum; i++){ + if(mask[i] != 0){ + pixel(p, i); + //copy p to X + for(unsigned k = 0; k < header.bands; k++){ + ((float *)X)[j] = p[k]; + j++; + } + count++; + if(count == N) + break; + } + else + continue; + } + if(count < N){ + std::cout << "number of valid pixels in the mask : " << count <<"is less than N: "<< N; + exit(1); + } + return true; + } -- libgit2 0.21.4