Commit ed158acd47e7f479f0958455267b32007c02241e

Authored by heziqi
1 parent 84a4e0f1

added band average in envi class

Showing 4 changed files with 99 additions and 1 deletions   Show diff stats
envi/bil.h
... ... @@ -727,6 +727,33 @@ 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 +
730 757 //close the file
731 758 bool close(){
732 759 file.close();
... ...
envi/bip.h
... ... @@ -821,6 +821,25 @@ 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 +
824 843 //close the file
825 844 bool close(){
826 845 file.close();
... ...
envi/bsq.h
... ... @@ -663,6 +663,25 @@ 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 + }
666 685  
667 686 //close the file
668 687 bool close(){
... ...
envi/envi.h
... ... @@ -642,7 +642,40 @@ public:
642 642 return true;
643 643 }
644 644  
645   -
  645 + //calculate band average
  646 + bool band_avg(void * p){
  647 + if (header.interleave == envi_header::BSQ){
  648 + if (header.data_type == envi_header::float32)
  649 + return ((bsq<float>*)file)->band_avg((float*)p);
  650 + else if (header.data_type == envi_header::float64)
  651 + return ((bsq<double>*)file)->band_avg((double*)p);
  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)->band_avg((float*)p);
  660 + else if (header.data_type == envi_header::float64)
  661 + return ((bil<double>*)file)->band_avg((double*)p);
  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)->band_avg((float*)p);
  670 + else if (header.data_type == envi_header::float64)
  671 + return ((bip<double>*)file)->band_avg((double*)p);
  672 + else{
  673 + std::cout << "ERROR: unidentified data type" << std::endl;
  674 + exit(1);
  675 + }
  676 + }
  677 + return false;
  678 + }
646 679  
647 680 };
648 681  
... ...