| 1 | /********************************************************************** |
| 2 | Filter.cc: class for filtering |
| 3 | Author(s): [email protected] |
| 4 | Copyright (C) 2016-2022 European Union (Joint Research Centre) |
| 5 | |
| 6 | This file is part of jiplib. |
| 7 | |
| 8 | jiplib is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | jiplib is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with jiplib. If not, see <https://www.gnu.org/licenses/>. |
| 20 | ***********************************************************************/ |
| 21 | #include "Filter.h" |
| 22 | #include <assert.h> |
| 23 | // #include <math.h> |
| 24 | #include <cmath> |
| 25 | #include <iostream> |
| 26 | |
| 27 | using namespace std; |
| 28 | |
| 29 | filter::Filter::Filter(void) |
| 30 | : m_padding("symmetric") |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | |
| 35 | filter::Filter::Filter(const vector<double> &taps) |
| 36 | : m_padding("symmetric") |
| 37 | { |
| 38 | setTaps(taps); |
| 39 | } |
| 40 | |
| 41 | void filter::Filter::setTaps(const vector<double> &taps, bool normalize) |
| 42 | { |
| 43 | m_taps.resize(taps.size()); |
| 44 | double norm=0; |
| 45 | for(int itap=0;itap<taps.size();++itap) |
| 46 | norm+=taps[itap]; |
| 47 | if(norm){ |
| 48 | for(int itap=0;itap<taps.size();++itap) |
| 49 | m_taps[itap]=taps[itap]/norm; |
| 50 | } |
| 51 | else |
| 52 | m_taps=taps; |
| 53 | assert(m_taps.size()%2); |
| 54 | } |
| 55 | |
| 56 | unsigned int filter::Filter::pushNoDataValue(double noDataValue){ |
| 57 | if(find(m_noDataValues.begin(),m_noDataValues.end(),noDataValue)==m_noDataValues.end()) |
| 58 | m_noDataValues.push_back(noDataValue); |
| 59 | return m_noDataValues.size(); |
| 60 | }; |
| 61 | |
| 62 | unsigned int filter::Filter::setNoDataValues(std::vector<double> vnodata){ |
| 63 | m_noDataValues=vnodata; |
| 64 | return m_noDataValues.size(); |
| 65 | }; |
| 66 | |
| 67 | void filter::Filter::dwtForward(Jim& input, Jim& output, const std::string& wavelet_type, int family){ |
| 68 | const char* pszMessage; |
| 69 | // void* pProgressArg=NULL; |
| 70 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 71 | // double progress=0; |
| 72 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 73 | Vector2d<double> lineInput(input.nrOfBand(),input.nrOfCol()); |
| 74 | Vector2d<double> lineOutput(input.nrOfBand(),input.nrOfCol()); |
| 75 | for(unsigned int y=0;y<input.nrOfRow();++y){ |
| 76 | for(unsigned int iband=0;iband<input.nrOfBand();++iband) |
| 77 | input.readData(lineInput[iband],y,iband); |
| 78 | vector<double> pixelInput(input.nrOfBand()); |
| 79 | for(unsigned int x=0;x<input.nrOfCol();++x){ |
| 80 | pixelInput=lineInput.selectCol(x); |
| 81 | dwtForward(pixelInput,wavelet_type,family); |
| 82 | for(int iband=0;iband<input.nrOfBand();++iband) |
| 83 | lineOutput[iband][x]=pixelInput[iband]; |
| 84 | } |
| 85 | for(unsigned int iband=0;iband<input.nrOfBand();++iband){ |
| 86 | try{ |
| 87 | output.writeData(lineOutput[iband],y,iband); |
| 88 | } |
| 89 | catch(string errorstring){ |
| 90 | cerr << errorstring << "in band " << iband << ", line " << y << endl; |
| 91 | } |
| 92 | } |
| 93 | // progress=(1.0+y)/output.nrOfRow(); |
| 94 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void filter::Filter::dwtInverse(Jim& input, Jim& output, const std::string& wavelet_type, int family){ |
| 99 | const char* pszMessage; |
| 100 | // void* pProgressArg=NULL; |
| 101 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 102 | // double progress=0; |
| 103 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 104 | Vector2d<double> lineInput(input.nrOfBand(),input.nrOfCol()); |
| 105 | Vector2d<double> lineOutput(input.nrOfBand(),input.nrOfCol()); |
| 106 | for(unsigned int y=0;y<input.nrOfRow();++y){ |
| 107 | for(unsigned int iband=0;iband<input.nrOfBand();++iband) |
| 108 | input.readData(lineInput[iband],y,iband); |
| 109 | vector<double> pixelInput(input.nrOfBand()); |
| 110 | for(unsigned int x=0;x<input.nrOfCol();++x){ |
| 111 | pixelInput=lineInput.selectCol(x); |
| 112 | dwtInverse(pixelInput,wavelet_type,family); |
| 113 | for(unsigned int iband=0;iband<input.nrOfBand();++iband) |
| 114 | lineOutput[iband][x]=pixelInput[iband]; |
| 115 | } |
| 116 | for(unsigned int iband=0;iband<input.nrOfBand();++iband){ |
| 117 | try{ |
| 118 | output.writeData(lineOutput[iband],y,iband); |
| 119 | } |
| 120 | catch(string errorstring){ |
| 121 | cerr << errorstring << "in band " << iband << ", line " << y << endl; |
| 122 | } |
| 123 | } |
| 124 | // progress=(1.0+y)/output.nrOfRow(); |
| 125 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void filter::Filter::dwtCut(Jim& input, Jim& output, const std::string& wavelet_type, int family, double cut){ |
| 130 | const char* pszMessage; |
| 131 | // void* pProgressArg=NULL; |
| 132 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 133 | // double progress=0; |
| 134 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 135 | Vector2d<double> lineInput(input.nrOfBand(),input.nrOfCol()); |
| 136 | Vector2d<double> lineOutput(input.nrOfBand(),input.nrOfCol()); |
| 137 | for(unsigned int y=0;y<input.nrOfRow();++y){ |
| 138 | for(unsigned int iband=0;iband<input.nrOfBand();++iband) |
| 139 | input.readData(lineInput[iband],y,iband); |
| 140 | vector<double> pixelInput(input.nrOfBand()); |
| 141 | for(unsigned int x=0;x<input.nrOfCol();++x){ |
| 142 | pixelInput=lineInput.selectCol(x); |
| 143 | dwtCut(pixelInput,wavelet_type,family,cut); |
| 144 | for(unsigned int iband=0;iband<input.nrOfBand();++iband) |
| 145 | lineOutput[iband][x]=pixelInput[iband]; |
| 146 | } |
| 147 | for(unsigned int iband=0;iband<input.nrOfBand();++iband){ |
| 148 | try{ |
| 149 | output.writeData(lineOutput[iband],y,iband); |
| 150 | } |
| 151 | catch(string errorstring){ |
| 152 | cerr << errorstring << "in band " << iband << ", line " << y << endl; |
| 153 | } |
| 154 | } |
| 155 | // progress=(1.0+y)/output.nrOfRow(); |
| 156 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void filter::Filter::dwtCutFrom(Jim& input, Jim& output, const std::string& wavelet_type, int family, int band){ |
| 161 | const char* pszMessage; |
| 162 | // void* pProgressArg=NULL; |
| 163 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 164 | // double progress=0; |
| 165 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 166 | Vector2d<double> lineInput(input.nrOfBand(),input.nrOfCol()); |
| 167 | Vector2d<double> lineOutput(input.nrOfBand(),input.nrOfCol()); |
| 168 | for(unsigned int y=0;y<input.nrOfRow();++y){ |
| 169 | for(unsigned int iband=0;iband<input.nrOfBand();++iband) |
| 170 | input.readData(lineInput[iband],y,iband); |
| 171 | vector<double> pixelInput(input.nrOfBand()); |
| 172 | for(unsigned int x=0;x<input.nrOfCol();++x){ |
| 173 | pixelInput=lineInput.selectCol(x); |
| 174 | dwtForward(pixelInput,wavelet_type,family); |
| 175 | for(unsigned int iband=0;iband<input.nrOfBand();++iband){ |
| 176 | if(iband>=band) |
| 177 | pixelInput[iband]=0; |
| 178 | } |
| 179 | dwtInverse(pixelInput,wavelet_type,family); |
| 180 | for(unsigned int iband=0;iband<input.nrOfBand();++iband) |
| 181 | lineOutput[iband][x]=pixelInput[iband]; |
| 182 | } |
| 183 | for(unsigned int iband=0;iband<input.nrOfBand();++iband){ |
| 184 | try{ |
| 185 | output.writeData(lineOutput[iband],y,iband); |
| 186 | } |
| 187 | catch(string errorstring){ |
| 188 | cerr << errorstring << "in band " << iband << ", line " << y << endl; |
| 189 | } |
| 190 | } |
| 191 | // progress=(1.0+y)/output.nrOfRow(); |
| 192 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | //todo: support different padding strategies |
| 197 | void filter::Filter::dwtForward(std::vector<double>& data, const std::string& wavelet_type, int family){ |
| 198 | int origsize=data.size(); |
| 199 | //make sure data size if power of 2 |
| 200 | while(data.size()&(data.size()-1)) |
| 201 | data.push_back(data.back()); |
| 202 | |
| 203 | int nsize=data.size(); |
| 204 | gsl_wavelet *w; |
| 205 | gsl_wavelet_workspace *work; |
| 206 | assert(nsize); |
| 207 | w=gsl_wavelet_alloc(getWaveletType(wavelet_type),family); |
| 208 | work=gsl_wavelet_workspace_alloc(nsize); |
| 209 | gsl_wavelet_transform_forward(w,&(data[0]),1,nsize,work); |
| 210 | data.erase(data.begin()+origsize,data.end()); |
| 211 | gsl_wavelet_free (w); |
| 212 | gsl_wavelet_workspace_free (work); |
| 213 | } |
| 214 | |
| 215 | //todo: support different padding strategies |
| 216 | void filter::Filter::dwtInverse(std::vector<double>& data, const std::string& wavelet_type, int family){ |
| 217 | int origsize=data.size(); |
| 218 | //make sure data size if power of 2 |
| 219 | while(data.size()&(data.size()-1)) |
| 220 | data.push_back(data.back()); |
| 221 | int nsize=data.size(); |
| 222 | gsl_wavelet *w; |
| 223 | gsl_wavelet_workspace *work; |
| 224 | assert(nsize); |
| 225 | w=gsl_wavelet_alloc(getWaveletType(wavelet_type),family); |
| 226 | work=gsl_wavelet_workspace_alloc(nsize); |
| 227 | gsl_wavelet_transform_inverse(w,&(data[0]),1,nsize,work); |
| 228 | data.erase(data.begin()+origsize,data.end()); |
| 229 | gsl_wavelet_free (w); |
| 230 | gsl_wavelet_workspace_free (work); |
| 231 | } |
| 232 | |
| 233 | //todo: support different padding strategies |
| 234 | void filter::Filter::dwtCut(std::vector<double>& data, const std::string& wavelet_type, int family, double cut){ |
| 235 | int origsize=data.size(); |
| 236 | //make sure data size if power of 2 |
| 237 | while(data.size()&(data.size()-1)) |
| 238 | data.push_back(data.back()); |
| 239 | int nsize=data.size(); |
| 240 | gsl_wavelet *w; |
| 241 | gsl_wavelet_workspace *work; |
| 242 | assert(nsize); |
| 243 | w=gsl_wavelet_alloc(getWaveletType(wavelet_type),family); |
| 244 | work=gsl_wavelet_workspace_alloc(nsize); |
| 245 | gsl_wavelet_transform_forward(w,&(data[0]),1,nsize,work); |
| 246 | std::vector<double> abscoeff(data.size()); |
| 247 | size_t* p=new size_t[data.size()]; |
| 248 | for(int index=0;index<data.size();++index){ |
| 249 | abscoeff[index]=fabs(data[index]); |
| 250 | } |
| 251 | int nc=(100-cut)/100.0*nsize; |
| 252 | gsl_sort_index(p,&(abscoeff[0]),1,nsize); |
| 253 | for(int i=0;(i+nc)<nsize;i++) |
| 254 | data[p[i]]=0; |
| 255 | gsl_wavelet_transform_inverse(w,&(data[0]),1,nsize,work); |
| 256 | data.erase(data.begin()+origsize,data.end()); |
| 257 | delete[] p; |
| 258 | gsl_wavelet_free (w); |
| 259 | gsl_wavelet_workspace_free (work); |
| 260 | } |
| 261 | |
| 262 | void filter::Filter::morphology(Jim& input, Jim& output, const std::string& method, int dim, short verbose) |
| 263 | { |
| 264 | // bool bverbose=(verbose>1)? true:false; |
| 265 | Vector2d<double> lineInput(input.nrOfBand(),input.nrOfCol()); |
| 266 | Vector2d<double> lineOutput(input.nrOfBand(),input.nrOfCol()); |
| 267 | if(verbose) |
| 268 | std::cout << "Number of bands in input: " << lineInput.size() << std::endl; |
| 269 | const char* pszMessage; |
| 270 | // void* pProgressArg=NULL; |
| 271 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 272 | // double progress=0; |
| 273 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 274 | for(unsigned int y=0;y<input.nrOfRow();++y){ |
| 275 | try{ |
| 276 | for(unsigned int iband=0;iband<input.nrOfBand();++iband){ |
| 277 | input.readData(lineInput[iband],y,iband); |
| 278 | } |
| 279 | } |
| 280 | catch(string errorString){ |
| 281 | std::cerr << "Error: could not read data from input" << endl; |
| 282 | exit(1); |
| 283 | } |
| 284 | catch(...){ |
| 285 | std::cerr << "Error: " << std::endl; |
| 286 | } |
| 287 | vector<double> pixelInput(input.nrOfBand()); |
| 288 | vector<double> pixelOutput(input.nrOfBand()); |
| 289 | for(unsigned int x=0;x<input.nrOfCol();++x){ |
| 290 | pixelInput=lineInput.selectCol(x); |
| 291 | filter(pixelInput,pixelOutput,method,dim); |
| 292 | // morphology(pixelInput,pixelOutput,method,dim,bverbose); |
| 293 | for(unsigned int iband=0;iband<input.nrOfBand();++iband) |
| 294 | lineOutput[iband][x]=pixelOutput[iband]; |
| 295 | } |
| 296 | for(unsigned int iband=0;iband<input.nrOfBand();++iband){ |
| 297 | try{ |
| 298 | output.writeData(lineOutput[iband],y,iband); |
| 299 | } |
| 300 | catch(string errorstring){ |
| 301 | cerr << errorstring << "in band " << iband << ", line " << y << endl; |
| 302 | } |
| 303 | } |
| 304 | // progress=(1.0+y)/output.nrOfRow(); |
| 305 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void filter::Filter::smoothNoData(Jim& input, const std::string& interpolationType, Jim& output) |
| 310 | { |
| 311 | Vector2d<double> lineInput(input.nrOfBand(),input.nrOfCol()); |
| 312 | Vector2d<double> lineOutput(input.nrOfBand(),input.nrOfCol()); |
| 313 | const char* pszMessage; |
| 314 | // void* pProgressArg=NULL; |
| 315 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 316 | // double progress=0; |
| 317 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 318 | for(unsigned int y=0;y<input.nrOfRow();++y){ |
| 319 | for(unsigned int iband=0;iband<input.nrOfBand();++iband) |
| 320 | input.readData(lineInput[iband],y,iband); |
| 321 | vector<double> pixelInput(input.nrOfBand()); |
| 322 | vector<double> pixelOutput(input.nrOfBand()); |
| 323 | for(unsigned int x=0;x<input.nrOfCol();++x){ |
| 324 | pixelInput=lineInput.selectCol(x); |
| 325 | smoothNoData(pixelInput,interpolationType,pixelOutput); |
| 326 | for(unsigned int iband=0;iband<input.nrOfBand();++iband) |
| 327 | lineOutput[iband][x]=pixelOutput[iband]; |
| 328 | } |
| 329 | for(unsigned int iband=0;iband<input.nrOfBand();++iband){ |
| 330 | try{ |
| 331 | output.writeData(lineOutput[iband],y,iband); |
| 332 | } |
| 333 | catch(string errorstring){ |
| 334 | cerr << errorstring << "in band " << iband << ", line " << y << endl; |
| 335 | } |
| 336 | } |
| 337 | // progress=(1.0+y)/output.nrOfRow(); |
| 338 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | void filter::Filter::smooth(Jim& input, Jim& output, short dim) |
| 343 | { |
| 344 | assert(dim>0); |
| 345 | m_taps.resize(dim); |
| 346 | for(int itap=0;itap<dim;++itap) |
| 347 | m_taps[itap]=1.0/dim; |
| 348 | filter(input,output); |
| 349 | } |
| 350 | |
| 351 | void filter::Filter::filter(Jim& input, Jim& output) |
| 352 | { |
| 353 | Vector2d<double> lineInput(input.nrOfBand(),input.nrOfCol()); |
| 354 | Vector2d<double> lineOutput(input.nrOfBand(),input.nrOfCol()); |
| 355 | const char* pszMessage; |
| 356 | // void* pProgressArg=NULL; |
| 357 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 358 | // double progress=0; |
| 359 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 360 | for(unsigned int y=0;y<input.nrOfRow();++y){ |
| 361 | for(unsigned int iband=0;iband<input.nrOfBand();++iband) |
| 362 | input.readData(lineInput[iband],y,iband); |
| 363 | vector<double> pixelInput(input.nrOfBand()); |
| 364 | vector<double> pixelOutput(input.nrOfBand()); |
| 365 | for(unsigned int x=0;x<input.nrOfCol();++x){ |
| 366 | pixelInput=lineInput.selectCol(x); |
| 367 | filter(pixelInput,pixelOutput); |
| 368 | for(unsigned int iband=0;iband<input.nrOfBand();++iband) |
| 369 | lineOutput[iband][x]=pixelOutput[iband]; |
| 370 | } |
| 371 | for(unsigned int iband=0;iband<input.nrOfBand();++iband){ |
| 372 | try{ |
| 373 | output.writeData(lineOutput[iband],y,iband); |
| 374 | } |
| 375 | catch(string errorstring){ |
| 376 | cerr << errorstring << "in band " << iband << ", line " << y << endl; |
| 377 | } |
| 378 | } |
| 379 | // progress=(1.0+y)/output.nrOfRow(); |
| 380 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | void filter::Filter::stat(Jim& input, Jim& output, const std::string& method) |
| 385 | { |
| 386 | Vector2d<double> lineInput(input.nrOfBand(),input.nrOfCol()); |
| 387 | assert(output.nrOfCol()==input.nrOfCol()); |
| 388 | vector<double> lineOutput(output.nrOfCol()); |
| 389 | statfactory::StatFactory stat; |
| 390 | stat.setNoDataValues(m_noDataValues); |
| 391 | const char* pszMessage; |
| 392 | // void* pProgressArg=NULL; |
| 393 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 394 | // double progress=0; |
| 395 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 396 | for(unsigned int y=0;y<input.nrOfRow();++y){ |
| 397 | for(unsigned int iband=0;iband<input.nrOfBand();++iband) |
| 398 | input.readData(lineInput[iband],y,iband); |
| 399 | vector<double> pixelInput(input.nrOfBand()); |
| 400 | for(unsigned int x=0;x<input.nrOfCol();++x){ |
| 401 | pixelInput=lineInput.selectCol(x); |
| 402 | switch(getFilterType(method)){ |
| 403 | case(filter::median): |
| 404 | lineOutput[x]=stat.median(pixelInput); |
| 405 | break; |
| 406 | case(filter::min): |
| 407 | lineOutput[x]=stat.mymin(pixelInput); |
| 408 | break; |
| 409 | case(filter::max): |
| 410 | lineOutput[x]=stat.mymax(pixelInput); |
| 411 | break; |
| 412 | case(filter::sum): |
| 413 | lineOutput[x]=stat.sum(pixelInput); |
| 414 | break; |
| 415 | case(filter::var): |
| 416 | lineOutput[x]=stat.var(pixelInput); |
| 417 | break; |
| 418 | case(filter::stdev): |
| 419 | lineOutput[x]=sqrt(stat.var(pixelInput)); |
| 420 | break; |
| 421 | case(filter::mean): |
| 422 | lineOutput[x]=stat.mean(pixelInput); |
| 423 | break; |
| 424 | case(filter::percentile): |
| 425 | assert(m_threshold.size()); |
| 426 | lineOutput[x]=stat.percentile(pixelInput,pixelInput.begin(),pixelInput.end(),m_threshold[0]); |
| 427 | break; |
| 428 | default: |
| 429 | std::string errorString="method not supported"; |
| 430 | throw(errorString); |
| 431 | break; |
| 432 | } |
| 433 | } |
| 434 | try{ |
| 435 | output.writeData(lineOutput,y); |
| 436 | } |
| 437 | catch(string errorstring){ |
| 438 | cerr << errorstring << "in line " << y << endl; |
| 439 | } |
| 440 | // progress=(1.0+y)/output.nrOfRow(); |
| 441 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | void filter::Filter::stats(Jim& input, Jim& output, const vector<std::string>& methods) |
| 446 | { |
| 447 | assert(output.nrOfBand()==methods.size()); |
| 448 | Vector2d<double> lineInput(input.nrOfBand(),input.nrOfCol()); |
| 449 | assert(output.nrOfCol()==input.nrOfCol()); |
| 450 | Vector2d<double> lineOutput(methods.size(),output.nrOfCol()); |
| 451 | statfactory::StatFactory stat; |
| 452 | stat.setNoDataValues(m_noDataValues); |
| 453 | const char* pszMessage; |
| 454 | // void* pProgressArg=NULL; |
| 455 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 456 | // double progress=0; |
| 457 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 458 | for(unsigned int y=0;y<input.nrOfRow();++y){ |
| 459 | for(unsigned int iband=0;iband<input.nrOfBand();++iband) |
| 460 | input.readData(lineInput[iband],y,iband); |
| 461 | vector<double> pixelInput(input.nrOfBand()); |
| 462 | for(unsigned int x=0;x<input.nrOfCol();++x){ |
| 463 | pixelInput=lineInput.selectCol(x); |
| 464 | int ithreshold=0;//threshold to use for percentiles |
| 465 | for(int imethod=0;imethod<methods.size();++imethod){ |
| 466 | switch(getFilterType(methods[imethod])){ |
| 467 | case(filter::nvalid): |
| 468 | lineOutput[imethod][x]=stat.nvalid(pixelInput); |
| 469 | break; |
| 470 | case(filter::median): |
| 471 | lineOutput[imethod][x]=stat.median(pixelInput); |
| 472 | break; |
| 473 | case(filter::min): |
| 474 | lineOutput[imethod][x]=stat.mymin(pixelInput); |
| 475 | break; |
| 476 | case(filter::max): |
| 477 | lineOutput[imethod][x]=stat.mymax(pixelInput); |
| 478 | break; |
| 479 | case(filter::sum): |
| 480 | lineOutput[imethod][x]=stat.sum(pixelInput); |
| 481 | break; |
| 482 | case(filter::var): |
| 483 | lineOutput[imethod][x]=stat.var(pixelInput); |
| 484 | break; |
| 485 | case(filter::stdev): |
| 486 | lineOutput[imethod][x]=sqrt(stat.var(pixelInput)); |
| 487 | break; |
| 488 | case(filter::mean): |
| 489 | lineOutput[imethod][x]=stat.mean(pixelInput); |
| 490 | break; |
| 491 | case(filter::percentile):{ |
| 492 | assert(m_threshold.size()); |
| 493 | double threshold=(ithreshold<m_threshold.size())? m_threshold[ithreshold] : m_threshold[0]; |
| 494 | lineOutput[imethod][x]=stat.percentile(pixelInput,pixelInput.begin(),pixelInput.end(),threshold); |
| 495 | ++ithreshold; |
| 496 | break; |
| 497 | } |
| 498 | default: |
| 499 | std::string errorString="method not supported"; |
| 500 | throw(errorString); |
| 501 | break; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | for(int imethod=0;imethod<methods.size();++imethod){ |
| 506 | try{ |
| 507 | output.writeData(lineOutput[imethod],y,imethod); |
| 508 | } |
| 509 | catch(string errorstring){ |
| 510 | cerr << errorstring << "in line " << y << endl; |
| 511 | } |
| 512 | } |
| 513 | // progress=(1.0+y)/output.nrOfRow(); |
| 514 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | void filter::Filter::filter(Jim& input, Jim& output, const std::string& method, int dim) |
| 519 | { |
| 520 | Vector2d<double> lineInput(input.nrOfBand(),input.nrOfCol()); |
| 521 | Vector2d<double> lineOutput(input.nrOfBand(),input.nrOfCol());; |
| 522 | const char* pszMessage; |
| 523 | // void* pProgressArg=NULL; |
| 524 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 525 | // double progress=0; |
| 526 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 527 | for(unsigned int y=0;y<input.nrOfRow();++y){ |
| 528 | for(unsigned int iband=0;iband<input.nrOfBand();++iband) |
| 529 | input.readData(lineInput[iband],y,iband); |
| 530 | vector<double> pixelInput(input.nrOfBand()); |
| 531 | vector<double> pixelOutput; |
| 532 | for(unsigned int x=0;x<input.nrOfCol();++x){ |
| 533 | pixelInput=lineInput.selectCol(x); |
| 534 | filter(pixelInput,pixelOutput,method,dim); |
| 535 | for(unsigned int iband=0;iband<pixelOutput.size();++iband){ |
| 536 | lineOutput[iband][x]=pixelOutput[iband]; |
| 537 | // if(pixelInput[iband]!=0) |
| 538 | // assert(pixelOutput[iband]!=0); |
| 539 | } |
| 540 | } |
| 541 | for(unsigned int iband=0;iband<input.nrOfBand();++iband){ |
| 542 | try{ |
| 543 | output.writeData(lineOutput[iband],y,iband); |
| 544 | } |
| 545 | catch(string errorstring){ |
| 546 | cerr << errorstring << "in band " << iband << ", line " << y << endl; |
| 547 | } |
| 548 | } |
| 549 | // progress=(1.0+y)/output.nrOfRow(); |
| 550 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | void filter::Filter::getSavGolayCoefficients(vector<double> &tapz, int np, int nl, int nr, int ld, int m) { |
| 555 | int j, k, imj, ipj, kk, mm; |
| 556 | double d, fac, sum; |
| 557 | |
| 558 | // c.resize(nl+1+nr); |
| 559 | vector<double> tmpc(np); |
| 560 | if(np < nl + nr + 1 || nl < 0 || nr < 0 || ld > m || nl + nr < m) { |
| 561 | std::string errorString="Error: could not get savgolay coefficients, inconsistent arguments (e.g., number of planes are insufficient to perform Savgolay filtering)"; |
| 562 | throw(errorString); |
| 563 | } |
| 564 | vector<int> indx(m + 1, 0); |
| 565 | vector<double> a((m + 1) * (m + 1), 0.0); |
| 566 | vector<double> b(m + 1, 0.0); |
| 567 | |
| 568 | for(ipj = 0; ipj <= (m << 1); ++ipj) { |
| 569 | sum = (ipj ? 0.0 : 1.0); |
| 570 | for(k = 1; k <= nr; ++k) |
| 571 | sum += (int)pow((double)k, (double)ipj); |
| 572 | for(k = 1; k <= nl; ++k) |
| 573 | sum += (int)pow((double) - k, (double)ipj); |
| 574 | mm = (ipj < 2 * m - ipj ? ipj : 2 * m - ipj); |
| 575 | for(imj = -mm; imj <= mm; imj += 2) |
| 576 | a[(ipj + imj) / 2 * (m + 1) + (ipj - imj) / 2] = sum; |
| 577 | } |
| 578 | ludcmp(a, indx, d); |
| 579 | |
| 580 | for(j = 0; j < m + 1; ++j) |
| 581 | b[j] = 0.0; |
| 582 | b[ld] = 1.0; |
| 583 | |
| 584 | lubksb(a, indx, b); |
| 585 | // for(kk = 0; kk < np; ++kk) |
| 586 | // c[kk] = 0.0; |
| 587 | for(k = -nl; k <= nr; ++k) { |
| 588 | // for(k = -nl; k < nr; ++k) { |
| 589 | sum = b[0]; |
| 590 | fac = 1.0; |
| 591 | for(mm = 1; mm <= m; ++mm) |
| 592 | sum += b[mm] * (fac *= k); |
| 593 | // store in wrap=around order |
| 594 | kk = (np - k) % np; |
| 595 | //re-order c as I need for taps |
| 596 | // kk=k+nl; |
| 597 | tmpc[kk] = sum; |
| 598 | } |
| 599 | tapz.resize(nl+1+nr); |
| 600 | // for(k=0;k<nl+1+nr) |
| 601 | tapz[tapz.size()/2]=tmpc[0]; |
| 602 | //past data points |
| 603 | for(k=1;k<=tapz.size()/2;++k) |
| 604 | tapz[tapz.size()/2-k]=tmpc[k]; |
| 605 | //future data points |
| 606 | for(k=1;k<=tapz.size()/2;++k) |
| 607 | tapz[tapz.size()/2+k]=tmpc[np-k]; |
| 608 | } |
| 609 | |
| 610 | void filter::Filter::ludcmp(vector<double> &a, vector<int> &indx, double &d) { |
| 611 | const double TINY = 1.0e-20; |
| 612 | int i, imax = -1, j, k; |
| 613 | double big, dum, sum, temp; |
| 614 | |
| 615 | int n = indx.size(); |
| 616 | vector<double> vv(n, 0.0); |
| 617 | |
| 618 | d = 1.0; |
| 619 | for(i = 0; i < n; ++i) { |
| 620 | big = 0.0; |
| 621 | for(j = 0; j < n; ++j) |
| 622 | if((temp = fabs(a[i * n + j])) > big) big = temp; |
| 623 | |
| 624 | if(big == 0.0) { |
| 625 | cerr << "Singular matrix in routine ludcmp" << endl; |
| 626 | return; |
| 627 | } |
| 628 | vv[i] = 1. / big; |
| 629 | } |
| 630 | |
| 631 | for(j = 0; j < n; ++j) { |
| 632 | for(i = 0; i < j; ++i) { |
| 633 | sum = a[i * n + j]; |
| 634 | for(k = 0; k < i; ++k) |
| 635 | sum -= a[i * n + k] * a[k * n + j]; |
| 636 | a[i * n + j] = sum; |
| 637 | } |
| 638 | big = 0.0; |
| 639 | for(i = j; i < n; ++i) { |
| 640 | sum = a[i * n + j]; |
| 641 | for(k = 0; k < j; ++k) |
| 642 | sum -= a[i * n + k] * a[k * n + j]; |
| 643 | a[i * n + j] = sum; |
| 644 | if((dum = vv[i] * fabs(sum)) >= big) { |
| 645 | big = dum; |
| 646 | imax = i; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | if(j != imax) { |
| 651 | for(k = 0; k < n; ++k) { |
| 652 | dum = a[imax * n + k]; |
| 653 | a[imax * n + k] = a[j * n + k]; |
| 654 | a[j * n + k] = dum; |
| 655 | } |
| 656 | d = -d; |
| 657 | vv[imax] = vv[j]; |
| 658 | } |
| 659 | indx[j] = imax; |
| 660 | if(a[j * n + j] == 0.0) a[j * n + j] = TINY; |
| 661 | if(j != n - 1) { |
| 662 | dum = 1. / a[j * n + j]; |
| 663 | for(i = j + 1; i < n; ++i) |
| 664 | a[i * n + j] *= dum; |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | void filter::Filter::lubksb(vector<double> &a, vector<int> &indx, vector<double> &b) { |
| 670 | int i, ii = 0, ip, j; |
| 671 | double sum; |
| 672 | int n = indx.size(); |
| 673 | |
| 674 | for(i = 0; i < n; ++i) { |
| 675 | ip = indx[i]; |
| 676 | sum = b[ip]; |
| 677 | b[ip] = b[i]; |
| 678 | if(ii != 0) |
| 679 | for(j = ii - 1; j < i; ++j) |
| 680 | sum -= a[i * n + j] * b[j]; |
| 681 | else if(sum != 0.0) |
| 682 | ii = i + 1; |
| 683 | b[i] = sum; |
| 684 | } |
| 685 | for(i = n - 1; i >= 0; --i) { |
| 686 | sum = b[i]; |
| 687 | for(j = i + 1; j < n; ++j) |
| 688 | sum -= a[i * n + j] * b[j]; |
| 689 | b[i] = sum / a[i * n + i]; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | double filter::Filter::getCentreWavelength(const std::vector<double> &wavelengthIn, const Vector2d<double>& srf, const std::string& interpolationType, double delta, bool verbose) |
| 694 | { |
| 695 | assert(srf.size()==2);//[0]: wavelength, [1]: response function |
| 696 | int nband=srf[0].size(); |
| 697 | double start=floor(wavelengthIn[0]); |
| 698 | double end=ceil(wavelengthIn.back()); |
| 699 | if(verbose) |
| 700 | std::cout << "wavelengths in [" << start << "," << end << "]" << std::endl << std::flush; |
| 701 | |
| 702 | statfactory::StatFactory stat; |
| 703 | |
| 704 | gsl_interp_accel *acc; |
| 705 | stat.allocAcc(acc); |
| 706 | gsl_spline *spline; |
| 707 | stat.getSpline(interpolationType,nband,spline); |
| 708 | stat.initSpline(spline,&(srf[0][0]),&(srf[1][0]),nband); |
| 709 | if(verbose) |
| 710 | std::cout << "calculating norm of srf" << std::endl << std::flush; |
| 711 | double norm=0; |
| 712 | norm=gsl_spline_eval_integ(spline,srf[0].front(),srf[0].back(),acc); |
| 713 | if(verbose) |
| 714 | std::cout << "norm of srf: " << norm << std::endl << std::flush; |
| 715 | gsl_spline_free(spline); |
| 716 | gsl_interp_accel_free(acc); |
| 717 | std::vector<double> wavelength_fine; |
| 718 | for(double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta) |
| 719 | wavelength_fine.push_back(win); |
| 720 | |
| 721 | if(verbose) |
| 722 | std::cout << "interpolate wavelengths to " << wavelength_fine.size() << " entries " << std::endl; |
| 723 | std::vector<double> srf_fine;//spectral response function, interpolated for wavelength_fine |
| 724 | |
| 725 | stat.interpolateUp(srf[0],srf[1],wavelength_fine,interpolationType,srf_fine,verbose); |
| 726 | assert(srf_fine.size()==wavelength_fine.size()); |
| 727 | |
| 728 | gsl_interp_accel *accOut; |
| 729 | stat.allocAcc(accOut); |
| 730 | gsl_spline *splineOut; |
| 731 | stat.getSpline(interpolationType,wavelength_fine.size(),splineOut); |
| 732 | assert(splineOut); |
| 733 | |
| 734 | std::vector<double> wavelengthOut(wavelength_fine.size()); |
| 735 | |
| 736 | for(unsigned int iband=0;iband<wavelengthOut.size();++iband) |
| 737 | wavelengthOut[iband]=wavelength_fine[iband]*srf_fine[iband]; |
| 738 | |
| 739 | stat.initSpline(splineOut,&(wavelength_fine[0]),&(wavelengthOut[0]),wavelength_fine.size()); |
| 740 | double centreWavelength=gsl_spline_eval_integ(splineOut,start,end,accOut)/norm; |
| 741 | |
| 742 | gsl_spline_free(splineOut); |
| 743 | gsl_interp_accel_free(accOut); |
| 744 | |
| 745 | return(centreWavelength); |
| 746 | } |
| 747 | |
| 748 | // void filter::Filter::applyFwhm(const vector<double> &wavelengthIn, const Jim& input, const vector<double> &wavelengthOut, const vector<double> &fwhm, const std::string& interpolationType, Jim& output, bool verbose){ |
| 749 | // Vector2d<double> lineInput(input.nrOfBand(),input.nrOfCol()); |
| 750 | // Vector2d<double> lineOutput(wavelengthOut.size(),input.nrOfCol()); |
| 751 | // const char* pszMessage; |
| 752 | // void* pProgressArg=NULL; |
| 753 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 754 | // double progress=0; |
| 755 | // pfnProgress(progress,pszMessage,pProgressArg); |
| 756 | // for(unsigned int y=0;y<input.nrOfRow();++y){ |
| 757 | // for(unsigned int iband=0;iband<input.nrOfBand();++iband) |
| 758 | // input.readData(lineInput[iband],GDT_Float64,y,iband); |
| 759 | // applyFwhm<double>(wavelengthIn,lineInput,wavelengthOut,fwhm, interpolationType, lineOutput, verbose); |
| 760 | // for(unsigned int iband=0;iband<output.nrOfBand();++iband){ |
| 761 | // try{ |
| 762 | // output.writeData(lineOutput[iband],GDT_Float64,y,iband); |
| 763 | // } |
| 764 | // catch(string errorstring){ |
| 765 | // cerr << errorstring << "in band " << iband << ", line " << y << endl; |
| 766 | // } |
| 767 | // } |
| 768 | // progress=(1.0+y)/output.nrOfRow(); |
| 769 | // pfnProgress(progress,pszMessage,pProgressArg); |
| 770 | // } |
| 771 | // } |
| 772 | |
| 773 | // void filter::Filter::applySrf(const vector<double> &wavelengthIn, const Jim& input, const vector< Vector2d<double> > &srf, const std::string& interpolationType, Jim& output, bool verbose){ |
| 774 | // assert(output.nrOfBand()==srf.size()); |
| 775 | // double centreWavelength=0; |
| 776 | // Vector2d<double> lineInput(input.nrOfBand(),input.nrOfCol()); |
| 777 | // const char* pszMessage; |
| 778 | // void* pProgressArg=NULL; |
| 779 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 780 | // double progress=0; |
| 781 | // pfnProgress(progress,pszMessage,pProgressArg); |
| 782 | // for(unsigned int y=0;y<input.nrOfRow();++y){ |
| 783 | // for(unsigned int iband=0;iband<input.nrOfBand();++iband) |
| 784 | // input.readData(lineInput[iband],GDT_Float64,y,iband); |
| 785 | // for(int isrf=0;isrf<srf.size();++isrf){ |
| 786 | // vector<double> lineOutput(input.nrOfCol()); |
| 787 | // centreWavelength=applySrf<double>(wavelengthIn,lineInput,srf[isrf], interpolationType, lineOutput, verbose); |
| 788 | // for(unsigned int iband=0;iband<output.nrOfBand();++iband){ |
| 789 | // try{ |
| 790 | // output.writeData(lineOutput,GDT_Float64,y,isrf); |
| 791 | // } |
| 792 | // catch(string errorstring){ |
| 793 | // cerr << errorstring << "in band " << iband << ", line " << y << endl; |
| 794 | // } |
| 795 | // } |
| 796 | // } |
| 797 | // progress=(1.0+y)/output.nrOfRow(); |
| 798 | // pfnProgress(progress,pszMessage,pProgressArg); |
| 799 | // } |
| 800 | // } |