diff --git a/stim/envi/bil.h b/stim/envi/bil.h index d491310..bb0ec03 100644 --- a/stim/envi/bil.h +++ b/stim/envi/bil.h @@ -1182,7 +1182,7 @@ public: /// @param nbands is the number of bands to process /// @param center is the index for the center coefficient for the kernel (used to set the wavelengths in the output file) - void convolve(std::string outfile, std::vector C, size_t start, size_t end, bool PROGRESS = false){ + void convolve(std::string outfile, std::vector C, size_t start, size_t end, unsigned char* mask = NULL, bool PROGRESS = false){ std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing size_t B = end - start + 1; size_t Xb = X() * sizeof(T); //calculate the size of a band (frame) in bytes @@ -1206,7 +1206,9 @@ public: memset(outline, 0, Xb); //set the output band to zero (0) for(size_t c = 0; c < N; c++){ //for each frame (corresponding to each coefficient) for(size_t x = 0; x < X(); x++){ //for each pixel - outline[x] += (T)(C[c] * frame[c][x]); //calculate the contribution of the current frame (scaled by the corresponding coefficient) + if(mask == NULL || mask[y * X() + x]){ + outline[x] += (T)(C[c] * frame[c][x]); //calculate the contribution of the current frame (scaled by the corresponding coefficient) + } } } out.write((char*)outline, Xb); //output the band @@ -1219,8 +1221,7 @@ public: } /// Approximate the spectral derivative of the image - - void deriv(std::string outfile, size_t d, size_t order, const std::vector w = std::vector(), bool PROGRESS = false){ + void deriv(std::string outfile, size_t d, size_t order, const std::vector w = std::vector(), unsigned char* mask = NULL, bool PROGRESS = false){ std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing size_t Xb = X() * sizeof(T); //calculate the size of a line (frame) in bytes @@ -1264,7 +1265,9 @@ public: memset(outline, 0, Xb); //set the output band to zero (0) for(size_t c = 0; c < N; c++){ //for each frame (corresponding to each coefficient) for(size_t x = 0; x < X(); x++){ //for each pixel - outline[x] += (T)(C[c] * frame[c][x]); //calculate the contribution of the current frame (scaled by the corresponding coefficient) + if(mask == NULL || mask[y * X() + x]){ + outline[x] += (T)(C[c] * frame[c][x]); //calculate the contribution of the current frame (scaled by the corresponding coefficient) + } } } out.write((char*)outline, Xb); //output the band diff --git a/stim/envi/bip.h b/stim/envi/bip.h index bce3cd5..2a0a85c 100644 --- a/stim/envi/bip.h +++ b/stim/envi/bip.h @@ -1263,7 +1263,7 @@ public: /// @param nbands is the number of bands to process /// @param center is the index for the center coefficient for the kernel (used to set the wavelengths in the output file) - void convolve(std::string outfile, std::vector C, size_t start, size_t end, bool PROGRESS = false){ + void convolve(std::string outfile, std::vector C, size_t start, size_t end, unsigned char* mask = NULL, bool PROGRESS = false){ std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing size_t N = end - start + 1; //number of bands in the output spectrum @@ -1281,9 +1281,11 @@ public: for(size_t xy = 0; xy < XY; xy++){ //for each pixel file.read((char*)inspec, Bb); //read an input spectrum memset(outspec, 0, Nb); //set the output spectrum to zero (0) - for(size_t b = 0; b < N; b++){ //for each component of the spectrum - for(size_t c = 0; c < nC; c++){ //for each coefficient in the kernel - outspec[b] += (T)(inspec[b + start + c] * C[c]); //perform the sum/multiply part of the convolution + if(mask == NULL || mask[xy]){ + for(size_t b = 0; b < N; b++){ //for each component of the spectrum + for(size_t c = 0; c < nC; c++){ //for each coefficient in the kernel + outspec[b] += (T)(inspec[b + start + c] * C[c]); //perform the sum/multiply part of the convolution + } } } out.write((char*)outspec, Nb); //output the band @@ -1291,7 +1293,7 @@ public: } } - void deriv(std::string outfile, size_t d, size_t order, const std::vector w, bool PROGRESS = false){ + void deriv(std::string outfile, size_t d, size_t order, const std::vector w, unsigned char* mask = NULL, bool PROGRESS = false){ std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing @@ -1316,33 +1318,33 @@ public: for(size_t xy = 0; xy < XY; xy++){ //for each pixel file.read((char*)inspec, Bb); //read an input spectrum memset(outspec, 0, Bb); //set the output spectrum to zero (0) - - iw = 0; - for(size_t b = 0; b < mid; b++){ //for each component of the spectrum - std::vector w_pts(w.begin() + iw, w.begin() + iw + nC); //get the wavelengths corresponding to each sample - std::vector C = diff_coefficients(w[b], w_pts, d); //get the optimal sample weights - for(size_t c = 0; c < nC; c++) //for each coefficient in the kernel - outspec[b] += (T)(inspec[iw + c] * C[c]); //perform the sum/multiply part of the convolution - } - std::vector w_pts(w.begin(), w.begin() + nC); //get the wavelengths corresponding to each sample - std::vector C = diff_coefficients(w[0], w_pts, d); //get the optimal sample weights - for(size_t b = mid; b <= B - (nC - mid); b++){ - iw = b - mid; - if(!UNIFORM){ //if the spacing is non-uniform, we have to re-calculate these points every iteration + if(mask == NULL || mask[xy]){ + iw = 0; + for(size_t b = 0; b < mid; b++){ //for each component of the spectrum std::vector w_pts(w.begin() + iw, w.begin() + iw + nC); //get the wavelengths corresponding to each sample std::vector C = diff_coefficients(w[b], w_pts, d); //get the optimal sample weights + for(size_t c = 0; c < nC; c++) //for each coefficient in the kernel + outspec[b] += (T)(inspec[iw + c] * C[c]); //perform the sum/multiply part of the convolution + } + std::vector w_pts(w.begin(), w.begin() + nC); //get the wavelengths corresponding to each sample + std::vector C = diff_coefficients(w[0], w_pts, d); //get the optimal sample weights + for(size_t b = mid; b <= B - (nC - mid); b++){ + iw = b - mid; + if(!UNIFORM){ //if the spacing is non-uniform, we have to re-calculate these points every iteration + std::vector w_pts(w.begin() + iw, w.begin() + iw + nC); //get the wavelengths corresponding to each sample + std::vector C = diff_coefficients(w[b], w_pts, d); //get the optimal sample weights + } + for(size_t c = 0; c < nC; c++) //for each coefficient in the kernel + outspec[b] += (T)(inspec[iw + c] * C[c]); //perform the sum/multiply part of the convolution + } + iw = B - nC; + for(size_t b = B - (nC - mid) + 1; b < B; b++){ + std::vector w_pts(w.begin() + iw, w.begin() + iw + nC); //get the wavelengths corresponding to each sample + std::vector C = diff_coefficients(w[b], w_pts, d); //get the optimal sample weights + for(size_t c = 0; c < nC; c++) //for each coefficient in the kernel + outspec[b] += (T)(inspec[iw + c] * C[c]); //perform the sum/multiply part of the convolution } - for(size_t c = 0; c < nC; c++) //for each coefficient in the kernel - outspec[b] += (T)(inspec[iw + c] * C[c]); //perform the sum/multiply part of the convolution - } - iw = B - nC; - for(size_t b = B - (nC - mid) + 1; b < B; b++){ - std::vector w_pts(w.begin() + iw, w.begin() + iw + nC); //get the wavelengths corresponding to each sample - std::vector C = diff_coefficients(w[b], w_pts, d); //get the optimal sample weights - for(size_t c = 0; c < nC; c++) //for each coefficient in the kernel - outspec[b] += (T)(inspec[iw + c] * C[c]); //perform the sum/multiply part of the convolution } - out.write((char*)outspec, Bb); //output the band if(PROGRESS) progress = (double)(xy+1) / (double)XY * 100; } diff --git a/stim/envi/bsq.h b/stim/envi/bsq.h index c131299..78ebf73 100644 --- a/stim/envi/bsq.h +++ b/stim/envi/bsq.h @@ -8,6 +8,7 @@ #include #include #include +#include @@ -1070,7 +1071,7 @@ public: /// @param nbands is the number of bands to process /// @param center is the index for the center coefficient for the kernel (used to set the wavelengths in the output file) - void convolve(std::ofstream& out, std::vector C, size_t start, size_t end, bool PROGRESS = false){ + void convolve(std::ofstream& out, std::vector C, size_t start, size_t end, unsigned char* mask = NULL, bool PROGRESS = false){ size_t nbands = end - start + 1; size_t XY = X() * Y(); //calculate the number of values in a band size_t XYb = XY * sizeof(T); //calculate the size of a band (frame) in bytes @@ -1091,9 +1092,14 @@ public: // re-inserted into the deque. for(size_t b = 0; b < nbands; b++){ //for each band memset(outband, 0, XYb); //set the output band to zero (0) - for(size_t c = 0; c < nframes; c++){ //for each frame (corresponding to each coefficient) - for(size_t xy = 0; xy < XY; xy++){ //for each pixel - outband[xy] += (T)(C[c] * frame[c][xy]); //calculate the contribution of the current frame (scaled by the corresponding coefficient) + size_t c, xy; + double coeff; + for(c = 0; c < nframes; c++){ //for each frame (corresponding to each coefficient) + coeff = C[c]; + for(xy = 0; xy < XY; xy++){ //for each pixel + if(mask == NULL || mask[xy]){ + outband[xy] += (T)(coeff * frame[c][xy]); //calculate the contribution of the current frame (scaled by the corresponding coefficient) + } } } out.write((char*)outband, XYb); //output the band @@ -1110,9 +1116,9 @@ public: /// @param C is an array of coefficients /// @param start is the band to start processing (the first coefficient starts here) /// @param nbands is the number of bands to process - void convolve(std::string outfile, std::vector C, size_t start, size_t end, bool PROGRESS = false){ + void convolve(std::string outfile, std::vector C, size_t start, size_t end, unsigned char* mask = NULL, bool PROGRESS = false){ std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing - convolve(out, C, start, end, PROGRESS); //start the convolution + convolve(out, C, start, end, mask, PROGRESS); //start the convolution out.close(); } @@ -1122,21 +1128,21 @@ public: /// @param C is an array containing an array of coefficients for each kernel /// @param start is the list of start bands for each kernel /// @param end is the list of end bands for each kernel - void convolve(std::string outfile, std::vector< std::vector > C, std::vector start, std::vector end, bool PROGRESS = false){ + void convolve(std::string outfile, std::vector< std::vector > C, std::vector start, std::vector end, unsigned char* mask = NULL, bool PROGRESS = false){ std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing size_t K = C.size(); //get the number of kernels for(size_t k = 0; k < K; k++){ size_t b0 = start[k]; //calculate the range of the convolution size_t b1 = end[k]; - convolve(out, C[k], b0, b1, PROGRESS); //perform the convolution with the current kernel in the given range + convolve(out, C[k], b0, b1, mask, PROGRESS); //perform the convolution with the current kernel in the given range } out.close(); } /// Approximate the spectral derivative of the image - void deriv(std::string outfile, size_t d, size_t order, const std::vector w = std::vector(), bool PROGRESS = false){ + void deriv(std::string outfile, size_t d, size_t order, const std::vector w = std::vector(), unsigned char* mask = NULL, bool PROGRESS = false){ std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing size_t XY = X() * Y(); //calculate the number of values in a band @@ -1175,7 +1181,9 @@ public: memset(outband, 0, XYb); //set the output band to zero (0) for(size_t c = 0; c < N; c++){ //for each frame (corresponding to each coefficient) for(size_t xy = 0; xy < XY; xy++){ //for each pixel - outband[xy] += (T)(C[c] * frame[c][xy]); //calculate the contribution of the current frame (scaled by the corresponding coefficient) + if(mask == NULL || mask[xy]){ + outband[xy] += (T)(C[c] * frame[c][xy]); //calculate the contribution of the current frame (scaled by the corresponding coefficient) + } } } out.write((char*)outband, XYb); //output the band diff --git a/stim/envi/envi.h b/stim/envi/envi.h index 9706f6e..79e42ff 100644 --- a/stim/envi/envi.h +++ b/stim/envi/envi.h @@ -1350,7 +1350,7 @@ public: /// @param start is the band to start processing (the first coefficient starts here) /// @param nbands is the number of bands to process /// @param center is the index for the center coefficient for the kernel (used to set the wavelengths in the output file) - void convolve(std::string outfile, std::vector C, size_t start, size_t end, size_t center = 0, bool PROGRESS = false){ + void convolve(std::string outfile, std::vector C, size_t start, size_t end, size_t center = 0, unsigned char* mask = NULL, bool PROGRESS = false){ size_t nbands = end - start + 1; envi_header h = header; //copy the current header h.bands = nbands; //set the number of new bands @@ -1368,9 +1368,9 @@ public: if (header.interleave == envi_header::BSQ){ if (header.data_type == envi_header::float32) - ((bsq*)file)->convolve(outfile, C, start, end, PROGRESS); + ((bsq*)file)->convolve(outfile, C, start, end, mask, PROGRESS); else if (header.data_type == envi_header::float64) - ((bsq*)file)->convolve(outfile, C, start, end, PROGRESS); + ((bsq*)file)->convolve(outfile, C, start, end, mask, PROGRESS); else{ std::cout << "ERROR: unidentified data type" << std::endl; exit(1); @@ -1378,9 +1378,9 @@ public: } else if (header.interleave == envi_header::BIL){ if (header.data_type == envi_header::float32) - ((bil*)file)->convolve(outfile, C, start, end, PROGRESS); + ((bil*)file)->convolve(outfile, C, start, end, mask, PROGRESS); else if (header.data_type == envi_header::float64) - ((bil*)file)->convolve(outfile, C, start, end, PROGRESS); + ((bil*)file)->convolve(outfile, C, start, end, mask, PROGRESS); else{ std::cout << "ERROR: unidentified data type" << std::endl; exit(1); @@ -1388,9 +1388,9 @@ public: } else if (header.interleave == envi_header::BIP){ if (header.data_type == envi_header::float32) - ((bip*)file)->convolve(outfile, C, start, end, PROGRESS); + ((bip*)file)->convolve(outfile, C, start, end, mask, PROGRESS); else if (header.data_type == envi_header::float64) - ((bip*)file)->convolve(outfile, C, start, end, PROGRESS); + ((bip*)file)->convolve(outfile, C, start, end, mask, PROGRESS); else{ std::cout << "ERROR: unidentified data type" << std::endl; exit(1); @@ -1403,13 +1403,13 @@ public: /// @param outfile is the file where the derivative approximation will be saved /// @n is the derivative to be calculated /// @order is the order of the error (must be even) - void deriv(std::string outfile, size_t d, size_t order, bool PROGRESS = false){ + void deriv(std::string outfile, size_t d, size_t order, unsigned char* mask = NULL, bool PROGRESS = false){ header.save(outfile + ".hdr"); if (header.interleave == envi_header::BSQ){ if (header.data_type == envi_header::float32) - ((bsq*)file)->deriv(outfile, d, order, header.wavelength, PROGRESS); + ((bsq*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS); else if (header.data_type == envi_header::float64) - ((bsq*)file)->deriv(outfile, d, order, header.wavelength, PROGRESS); + ((bsq*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS); else{ std::cout << "ERROR: unidentified data type" << std::endl; exit(1); @@ -1418,9 +1418,9 @@ public: else if (header.interleave == envi_header::BIL){ if (header.data_type == envi_header::float32) - ((bil*)file)->deriv(outfile, d, order, header.wavelength, PROGRESS); + ((bil*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS); else if (header.data_type == envi_header::float64) - ((bil*)file)->deriv(outfile, d, order, header.wavelength, PROGRESS); + ((bil*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS); else{ std::cout << "ERROR: unidentified data type" << std::endl; exit(1); @@ -1429,9 +1429,9 @@ public: else if (header.interleave == envi_header::BIP){ if (header.data_type == envi_header::float32) - ((bip*)file)->deriv(outfile, d, order, header.wavelength, PROGRESS); + ((bip*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS); else if (header.data_type == envi_header::float64) - ((bip*)file)->deriv(outfile, d, order, header.wavelength, PROGRESS); + ((bip*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS); else{ std::cout << "ERROR: unidentified data type" << std::endl; exit(1); -- libgit2 0.21.4