Commit a43c4fe1a8ecec4fdc99cfcd4b4518d2ab512be1

Authored by heziqi
1 parent 84a4e0f1

Added crop in envi class

Showing 4 changed files with 447 additions and 0 deletions   Show diff stats
envi/bil.h
... ... @@ -727,6 +727,130 @@ public:
727 727 return true;
728 728 }
729 729  
  730 + //calculate the average band value
  731 + bool band_avg(T* p){
  732 + unsigned long long XZ = R[0] * R[2];
  733 + T* temp = (T*)malloc(sizeof(T) * XZ);
  734 + T* line = (T*)malloc(sizeof(T) * R[0]);
  735 +
  736 + for (unsigned i = 0; i < R[1]; i++){
  737 + getY(temp, i);
  738 + //initialize x-line
  739 + for (unsigned j = 0; j < R[0]; j++){
  740 + line[j] = 0;
  741 + }
  742 + unsigned c = 0;
  743 + for (unsigned j = 0; j < R[2]; j++){
  744 + for (unsigned k = 0; k < R[0]; k++){
  745 + line[k] += temp[c] / (T)R[2];
  746 + c++;
  747 + }
  748 + }
  749 + for (unsigned j = 0; j < R[0]; j++){
  750 + p[j + i * R[0]] = line[j];
  751 + }
  752 + }
  753 + free(temp);
  754 + return true;
  755 + }
  756 +
  757 + //calculate the average number of every band
  758 + bool avg_band(T*p, unsigned char* mask){
  759 + unsigned long long XZ = R[0] * R[2];
  760 + unsigned long long XY = R[0] * R[1];
  761 + T* temp = (T*)malloc(sizeof(T) * XZ);
  762 + for (unsigned j = 0; j < R[2]; j++){
  763 + p[j] = 0;
  764 + }
  765 + //calculate vaild number in a band
  766 + unsigned count = 0;
  767 + for (unsigned j = 0; j < XY; j++){
  768 + if (mask[j] != 0){
  769 + count++;
  770 + }
  771 + }
  772 + for (unsigned k = 0; k < R[1]; k++){
  773 + getY(temp, k);
  774 + unsigned kx = k * R[0];
  775 + for (unsigned i = 0; i < R[0]; i++){
  776 + if (mask[kx + i] != 0){
  777 + for (unsigned j = 0; j < R[2]; j++){
  778 + p[j] += temp[j * R[0] + i] / (T)count;
  779 + }
  780 + }
  781 + }
  782 + }
  783 + free(temp);
  784 + return true;
  785 + }
  786 +
  787 + //calculate correlation coefficient matrix
  788 + bool co_matrix(T* co, T* avg, unsigned char *mask){
  789 + //memory allocation
  790 + unsigned long long xy = R[0] * R[1];
  791 + unsigned int B = R[2];
  792 + T* temp = (T*)malloc(sizeof(T) * B);
  793 + //count vaild pixels in a band
  794 + unsigned count = 0;
  795 + for (unsigned j = 0; j < xy; j++){
  796 + if (mask[j] != 0){
  797 + count++;
  798 + }
  799 + }
  800 + //initialize correlation matrix
  801 + for (unsigned i = 0; i < B; i++){
  802 + for (unsigned k = 0; k < B; k++){
  803 + co[i * B + k] = 0;
  804 + }
  805 + }
  806 + //calculate correlation coefficient matrix
  807 + for (unsigned j = 0; j < xy; j++){
  808 + if (mask[j] != 0){
  809 + pixel(temp, j);
  810 + for (unsigned i = 0; i < B; i++){
  811 + for (unsigned k = i; k < B; k++){
  812 + co[i * B + k] += (temp[i] - avg[i]) * (temp[k] - avg[k]) / count;
  813 + }
  814 + }
  815 + }
  816 + }
  817 + //because correlation matrix is symmetric
  818 + for (unsigned i = 0; i < B; i++){
  819 + for (unsigned k = i + 1; k < B; k++){
  820 + co[k * B + i] = co[i * B + k];
  821 + }
  822 + }
  823 +
  824 + free(temp);
  825 + return true;
  826 + }
  827 +
  828 + //crop specified area the of the original file
  829 + bool crop(std::string outfile, unsigned x0, unsigned y0, unsigned x1, unsigned y1){
  830 +
  831 + //calculate the new number of samples and lines
  832 + unsigned long long sam = x1 - x0; //samples
  833 + unsigned long long lin = y1 - y0; //lines
  834 + unsigned long long L = sam * R[2] * sizeof(T);
  835 + //get specified band and save
  836 + T* temp = (T*)malloc(L);
  837 + std::ofstream out(outfile.c_str(), std::ios::binary);
  838 + unsigned long long jumpb = (R[0] - sam) * sizeof(T); //jump pointer to the next band
  839 + //get start
  840 + file.seekg((y0 * R[0] * R[2] + x0) * sizeof(T), std::ios::beg);
  841 + for (unsigned i = 0; i < lin; i++)
  842 + {
  843 + for (unsigned j = 0; j < R[2]; j++)
  844 + {
  845 + file.read((char *)(temp + j * sam), sizeof(T) * sam);
  846 + file.seekg(jumpb, std::ios::cur); //go to the next band
  847 + }
  848 + out.write(reinterpret_cast<const char*>(temp), L); //write slice data into target file
  849 + }
  850 + free(temp);
  851 + return true;
  852 + }
  853 +
730 854 //close the file
731 855 bool close(){
732 856 file.close();
... ...
envi/bip.h
... ... @@ -821,6 +821,117 @@ public:
821 821 return true;
822 822 }
823 823  
  824 + //calculate the average band of the file
  825 + bool band_avg(T* p){
  826 + unsigned long long XY = R[0] * R[1];
  827 + //get every pixel and calculate average value
  828 + T* temp = (T*)malloc(sizeof(T) * R[2]);
  829 + T sum;
  830 + for (unsigned i = 0; i < XY; i++){
  831 + pixel(temp, i);
  832 + //calculate the sum value of every value
  833 + sum = 0; //initialize sum value
  834 + for (unsigned j = 0; j < R[2]; j++){
  835 + sum += temp[j]/(T)R[2];
  836 + }
  837 + p[i] = sum;
  838 + }
  839 + free(temp);
  840 + return true;
  841 + }
  842 +
  843 + //calculate the average number of every band
  844 + bool avg_band(T*p, unsigned char* mask){
  845 + unsigned long long XY = R[0] * R[1];
  846 + T* temp = (T*)malloc(sizeof(T) * R[2]);
  847 + //Iinitialize
  848 + for (unsigned j = 0; j < R[2]; j++){
  849 + p[j] = 0;
  850 + }
  851 + //calculate vaild number in a band
  852 + unsigned count = 0;
  853 + for (unsigned j = 0; j < XY; j++){
  854 + if (mask[j] != 0){
  855 + count++;
  856 + }
  857 + }
  858 + //calculate average number of a band
  859 + for (unsigned i = 0; i < XY; i++){
  860 + if (mask[i] != 0){
  861 + pixel(temp, i);
  862 + for (unsigned j = 0; j < R[2]; j++){
  863 + p[j] += temp[j] / (T)count;
  864 + }
  865 + }
  866 + }
  867 + free(temp);
  868 + return true;
  869 + }
  870 + //calculate correlation coefficient matrix
  871 + bool co_matrix(T* co, T* avg, unsigned char *mask){
  872 + //memory allocation
  873 + unsigned long long xy = R[0] * R[1];
  874 + unsigned int B = R[2];
  875 + T* temp = (T*)malloc(sizeof(T) * B);
  876 + //count vaild pixels in a band
  877 + unsigned count = 0;
  878 + for (unsigned j = 0; j < xy; j++){
  879 + if (mask[j] != 0){
  880 + count++;
  881 + }
  882 + }
  883 + //initialize correlation matrix
  884 + for (unsigned i = 0; i < B; i++){
  885 + for (unsigned k = 0; k < B; k++){
  886 + co[i * B + k] = 0;
  887 + }
  888 + }
  889 + //calculate correlation coefficient matrix
  890 + for (unsigned j = 0; j < xy; j++){
  891 + if (mask[j] != 0){
  892 + pixel(temp, j);
  893 + for (unsigned i = 0; i < B; i++){
  894 + for (unsigned k = i; k < B; k++){
  895 + co[i * B + k] += (temp[i] - avg[i]) * (temp[k] - avg[k]) / count;
  896 + }
  897 + }
  898 + }
  899 + }
  900 + //because correlation matrix is symmetric
  901 + for (unsigned i = 0; i < B; i++){
  902 + for (unsigned k = i + 1; k < B; k++){
  903 + co[k * B + i] = co[i * B + k];
  904 + }
  905 + }
  906 +
  907 + free(temp);
  908 + return true;
  909 + }
  910 +
  911 + //crop specified area the of the original file
  912 + bool crop(std::string outfile, unsigned x0, unsigned y0, unsigned x1, unsigned y1){
  913 +
  914 + //calculate the new number of samples and lines
  915 + unsigned long long sam = x1 - x0; //samples
  916 + unsigned long long lin = y1 - y0; //lines
  917 + unsigned long long L = R[2] * sizeof(T);
  918 + //get specified band and save
  919 + T* temp = (T*)malloc(L);
  920 + std::ofstream out(outfile.c_str(), std::ios::binary);
  921 + //get start
  922 + unsigned long long sp = y0 * R[0] + x0; //start pixel
  923 + for (unsigned i = 0; i < lin; i++)
  924 + {
  925 + for (unsigned j = 0; j < sam; j++)
  926 + {
  927 + pixel(temp, sp + j + i * R[0]);
  928 + out.write(reinterpret_cast<const char*>(temp), L); //write slice data into target file
  929 + }
  930 + }
  931 + free(temp);
  932 + return true;
  933 + }
  934 +
824 935 //close the file
825 936 bool close(){
826 937 file.close();
... ...
envi/bsq.h
... ... @@ -663,6 +663,115 @@ public:
663 663 return true;
664 664 }
665 665  
  666 + //calculate the average band
  667 + bool band_avg(T* p){
  668 + unsigned long long XY = R[0] * R[1];
  669 + T* temp = (T*)malloc(sizeof(T) * XY);
  670 + //initialize p
  671 + band_index(p, 0);
  672 + for (unsigned j = 0; j < XY; j++){
  673 + p[j] /= (T)R[2];
  674 + }
  675 + //get every band and add them all
  676 + for (unsigned i = 1; i < R[2]; i++){
  677 + band_index(temp, i);
  678 + for (unsigned j = 0; j < XY; j++){
  679 + p[j] += temp[j]/(T)R[2];
  680 + }
  681 + }
  682 + free(temp);
  683 + return true;
  684 + }
  685 +
  686 + //calculate the average number of every band
  687 + bool avg_band(T*p, unsigned char* mask){
  688 + unsigned long long XY = R[0] * R[1];
  689 + unsigned count = 0; //number of vaild pixel in a band
  690 + T* temp = (T*)malloc(sizeof(T) * XY);
  691 + //calculate valid pixel number
  692 + for (unsigned j = 0; j < XY; j++){
  693 + if (mask[j] != 0){
  694 + count++;
  695 + }
  696 + }
  697 + //calculate average of a band
  698 + for (unsigned i = 0; i < R[2]; i++){
  699 + p[i] = 0;
  700 + band_index(temp, i);
  701 + for (unsigned j = 0; j < XY; j++){
  702 + if (mask[j] != 0){
  703 + p[i] += temp[j] / (T)count;
  704 + }
  705 + }
  706 + }
  707 + free(temp);
  708 + return true;
  709 + }
  710 +
  711 + //calculate correlated matrix
  712 + bool co_matrix(T* co, T* avg, unsigned char *mask){
  713 + //memory allocation
  714 + unsigned long long xy = R[0] * R[1];
  715 + unsigned int B = R[2];
  716 + T* bandi = (T*)malloc(sizeof(T) * xy);
  717 + T* bandj = (T*)malloc(sizeof(T) * xy);
  718 +
  719 + //count vaild pixels in a band
  720 + unsigned count = 0;
  721 + for (unsigned j = 0; j < xy; j++){
  722 + if (mask[j] != 0){
  723 + count++;
  724 + }
  725 + }
  726 + //calculate correlation coefficient matrix
  727 + for (unsigned i = 0; i < B; i++)
  728 + {
  729 + band_index(bandi, i);
  730 + for (unsigned j = i; j < B; j++){
  731 + band_index(bandj, j);
  732 + T numerator = 0; //to calculate element in correlation coefficient matrix, numerator part
  733 + //calculate the R(i,j) in correlation coeffient matrix
  734 + for (unsigned k = 0; k < xy; k++){
  735 + if (mask[k] != 0){
  736 + numerator += (bandi[k] - avg[i]) * (bandj[k] - avg[j]) / count;
  737 + }
  738 + }
  739 + co[i*B + j] = numerator;
  740 + co[j*B + i] = numerator; //because correlated matrix is a symmetric matrix
  741 + }
  742 + }
  743 + free(bandi);
  744 + free(bandj);
  745 + return true;
  746 + }
  747 +
  748 + //crop specified area the of the original file
  749 + bool crop(std::string outfile, unsigned x0, unsigned y0, unsigned x1, unsigned y1){
  750 +
  751 + //calculate the new number of samples and lines
  752 + unsigned long long sam = x1 - x0; //samples
  753 + unsigned long long lin = y1 - y0; //lines
  754 + unsigned long long L = sam * lin * sizeof(T);
  755 + //get specified band and save
  756 + T* temp = (T*)malloc(L);
  757 + std::ofstream out(outfile.c_str(), std::ios::binary);
  758 + unsigned long long jumpb = R[0] * (R[1] - lin) * sizeof(T); //jump pointer to the next band
  759 + unsigned long long jumpl = (R[0] - sam) * sizeof(T); //jump pointer to the next line
  760 + //get start
  761 + file.seekg((y0 * R[0] + x0) * sizeof(T), std::ios::beg);
  762 + for (unsigned i = 0; i < R[2]; i++)
  763 + {
  764 + for (unsigned j = 0; j < lin; j++)
  765 + {
  766 + file.read((char *)(temp + j * sam), sizeof(T) * sam);
  767 + file.seekg(jumpl, std::ios::cur); //go to the next band
  768 + }
  769 + out.write(reinterpret_cast<const char*>(temp), L); //write slice data into target file
  770 + file.seekg(jumpb, std::ios::cur);
  771 + }
  772 + free(temp);
  773 + return true;
  774 + }
666 775  
667 776 //close the file
668 777 bool close(){
... ...
envi/envi.h
... ... @@ -642,8 +642,111 @@ public:
642 642 return true;
643 643 }
644 644  
  645 + //calculate band average
  646 + bool avg_band(void * p, unsigned char* mask){
  647 + if (header.interleave == envi_header::BSQ){
  648 + if (header.data_type == envi_header::float32)
  649 + return ((bsq<float>*)file)->avg_band((float*)p, mask);
  650 + else if (header.data_type == envi_header::float64)
  651 + return ((bsq<double>*)file)->avg_band((double*)p, mask);
  652 + else{
  653 + std::cout << "ERROR: unidentified data type" << std::endl;
  654 + exit(1);
  655 + }
  656 + }
  657 + else if (header.interleave == envi_header::BIL){
  658 + if (header.data_type == envi_header::float32)
  659 + return ((bil<float>*)file)->avg_band((float*)p, mask);
  660 + else if (header.data_type == envi_header::float64)
  661 + return ((bil<double>*)file)->avg_band((double*)p, mask);
  662 + else{
  663 + std::cout << "ERROR: unidentified data type" << std::endl;
  664 + exit(1);
  665 + }
  666 + }
  667 + else if (header.interleave == envi_header::BIP){
  668 + if (header.data_type == envi_header::float32)
  669 + return ((bip<float>*)file)->avg_band((float*)p, mask);
  670 + else if (header.data_type == envi_header::float64)
  671 + return ((bip<double>*)file)->avg_band((double*)p, mask);
  672 + else{
  673 + std::cout << "ERROR: unidentified data type" << std::endl;
  674 + exit(1);
  675 + }
  676 + }
  677 + return false;
  678 + }
645 679  
  680 + //calculate correlation coefficient matrix with mask
  681 + bool co_matrix(void* co, void* avg, unsigned char* mask){
  682 + if (header.interleave == envi_header::BSQ){
  683 + if (header.data_type == envi_header::float32)
  684 + return ((bsq<float>*)file)->co_matrix((float*)co, (float*)avg, mask);
  685 + else if (header.data_type == envi_header::float64)
  686 + return ((bsq<double>*)file)->co_matrix((double*)co, (double*)avg, mask);
  687 + else{
  688 + std::cout << "ERROR: unidentified data type" << std::endl;
  689 + exit(1);
  690 + }
  691 + }
  692 + else if (header.interleave == envi_header::BIL){
  693 + if (header.data_type == envi_header::float32)
  694 + return ((bil<float>*)file)->co_matrix((float*)co, (float*)avg, mask);
  695 + else if (header.data_type == envi_header::float64)
  696 + return ((bil<double>*)file)->co_matrix((double*)co, (double*)avg, mask);
  697 + else{
  698 + std::cout << "ERROR: unidentified data type" << std::endl;
  699 + exit(1);
  700 + }
  701 + }
  702 + else if (header.interleave == envi_header::BIP){
  703 + if (header.data_type == envi_header::float32)
  704 + return ((bip<float>*)file)->co_matrix((float*)co, (float*)avg, mask);
  705 + else if (header.data_type == envi_header::float64)
  706 + return ((bip<double>*)file)->co_matrix((double*)co, (double*)avg, mask);
  707 + else{
  708 + std::cout << "ERROR: unidentified data type" << std::endl;
  709 + exit(1);
  710 + }
  711 + }
  712 + return false;
  713 + }
646 714  
  715 + //crop specified area the of the original file
  716 + bool crop(std::string outfile,unsigned x0, unsigned y0, unsigned x1, unsigned y1){
  717 +
  718 + if (header.interleave == envi_header::BSQ){
  719 + if (header.data_type == envi_header::float32)
  720 + return ((bsq<float>*)file)->crop(outfile, x0, y0, x1, y1);
  721 + else if (header.data_type == envi_header::float64)
  722 + return ((bsq<double>*)file)->crop(outfile, x0, y0, x1, y1);
  723 + else{
  724 + std::cout << "ERROR: unidentified data type" << std::endl;
  725 + exit(1);
  726 + }
  727 + }
  728 + else if (header.interleave == envi_header::BIL){
  729 + if (header.data_type == envi_header::float32)
  730 + return ((bil<float>*)file)->crop(outfile, x0, y0, x1, y1);
  731 + else if (header.data_type == envi_header::float64)
  732 + return ((bil<double>*)file)->crop(outfile, x0, y0, x1, y1);
  733 + else{
  734 + std::cout << "ERROR: unidentified data type" << std::endl;
  735 + exit(1);
  736 + }
  737 + }
  738 + else if (header.interleave == envi_header::BIP){
  739 + if (header.data_type == envi_header::float32)
  740 + return ((bip<float>*)file)->crop(outfile, x0, y0, x1, y1);
  741 + else if (header.data_type == envi_header::float64)
  742 + return ((bip<double>*)file)->crop(outfile, x0, y0, x1, y1);
  743 + else{
  744 + std::cout << "ERROR: unidentified data type" << std::endl;
  745 + exit(1);
  746 + }
  747 + }
  748 + return false;
  749 + }
647 750 };
648 751  
649 752 } //end namespace rts
... ...