Commit e37ce7bf1167070f130fa7368032073a597d9d62
1 parent
3a880d1c
added the ability to build masks from other masks
Showing
5 changed files
with
45 additions
and
32 deletions
Show diff stats
stim/envi/bil.h
... | ... | @@ -828,16 +828,19 @@ public: |
828 | 828 | /// @param mask_band is the band used to specify the mask |
829 | 829 | /// @param threshold is the threshold used to determine if the mask value is true or false |
830 | 830 | /// @param p is a pointer to a pre-allocated array at least X * Y in size |
831 | - bool build_mask(unsigned char* mask, double mask_band, double threshold, bool PROGRESS = false){ | |
831 | + bool build_mask(unsigned char* out_mask, double mask_band, double lower, double upper, unsigned char* mask = NULL, bool PROGRESS = false){ | |
832 | 832 | |
833 | 833 | T* temp = (T*)malloc(X() * Y() * sizeof(T)); //allocate memory for the certain band |
834 | 834 | band(temp, mask_band, PROGRESS); |
835 | 835 | |
836 | 836 | for (unsigned long long i = 0; i < X() * Y(); i++) { |
837 | - if (temp[i] < threshold) | |
838 | - mask[i] = 0; | |
839 | - else | |
840 | - mask[i] = 255; | |
837 | + if(mask == NULL || mask[i] != 0){ | |
838 | + if(temp[i] > lower && temp[i] < upper){ | |
839 | + out_mask[i] = 255; | |
840 | + } | |
841 | + else | |
842 | + out_mask[i] = 0; | |
843 | + } | |
841 | 844 | } |
842 | 845 | |
843 | 846 | free(temp); | ... | ... |
stim/envi/bip.h
... | ... | @@ -762,16 +762,19 @@ public: |
762 | 762 | /// @param mask_band is the band used to specify the mask |
763 | 763 | /// @param threshold is the threshold used to determine if the mask value is true or false |
764 | 764 | /// @param p is a pointer to a pre-allocated array at least X * Y in size |
765 | - bool build_mask(unsigned char* mask, double mask_band, double threshold, bool PROGRESS = false){ | |
765 | + bool build_mask(unsigned char* out_mask, double mask_band, double lower, double upper, unsigned char* mask = NULL, bool PROGRESS = false){ | |
766 | 766 | |
767 | 767 | T* temp = (T*)malloc(X() * Y() * sizeof(T)); //allocate memory for the certain band |
768 | 768 | band(temp, mask_band, PROGRESS); |
769 | 769 | |
770 | 770 | for (unsigned long long i = 0; i < X() * Y();i++) { |
771 | - if (temp[i] < threshold) | |
772 | - mask[i] = 0; | |
773 | - else | |
774 | - mask[i] = 255; | |
771 | + if(mask == NULL || mask[i] != 0){ | |
772 | + if(temp[i] > lower && temp[i] < upper){ | |
773 | + out_mask[i] = 255; | |
774 | + } | |
775 | + else | |
776 | + out_mask[i] = 0; | |
777 | + } | |
775 | 778 | } |
776 | 779 | |
777 | 780 | free(temp); | ... | ... |
stim/envi/bsq.h
... | ... | @@ -914,16 +914,20 @@ public: |
914 | 914 | /// @param mask_band is the band used to specify the mask |
915 | 915 | /// @param threshold is the threshold used to determine if the mask value is true or false |
916 | 916 | /// @param p is a pointer to a pre-allocated array at least X * Y in size |
917 | - bool build_mask(unsigned char* mask, double mask_band, double threshold, bool PROGRESS = false){ | |
917 | + bool build_mask(unsigned char* out_mask, double mask_band, double lower, double upper, unsigned char* mask = NULL, bool PROGRESS = false){ | |
918 | + memset(out_mask, 0, X() * Y()); //initialize the mask to zero | |
918 | 919 | |
919 | 920 | T* temp = (T*)malloc(X() * Y() * sizeof(T)); //allocate memory for the certain band |
920 | 921 | band(temp, mask_band); |
921 | 922 | |
922 | 923 | for (unsigned long long i = 0; i < X() * Y(); i++) { |
923 | - if (temp[i] < threshold) | |
924 | - mask[i] = 0; | |
925 | - else | |
926 | - mask[i] = 255; | |
924 | + if(mask == NULL || mask[i] != 0){ | |
925 | + if(temp[i] > lower && temp[i] < upper){ | |
926 | + out_mask[i] = 255; | |
927 | + } | |
928 | + else | |
929 | + out_mask[i] = 0; | |
930 | + } | |
927 | 931 | |
928 | 932 | if(PROGRESS) progress = (double) (i+1) / (X() * Y()) * 100; |
929 | 933 | } | ... | ... |
stim/envi/envi.h
... | ... | @@ -718,31 +718,31 @@ public: |
718 | 718 | /// @param mask_band is the label for the band that will be used to build the mask |
719 | 719 | /// @param threshold is a value selected such that all band values greater than threshold will have a mask value of 'true' |
720 | 720 | /// @param p is memory of size X*Y that will store the resulting mask |
721 | - bool build_mask(unsigned char* mask, double mask_band, double threshold, bool PROGRESS = false) { | |
721 | + bool build_mask(unsigned char* out_mask, double mask_band, double lower, double upper, unsigned char* mask = NULL, bool PROGRESS = false) { | |
722 | 722 | |
723 | 723 | if(header.interleave == envi_header::BSQ){ //if the infile is bsq file |
724 | 724 | if(header.data_type ==envi_header::float32) |
725 | - return ((bsq<float>*)file)->build_mask(mask, mask_band, threshold, PROGRESS); | |
725 | + return ((bsq<float>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS); | |
726 | 726 | else if(header.data_type == envi_header::float64) |
727 | - return ((bsq<double>*)file)->build_mask(mask, mask_band, threshold, PROGRESS); | |
727 | + return ((bsq<double>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS); | |
728 | 728 | else |
729 | 729 | std::cout<<"ERROR: unidentified data type"<<std::endl; |
730 | 730 | } |
731 | 731 | |
732 | 732 | else if(header.interleave == envi_header::BIL){ //if the infile is bil file |
733 | 733 | if(header.data_type ==envi_header::float32) |
734 | - return ((bil<float>*)file)->build_mask(mask, mask_band, threshold, PROGRESS); | |
734 | + return ((bil<float>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS); | |
735 | 735 | else if(header.data_type == envi_header::float64) |
736 | - return ((bil<double>*)file)->build_mask(mask, mask_band, threshold, PROGRESS); | |
736 | + return ((bil<double>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS); | |
737 | 737 | else |
738 | 738 | std::cout<<"ERROR: unidentified data type"<<std::endl; |
739 | 739 | } |
740 | 740 | |
741 | 741 | else if(header.interleave == envi_header::BIP){ //if the infile is bip file |
742 | 742 | if(header.data_type ==envi_header::float32) |
743 | - return ((bip<float>*)file)->build_mask(mask, mask_band, threshold, PROGRESS); | |
743 | + return ((bip<float>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS); | |
744 | 744 | else if(header.data_type == envi_header::float64) |
745 | - return ((bip<double>*)file)->build_mask(mask, mask_band, threshold, PROGRESS); | |
745 | + return ((bip<double>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS); | |
746 | 746 | else |
747 | 747 | std::cout<<"ERROR: unidentified data type"<<std::endl; |
748 | 748 | } |
... | ... | @@ -751,30 +751,30 @@ public: |
751 | 751 | } |
752 | 752 | |
753 | 753 | /// Creates a mask with a true value for all pixels that contain finite values |
754 | - void mask_finite(unsigned char* mask, bool PROGRESS = false){ | |
754 | + void mask_finite(unsigned char* out_mask, unsigned char* mask = NULL, bool PROGRESS = false){ | |
755 | 755 | if(header.interleave == envi_header::BSQ){ //if the infile is bsq file |
756 | 756 | if(header.data_type ==envi_header::float32) |
757 | - ((bsq<float>*)file)->mask_finite(mask, PROGRESS); | |
757 | + ((bsq<float>*)file)->mask_finite(out_mask, mask, PROGRESS); | |
758 | 758 | else if(header.data_type == envi_header::float64) |
759 | - ((bsq<double>*)file)->mask_finite(mask, PROGRESS); | |
759 | + ((bsq<double>*)file)->mask_finite(out_mask, mask, PROGRESS); | |
760 | 760 | else |
761 | 761 | std::cout<<"ERROR: unidentified data type"<<std::endl; |
762 | 762 | } |
763 | 763 | |
764 | 764 | else if(header.interleave == envi_header::BIL){ //if the infile is bil file |
765 | 765 | if(header.data_type ==envi_header::float32) |
766 | - ((bil<float>*)file)->mask_finite(mask, PROGRESS); | |
766 | + ((bil<float>*)file)->mask_finite(out_mask, mask, PROGRESS); | |
767 | 767 | else if(header.data_type == envi_header::float64) |
768 | - ((bil<double>*)file)->mask_finite(mask, PROGRESS); | |
768 | + ((bil<double>*)file)->mask_finite(out_mask, mask, PROGRESS); | |
769 | 769 | else |
770 | 770 | std::cout<<"ERROR: unidentified data type"<<std::endl; |
771 | 771 | } |
772 | 772 | |
773 | 773 | else if(header.interleave == envi_header::BIP){ //if the infile is bip file |
774 | 774 | if(header.data_type ==envi_header::float32) |
775 | - ((bip<float>*)file)->mask_finite(mask, PROGRESS); | |
775 | + ((bip<float>*)file)->mask_finite(out_mask, mask, PROGRESS); | |
776 | 776 | else if(header.data_type == envi_header::float64) |
777 | - ((bip<double>*)file)->mask_finite(mask, PROGRESS); | |
777 | + ((bip<double>*)file)->mask_finite(out_mask, mask, PROGRESS); | |
778 | 778 | else |
779 | 779 | std::cout<<"ERROR: unidentified data type"<<std::endl; |
780 | 780 | } | ... | ... |
stim/envi/hsi.h
... | ... | @@ -139,9 +139,12 @@ protected: |
139 | 139 | |
140 | 140 | public: |
141 | 141 | /// Get a mask that has all pixels with inf or NaN values masked out (false) |
142 | - void mask_finite(unsigned char* mask, bool PROGRESS = false){ | |
142 | + void mask_finite(unsigned char* out_mask, unsigned char* mask, bool PROGRESS = false){ | |
143 | 143 | size_t XY = X() * Y(); |
144 | - memset(mask, 255, XY * sizeof(unsigned char)); //initialize the mask to zero (0) | |
144 | + if(mask == NULL) //if no mask is provided | |
145 | + memset(mask, 255, XY * sizeof(unsigned char)); //initialize the mask to 255 | |
146 | + else //if a mask is provided | |
147 | + memcpy(out_mask, mask, XY * sizeof(unsigned char)); //initialize the current mask to that one | |
145 | 148 | T* page = (T*)malloc(R[0] * R[1] * sizeof(T)); //allocate space for a page of data |
146 | 149 | |
147 | 150 | for(size_t p = 0; p < R[2]; p++){ //for each page |
... | ... | @@ -155,7 +158,7 @@ public: |
155 | 158 | #endif |
156 | 159 | size_t x, y, b; |
157 | 160 | xyb(p * R[0] * R[1] + i, x, y, b); //find the 3D coordinates of the value |
158 | - mask[ y * X() + x ] = 0; //remove the pixel (it's bad) | |
161 | + out_mask[ y * X() + x ] = 0; //remove the pixel (it's bad) | |
159 | 162 | } |
160 | 163 | } |
161 | 164 | if(PROGRESS) progress = (double)(p + 1) / (double)R[2] * 100; | ... | ... |