Authored by heziqi
1 parent 1cba0efe

### apply mask in correlation matrix

Showing 4 changed files with 191 additions and 125 deletions
envi/bil.h

 ... ... @@ -755,19 +755,28 @@ public: 755 755 } 756 756 757 757 //calculate the average number of every band 758 - bool avg_band(T*p){ 758 + bool avg_band(T*p, unsigned char* mask){ 759 759 unsigned long long XZ = R[0] * R[2]; 760 760 unsigned long long XY = R[0] * R[1]; 761 761 T* temp = (T*)malloc(sizeof(T) * XZ); 762 762 for (unsigned j = 0; j < R[2]; j++){ 763 763 p[j] = 0; 764 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 + } 765 772 for (unsigned k = 0; k < R[1]; k++){ 766 773 getY(temp, k); 767 - for (unsigned j = 0; j < R[2]; j++){ 768 - unsigned jx = j * R[0]; 769 - for (unsigned i = 0; i < R[0]; i++){ 770 - p[j] += temp[jx + i] / (T)XY; 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 + } 771 780 } 772 781 } 773 782 } ... ... @@ -775,6 +784,46 @@ public: 775 784 return true; 776 785 } 777 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 + } 778 827 779 828 //close the file 780 829 bool close(){ ... ...
envi/bip.h

 ... ... @@ -841,24 +841,72 @@ public: 841 841 } 842 842 843 843 //calculate the average number of every band 844 - bool avg_band(T*p){ 844 + bool avg_band(T*p, unsigned char* mask){ 845 845 unsigned long long XY = R[0] * R[1]; 846 846 T* temp = (T*)malloc(sizeof(T) * R[2]); 847 847 //Iinitialize 848 848 for (unsigned j = 0; j < R[2]; j++){ 849 849 p[j] = 0; 850 850 } 851 - 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 852 859 for (unsigned i = 0; i < XY; i++){ 853 - pixel(temp, i); 854 - for (unsigned j = 0; j < R[2]; j++){ 855 - p[j] += temp[j] / (T)XY; 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 + } 856 865 } 857 866 } 858 867 free(temp); 859 868 return true; 860 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 + } 861 906 907 + free(temp); 908 + return true; 909 + } 862 910 863 911 //close the file 864 912 bool close(){ ... ...
envi/bsq.h

 ... ... @@ -684,20 +684,67 @@ public: 684 684 } 685 685 686 686 //calculate the average number of every band 687 - bool avg_band(T*p){ 687 + bool avg_band(T*p, unsigned char* mask){ 688 688 unsigned long long XY = R[0] * R[1]; 689 + unsigned count = 0; //number of vaild pixel in a band 689 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 690 698 for (unsigned i = 0; i < R[2]; i++){ 691 699 p[i] = 0; 692 700 band_index(temp, i); 693 701 for (unsigned j = 0; j < XY; j++){ 694 - p[i] += temp[j] / (T)XY; 702 + if (mask[j] != 0){ 703 + p[i] += temp[j] / (T)count; 704 + } 695 705 } 696 706 } 697 707 free(temp); 698 708 return true; 699 709 } 700 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 + 701 748 //close the file 702 749 bool close(){ 703 750 file.close(); ... ...
envi/envi.h