| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#ifndef _MYFILTER_H_ |
| 22 |
#define _MYFILTER_H_ |
| 23 |
|
| 24 |
#include <vector> |
| 25 |
#include <iostream> |
| 26 |
extern "C" { |
| 27 |
#include <gsl/gsl_sort.h> |
| 28 |
#include <gsl/gsl_wavelet.h> |
| 29 |
} |
| 30 |
#include "StatFactory.h" |
| 31 |
#include "imageclasses/Jim.h" |
| 32 |
|
| 33 |
namespace filter |
| 34 |
{ |
| 35 |
|
| 36 |
enum FILTER_TYPE { median=100, var=101 , min=102, max=103, sum=104, mean=105, minmax=106, dilate=107, erode=108, close=109, open=110, homog=111, sobelx=112, sobely=113, sobelxy=114, sobelyx=115, smooth=116, density=117, mode=118, mixed=119, threshold=120, ismin=121, ismax=122, heterog=123, order=124, stdev=125, mrf=126, dwt=127, dwti=128, dwt_cut=129, scramble=130, shift=131, linearfeature=132, smoothnodata=133, countid=134, dwt_cut_from=135, savgolay=136, percentile=137, proportion=138, nvalid=139, sauvola=140,first=141,last=142, minindex=143, maxindex=144}; |
| 37 |
|
| 38 |
enum PADDING { symmetric=0, replicate=1, circular=2, zero=3}; |
| 39 |
|
| 40 |
class Filter |
| 41 |
{ |
| 42 |
public: |
| 43 |
Filter(void); |
| 44 |
Filter(const std::vector<double> &taps); |
| 45 |
virtual ~Filter(){}; |
| 46 |
|
| 47 |
void setPadding(const std::string& padString){ |
| 48 |
m_padding=padString; |
| 49 |
}; |
| 50 |
|
| 51 |
static const gsl_wavelet_type* getWaveletType(const std::string waveletType){ |
| 52 |
if(waveletType=="daubechies") return(gsl_wavelet_daubechies); |
| 53 |
else if(waveletType=="daubechies_centered") return(gsl_wavelet_daubechies_centered); |
| 54 |
else if(waveletType=="haar") return(gsl_wavelet_haar); |
| 55 |
else if(waveletType=="haar_centered") return(gsl_wavelet_haar_centered); |
| 56 |
else if(waveletType=="bspline") return(gsl_wavelet_bspline); |
| 57 |
else if(waveletType=="bspline_centered") return(gsl_wavelet_bspline_centered); |
| 58 |
else{ |
| 59 |
std::string errorString="Error: wavelet type not supported"; |
| 60 |
throw(errorString); |
| 61 |
} |
| 62 |
} |
| 63 |
static FILTER_TYPE getFilterType(const std::string filterType){ |
| 64 |
std::map<std::string, FILTER_TYPE> m_filterMap; |
| 65 |
initFilterMap(m_filterMap); |
| 66 |
return m_filterMap[filterType]; |
| 67 |
}; |
| 68 |
|
| 69 |
void setTaps(const std::vector<double> &taps, bool normalize=true); |
| 70 |
void pushClass(short theClass=1){m_class.push_back(theClass);}; |
| 71 |
void pushMask(short theMask=0){m_mask.push_back(theMask);}; |
| 72 |
unsigned int pushNoDataValue(double noDataValue); |
| 73 |
unsigned int setNoDataValues(std::vector<double> vnodata); |
| 74 |
void pushThreshold(double theThreshold){m_threshold.push_back(theThreshold);}; |
| 75 |
void setThresholds(const std::vector<double>& theThresholds){m_threshold=theThresholds;}; |
| 76 |
template<class T> void filter(const std::vector<T>& input, std::vector<T>& output); |
| 77 |
template<class T> void filter(const std::vector<T>& input, std::vector<T>& output, const std::string& method, int dim); |
| 78 |
template<class T> void smooth(const std::vector<T>& input, std::vector<T>& output, short dim); |
| 79 |
template<class T> void smoothNoData(const std::vector<T>& input, const std::string& interpolationType, std::vector<T>& output); |
| 80 |
template<class T> void filter(T* input, int inputSize, std::vector<T>& output); |
| 81 |
template<class T> void smooth(T* input, int inputSize, std::vector<T>& output, short dim); |
| 82 |
|
| 83 |
void morphology(Jim& input, Jim& output, const std::string& method, int dim, short verbose=0); |
| 84 |
void filter(Jim& input, Jim& output); |
| 85 |
void stat(Jim& input, Jim& output, const std::string& method); |
| 86 |
void stats(Jim& input, Jim& output, const std::vector<std::string >& methods); |
| 87 |
void filter(Jim& input, Jim& output, const std::string& method, int dim); |
| 88 |
void getSavGolayCoefficients(std::vector<double> &c, int np, int nl, int nr, int ld, int m); |
| 89 |
void ludcmp(std::vector<double> &a, std::vector<int> &indx, double &d); |
| 90 |
void lubksb(std::vector<double> &a, std::vector<int> &indx, std::vector<double> &b); |
| 91 |
|
| 92 |
void smooth(Jim& input, Jim& output, short dim); |
| 93 |
void smoothNoData(Jim& input, const std::string& interpolationType, Jim& output); |
| 94 |
double getCentreWavelength(const std::vector<double> &wavelengthIn, const Vector2d<double>& srf, const std::string& interpolationType, double delta=1.0, bool verbose=false); |
| 95 |
template<class T> double applySrf(const std::vector<double> &wavelengthIn, const std::vector<T>& input, const Vector2d<double>& srf, const std::string& interpolationType, T& output, double delta=1.0, bool normalize=false, bool verbose=false); |
| 96 |
template<class T> double applySrf(const std::vector<double> &wavelengthIn, const Vector2d<T>& input, const Vector2d<double>& srf, const std::string& interpolationType, std::vector<T>& output, double delta=1.0, bool normalize=false, int down=1, bool transposeInput=false, bool verbose=false); |
| 97 |
|
| 98 |
template<class T> void applyFwhm(const std::vector<double> &wavelengthIn, const std::vector<T>& input, const std::vector<double> &wavelengthOut, const std::vector<double> &fwhm, const std::string& interpolationType, std::vector<T>& output, bool verbose=false); |
| 99 |
template<class T> void applyFwhm(const std::vector<double> &wavelengthIn, const Vector2d<T>& input, const std::vector<double> &wavelengthOut, const std::vector<double> &fwhm, const std::string& interpolationType, Vector2d<T>& output, int down=1, bool verbose=false); |
| 100 |
void dwtForward(Jim& input, Jim& output, const std::string& wavelet_type, int family); |
| 101 |
void dwtInverse(Jim& input, Jim& output, const std::string& wavelet_type, int family); |
| 102 |
void dwtCut(Jim& input, Jim& output, const std::string& wavelet_type, int family, double cut); |
| 103 |
void dwtForward(std::vector<double>& data, const std::string& wavelet_type, int family); |
| 104 |
void dwtInverse(std::vector<double>& data, const std::string& wavelet_type, int family); |
| 105 |
void dwtCut(std::vector<double>& data, const std::string& wavelet_type, int family, double cut); |
| 106 |
void dwtCutFrom(Jim& input, Jim& output, const std::string& wavelet_type, int family, int band); |
| 107 |
|
| 108 |
static PADDING getPadding(const std::string& padString){ |
| 109 |
std::map<std::string, PADDING> padMap; |
| 110 |
padMap["zero"]=filter::zero; |
| 111 |
padMap["symmetric"]=filter::symmetric; |
| 112 |
padMap["replicate"]=filter::replicate; |
| 113 |
padMap["circular"]=filter::circular; |
| 114 |
return(padMap[padString]); |
| 115 |
}; |
| 116 |
|
| 117 |
private: |
| 118 |
|
| 119 |
static void initFilterMap(std::map<std::string, FILTER_TYPE>& m_filterMap){ |
| 120 |
|
| 121 |
m_filterMap["median"]=filter::median; |
| 122 |
m_filterMap["var"]=filter::var; |
| 123 |
m_filterMap["min"]=filter::min; |
| 124 |
m_filterMap["max"]=filter::max; |
| 125 |
m_filterMap["sum"]=filter::sum; |
| 126 |
m_filterMap["mean"]=filter::mean; |
| 127 |
m_filterMap["minmax"]=filter::minmax; |
| 128 |
m_filterMap["dilate"]=filter::dilate; |
| 129 |
m_filterMap["erode"]=filter::erode; |
| 130 |
m_filterMap["close"]=filter::close; |
| 131 |
m_filterMap["open"]=filter::open; |
| 132 |
m_filterMap["homog"]=filter::homog; |
| 133 |
m_filterMap["sobelx"]=filter::sobelx; |
| 134 |
m_filterMap["sobely"]=filter::sobely; |
| 135 |
m_filterMap["sobelxy"]=filter::sobelxy; |
| 136 |
m_filterMap["sobelyx"]=filter::sobelyx; |
| 137 |
m_filterMap["smooth"]=filter::smooth; |
| 138 |
m_filterMap["density"]=filter::density; |
| 139 |
m_filterMap["mode"]=filter::mode; |
| 140 |
m_filterMap["mixed"]=filter::mixed; |
| 141 |
m_filterMap["threshold"]=filter::threshold; |
| 142 |
m_filterMap["ismin"]=filter::ismin; |
| 143 |
m_filterMap["ismax"]=filter::ismax; |
| 144 |
m_filterMap["heterog"]=filter::heterog; |
| 145 |
m_filterMap["order"]=filter::order; |
| 146 |
m_filterMap["stdev"]=filter::stdev; |
| 147 |
m_filterMap["mrf"]=filter::mrf; |
| 148 |
m_filterMap["dwt"]=filter::dwt; |
| 149 |
m_filterMap["dwti"]=filter::dwti; |
| 150 |
m_filterMap["dwt_cut"]=filter::dwt_cut; |
| 151 |
m_filterMap["scramble"]=filter::scramble; |
| 152 |
m_filterMap["shift"]=filter::shift; |
| 153 |
m_filterMap["linearfeature"]=filter::linearfeature; |
| 154 |
m_filterMap["smoothnodata"]=filter::smoothnodata; |
| 155 |
m_filterMap["countid"]=filter::countid; |
| 156 |
m_filterMap["dwt_cut_from"]=filter::dwt_cut_from; |
| 157 |
m_filterMap["savgolay"]=filter::savgolay; |
| 158 |
m_filterMap["percentile"]=filter::percentile; |
| 159 |
m_filterMap["proportion"]=filter::proportion; |
| 160 |
m_filterMap["nvalid"]=filter::nvalid; |
| 161 |
m_filterMap["sauvola"]=filter::sauvola; |
| 162 |
m_filterMap["first"]=filter::first; |
| 163 |
m_filterMap["last"]=filter::last; |
| 164 |
m_filterMap["minindex"]=filter::minindex; |
| 165 |
m_filterMap["maxindex"]=filter::maxindex; |
| 166 |
} |
| 167 |
|
| 168 |
std::vector<double> m_taps; |
| 169 |
std::vector<short> m_class; |
| 170 |
std::vector<short> m_mask; |
| 171 |
std::string m_padding; |
| 172 |
std::vector<double> m_noDataValues; |
| 173 |
std::vector<double> m_threshold; |
| 174 |
}; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
template<class T> double Filter::applySrf(const std::vector<double> &wavelengthIn, const std::vector<T>& input, const Vector2d<double>& srf, const std::string& interpolationType, T& output, double delta, bool normalize, bool verbose) |
| 180 |
{ |
| 181 |
assert(srf.size()==2); |
| 182 |
int nband=srf[0].size(); |
| 183 |
double start=floor(wavelengthIn[0]); |
| 184 |
double end=ceil(wavelengthIn.back()); |
| 185 |
if(verbose) |
| 186 |
std::cout << "wavelengths in [" << start << "," << end << "]" << std::endl << std::flush; |
| 187 |
|
| 188 |
statfactory::StatFactory stat; |
| 189 |
|
| 190 |
gsl_interp_accel *acc; |
| 191 |
stat.allocAcc(acc); |
| 192 |
gsl_spline *spline; |
| 193 |
stat.getSpline(interpolationType,nband,spline); |
| 194 |
stat.initSpline(spline,&(srf[0][0]),&(srf[1][0]),nband); |
| 195 |
if(verbose) |
| 196 |
std::cout << "calculating norm of srf" << std::endl << std::flush; |
| 197 |
double norm=0; |
| 198 |
norm=gsl_spline_eval_integ(spline,srf[0].front(),srf[0].back(),acc); |
| 199 |
if(verbose) |
| 200 |
std::cout << "norm of srf: " << norm << std::endl << std::flush; |
| 201 |
gsl_spline_free(spline); |
| 202 |
gsl_interp_accel_free(acc); |
| 203 |
|
| 204 |
|
| 205 |
std::vector<double> wavelength_fine; |
| 206 |
for(double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta) |
| 207 |
wavelength_fine.push_back(win); |
| 208 |
|
| 209 |
if(verbose) |
| 210 |
std::cout << "interpolate wavelengths to " << wavelength_fine.size() << " entries " << std::endl; |
| 211 |
std::vector<double> srf_fine; |
| 212 |
|
| 213 |
stat.interpolateUp(srf[0],srf[1],wavelength_fine,interpolationType,srf_fine,verbose); |
| 214 |
assert(srf_fine.size()==wavelength_fine.size()); |
| 215 |
|
| 216 |
gsl_interp_accel *accOut; |
| 217 |
stat.allocAcc(accOut); |
| 218 |
gsl_spline *splineOut; |
| 219 |
stat.getSpline(interpolationType,wavelength_fine.size(),splineOut); |
| 220 |
assert(splineOut); |
| 221 |
|
| 222 |
assert(wavelengthIn.size()==input.size()); |
| 223 |
std::vector<double> input_fine; |
| 224 |
std::vector<double> product(wavelength_fine.size()); |
| 225 |
std::vector<double> wavelengthOut(wavelength_fine.size()); |
| 226 |
stat.interpolateUp(wavelengthIn,input,wavelength_fine,interpolationType,input_fine,verbose); |
| 227 |
|
| 228 |
if(verbose) |
| 229 |
std::cout << "input_fine.size(): " << input_fine.size() << std::endl; |
| 230 |
for(int iband=0;iband<input_fine.size();++iband){ |
| 231 |
product[iband]=input_fine[iband]*srf_fine[iband]; |
| 232 |
wavelengthOut[iband]=wavelength_fine[iband]*srf_fine[iband]; |
| 233 |
} |
| 234 |
|
| 235 |
assert(input_fine.size()==srf_fine.size()); |
| 236 |
assert(input_fine.size()==wavelength_fine.size()); |
| 237 |
stat.initSpline(splineOut,&(wavelength_fine[0]),&(product[0]),wavelength_fine.size()); |
| 238 |
if(normalize) |
| 239 |
output=gsl_spline_eval_integ(splineOut,start,end,accOut)/norm; |
| 240 |
else |
| 241 |
output=gsl_spline_eval_integ(splineOut,start,end,accOut); |
| 242 |
|
| 243 |
stat.initSpline(splineOut,&(wavelength_fine[0]),&(wavelengthOut[0]),wavelength_fine.size()); |
| 244 |
double centreWavelength=gsl_spline_eval_integ(splineOut,start,end,accOut)/norm; |
| 245 |
|
| 246 |
gsl_spline_free(splineOut); |
| 247 |
gsl_interp_accel_free(accOut); |
| 248 |
|
| 249 |
return(centreWavelength); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
template<class T> double Filter::applySrf(const std::vector<double> &wavelengthIn, const Vector2d<T>& input, const Vector2d<double>& srf, const std::string& interpolationType, std::vector<T>& output, double delta, bool normalize, int down, bool transposeInput, bool verbose) |
| 255 |
{ |
| 256 |
assert(srf.size()==2); |
| 257 |
int nband=srf[0].size(); |
| 258 |
unsigned int nsample=(transposeInput)? input.size():input[0].size(); |
| 259 |
output.resize((nsample+down-1)/down); |
| 260 |
double start=floor(wavelengthIn[0]); |
| 261 |
double end=ceil(wavelengthIn.back()); |
| 262 |
if(verbose) |
| 263 |
std::cout << "wavelengths in [" << start << "," << end << "]" << std::endl << std::flush; |
| 264 |
|
| 265 |
statfactory::StatFactory stat; |
| 266 |
|
| 267 |
gsl_interp_accel *acc; |
| 268 |
stat.allocAcc(acc); |
| 269 |
gsl_spline *spline; |
| 270 |
stat.getSpline(interpolationType,nband,spline); |
| 271 |
stat.initSpline(spline,&(srf[0][0]),&(srf[1][0]),nband); |
| 272 |
if(verbose) |
| 273 |
std::cout << "calculating norm of srf" << std::endl << std::flush; |
| 274 |
double norm=0; |
| 275 |
norm=gsl_spline_eval_integ(spline,srf[0].front(),srf[0].back(),acc); |
| 276 |
if(verbose) |
| 277 |
std::cout << "norm of srf: " << norm << std::endl << std::flush; |
| 278 |
gsl_spline_free(spline); |
| 279 |
gsl_interp_accel_free(acc); |
| 280 |
|
| 281 |
|
| 282 |
std::vector<double> wavelength_fine; |
| 283 |
for(double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta) |
| 284 |
wavelength_fine.push_back(win); |
| 285 |
|
| 286 |
if(verbose) |
| 287 |
std::cout << "interpolate wavelengths to " << wavelength_fine.size() << " entries " << std::endl; |
| 288 |
std::vector<double> srf_fine; |
| 289 |
|
| 290 |
stat.interpolateUp(srf[0],srf[1],wavelength_fine,interpolationType,srf_fine,verbose); |
| 291 |
assert(srf_fine.size()==wavelength_fine.size()); |
| 292 |
|
| 293 |
gsl_interp_accel *accOut; |
| 294 |
stat.allocAcc(accOut); |
| 295 |
gsl_spline *splineOut; |
| 296 |
stat.getSpline(interpolationType,wavelength_fine.size(),splineOut); |
| 297 |
assert(splineOut); |
| 298 |
|
| 299 |
std::vector<double> wavelengthOut; |
| 300 |
double centreWavelength=0; |
| 301 |
for(int isample=0;isample<nsample;++isample){ |
| 302 |
if((isample+1+down/2)%down) |
| 303 |
continue; |
| 304 |
std::vector<T> inputValues; |
| 305 |
if(transposeInput) |
| 306 |
inputValues=input[isample]; |
| 307 |
else |
| 308 |
input.selectCol(isample,inputValues); |
| 309 |
assert(wavelengthIn.size()==inputValues.size()); |
| 310 |
std::vector<double> input_fine; |
| 311 |
std::vector<double> product(wavelength_fine.size()); |
| 312 |
stat.interpolateUp(wavelengthIn,inputValues,wavelength_fine,interpolationType,input_fine,verbose); |
| 313 |
|
| 314 |
for(int iband=0;iband<input_fine.size();++iband){ |
| 315 |
product[iband]=input_fine[iband]*srf_fine[iband]; |
| 316 |
if(wavelengthOut.size()<input_fine.size()) |
| 317 |
wavelengthOut.push_back(wavelength_fine[iband]*srf_fine[iband]); |
| 318 |
} |
| 319 |
|
| 320 |
assert(input_fine.size()==srf_fine.size()); |
| 321 |
assert(input_fine.size()==wavelength_fine.size()); |
| 322 |
stat.initSpline(splineOut,&(wavelength_fine[0]),&(product[0]),wavelength_fine.size()); |
| 323 |
if(normalize) |
| 324 |
output[isample/down]=gsl_spline_eval_integ(splineOut,start,end,accOut)/norm; |
| 325 |
else |
| 326 |
output[isample/down]=gsl_spline_eval_integ(splineOut,start,end,accOut); |
| 327 |
|
| 328 |
stat.initSpline(splineOut,&(wavelength_fine[0]),&(wavelengthOut[0]),wavelength_fine.size()); |
| 329 |
if(centreWavelength>0); |
| 330 |
else |
| 331 |
centreWavelength=gsl_spline_eval_integ(splineOut,start,end,accOut)/norm; |
| 332 |
} |
| 333 |
gsl_spline_free(splineOut); |
| 334 |
gsl_interp_accel_free(accOut); |
| 335 |
|
| 336 |
return(centreWavelength); |
| 337 |
} |
| 338 |
|
| 339 |
template<class T> void Filter::applyFwhm(const std::vector<double> &wavelengthIn, const std::vector<T>& input, const std::vector<double> &wavelengthOut, const std::vector<double> &fwhm, const std::string& interpolationType, std::vector<T>& output, bool verbose){ |
| 340 |
double delta=1; |
| 341 |
std::vector<double> stddev(fwhm.size()); |
| 342 |
for(int index=0;index<fwhm.size();++index) |
| 343 |
stddev[index]=fwhm[index]/2.0/sqrt(2*log(2.0)); |
| 344 |
assert(wavelengthOut.size()==fwhm.size()); |
| 345 |
assert(wavelengthIn.size()==input.size()); |
| 346 |
assert(wavelengthIn[0]<=wavelengthOut[0]); |
| 347 |
assert(wavelengthIn.back()>=wavelengthOut.back()); |
| 348 |
statfactory::StatFactory stat; |
| 349 |
std::vector<double> input_fine; |
| 350 |
std::vector<double> wavelength_fine; |
| 351 |
for(double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta) |
| 352 |
wavelength_fine.push_back(win); |
| 353 |
if(verbose){ |
| 354 |
for(int index=0;index<wavelength_fine.size();++index) |
| 355 |
std::cout << " " << wavelength_fine[index]; |
| 356 |
std::cout << std::endl; |
| 357 |
std::cout << "interpolate input wavelength to " << delta << " nm resolution (size=" << wavelength_fine.size() << ")" << std::endl; |
| 358 |
} |
| 359 |
stat.interpolateUp(wavelengthIn,input,wavelength_fine,interpolationType,input_fine,verbose); |
| 360 |
int nbandIn=wavelength_fine.size(); |
| 361 |
|
| 362 |
int nbandOut=wavelengthOut.size(); |
| 363 |
output.resize(nbandOut); |
| 364 |
Vector2d<double> tf(nbandIn,nbandOut); |
| 365 |
for(int indexOut=0;indexOut<nbandOut;++indexOut){ |
| 366 |
double norm=0; |
| 367 |
for(int indexIn=0;indexIn<nbandIn;++indexIn){ |
| 368 |
|
| 369 |
tf[indexIn][indexOut]= |
| 370 |
exp((wavelengthOut[indexOut]-wavelength_fine[indexIn]) |
| 371 |
*(wavelength_fine[indexIn]-wavelengthOut[indexOut]) |
| 372 |
/2.0/stddev[indexOut] |
| 373 |
/stddev[indexOut]); |
| 374 |
tf[indexIn][indexOut]/=sqrt(2.0*M_PI); |
| 375 |
tf[indexIn][indexOut]/=stddev[indexOut]; |
| 376 |
norm+=tf[indexIn][indexOut]; |
| 377 |
} |
| 378 |
output[indexOut]=0; |
| 379 |
for(int indexIn=0;indexIn<nbandIn;++indexIn) |
| 380 |
output[indexOut]+=input_fine[indexIn]*tf[indexIn][indexOut]/norm; |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
template<class T> void Filter::applyFwhm(const std::vector<double> &wavelengthIn, const Vector2d<T>& input, const std::vector<double> &wavelengthOut, const std::vector<double> &fwhm, const std::string& interpolationType, Vector2d<T>& output, int down, bool verbose){ |
| 387 |
double delta=1; |
| 388 |
std::vector<double> stddev(fwhm.size()); |
| 389 |
for(int index=0;index<fwhm.size();++index) |
| 390 |
stddev[index]=fwhm[index]/2.0/sqrt(2*log(2.0)); |
| 391 |
statfactory::StatFactory stat; |
| 392 |
std::vector<double> wavelength_fine; |
| 393 |
for(double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta) |
| 394 |
wavelength_fine.push_back(win); |
| 395 |
assert(wavelengthOut.size()==fwhm.size()); |
| 396 |
assert(wavelengthIn[0]<=wavelengthOut[0]); |
| 397 |
assert(wavelengthIn.back()>=wavelengthOut.back()); |
| 398 |
if(verbose){ |
| 399 |
for(int index=0;index<wavelength_fine.size();++index) |
| 400 |
std::cout << " " << wavelength_fine[index]; |
| 401 |
std::cout << std::endl; |
| 402 |
std::cout << "interpolate input wavelength to " << delta << " nm resolution (size=" << wavelength_fine.size() << ")" << std::endl; |
| 403 |
} |
| 404 |
int nbandIn=wavelength_fine.size(); |
| 405 |
int nbandOut=wavelengthOut.size(); |
| 406 |
output.resize(nbandOut,(input[0].size()+down-1)/down); |
| 407 |
|
| 408 |
Vector2d<double> tf(nbandIn,nbandOut); |
| 409 |
std::vector<double> norm(nbandOut); |
| 410 |
for(int indexOut=0;indexOut<nbandOut;++indexOut){ |
| 411 |
norm[indexOut]=0; |
| 412 |
for(int indexIn=0;indexIn<nbandIn;++indexIn){ |
| 413 |
tf[indexIn][indexOut]= |
| 414 |
exp((wavelengthOut[indexOut]-wavelength_fine[indexIn]) |
| 415 |
*(wavelength_fine[indexIn]-wavelengthOut[indexOut]) |
| 416 |
/2.0/stddev[indexOut] |
| 417 |
/stddev[indexOut]); |
| 418 |
tf[indexIn][indexOut]/=sqrt(2.0*M_PI); |
| 419 |
tf[indexIn][indexOut]/=stddev[indexOut]; |
| 420 |
norm[indexOut]+=tf[indexIn][indexOut]; |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
for(int isample=0;isample<input[0].size();++isample){ |
| 425 |
if((isample+1+down/2)%down) |
| 426 |
continue; |
| 427 |
std::vector<T> inputValues; |
| 428 |
input.selectCol(isample,inputValues); |
| 429 |
assert(wavelengthIn.size()==inputValues.size()); |
| 430 |
for(int indexOut=0;indexOut<nbandOut;++indexOut){ |
| 431 |
std::vector<double> input_fine; |
| 432 |
stat.interpolateUp(wavelengthIn,inputValues,wavelength_fine,interpolationType,input_fine,verbose); |
| 433 |
output[indexOut][(isample+down-1)/down]=0; |
| 434 |
for(int indexIn=0;indexIn<nbandIn;++indexIn){ |
| 435 |
output[indexOut][(isample+down-1)/down]+=input_fine[indexIn]*tf[indexIn][indexOut]/norm[indexOut]; |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
template<class T> void Filter::smooth(const std::vector<T>& input, std::vector<T>& output, short dim) |
| 442 |
{ |
| 443 |
assert(dim>0); |
| 444 |
m_taps.resize(dim); |
| 445 |
for(int itap=0;itap<dim;++itap) |
| 446 |
m_taps[itap]=1.0/dim; |
| 447 |
filter(input,output); |
| 448 |
} |
| 449 |
|
| 450 |
template<class T> void Filter::smoothNoData(const std::vector<T>& input, const std::string& interpolationType, std::vector<T>& output) |
| 451 |
{ |
| 452 |
statfactory::StatFactory stat; |
| 453 |
stat.setNoDataValues(m_noDataValues); |
| 454 |
std::vector<double> abscis(input.size()); |
| 455 |
for(int i=0;i<abscis.size();++i) |
| 456 |
abscis[i]=i; |
| 457 |
stat.interpolateNoData(abscis,input,interpolationType,output); |
| 458 |
} |
| 459 |
|
| 460 |
template<class T> void Filter::filter(const std::vector<T>& input, std::vector<T>& output) |
| 461 |
{ |
| 462 |
assert(input.size()>=m_taps.size()); |
| 463 |
output.resize(input.size()); |
| 464 |
int i=0; |
| 465 |
|
| 466 |
for(i=0;i<m_taps.size()/2;++i){ |
| 467 |
|
| 468 |
output[i]=m_taps[m_taps.size()/2]*input[i]; |
| 469 |
for(int t=1;t<=m_taps.size()/2;++t){ |
| 470 |
output[i]+=m_taps[m_taps.size()/2+t]*input[i+t]; |
| 471 |
if(i>=t) |
| 472 |
output[i]+=m_taps[m_taps.size()/2-t]*input[i-t]; |
| 473 |
else{ |
| 474 |
switch(getPadding(m_padding)){ |
| 475 |
case(replicate): |
| 476 |
output[i]+=m_taps[m_taps.size()/2-t]*input[0]; |
| 477 |
break; |
| 478 |
case(circular): |
| 479 |
output[i]+=m_taps[m_taps.size()/2-t]*input[input.size()+i-t]; |
| 480 |
break; |
| 481 |
case(zero): |
| 482 |
output[i]+=m_taps[m_taps.size()/2-t]*0; |
| 483 |
break; |
| 484 |
case(symmetric): |
| 485 |
default: |
| 486 |
output[i]+=m_taps[m_taps.size()/2-t]*input[t-i]; |
| 487 |
break; |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
for(i=m_taps.size()/2;i<input.size()-m_taps.size()/2;++i){ |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
output[i]=0; |
| 498 |
for(int t=0;t<m_taps.size();++t) |
| 499 |
output[i]+=input[i-m_taps.size()/2+t]*m_taps[t]; |
| 500 |
} |
| 501 |
|
| 502 |
for(i=input.size()-m_taps.size()/2;i<input.size();++i){ |
| 503 |
|
| 504 |
output[i]=m_taps[m_taps.size()/2]*input[i]; |
| 505 |
|
| 506 |
for(int t=1;t<=m_taps.size()/2;++t){ |
| 507 |
output[i]+=m_taps[m_taps.size()/2-t]*input[i-t]; |
| 508 |
if(i+t<input.size()) |
| 509 |
output[i]+=m_taps[m_taps.size()/2+t]*input[i+t]; |
| 510 |
else{ |
| 511 |
switch(getPadding(m_padding)){ |
| 512 |
case(replicate): |
| 513 |
output[i]+=m_taps[m_taps.size()/2+t]*input.back(); |
| 514 |
break; |
| 515 |
case(circular): |
| 516 |
output[i]+=m_taps[m_taps.size()/2+t]*input[t-1]; |
| 517 |
break; |
| 518 |
case(zero): |
| 519 |
output[i]+=m_taps[m_taps.size()/2+t]*0; |
| 520 |
break; |
| 521 |
case(symmetric): |
| 522 |
default: |
| 523 |
output[i]+=m_taps[m_taps.size()/2+t]*input[i-t]; |
| 524 |
break; |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
} |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
template<class T> void Filter::filter(const std::vector<T>& input, std::vector<T>& output, const std::string& method, int dim) |
| 534 |
{ |
| 535 |
assert(dim); |
| 536 |
output.resize(input.size()); |
| 537 |
int i=0; |
| 538 |
statfactory::StatFactory stat; |
| 539 |
stat.setNoDataValues(m_noDataValues); |
| 540 |
std::vector<T> statBuffer; |
| 541 |
short binValue=0; |
| 542 |
|
| 543 |
for(i=0;i<dim/2;++i){ |
| 544 |
binValue=0; |
| 545 |
for(int iclass=0;iclass<m_class.size();++iclass){ |
| 546 |
if(input[i]==m_class[iclass]){ |
| 547 |
binValue=m_class[0]; |
| 548 |
break; |
| 549 |
} |
| 550 |
} |
| 551 |
if(m_class.size()) |
| 552 |
statBuffer.push_back(binValue); |
| 553 |
else |
| 554 |
statBuffer.push_back(input[i]); |
| 555 |
|
| 556 |
for(int t=1;t<=dim/2;++t){ |
| 557 |
T theValue=input[i+t]; |
| 558 |
for(int iclass=0;iclass<m_class.size();++iclass){ |
| 559 |
if(theValue==m_class[iclass]){ |
| 560 |
binValue=m_class[0]; |
| 561 |
break; |
| 562 |
} |
| 563 |
} |
| 564 |
if(m_class.size()) |
| 565 |
statBuffer.push_back(binValue); |
| 566 |
else |
| 567 |
statBuffer.push_back(theValue); |
| 568 |
|
| 569 |
if(i>=t){ |
| 570 |
theValue=input[i-t]; |
| 571 |
} |
| 572 |
else{ |
| 573 |
switch(getPadding(m_padding)){ |
| 574 |
case(replicate): |
| 575 |
theValue=input[0]; |
| 576 |
break; |
| 577 |
case(circular): |
| 578 |
theValue=input[input.size()+i-t]; |
| 579 |
break; |
| 580 |
case(zero): |
| 581 |
theValue=0; |
| 582 |
break; |
| 583 |
case(symmetric): |
| 584 |
default: |
| 585 |
theValue=input[t-i]; |
| 586 |
break; |
| 587 |
} |
| 588 |
} |
| 589 |
for(int iclass=0;iclass<m_class.size();++iclass){ |
| 590 |
if(theValue==m_class[iclass]){ |
| 591 |
binValue=m_class[0]; |
| 592 |
break; |
| 593 |
} |
| 594 |
} |
| 595 |
if(m_class.size()) |
| 596 |
statBuffer.push_back(binValue); |
| 597 |
else |
| 598 |
statBuffer.push_back(theValue); |
| 599 |
} |
| 600 |
|
| 601 |
switch(getFilterType(method)){ |
| 602 |
case(filter::first): |
| 603 |
output[i]=statBuffer[0]; |
| 604 |
break; |
| 605 |
case(filter::last): |
| 606 |
output[i]=statBuffer.back(); |
| 607 |
break; |
| 608 |
case(filter::nvalid): |
| 609 |
output[i]=stat.nvalid(statBuffer); |
| 610 |
break; |
| 611 |
case(filter::median): |
| 612 |
output[i]=stat.median(statBuffer); |
| 613 |
break; |
| 614 |
case(filter::min): |
| 615 |
case(filter::erode): |
| 616 |
output[i]=stat.mymin(statBuffer); |
| 617 |
break; |
| 618 |
case(filter::max): |
| 619 |
case(filter::dilate): |
| 620 |
output[i]=stat.mymax(statBuffer); |
| 621 |
break; |
| 622 |
case(filter::sum): |
| 623 |
output[i]=sqrt(stat.sum(statBuffer)); |
| 624 |
break; |
| 625 |
case(filter::var): |
| 626 |
output[i]=stat.var(statBuffer); |
| 627 |
break; |
| 628 |
case(filter::stdev): |
| 629 |
output[i]=sqrt(stat.var(statBuffer)); |
| 630 |
break; |
| 631 |
case(filter::mean): |
| 632 |
output[i]=stat.mean(statBuffer); |
| 633 |
break; |
| 634 |
case(filter::percentile): |
| 635 |
assert(m_threshold.size()); |
| 636 |
output[i]=stat.percentile(statBuffer,statBuffer.begin(),statBuffer.end(),m_threshold[0]); |
| 637 |
break; |
| 638 |
default:{ |
| 639 |
std::ostringstream ess; |
| 640 |
ess << "method " << method << " (" << getFilterType(method) << ") not supported"; |
| 641 |
throw(ess.str()); |
| 642 |
break; |
| 643 |
} |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
statBuffer.clear(); |
| 648 |
for(i=dim/2;i<input.size()-dim/2;++i){ |
| 649 |
binValue=0; |
| 650 |
for(int t=0;t<dim;++t){ |
| 651 |
for(int iclass=0;iclass<m_class.size();++iclass){ |
| 652 |
if(input[i-dim/2+t]==m_class[iclass]){ |
| 653 |
binValue=m_class[0]; |
| 654 |
break; |
| 655 |
} |
| 656 |
} |
| 657 |
if(m_class.size()) |
| 658 |
statBuffer.push_back(binValue); |
| 659 |
else |
| 660 |
statBuffer.push_back(input[i-dim/2+t]); |
| 661 |
} |
| 662 |
switch(getFilterType(method)){ |
| 663 |
case(filter::first): |
| 664 |
output[i]=statBuffer[0]; |
| 665 |
break; |
| 666 |
case(filter::last): |
| 667 |
output[i]=statBuffer.back(); |
| 668 |
break; |
| 669 |
case(filter::nvalid): |
| 670 |
output[i]=stat.nvalid(statBuffer); |
| 671 |
break; |
| 672 |
case(filter::median): |
| 673 |
output[i]=stat.median(statBuffer); |
| 674 |
break; |
| 675 |
case(filter::min): |
| 676 |
case(filter::erode): |
| 677 |
output[i]=stat.mymin(statBuffer); |
| 678 |
break; |
| 679 |
case(filter::max): |
| 680 |
case(filter::dilate): |
| 681 |
output[i]=stat.mymax(statBuffer); |
| 682 |
break; |
| 683 |
case(filter::sum): |
| 684 |
output[i]=sqrt(stat.sum(statBuffer)); |
| 685 |
break; |
| 686 |
case(filter::var): |
| 687 |
output[i]=stat.var(statBuffer); |
| 688 |
break; |
| 689 |
case(filter::mean): |
| 690 |
output[i]=stat.mean(statBuffer); |
| 691 |
break; |
| 692 |
case(filter::percentile): |
| 693 |
assert(m_threshold.size()); |
| 694 |
output[i]=stat.percentile(statBuffer,statBuffer.begin(),statBuffer.end(),m_threshold[0]); |
| 695 |
break; |
| 696 |
default: |
| 697 |
std::string errorString="method not supported"; |
| 698 |
throw(errorString); |
| 699 |
break; |
| 700 |
} |
| 701 |
statBuffer.clear(); |
| 702 |
} |
| 703 |
|
| 704 |
for(i=input.size()-dim/2;i<input.size();++i){ |
| 705 |
binValue=0; |
| 706 |
for(int iclass=0;iclass<m_class.size();++iclass){ |
| 707 |
if(input[i]==m_class[iclass]){ |
| 708 |
binValue=m_class[0]; |
| 709 |
break; |
| 710 |
} |
| 711 |
} |
| 712 |
if(m_class.size()) |
| 713 |
statBuffer.push_back(binValue); |
| 714 |
else |
| 715 |
statBuffer.push_back(input[i]); |
| 716 |
|
| 717 |
for(int t=1;t<=dim/2;++t){ |
| 718 |
T theValue=input[i-t]; |
| 719 |
for(int iclass=0;iclass<m_class.size();++iclass){ |
| 720 |
if(theValue==m_class[iclass]){ |
| 721 |
binValue=m_class[0]; |
| 722 |
break; |
| 723 |
} |
| 724 |
} |
| 725 |
if(m_class.size()) |
| 726 |
statBuffer.push_back(binValue); |
| 727 |
else |
| 728 |
statBuffer.push_back(theValue); |
| 729 |
if(i+t<input.size()) |
| 730 |
theValue=input[i+t]; |
| 731 |
else{ |
| 732 |
switch(getPadding(m_padding)){ |
| 733 |
case(replicate): |
| 734 |
theValue=input.back(); |
| 735 |
break; |
| 736 |
case(circular): |
| 737 |
theValue=input[t-1]; |
| 738 |
break; |
| 739 |
case(zero): |
| 740 |
theValue=0; |
| 741 |
break; |
| 742 |
case(symmetric): |
| 743 |
default: |
| 744 |
theValue=input[i-t]; |
| 745 |
break; |
| 746 |
} |
| 747 |
} |
| 748 |
for(int iclass=0;iclass<m_class.size();++iclass){ |
| 749 |
if(theValue==m_class[iclass]){ |
| 750 |
binValue=m_class[0]; |
| 751 |
break; |
| 752 |
} |
| 753 |
} |
| 754 |
if(m_class.size()) |
| 755 |
statBuffer.push_back(binValue); |
| 756 |
else |
| 757 |
statBuffer.push_back(theValue); |
| 758 |
} |
| 759 |
switch(getFilterType(method)){ |
| 760 |
case(filter::first): |
| 761 |
output[i]=statBuffer[0]; |
| 762 |
break; |
| 763 |
case(filter::last): |
| 764 |
output[i]=statBuffer.back(); |
| 765 |
break; |
| 766 |
case(filter::nvalid): |
| 767 |
output[i]=stat.nvalid(statBuffer); |
| 768 |
break; |
| 769 |
case(filter::median): |
| 770 |
output[i]=stat.median(statBuffer); |
| 771 |
break; |
| 772 |
case(filter::min): |
| 773 |
case(filter::erode): |
| 774 |
output[i]=stat.mymin(statBuffer); |
| 775 |
break; |
| 776 |
case(filter::max): |
| 777 |
case(filter::dilate): |
| 778 |
output[i]=stat.mymax(statBuffer); |
| 779 |
break; |
| 780 |
case(filter::sum): |
| 781 |
output[i]=sqrt(stat.sum(statBuffer)); |
| 782 |
break; |
| 783 |
case(filter::var): |
| 784 |
output[i]=stat.var(statBuffer); |
| 785 |
break; |
| 786 |
case(filter::mean): |
| 787 |
output[i]=stat.mean(statBuffer); |
| 788 |
break; |
| 789 |
case(filter::percentile): |
| 790 |
assert(m_threshold.size()); |
| 791 |
output[i]=stat.percentile(statBuffer,statBuffer.begin(),statBuffer.end(),m_threshold[0]); |
| 792 |
break; |
| 793 |
default: |
| 794 |
std::string errorString="method not supported"; |
| 795 |
throw(errorString); |
| 796 |
break; |
| 797 |
} |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
template<class T> void Filter::smooth(T* input, int inputSize, std::vector<T>& output, short dim) |
| 802 |
{ |
| 803 |
assert(dim>0); |
| 804 |
m_taps.resize(dim); |
| 805 |
for(int itap=0;itap<dim;++itap) |
| 806 |
m_taps[itap]=1.0/dim; |
| 807 |
filter(input,output); |
| 808 |
} |
| 809 |
|
| 810 |
template<class T> void Filter::filter(T* input, int inputSize, std::vector<T>& output) |
| 811 |
{ |
| 812 |
assert(inputSize>=m_taps.size()); |
| 813 |
output.resize(inputSize); |
| 814 |
int i=0; |
| 815 |
|
| 816 |
|
| 817 |
for(i=0;i<m_taps.size()/2;++i){ |
| 818 |
|
| 819 |
output[i]=m_taps[m_taps.size()/2]*input[i]; |
| 820 |
|
| 821 |
for(int t=1;t<m_taps.size()/2;++t){ |
| 822 |
output[i]+=m_taps[m_taps.size()/2+t]*input[i+t]; |
| 823 |
if(i>=t) |
| 824 |
output[i]+=m_taps[m_taps.size()/2-t]*input[i-t]; |
| 825 |
else{ |
| 826 |
switch(getPadding(m_padding)){ |
| 827 |
case(replicate): |
| 828 |
output[i]+=m_taps[m_taps.size()/2-t]*input[0]; |
| 829 |
break; |
| 830 |
case(circular): |
| 831 |
output[i]+=m_taps[m_taps.size()/2-t]*input[input.size()+i-t]; |
| 832 |
break; |
| 833 |
case(zero): |
| 834 |
output[i]+=m_taps[m_taps.size()/2-t]*0; |
| 835 |
break; |
| 836 |
case(symmetric): |
| 837 |
default: |
| 838 |
output[i]+=m_taps[m_taps.size()/2-t]*input[t-i]; |
| 839 |
break; |
| 840 |
} |
| 841 |
} |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
for(i=m_taps.size()/2;i<input.size()-m_taps.size()/2;++i){ |
| 846 |
|
| 847 |
T leaveOut=(*(m_taps.begin()))*input[i-m_taps.size()/2]; |
| 848 |
T include=(m_taps.back())*input[i+m_taps.size()/2]; |
| 849 |
output[i]=0; |
| 850 |
for(int t=0;t<m_taps.size();++t) |
| 851 |
output[i]+=input[i-m_taps.size()/2+t]*m_taps[t]; |
| 852 |
} |
| 853 |
|
| 854 |
for(i=input.size()-m_taps.size()/2;i<input.size();++i){ |
| 855 |
|
| 856 |
output[i]=m_taps[m_taps.size()/2]*input[i]; |
| 857 |
|
| 858 |
for(int t=1;t<m_taps.size()/2;++t){ |
| 859 |
output[i]+=m_taps[m_taps.size()/2-t]*input[i-t]; |
| 860 |
if(i+t<input.size()) |
| 861 |
output[i]+=m_taps[m_taps.size()/2+t]*input[i+t]; |
| 862 |
else{ |
| 863 |
switch(getPadding(m_padding)){ |
| 864 |
case(replicate): |
| 865 |
output[i]+=m_taps[m_taps.size()/2+t]*input.back(); |
| 866 |
break; |
| 867 |
case(circular): |
| 868 |
output[i]+=m_taps[m_taps.size()/2+t]*input[t-1]; |
| 869 |
break; |
| 870 |
case(zero): |
| 871 |
output[i]+=m_taps[m_taps.size()/2+t]*0; |
| 872 |
break; |
| 873 |
case(symmetric): |
| 874 |
default: |
| 875 |
output[i]+=m_taps[m_taps.size()/2+t]*input[i-t]; |
| 876 |
break; |
| 877 |
} |
| 878 |
} |
| 879 |
} |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
} |
| 884 |
|
| 885 |
#endif |