26 #include <gsl/gsl_sort.h> 27 #include <gsl/gsl_wavelet.h> 29 #include "StatFactory.h" 30 #include "imageclasses/ImgReaderGdal.h" 31 #include "imageclasses/ImgWriterGdal.h" 36 enum FILTER_TYPE { median=0, var=1 , min=2, max=3, sum=4, mean=5, minmax=6, dilate=7, erode=8, close=9, open=10, homog=11, sobelx=12, sobely=13, sobelxy=14, sobelyx=-14, smooth=15, density=16, mode=17, mixed=18, smoothnodata=19, threshold=20, ismin=21, ismax=22, heterog=23, order=24, stdev=25, dwt=26, dwti=27, dwt_cut=28, dwt_cut_from=29, savgolay=30, percentile=31, nvalid=32};
38 enum PADDING { symmetric=0, replicate=1, circular=2, zero=3};
44 Filter(
const std::vector<double> &taps);
47 void setPadding(
const std::string& padString){
51 static const gsl_wavelet_type* getWaveletType(
const std::string waveletType){
52 if(waveletType==
"daubechies")
return(gsl_wavelet_daubechies);
53 if(waveletType==
"daubechies_centered")
return(gsl_wavelet_daubechies_centered);
54 if(waveletType==
"haar")
return(gsl_wavelet_haar);
55 if(waveletType==
"haar_centered")
return(gsl_wavelet_haar_centered);
56 if(waveletType==
"bspline")
return(gsl_wavelet_bspline);
57 if(waveletType==
"bspline_centered")
return(gsl_wavelet_bspline_centered);
59 static FILTER_TYPE getFilterType(
const std::string filterType){
60 std::map<std::string, FILTER_TYPE> m_filterMap;
61 initFilterMap(m_filterMap);
62 return m_filterMap[filterType];
65 void setTaps(
const std::vector<double> &taps,
bool normalize=
true);
66 void pushClass(
short theClass=1){m_class.push_back(theClass);};
67 void pushMask(
short theMask=0){m_mask.push_back(theMask);};
68 unsigned int pushNoDataValue(
double noDataValue);
69 unsigned int setNoDataValues(std::vector<double> vnodata);
70 void pushThreshold(
double theThreshold){m_threshold.push_back(theThreshold);};
71 void setThresholds(
const std::vector<double>& theThresholds){m_threshold=theThresholds;};
72 template<
class T>
void filter(
const std::vector<T>& input, std::vector<T>& output);
73 template<
class T>
void filter(
const std::vector<T>& input, std::vector<T>& output,
const std::string& method,
int dim);
74 template<
class T>
void smooth(
const std::vector<T>& input, std::vector<T>& output,
short dim);
75 template<
class T>
void smoothNoData(
const std::vector<T>& input,
const std::string& interpolationType, std::vector<T>& output);
76 template<
class T>
void filter(T* input,
int inputSize, std::vector<T>& output);
77 template<
class T>
void smooth(T* input,
int inputSize, std::vector<T>& output,
short dim);
84 void getSavGolayCoefficients(std::vector<double> &c,
int np,
int nl,
int nr,
int ld,
int m);
85 void ludcmp(std::vector<double> &a, std::vector<int> &indx,
double &d);
86 void lubksb(std::vector<double> &a, std::vector<int> &indx, std::vector<double> &b);
90 double getCentreWavelength(
const std::vector<double> &wavelengthIn,
const Vector2d<double>& srf,
const std::string& interpolationType,
double delta=1.0,
bool verbose=
false);
91 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);
92 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);
94 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);
95 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);
99 void dwtForward(std::vector<double>& data,
const std::string& wavelet_type,
int family);
100 void dwtInverse(std::vector<double>& data,
const std::string& wavelet_type,
int family);
101 void dwtCut(std::vector<double>& data,
const std::string& wavelet_type,
int family,
double cut);
106 static void initFilterMap(std::map<std::string, FILTER_TYPE>& m_filterMap){
108 m_filterMap[
"dwt"]=filter::dwt;
109 m_filterMap[
"dwti"]=filter::dwti;
110 m_filterMap[
"dwt_cut"]=filter::dwt_cut;
111 m_filterMap[
"dwt_cut_from"]=filter::dwt_cut_from;
112 m_filterMap[
"stdev"]=filter::stdev;
113 m_filterMap[
"var"]=filter::var;
114 m_filterMap[
"min"]=filter::min;
115 m_filterMap[
"max"]=filter::max;
116 m_filterMap[
"sum"]=filter::sum;
117 m_filterMap[
"mean"]=filter::mean;
118 m_filterMap[
"minmax"]=filter::minmax;
119 m_filterMap[
"dilate"]=filter::dilate;
120 m_filterMap[
"erode"]=filter::erode;
121 m_filterMap[
"close"]=filter::close;
122 m_filterMap[
"open"]=filter::open;
123 m_filterMap[
"homog"]=filter::homog;
124 m_filterMap[
"sobelx"]=filter::sobelx;
125 m_filterMap[
"sobely"]=filter::sobely;
126 m_filterMap[
"sobelxy"]=filter::sobelxy;
127 m_filterMap[
"sobelyx"]=filter::sobelyx;
128 m_filterMap[
"smooth"]=filter::smooth;
129 m_filterMap[
"density"]=filter::density;
130 m_filterMap[
"mode"]=filter::mode;
131 m_filterMap[
"mixed"]=filter::mixed;
132 m_filterMap[
"smoothnodata"]=filter::smoothnodata;
133 m_filterMap[
"threshold"]=filter::threshold;
134 m_filterMap[
"ismin"]=filter::ismin;
135 m_filterMap[
"ismax"]=filter::ismax;
136 m_filterMap[
"heterog"]=filter::heterog;
137 m_filterMap[
"order"]=filter::order;
138 m_filterMap[
"nvalid"]=filter::nvalid;
139 m_filterMap[
"median"]=filter::median;
140 m_filterMap[
"savgolay"]=filter::savgolay;
141 m_filterMap[
"percentile"]=filter::percentile;
145 static PADDING getPadding(
const std::string& padString){
146 std::map<std::string, PADDING> padMap;
147 padMap[
"zero"]=filter::zero;
148 padMap[
"symmetric"]=filter::symmetric;
149 padMap[
"replicate"]=filter::replicate;
150 padMap[
"circular"]=filter::circular;
151 return(padMap[padString]);
154 std::vector<double> m_taps;
155 std::vector<short> m_class;
156 std::vector<short> m_mask;
157 std::string m_padding;
158 std::vector<double> m_noDataValues;
159 std::vector<double> m_threshold;
165 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)
167 assert(srf.size()==2);
168 int nband=srf[0].size();
169 double start=floor(wavelengthIn[0]);
170 double end=ceil(wavelengthIn.back());
172 std::cout <<
"wavelengths in [" << start <<
"," << end <<
"]" << std::endl << std::flush;
176 gsl_interp_accel *acc;
179 stat.getSpline(interpolationType,nband,spline);
180 stat.initSpline(spline,&(srf[0][0]),&(srf[1][0]),nband);
182 std::cout <<
"calculating norm of srf" << std::endl << std::flush;
184 norm=gsl_spline_eval_integ(spline,srf[0].front(),srf[0].back(),acc);
186 std::cout <<
"norm of srf: " << norm << std::endl << std::flush;
187 gsl_spline_free(spline);
188 gsl_interp_accel_free(acc);
191 std::vector<double> wavelength_fine;
192 for(
double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta)
193 wavelength_fine.push_back(win);
196 std::cout <<
"interpolate wavelengths to " << wavelength_fine.size() <<
" entries " << std::endl;
197 std::vector<double> srf_fine;
199 stat.interpolateUp(srf[0],srf[1],wavelength_fine,interpolationType,srf_fine,verbose);
200 assert(srf_fine.size()==wavelength_fine.size());
202 gsl_interp_accel *accOut;
203 stat.allocAcc(accOut);
204 gsl_spline *splineOut;
205 stat.getSpline(interpolationType,wavelength_fine.size(),splineOut);
208 assert(wavelengthIn.size()==input.size());
209 std::vector<double> input_fine;
210 std::vector<double> product(wavelength_fine.size());
211 std::vector<double> wavelengthOut(wavelength_fine.size());
212 stat.interpolateUp(wavelengthIn,input,wavelength_fine,interpolationType,input_fine,verbose);
215 std::cout <<
"input_fine.size(): " << input_fine.size() << std::endl;
216 for(
int iband=0;iband<input_fine.size();++iband){
217 product[iband]=input_fine[iband]*srf_fine[iband];
218 wavelengthOut[iband]=wavelength_fine[iband]*srf_fine[iband];
221 assert(input_fine.size()==srf_fine.size());
222 assert(input_fine.size()==wavelength_fine.size());
223 stat.initSpline(splineOut,&(wavelength_fine[0]),&(product[0]),wavelength_fine.size());
225 output=gsl_spline_eval_integ(splineOut,start,end,accOut)/norm;
227 output=gsl_spline_eval_integ(splineOut,start,end,accOut);
229 stat.initSpline(splineOut,&(wavelength_fine[0]),&(wavelengthOut[0]),wavelength_fine.size());
230 double centreWavelength=gsl_spline_eval_integ(splineOut,start,end,accOut)/norm;
232 gsl_spline_free(splineOut);
233 gsl_interp_accel_free(accOut);
235 return(centreWavelength);
240 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)
242 assert(srf.size()==2);
243 int nband=srf[0].size();
244 unsigned int nsample=(transposeInput)? input.size():input[0].size();
245 output.resize((nsample+down-1)/down);
246 double start=floor(wavelengthIn[0]);
247 double end=ceil(wavelengthIn.back());
249 std::cout <<
"wavelengths in [" << start <<
"," << end <<
"]" << std::endl << std::flush;
253 gsl_interp_accel *acc;
256 stat.getSpline(interpolationType,nband,spline);
257 stat.initSpline(spline,&(srf[0][0]),&(srf[1][0]),nband);
259 std::cout <<
"calculating norm of srf" << std::endl << std::flush;
261 norm=gsl_spline_eval_integ(spline,srf[0].front(),srf[0].back(),acc);
263 std::cout <<
"norm of srf: " << norm << std::endl << std::flush;
264 gsl_spline_free(spline);
265 gsl_interp_accel_free(acc);
268 std::vector<double> wavelength_fine;
269 for(
double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta)
270 wavelength_fine.push_back(win);
273 std::cout <<
"interpolate wavelengths to " << wavelength_fine.size() <<
" entries " << std::endl;
274 std::vector<double> srf_fine;
276 stat.interpolateUp(srf[0],srf[1],wavelength_fine,interpolationType,srf_fine,verbose);
277 assert(srf_fine.size()==wavelength_fine.size());
279 gsl_interp_accel *accOut;
280 stat.allocAcc(accOut);
281 gsl_spline *splineOut;
282 stat.getSpline(interpolationType,wavelength_fine.size(),splineOut);
285 std::vector<double> wavelengthOut;
286 double centreWavelength=0;
287 for(
int isample=0;isample<nsample;++isample){
288 if((isample+1+down/2)%down)
290 std::vector<T> inputValues;
292 inputValues=input[isample];
294 input.selectCol(isample,inputValues);
295 assert(wavelengthIn.size()==inputValues.size());
296 std::vector<double> input_fine;
297 std::vector<double> product(wavelength_fine.size());
298 stat.interpolateUp(wavelengthIn,inputValues,wavelength_fine,interpolationType,input_fine,verbose);
300 for(
int iband=0;iband<input_fine.size();++iband){
301 product[iband]=input_fine[iband]*srf_fine[iband];
302 if(wavelengthOut.size()<input_fine.size())
303 wavelengthOut.push_back(wavelength_fine[iband]*srf_fine[iband]);
306 assert(input_fine.size()==srf_fine.size());
307 assert(input_fine.size()==wavelength_fine.size());
308 stat.initSpline(splineOut,&(wavelength_fine[0]),&(product[0]),wavelength_fine.size());
310 output[isample/down]=gsl_spline_eval_integ(splineOut,start,end,accOut)/norm;
312 output[isample/down]=gsl_spline_eval_integ(splineOut,start,end,accOut);
314 stat.initSpline(splineOut,&(wavelength_fine[0]),&(wavelengthOut[0]),wavelength_fine.size());
315 if(centreWavelength>0);
317 centreWavelength=gsl_spline_eval_integ(splineOut,start,end,accOut)/norm;
319 gsl_spline_free(splineOut);
320 gsl_interp_accel_free(accOut);
322 return(centreWavelength);
325 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){
327 std::vector<double> stddev(fwhm.size());
328 for(
int index=0;index<fwhm.size();++index)
329 stddev[index]=fwhm[index]/2.0/sqrt(2*log(2.0));
330 assert(wavelengthOut.size()==fwhm.size());
331 assert(wavelengthIn.size()==input.size());
332 assert(wavelengthIn[0]<=wavelengthOut[0]);
333 assert(wavelengthIn.back()>=wavelengthOut.back());
335 std::vector<double> input_fine;
336 std::vector<double> wavelength_fine;
337 for(
double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta)
338 wavelength_fine.push_back(win);
340 for(
int index=0;index<wavelength_fine.size();++index)
341 std::cout <<
" " << wavelength_fine[index];
342 std::cout << std::endl;
343 std::cout <<
"interpolate input wavelength to " << delta <<
" nm resolution (size=" << wavelength_fine.size() <<
")" << std::endl;
345 stat.interpolateUp(wavelengthIn,input,wavelength_fine,interpolationType,input_fine,verbose);
346 int nbandIn=wavelength_fine.size();
348 int nbandOut=wavelengthOut.size();
349 output.resize(nbandOut);
351 for(
int indexOut=0;indexOut<nbandOut;++indexOut){
353 for(
int indexIn=0;indexIn<nbandIn;++indexIn){
355 tf[indexIn][indexOut]=
356 exp((wavelengthOut[indexOut]-wavelength_fine[indexIn])
357 *(wavelength_fine[indexIn]-wavelengthOut[indexOut])
358 /2.0/stddev[indexOut]
360 tf[indexIn][indexOut]/=sqrt(2.0*M_PI);
361 tf[indexIn][indexOut]/=stddev[indexOut];
362 norm+=tf[indexIn][indexOut];
365 for(
int indexIn=0;indexIn<nbandIn;++indexIn)
366 output[indexOut]+=input_fine[indexIn]*tf[indexIn][indexOut]/norm;
372 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){
374 std::vector<double> stddev(fwhm.size());
375 for(
int index=0;index<fwhm.size();++index)
376 stddev[index]=fwhm[index]/2.0/sqrt(2*log(2.0));
378 std::vector<double> wavelength_fine;
379 for(
double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta)
380 wavelength_fine.push_back(win);
381 assert(wavelengthOut.size()==fwhm.size());
382 assert(wavelengthIn[0]<=wavelengthOut[0]);
383 assert(wavelengthIn.back()>=wavelengthOut.back());
385 for(
int index=0;index<wavelength_fine.size();++index)
386 std::cout <<
" " << wavelength_fine[index];
387 std::cout << std::endl;
388 std::cout <<
"interpolate input wavelength to " << delta <<
" nm resolution (size=" << wavelength_fine.size() <<
")" << std::endl;
390 int nbandIn=wavelength_fine.size();
391 int nbandOut=wavelengthOut.size();
392 output.resize(nbandOut,(input[0].size()+down-1)/down);
395 std::vector<double> norm(nbandOut);
396 for(
int indexOut=0;indexOut<nbandOut;++indexOut){
398 for(
int indexIn=0;indexIn<nbandIn;++indexIn){
399 tf[indexIn][indexOut]=
400 exp((wavelengthOut[indexOut]-wavelength_fine[indexIn])
401 *(wavelength_fine[indexIn]-wavelengthOut[indexOut])
402 /2.0/stddev[indexOut]
404 tf[indexIn][indexOut]/=sqrt(2.0*M_PI);
405 tf[indexIn][indexOut]/=stddev[indexOut];
406 norm[indexOut]+=tf[indexIn][indexOut];
410 for(
int isample=0;isample<input[0].size();++isample){
411 if((isample+1+down/2)%down)
413 std::vector<T> inputValues;
414 input.selectCol(isample,inputValues);
415 assert(wavelengthIn.size()==inputValues.size());
416 for(
int indexOut=0;indexOut<nbandOut;++indexOut){
417 std::vector<double> input_fine;
418 stat.interpolateUp(wavelengthIn,inputValues,wavelength_fine,interpolationType,input_fine,verbose);
419 output[indexOut][(isample+down-1)/down]=0;
420 for(
int indexIn=0;indexIn<nbandIn;++indexIn){
421 output[indexOut][(isample+down-1)/down]+=input_fine[indexIn]*tf[indexIn][indexOut]/norm[indexOut];
427 template<
class T>
void Filter::smooth(
const std::vector<T>& input, std::vector<T>& output,
short dim)
431 for(
int itap=0;itap<dim;++itap)
432 m_taps[itap]=1.0/dim;
436 template<
class T>
void Filter::smoothNoData(
const std::vector<T>& input,
const std::string& interpolationType, std::vector<T>& output)
439 stat.setNoDataValues(m_noDataValues);
440 std::vector<double> abscis(input.size());
441 for(
int i=0;i<abscis.size();++i)
443 stat.interpolateNoData(abscis,input,interpolationType,output);
446 template<
class T>
void Filter::filter(
const std::vector<T>& input, std::vector<T>& output)
448 assert(input.size()>=m_taps.size());
449 output.resize(input.size());
452 for(i=0;i<m_taps.size()/2;++i){
454 output[i]=m_taps[m_taps.size()/2]*input[i];
455 for(
int t=1;t<=m_taps.size()/2;++t){
456 output[i]+=m_taps[m_taps.size()/2+t]*input[i+t];
458 output[i]+=m_taps[m_taps.size()/2-t]*input[i-t];
460 switch(getPadding(m_padding)){
462 output[i]+=m_taps[m_taps.size()/2-t]*input[0];
465 output[i]+=m_taps[m_taps.size()/2-t]*input[input.size()+i-t];
468 output[i]+=m_taps[m_taps.size()/2-t]*0;
472 output[i]+=m_taps[m_taps.size()/2-t]*input[t-i];
479 for(i=m_taps.size()/2;i<input.size()-m_taps.size()/2;++i){
481 T leaveOut=(*(m_taps.begin()))*input[i-m_taps.size()/2];
482 T include=(m_taps.back())*input[i+m_taps.size()/2];
484 for(
int t=0;t<m_taps.size();++t)
485 output[i]+=input[i-m_taps.size()/2+t]*m_taps[t];
488 for(i=input.size()-m_taps.size()/2;i<input.size();++i){
490 output[i]=m_taps[m_taps.size()/2]*input[i];
492 for(
int t=1;t<=m_taps.size()/2;++t){
493 output[i]+=m_taps[m_taps.size()/2-t]*input[i-t];
495 output[i]+=m_taps[m_taps.size()/2+t]*input[i+t];
497 switch(getPadding(m_padding)){
499 output[i]+=m_taps[m_taps.size()/2+t]*input.back();
502 output[i]+=m_taps[m_taps.size()/2+t]*input[t-1];
505 output[i]+=m_taps[m_taps.size()/2+t]*0;
509 output[i]+=m_taps[m_taps.size()/2+t]*input[i-t];
519 template<
class T>
void Filter::filter(
const std::vector<T>& input, std::vector<T>& output,
const std::string& method,
int dim)
523 output.resize(input.size());
526 stat.setNoDataValues(m_noDataValues);
527 std::vector<T> statBuffer;
530 for(i=0;i<dim/2;++i){
532 for(
int iclass=0;iclass<m_class.size();++iclass){
533 if(input[i]==m_class[iclass]){
539 statBuffer.push_back(binValue);
541 statBuffer.push_back(input[i]);
543 for(
int t=1;t<=dim/2;++t){
544 T theValue=input[i+t];
545 for(
int iclass=0;iclass<m_class.size();++iclass){
546 if(theValue==m_class[iclass]){
552 statBuffer.push_back(binValue);
554 statBuffer.push_back(theValue);
560 switch(getPadding(m_padding)){
565 theValue=input[input.size()+i-t];
576 for(
int iclass=0;iclass<m_class.size();++iclass){
577 if(theValue==m_class[iclass]){
583 statBuffer.push_back(binValue);
585 statBuffer.push_back(theValue);
588 switch(getFilterType(method)){
589 case(filter::nvalid):
590 output[i]=stat.nvalid(statBuffer);
592 case(filter::median):
593 output[i]=stat.median(statBuffer);
597 output[i]=stat.mymin(statBuffer);
600 case(filter::dilate):
601 output[i]=stat.mymax(statBuffer);
604 output[i]=sqrt(stat.sum(statBuffer));
607 output[i]=stat.var(statBuffer);
610 output[i]=sqrt(stat.var(statBuffer));
613 output[i]=stat.mean(statBuffer);
615 case(filter::percentile):
616 assert(m_threshold.size());
617 output[i]=stat.percentile(statBuffer,statBuffer.begin(),statBuffer.end(),m_threshold[0]);
620 std::ostringstream ess;
621 ess <<
"method " << method <<
" (" << getFilterType(method) <<
") not supported";
629 for(i=dim/2;i<input.size()-dim/2;++i){
631 for(
int t=0;t<dim;++t){
632 for(
int iclass=0;iclass<m_class.size();++iclass){
633 if(input[i-dim/2+t]==m_class[iclass]){
639 statBuffer.push_back(binValue);
641 statBuffer.push_back(input[i-dim/2+t]);
643 switch(getFilterType(method)){
644 case(filter::nvalid):
645 output[i]=stat.nvalid(statBuffer);
647 case(filter::median):
648 output[i]=stat.median(statBuffer);
652 output[i]=stat.mymin(statBuffer);
655 case(filter::dilate):
656 output[i]=stat.mymax(statBuffer);
659 output[i]=sqrt(stat.sum(statBuffer));
662 output[i]=stat.var(statBuffer);
665 output[i]=stat.mean(statBuffer);
667 case(filter::percentile):
668 assert(m_threshold.size());
669 output[i]=stat.percentile(statBuffer,statBuffer.begin(),statBuffer.end(),m_threshold[0]);
672 std::string errorString=
"method not supported";
679 for(i=input.size()-dim/2;i<input.size();++i){
681 for(
int iclass=0;iclass<m_class.size();++iclass){
682 if(input[i]==m_class[iclass]){
688 statBuffer.push_back(binValue);
690 statBuffer.push_back(input[i]);
692 for(
int t=1;t<=dim/2;++t){
693 T theValue=input[i-t];
694 for(
int iclass=0;iclass<m_class.size();++iclass){
695 if(theValue==m_class[iclass]){
701 statBuffer.push_back(binValue);
703 statBuffer.push_back(theValue);
707 switch(getPadding(m_padding)){
709 theValue=input.back();
723 for(
int iclass=0;iclass<m_class.size();++iclass){
724 if(theValue==m_class[iclass]){
730 statBuffer.push_back(binValue);
732 statBuffer.push_back(theValue);
734 switch(getFilterType(method)){
735 case(filter::nvalid):
736 output[i]=stat.nvalid(statBuffer);
738 case(filter::median):
739 output[i]=stat.median(statBuffer);
743 output[i]=stat.mymin(statBuffer);
746 case(filter::dilate):
747 output[i]=stat.mymax(statBuffer);
750 output[i]=sqrt(stat.sum(statBuffer));
753 output[i]=stat.var(statBuffer);
756 output[i]=stat.mean(statBuffer);
758 case(filter::percentile):
759 assert(m_threshold.size());
760 output[i]=stat.percentile(statBuffer,statBuffer.begin(),statBuffer.end(),m_threshold[0]);
763 std::string errorString=
"method not supported";
770 template<
class T>
void Filter::smooth(T* input,
int inputSize, std::vector<T>& output,
short dim)
774 for(
int itap=0;itap<dim;++itap)
775 m_taps[itap]=1.0/dim;
779 template<
class T>
void Filter::filter(T* input,
int inputSize, std::vector<T>& output)
781 assert(inputSize>=m_taps.size());
782 output.resize(inputSize);
786 for(i=0;i<m_taps.size()/2;++i){
788 output[i]=m_taps[m_taps.size()/2]*input[i];
790 for(
int t=1;t<=m_taps.size()/2;++t){
791 output[i]+=m_taps[m_taps.size()/2+t]*input[i+t];
793 output[i]+=m_taps[m_taps.size()/2-t]*input[i-t];
795 switch(getPadding(m_padding)){
797 output[i]+=m_taps[m_taps.size()/2-t]*input[0];
800 output[i]+=m_taps[m_taps.size()/2-t]*input[input.size()+i-t];
803 output[i]+=m_taps[m_taps.size()/2-t]*0;
807 output[i]+=m_taps[m_taps.size()/2-t]*input[t-i];
814 for(i=m_taps.size()/2;i<input.size()-m_taps.size()/2;++i){
816 T leaveOut=(*(m_taps.begin()))*input[i-m_taps.size()/2];
817 T include=(m_taps.back())*input[i+m_taps.size()/2];
819 for(
int t=0;t<m_taps.size();++t)
820 output[i]+=input[i-m_taps.size()/2+t]*m_taps[t];
823 for(i=input.size()-m_taps.size()/2;i<input.size();++i){
825 output[i]=m_taps[m_taps.size()/2]*input[i];
827 for(
int t=1;t<=m_taps.size()/2;++t){
828 output[i]+=m_taps[m_taps.size()/2-t]*input[i-t];
830 output[i]+=m_taps[m_taps.size()/2+t]*input[i+t];
832 switch(getPadding(m_padding)){
834 output[i]+=m_taps[m_taps.size()/2+t]*input.back();
837 output[i]+=m_taps[m_taps.size()/2+t]*input[t-1];
840 output[i]+=m_taps[m_taps.size()/2+t]*0;
844 output[i]+=m_taps[m_taps.size()/2+t]*input[i-t];