From ed158acd47e7f479f0958455267b32007c02241e Mon Sep 17 00:00:00 2001 From: heziqi Date: Wed, 12 Nov 2014 06:38:45 +0800 Subject: [PATCH] added band average in envi class --- envi/bil.h | 27 +++++++++++++++++++++++++++ envi/bip.h | 19 +++++++++++++++++++ envi/bsq.h | 19 +++++++++++++++++++ envi/envi.h | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 99 insertions(+), 1 deletion(-) diff --git a/envi/bil.h b/envi/bil.h index 16edb2c..c9d9376 100644 --- a/envi/bil.h +++ b/envi/bil.h @@ -727,6 +727,33 @@ public: return true; } + //calculate the average band value + bool band_avg(T* p){ + unsigned long long XZ = R[0] * R[2]; + T* temp = (T*)malloc(sizeof(T) * XZ); + T* line = (T*)malloc(sizeof(T) * R[0]); + + for (unsigned i = 0; i < R[1]; i++){ + getY(temp, i); + //initialize x-line + for (unsigned j = 0; j < R[0]; j++){ + line[j] = 0; + } + unsigned c = 0; + for (unsigned j = 0; j < R[2]; j++){ + for (unsigned k = 0; k < R[0]; k++){ + line[k] += temp[c] / (T)R[2]; + c++; + } + } + for (unsigned j = 0; j < R[0]; j++){ + p[j + i * R[0]] = line[j]; + } + } + free(temp); + return true; + } + //close the file bool close(){ file.close(); diff --git a/envi/bip.h b/envi/bip.h index c1e9b61..7e21053 100644 --- a/envi/bip.h +++ b/envi/bip.h @@ -821,6 +821,25 @@ public: return true; } + //calculate the average band of the file + bool band_avg(T* p){ + unsigned long long XY = R[0] * R[1]; + //get every pixel and calculate average value + T* temp = (T*)malloc(sizeof(T) * R[2]); + T sum; + for (unsigned i = 0; i < XY; i++){ + pixel(temp, i); + //calculate the sum value of every value + sum = 0; //initialize sum value + for (unsigned j = 0; j < R[2]; j++){ + sum += temp[j]/(T)R[2]; + } + p[i] = sum; + } + free(temp); + return true; + } + //close the file bool close(){ file.close(); diff --git a/envi/bsq.h b/envi/bsq.h index 099df79..183b379 100644 --- a/envi/bsq.h +++ b/envi/bsq.h @@ -663,6 +663,25 @@ public: return true; } + //calculate the average band + bool band_avg(T* p){ + unsigned long long XY = R[0] * R[1]; + T* temp = (T*)malloc(sizeof(T) * XY); + //initialize p + band_index(p, 0); + for (unsigned j = 0; j < XY; j++){ + p[j] /= (T)R[2]; + } + //get every band and add them all + for (unsigned i = 1; i < R[2]; i++){ + band_index(temp, i); + for (unsigned j = 0; j < XY; j++){ + p[j] += temp[j]/(T)R[2]; + } + } + free(temp); + return true; + } //close the file bool close(){ diff --git a/envi/envi.h b/envi/envi.h index b2e2e4b..c45fce3 100644 --- a/envi/envi.h +++ b/envi/envi.h @@ -642,7 +642,40 @@ public: return true; } - + //calculate band average + bool band_avg(void * p){ + if (header.interleave == envi_header::BSQ){ + if (header.data_type == envi_header::float32) + return ((bsq*)file)->band_avg((float*)p); + else if (header.data_type == envi_header::float64) + return ((bsq*)file)->band_avg((double*)p); + else{ + std::cout << "ERROR: unidentified data type" << std::endl; + exit(1); + } + } + else if (header.interleave == envi_header::BIL){ + if (header.data_type == envi_header::float32) + return ((bil*)file)->band_avg((float*)p); + else if (header.data_type == envi_header::float64) + return ((bil*)file)->band_avg((double*)p); + else{ + std::cout << "ERROR: unidentified data type" << std::endl; + exit(1); + } + } + else if (header.interleave == envi_header::BIP){ + if (header.data_type == envi_header::float32) + return ((bip*)file)->band_avg((float*)p); + else if (header.data_type == envi_header::float64) + return ((bip*)file)->band_avg((double*)p); + else{ + std::cout << "ERROR: unidentified data type" << std::endl; + exit(1); + } + } + return false; + } }; -- libgit2 0.21.4