diff --git a/matlab/enviLoadRaw.m b/matlab/enviLoadRaw.m index 5451cac..fd83c59 100644 --- a/matlab/enviLoadRaw.m +++ b/matlab/enviLoadRaw.m @@ -1,4 +1,5 @@ %loads an ENVI file without any manipulation (changing orientation) +% enviLoadRaw(filename, headername) function M = enviLoadRaw(filename, headername) %if a header isn't provided, assume it's just the filename diff --git a/matlab/enviSaveRaw.m b/matlab/enviSaveRaw.m index bc2c63d..91efc9a 100644 --- a/matlab/enviSaveRaw.m +++ b/matlab/enviSaveRaw.m @@ -1,4 +1,5 @@ %saves an ENVI file without any manipulation, assumes (X, Y, S) +% enviSaveRaw(M, filename, headername) function enviSaveRaw(M, filename, headername) %if a header isn't provided, assume it's just the filename diff --git a/stim/cuda/cudatools/error.h b/stim/cuda/cudatools/error.h index b49cf4b..738bbb4 100644 --- a/stim/cuda/cudatools/error.h +++ b/stim/cuda/cudatools/error.h @@ -11,12 +11,7 @@ using namespace std; //handle error macro static void HandleError( cudaError_t err, const char *file, int line ) { if (err != cudaSuccess) { - //FILE* outfile = fopen("cudaErrorLog.txt", "w"); - //fprintf(outfile, "%s in %s at line %d\n", cudaGetErrorString( err ), file, line ); - //fclose(outfile); printf("%s in %s at line %d\n", cudaGetErrorString( err ), file, line ); - //exit( EXIT_FAILURE ); - } } #define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ )) diff --git a/stim/envi/bil.h b/stim/envi/bil.h index 08af5f2..d68e6ca 100644 --- a/stim/envi/bil.h +++ b/stim/envi/bil.h @@ -992,32 +992,35 @@ public: /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location - bool avg_band(double* p, unsigned char* mask = NULL, bool PROGRESS = false){ + bool mean_spectrum(double* m, double* std, unsigned char* mask = NULL, bool PROGRESS = false){ unsigned long long XZ = X() * Z(); unsigned long long XY = X() * Y(); T* temp = (T*)malloc(sizeof(T) * XZ); - for (unsigned long long j = 0; j < Z(); j++){ - p[j] = 0; - } + memset(m, 0, Z() * sizeof(double)); //initialize the mean to zero + double* e_x2 = (double*)malloc(Z() * sizeof(double)); //allocate space for E[x^2] + memset(e_x2, 0, Z() * sizeof(double)); //initialize E[x^2] to zero //calculate vaild number in a band - unsigned long long count = 0; - for (unsigned long long j = 0; j < XY; j++){ - if (mask == NULL || mask[j] != 0){ - count++; - } - } + size_t count = nnz(mask); //count the number of pixels in the mask + + double x; //create a register to store the pixel value for (unsigned long long k = 0; k < Y(); k++){ read_plane_y(temp, k); unsigned long long kx = k * X(); for (unsigned long long i = 0; i < X(); i++){ if (mask == NULL || mask[kx + i] != 0){ for (unsigned long long j = 0; j < Z(); j++){ - p[j] += temp[j * X() + i] / (double)count; + x = temp[j * X() + i]; + m[j] += x / (double)count; + e_x2[j] += x*x / (double)count; } } } if(PROGRESS) progress = (double)(k+1) / Y() * 100; } + + for(size_t i = 0; i < Z(); i++) //calculate the standard deviation + std[i] = sqrt(e_x2[i] - m[i] * m[i]); + free(temp); return true; } diff --git a/stim/envi/bip.h b/stim/envi/bip.h index 31d706f..8c1925f 100644 --- a/stim/envi/bip.h +++ b/stim/envi/bip.h @@ -954,26 +954,31 @@ public: /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location - bool avg_band(double* p, unsigned char* mask = NULL, bool PROGRESS = false){ + bool mean_spectrum(double* m, double* std, unsigned char* mask = NULL, bool PROGRESS = false){ unsigned long long XY = X() * Y(); //calculate the total number of pixels in the HSI T* temp = (T*)malloc(sizeof(T) * Z()); //allocate space for the current spectrum to be read - memset(p, 0, sizeof(double) * Z()); //initialize the average spectrum to zero (0) - //for (unsigned j = 0; j < Z(); j++){ - // p[j] = 0; - //} + memset(m, 0, Z() * sizeof(double)); //set the mean spectrum to zero + double* e_x2 = (double*)malloc(Z() * sizeof(double)); //allocate space for E[x^2] + memset(e_x2, 0, Z() * sizeof(double)); //set all values for E[x^2] to zero unsigned long long count = nnz(mask); //calculate the number of masked pixels - + double x; for (unsigned long long i = 0; i < XY; i++){ //for each pixel in the HSI if (mask == NULL || mask[i] != 0){ //if the pixel is masked pixel(temp, i); //get the spectrum for (unsigned long long j = 0; j < Z(); j++){ //for each spectral component - p[j] += (double)temp[j] / (double)count; //add the weighted value to the average + x = temp[j]; + m[j] += x / (double)count; //add the weighted value to the average + e_x2[j] += x*x / (double)count; } } if(PROGRESS) progress = (double)(i+1) / XY * 100; //increment the progress } + //calculate the standard deviation + for(size_t i = 0; i < Z(); i++) + std[i] = sqrt(e_x2[i] - m[i] * m[i]); + free(temp); return true; } diff --git a/stim/envi/bsq.h b/stim/envi/bsq.h index 670c1dd..8be33fb 100644 --- a/stim/envi/bsq.h +++ b/stim/envi/bsq.h @@ -561,12 +561,12 @@ public: free(src[1]); free(dst[0]); free(dst[1]); - //if(VERBOSE){ + if(VERBOSE){ std::cout<<"total time to execute bsq::bip(): "< band_values(count); //create an STD vector of band values + + //this loops goes through each band in B (Z()) + // masked (or valid) pixels from that band are averaged and the average is stored in p + size_t k; + for (size_t i = 0; i < Z(); i++){ //for each band + band_index(temp, i); //get the band image and store it in temp + k = 0; //initialize the band_value index to zero + for (size_t j = 0; j < XY; j++){ //loop through temp, averaging valid pixels + if (mask == NULL || mask[j] != 0){ + band_values[k] = temp[j]; //store the value in the band_values array + k++; //increment the band_values index } } - if(PROGRESS) progress = (double)(i+1) / Z() * 100; + std::sort(band_values.begin(), band_values.end()); //sort all of the values in the band + m[i] = band_values[ count/2 ]; //store the center value in the array + if(PROGRESS) progress = (double)(i+1) / Z() * 100; //update the progress counter } free(temp); return true; diff --git a/stim/envi/envi.h b/stim/envi/envi.h index cd7e105..5ebba33 100644 --- a/stim/envi/envi.h +++ b/stim/envi/envi.h @@ -1380,12 +1380,12 @@ public: /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location - bool avg_band(double * p, unsigned char* mask, bool PROGRESS = false){ + bool mean_spectrum(double * p, double* std, unsigned char* mask, bool PROGRESS = false){ if (header.interleave == envi_header::BSQ){ if (header.data_type == envi_header::float32) - return ((bsq*)file)->avg_band(p, mask, PROGRESS); + return ((bsq*)file)->mean_spectrum(p, std, mask, PROGRESS); else if (header.data_type == envi_header::float64) - return ((bsq*)file)->avg_band(p, mask, PROGRESS); + return ((bsq*)file)->mean_spectrum(p, std, mask, PROGRESS); else{ std::cout << "ERROR: unidentified data type" << std::endl; exit(1); @@ -1393,9 +1393,9 @@ public: } else if (header.interleave == envi_header::BIL){ if (header.data_type == envi_header::float32) - return ((bil*)file)->avg_band(p, mask, PROGRESS); + return ((bil*)file)->mean_spectrum(p, std, mask, PROGRESS); else if (header.data_type == envi_header::float64) - return ((bil*)file)->avg_band(p, mask, PROGRESS); + return ((bil*)file)->mean_spectrum(p, std, mask, PROGRESS); else{ std::cout << "ERROR: unidentified data type" << std::endl; exit(1); @@ -1403,9 +1403,9 @@ public: } else if (header.interleave == envi_header::BIP){ if (header.data_type == envi_header::float32) - return ((bip*)file)->avg_band(p, mask, PROGRESS); + return ((bip*)file)->mean_spectrum(p, std, mask, PROGRESS); else if (header.data_type == envi_header::float64) - return ((bip*)file)->avg_band(p, mask, PROGRESS); + return ((bip*)file)->mean_spectrum(p, std, mask, PROGRESS); else{ std::cout << "ERROR: unidentified data type" << std::endl; exit(1); @@ -1414,6 +1414,28 @@ public: return false; } + /// Calculate the mean value for all masked (or valid) pixels in a band and returns the average spectrum + + /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum + /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location + bool median_spectrum(double* m, unsigned char* mask, bool PROGRESS = false){ + if (header.interleave == envi_header::BSQ){ + if (header.data_type == envi_header::float32) + return ((bsq*)file)->median_spectrum(m, mask, PROGRESS); + else if (header.data_type == envi_header::float64) + return ((bsq*)file)->median_spectrum(m, mask, PROGRESS); + else{ + std::cout << "ERROR: unidentified data type" << std::endl; + exit(1); + } + } + else{ + std::cout<<"ERROR: median calculation is only supported for BSQ interleave types. Convert to process."< glEndList(); ///finilize the display list. #ifdef DEBUG for(int i = 0; i < numSamplesPos; i++) - std::cout << pV[i] << std::endl; + std::cout << pV[i].str() << std::endl; #endif } @@ -1145,8 +1145,8 @@ class gl_spider // : public virtual gl_texture out[3] = temp[2]; } #ifdef DEBUG - std::cout << "out is " << out << std::endl; - std::cout << "when rotating from " << from << " to " << dir << std::endl; + std::cout << "out is " << out.str() << std::endl; + std::cout << "when rotating from " << from.str() << " to " << dir.str() << std::endl; #endif return out; } @@ -1531,7 +1531,7 @@ class gl_spider // : public virtual gl_texture setMagnitude(curSeedMag); #ifdef DEBUG - std::cout << "The new seed " << curSeed << curSeedVec << curSeedMag << std::endl; + std::cout << "The new seed " << curSeed.str() << curSeedVec.str() << curSeedMag << std::endl; #endif // Bind(direction_texID, direction_buffID, numSamples, n_pixels); diff --git a/stim/math/vector.h b/stim/math/vector.h index 05b35f2..d1b90f7 100644 --- a/stim/math/vector.h +++ b/stim/math/vector.h @@ -74,11 +74,11 @@ struct vec : public std::vector at(i) = other[i]; } } - + // vec( vec3& other){ // resize(3); //resize the current vector to match the copy // for(size_t i=0; i<3; i++){ //copy each element -// at(i) = other[i]; +// at(i) = other[i]; // } // } @@ -139,16 +139,16 @@ struct vec : public std::vector } - - vec cyl2cart() const - { - vec cyl; - cyl.push_back(at(0)*std::sin(at(1))); - cyl.push_back(at(0)*std::cos(at(1))); - cyl.push_back(at(2)); - return(cyl); - - } + + vec cyl2cart() const + { + vec cyl; + cyl.push_back(at(0)*std::sin(at(1))); + cyl.push_back(at(0)*std::cos(at(1))); + cyl.push_back(at(2)); + return(cyl); + + } /// Convert the vector from cartesian to spherical coordinates (x, y, z -> r, theta, phi where theta = [0, 2*pi]) vec cart2sph() const { @@ -335,16 +335,16 @@ struct vec : public std::vector return *this; } - /// Cast to a vec3 - operator stim::vec3(){ - stim::vec3 r; - size_t N = std::min(size(), 3); - for(size_t i = 0; i < N; i++) - r[i] = at(i); - return r; - } - - + /// Cast to a vec3 + operator stim::vec3(){ + stim::vec3 r; + size_t N = min(size(), (size_t)3); + for(size_t i = 0; i < N; i++) + r[i] = at(i); + return r; + } + + /// Casting and assignment template vec & operator=(vec rhs){ @@ -355,16 +355,16 @@ struct vec : public std::vector at(i) = rhs[i]; return *this; } - - /// Assign a vec = vec3 - template - vec & operator=(vec3 rhs) - { - resize(3); - for(size_t i=0; i<3; i++) - at(i) = rhs[i]; - return *this; - } + + /// Assign a vec = vec3 + template + vec & operator=(vec3 rhs) + { + resize(3); + for(size_t i=0; i<3; i++) + at(i) = rhs[i]; + return *this; + } /// Unary minus (returns the negative of the vector) vec operator-() const{ -- libgit2 0.21.4