diff --git a/envi/bil.h b/envi/bil.h index 16edb2c..f9007fc 100644 --- a/envi/bil.h +++ b/envi/bil.h @@ -727,6 +727,130 @@ 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; + } + + //calculate the average number of every band + bool avg_band(T*p, unsigned char* mask){ + unsigned long long XZ = R[0] * R[2]; + unsigned long long XY = R[0] * R[1]; + T* temp = (T*)malloc(sizeof(T) * XZ); + for (unsigned j = 0; j < R[2]; j++){ + p[j] = 0; + } + //calculate vaild number in a band + unsigned count = 0; + for (unsigned j = 0; j < XY; j++){ + if (mask[j] != 0){ + count++; + } + } + for (unsigned k = 0; k < R[1]; k++){ + getY(temp, k); + unsigned kx = k * R[0]; + for (unsigned i = 0; i < R[0]; i++){ + if (mask[kx + i] != 0){ + for (unsigned j = 0; j < R[2]; j++){ + p[j] += temp[j * R[0] + i] / (T)count; + } + } + } + } + free(temp); + return true; + } + + //calculate correlation coefficient matrix + bool co_matrix(T* co, T* avg, unsigned char *mask){ + //memory allocation + unsigned long long xy = R[0] * R[1]; + unsigned int B = R[2]; + T* temp = (T*)malloc(sizeof(T) * B); + //count vaild pixels in a band + unsigned count = 0; + for (unsigned j = 0; j < xy; j++){ + if (mask[j] != 0){ + count++; + } + } + //initialize correlation matrix + for (unsigned i = 0; i < B; i++){ + for (unsigned k = 0; k < B; k++){ + co[i * B + k] = 0; + } + } + //calculate correlation coefficient matrix + for (unsigned j = 0; j < xy; j++){ + if (mask[j] != 0){ + pixel(temp, j); + for (unsigned i = 0; i < B; i++){ + for (unsigned k = i; k < B; k++){ + co[i * B + k] += (temp[i] - avg[i]) * (temp[k] - avg[k]) / count; + } + } + } + } + //because correlation matrix is symmetric + for (unsigned i = 0; i < B; i++){ + for (unsigned k = i + 1; k < B; k++){ + co[k * B + i] = co[i * B + k]; + } + } + + free(temp); + return true; + } + + //crop specified area the of the original file + bool crop(std::string outfile, unsigned x0, unsigned y0, unsigned x1, unsigned y1){ + + //calculate the new number of samples and lines + unsigned long long sam = x1 - x0; //samples + unsigned long long lin = y1 - y0; //lines + unsigned long long L = sam * R[2] * sizeof(T); + //get specified band and save + T* temp = (T*)malloc(L); + std::ofstream out(outfile.c_str(), std::ios::binary); + unsigned long long jumpb = (R[0] - sam) * sizeof(T); //jump pointer to the next band + //get start + file.seekg((y0 * R[0] * R[2] + x0) * sizeof(T), std::ios::beg); + for (unsigned i = 0; i < lin; i++) + { + for (unsigned j = 0; j < R[2]; j++) + { + file.read((char *)(temp + j * sam), sizeof(T) * sam); + file.seekg(jumpb, std::ios::cur); //go to the next band + } + out.write(reinterpret_cast(temp), L); //write slice data into target file + } + free(temp); + return true; + } + //close the file bool close(){ file.close(); diff --git a/envi/bip.h b/envi/bip.h index c1e9b61..87eb151 100644 --- a/envi/bip.h +++ b/envi/bip.h @@ -821,6 +821,117 @@ 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; + } + + //calculate the average number of every band + bool avg_band(T*p, unsigned char* mask){ + unsigned long long XY = R[0] * R[1]; + T* temp = (T*)malloc(sizeof(T) * R[2]); + //Iinitialize + for (unsigned j = 0; j < R[2]; j++){ + p[j] = 0; + } + //calculate vaild number in a band + unsigned count = 0; + for (unsigned j = 0; j < XY; j++){ + if (mask[j] != 0){ + count++; + } + } + //calculate average number of a band + for (unsigned i = 0; i < XY; i++){ + if (mask[i] != 0){ + pixel(temp, i); + for (unsigned j = 0; j < R[2]; j++){ + p[j] += temp[j] / (T)count; + } + } + } + free(temp); + return true; + } + //calculate correlation coefficient matrix + bool co_matrix(T* co, T* avg, unsigned char *mask){ + //memory allocation + unsigned long long xy = R[0] * R[1]; + unsigned int B = R[2]; + T* temp = (T*)malloc(sizeof(T) * B); + //count vaild pixels in a band + unsigned count = 0; + for (unsigned j = 0; j < xy; j++){ + if (mask[j] != 0){ + count++; + } + } + //initialize correlation matrix + for (unsigned i = 0; i < B; i++){ + for (unsigned k = 0; k < B; k++){ + co[i * B + k] = 0; + } + } + //calculate correlation coefficient matrix + for (unsigned j = 0; j < xy; j++){ + if (mask[j] != 0){ + pixel(temp, j); + for (unsigned i = 0; i < B; i++){ + for (unsigned k = i; k < B; k++){ + co[i * B + k] += (temp[i] - avg[i]) * (temp[k] - avg[k]) / count; + } + } + } + } + //because correlation matrix is symmetric + for (unsigned i = 0; i < B; i++){ + for (unsigned k = i + 1; k < B; k++){ + co[k * B + i] = co[i * B + k]; + } + } + + free(temp); + return true; + } + + //crop specified area the of the original file + bool crop(std::string outfile, unsigned x0, unsigned y0, unsigned x1, unsigned y1){ + + //calculate the new number of samples and lines + unsigned long long sam = x1 - x0; //samples + unsigned long long lin = y1 - y0; //lines + unsigned long long L = R[2] * sizeof(T); + //get specified band and save + T* temp = (T*)malloc(L); + std::ofstream out(outfile.c_str(), std::ios::binary); + //get start + unsigned long long sp = y0 * R[0] + x0; //start pixel + for (unsigned i = 0; i < lin; i++) + { + for (unsigned j = 0; j < sam; j++) + { + pixel(temp, sp + j + i * R[0]); + out.write(reinterpret_cast(temp), L); //write slice data into target file + } + } + free(temp); + return true; + } + //close the file bool close(){ file.close(); diff --git a/envi/bsq.h b/envi/bsq.h index 099df79..ba6e4dd 100644 --- a/envi/bsq.h +++ b/envi/bsq.h @@ -663,6 +663,115 @@ 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; + } + + //calculate the average number of every band + bool avg_band(T*p, unsigned char* mask){ + unsigned long long XY = R[0] * R[1]; + unsigned count = 0; //number of vaild pixel in a band + T* temp = (T*)malloc(sizeof(T) * XY); + //calculate valid pixel number + for (unsigned j = 0; j < XY; j++){ + if (mask[j] != 0){ + count++; + } + } + //calculate average of a band + for (unsigned i = 0; i < R[2]; i++){ + p[i] = 0; + band_index(temp, i); + for (unsigned j = 0; j < XY; j++){ + if (mask[j] != 0){ + p[i] += temp[j] / (T)count; + } + } + } + free(temp); + return true; + } + + //calculate correlated matrix + bool co_matrix(T* co, T* avg, unsigned char *mask){ + //memory allocation + unsigned long long xy = R[0] * R[1]; + unsigned int B = R[2]; + T* bandi = (T*)malloc(sizeof(T) * xy); + T* bandj = (T*)malloc(sizeof(T) * xy); + + //count vaild pixels in a band + unsigned count = 0; + for (unsigned j = 0; j < xy; j++){ + if (mask[j] != 0){ + count++; + } + } + //calculate correlation coefficient matrix + for (unsigned i = 0; i < B; i++) + { + band_index(bandi, i); + for (unsigned j = i; j < B; j++){ + band_index(bandj, j); + T numerator = 0; //to calculate element in correlation coefficient matrix, numerator part + //calculate the R(i,j) in correlation coeffient matrix + for (unsigned k = 0; k < xy; k++){ + if (mask[k] != 0){ + numerator += (bandi[k] - avg[i]) * (bandj[k] - avg[j]) / count; + } + } + co[i*B + j] = numerator; + co[j*B + i] = numerator; //because correlated matrix is a symmetric matrix + } + } + free(bandi); + free(bandj); + return true; + } + + //crop specified area the of the original file + bool crop(std::string outfile, unsigned x0, unsigned y0, unsigned x1, unsigned y1){ + + //calculate the new number of samples and lines + unsigned long long sam = x1 - x0; //samples + unsigned long long lin = y1 - y0; //lines + unsigned long long L = sam * lin * sizeof(T); + //get specified band and save + T* temp = (T*)malloc(L); + std::ofstream out(outfile.c_str(), std::ios::binary); + unsigned long long jumpb = R[0] * (R[1] - lin) * sizeof(T); //jump pointer to the next band + unsigned long long jumpl = (R[0] - sam) * sizeof(T); //jump pointer to the next line + //get start + file.seekg((y0 * R[0] + x0) * sizeof(T), std::ios::beg); + for (unsigned i = 0; i < R[2]; i++) + { + for (unsigned j = 0; j < lin; j++) + { + file.read((char *)(temp + j * sam), sizeof(T) * sam); + file.seekg(jumpl, std::ios::cur); //go to the next band + } + out.write(reinterpret_cast(temp), L); //write slice data into target file + file.seekg(jumpb, std::ios::cur); + } + free(temp); + return true; + } //close the file bool close(){ diff --git a/envi/envi.h b/envi/envi.h index b2e2e4b..040883e 100644 --- a/envi/envi.h +++ b/envi/envi.h @@ -642,8 +642,111 @@ public: return true; } + //calculate band average + bool avg_band(void * p, unsigned char* mask){ + if (header.interleave == envi_header::BSQ){ + if (header.data_type == envi_header::float32) + return ((bsq*)file)->avg_band((float*)p, mask); + else if (header.data_type == envi_header::float64) + return ((bsq*)file)->avg_band((double*)p, mask); + 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)->avg_band((float*)p, mask); + else if (header.data_type == envi_header::float64) + return ((bil*)file)->avg_band((double*)p, mask); + 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)->avg_band((float*)p, mask); + else if (header.data_type == envi_header::float64) + return ((bip*)file)->avg_band((double*)p, mask); + else{ + std::cout << "ERROR: unidentified data type" << std::endl; + exit(1); + } + } + return false; + } + //calculate correlation coefficient matrix with mask + bool co_matrix(void* co, void* avg, unsigned char* mask){ + if (header.interleave == envi_header::BSQ){ + if (header.data_type == envi_header::float32) + return ((bsq*)file)->co_matrix((float*)co, (float*)avg, mask); + else if (header.data_type == envi_header::float64) + return ((bsq*)file)->co_matrix((double*)co, (double*)avg, mask); + 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)->co_matrix((float*)co, (float*)avg, mask); + else if (header.data_type == envi_header::float64) + return ((bil*)file)->co_matrix((double*)co, (double*)avg, mask); + 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)->co_matrix((float*)co, (float*)avg, mask); + else if (header.data_type == envi_header::float64) + return ((bip*)file)->co_matrix((double*)co, (double*)avg, mask); + else{ + std::cout << "ERROR: unidentified data type" << std::endl; + exit(1); + } + } + return false; + } + //crop specified area the of the original file + bool crop(std::string outfile,unsigned x0, unsigned y0, unsigned x1, unsigned y1){ + + if (header.interleave == envi_header::BSQ){ + if (header.data_type == envi_header::float32) + return ((bsq*)file)->crop(outfile, x0, y0, x1, y1); + else if (header.data_type == envi_header::float64) + return ((bsq*)file)->crop(outfile, x0, y0, x1, y1); + 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)->crop(outfile, x0, y0, x1, y1); + else if (header.data_type == envi_header::float64) + return ((bil*)file)->crop(outfile, x0, y0, x1, y1); + 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)->crop(outfile, x0, y0, x1, y1); + else if (header.data_type == envi_header::float64) + return ((bip*)file)->crop(outfile, x0, y0, x1, y1); + else{ + std::cout << "ERROR: unidentified data type" << std::endl; + exit(1); + } + } + return false; + } }; } //end namespace rts -- libgit2 0.21.4