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};
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 int pushNoDataValue(
double noDataValue=0);
69 void pushThreshold(
double theThreshold){m_threshold.push_back(theThreshold);};
70 void setThresholds(
const std::vector<double>& theThresholds){m_threshold=theThresholds;};
71 template<
class T>
void filter(
const std::vector<T>& input, std::vector<T>& output);
72 template<
class T>
void filter(
const std::vector<T>& input, std::vector<T>& output,
const std::string& method,
int dim);
73 template<
class T>
void smooth(
const std::vector<T>& input, std::vector<T>& output,
short dim);
74 template<
class T>
void smoothNoData(
const std::vector<T>& input,
const std::string& interpolationType, std::vector<T>& output);
75 template<
class T>
void filter(T* input,
int inputSize, std::vector<T>& output);
76 template<
class T>
void smooth(T* input,
int inputSize, std::vector<T>& output,
short dim);
82 void getSavGolayCoefficients(std::vector<double> &c,
int np,
int nl,
int nr,
int ld,
int m);
83 void ludcmp(std::vector<double> &a, std::vector<int> &indx,
double &d);
84 void lubksb(std::vector<double> &a, std::vector<int> &indx, std::vector<double> &b);
88 double getCentreWavelength(
const std::vector<double> &wavelengthIn,
const Vector2d<double>& srf,
const std::string& interpolationType,
double delta=1.0,
bool verbose=
false);
89 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);
90 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);
92 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);
93 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);
97 void dwtForward(std::vector<double>& data,
const std::string& wavelet_type,
int family);
98 void dwtInverse(std::vector<double>& data,
const std::string& wavelet_type,
int family);
99 void dwtCut(std::vector<double>& data,
const std::string& wavelet_type,
int family,
double cut);
104 static void initFilterMap(std::map<std::string, FILTER_TYPE>& m_filterMap){
106 m_filterMap[
"dwt"]=filter::dwt;
107 m_filterMap[
"dwti"]=filter::dwti;
108 m_filterMap[
"dwt_cut"]=filter::dwt_cut;
109 m_filterMap[
"dwt_cut_from"]=filter::dwt_cut_from;
110 m_filterMap[
"stdev"]=filter::stdev;
111 m_filterMap[
"var"]=filter::var;
112 m_filterMap[
"min"]=filter::min;
113 m_filterMap[
"max"]=filter::max;
114 m_filterMap[
"sum"]=filter::sum;
115 m_filterMap[
"mean"]=filter::mean;
116 m_filterMap[
"minmax"]=filter::minmax;
117 m_filterMap[
"dilate"]=filter::dilate;
118 m_filterMap[
"erode"]=filter::erode;
119 m_filterMap[
"close"]=filter::close;
120 m_filterMap[
"open"]=filter::open;
121 m_filterMap[
"homog"]=filter::homog;
122 m_filterMap[
"sobelx"]=filter::sobelx;
123 m_filterMap[
"sobely"]=filter::sobely;
124 m_filterMap[
"sobelxy"]=filter::sobelxy;
125 m_filterMap[
"sobelyx"]=filter::sobelyx;
126 m_filterMap[
"smooth"]=filter::smooth;
127 m_filterMap[
"density"]=filter::density;
128 m_filterMap[
"mode"]=filter::mode;
129 m_filterMap[
"mixed"]=filter::mixed;
130 m_filterMap[
"smoothnodata"]=filter::smoothnodata;
131 m_filterMap[
"threshold"]=filter::threshold;
132 m_filterMap[
"ismin"]=filter::ismin;
133 m_filterMap[
"ismax"]=filter::ismax;
134 m_filterMap[
"heterog"]=filter::heterog;
135 m_filterMap[
"order"]=filter::order;
136 m_filterMap[
"median"]=filter::median;
137 m_filterMap[
"savgolay"]=filter::savgolay;
138 m_filterMap[
"percentile"]=filter::percentile;
142 static PADDING getPadding(
const std::string& padString){
143 std::map<std::string, PADDING> padMap;
144 padMap[
"zero"]=filter::zero;
145 padMap[
"symmetric"]=filter::symmetric;
146 padMap[
"replicate"]=filter::replicate;
147 padMap[
"circular"]=filter::circular;
148 return(padMap[padString]);
151 std::vector<double> m_taps;
152 std::vector<short> m_class;
153 std::vector<short> m_mask;
154 std::string m_padding;
155 std::vector<double> m_noDataValues;
156 std::vector<double> m_threshold;
162 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)
164 assert(srf.size()==2);
165 int nband=srf[0].size();
166 double start=floor(wavelengthIn[0]);
167 double end=ceil(wavelengthIn.back());
169 std::cout <<
"wavelengths in [" << start <<
"," << end <<
"]" << std::endl << std::flush;
173 gsl_interp_accel *acc;
176 stat.getSpline(interpolationType,nband,spline);
177 stat.initSpline(spline,&(srf[0][0]),&(srf[1][0]),nband);
179 std::cout <<
"calculating norm of srf" << std::endl << std::flush;
181 norm=gsl_spline_eval_integ(spline,srf[0].front(),srf[0].back(),acc);
183 std::cout <<
"norm of srf: " << norm << std::endl << std::flush;
184 gsl_spline_free(spline);
185 gsl_interp_accel_free(acc);
188 std::vector<double> wavelength_fine;
189 for(
double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta)
190 wavelength_fine.push_back(win);
193 std::cout <<
"interpolate wavelengths to " << wavelength_fine.size() <<
" entries " << std::endl;
194 std::vector<double> srf_fine;
196 stat.interpolateUp(srf[0],srf[1],wavelength_fine,interpolationType,srf_fine,verbose);
197 assert(srf_fine.size()==wavelength_fine.size());
199 gsl_interp_accel *accOut;
200 stat.allocAcc(accOut);
201 gsl_spline *splineOut;
202 stat.getSpline(interpolationType,wavelength_fine.size(),splineOut);
205 assert(wavelengthIn.size()==input.size());
206 std::vector<double> input_fine;
207 std::vector<double> product(wavelength_fine.size());
208 std::vector<double> wavelengthOut(wavelength_fine.size());
209 stat.interpolateUp(wavelengthIn,input,wavelength_fine,interpolationType,input_fine,verbose);
212 std::cout <<
"input_fine.size(): " << input_fine.size() << std::endl;
213 for(
int iband=0;iband<input_fine.size();++iband){
214 product[iband]=input_fine[iband]*srf_fine[iband];
215 wavelengthOut[iband]=wavelength_fine[iband]*srf_fine[iband];
218 assert(input_fine.size()==srf_fine.size());
219 assert(input_fine.size()==wavelength_fine.size());
220 stat.initSpline(splineOut,&(wavelength_fine[0]),&(product[0]),wavelength_fine.size());
222 output=gsl_spline_eval_integ(splineOut,start,end,accOut)/norm;
224 output=gsl_spline_eval_integ(splineOut,start,end,accOut);
226 stat.initSpline(splineOut,&(wavelength_fine[0]),&(wavelengthOut[0]),wavelength_fine.size());
227 double centreWavelength=gsl_spline_eval_integ(splineOut,start,end,accOut)/norm;
229 gsl_spline_free(splineOut);
230 gsl_interp_accel_free(accOut);
232 return(centreWavelength);
237 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)
239 assert(srf.size()==2);
240 int nband=srf[0].size();
241 unsigned int nsample=(transposeInput)? input.size():input[0].size();
242 output.resize((nsample+down-1)/down);
243 double start=floor(wavelengthIn[0]);
244 double end=ceil(wavelengthIn.back());
246 std::cout <<
"wavelengths in [" << start <<
"," << end <<
"]" << std::endl << std::flush;
250 gsl_interp_accel *acc;
253 stat.getSpline(interpolationType,nband,spline);
254 stat.initSpline(spline,&(srf[0][0]),&(srf[1][0]),nband);
256 std::cout <<
"calculating norm of srf" << std::endl << std::flush;
258 norm=gsl_spline_eval_integ(spline,srf[0].front(),srf[0].back(),acc);
260 std::cout <<
"norm of srf: " << norm << std::endl << std::flush;
261 gsl_spline_free(spline);
262 gsl_interp_accel_free(acc);
265 std::vector<double> wavelength_fine;
266 for(
double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta)
267 wavelength_fine.push_back(win);
270 std::cout <<
"interpolate wavelengths to " << wavelength_fine.size() <<
" entries " << std::endl;
271 std::vector<double> srf_fine;
273 stat.interpolateUp(srf[0],srf[1],wavelength_fine,interpolationType,srf_fine,verbose);
274 assert(srf_fine.size()==wavelength_fine.size());
276 gsl_interp_accel *accOut;
277 stat.allocAcc(accOut);
278 gsl_spline *splineOut;
279 stat.getSpline(interpolationType,wavelength_fine.size(),splineOut);
282 std::vector<double> wavelengthOut;
283 double centreWavelength=0;
284 for(
int isample=0;isample<nsample;++isample){
285 if((isample+1+down/2)%down)
287 std::vector<T> inputValues;
289 inputValues=input[isample];
291 input.selectCol(isample,inputValues);
292 assert(wavelengthIn.size()==inputValues.size());
293 std::vector<double> input_fine;
294 std::vector<double> product(wavelength_fine.size());
295 stat.interpolateUp(wavelengthIn,inputValues,wavelength_fine,interpolationType,input_fine,verbose);
297 for(
int iband=0;iband<input_fine.size();++iband){
298 product[iband]=input_fine[iband]*srf_fine[iband];
299 if(wavelengthOut.size()<input_fine.size())
300 wavelengthOut.push_back(wavelength_fine[iband]*srf_fine[iband]);
303 assert(input_fine.size()==srf_fine.size());
304 assert(input_fine.size()==wavelength_fine.size());
305 stat.initSpline(splineOut,&(wavelength_fine[0]),&(product[0]),wavelength_fine.size());
307 output[isample/down]=gsl_spline_eval_integ(splineOut,start,end,accOut)/norm;
309 output[isample/down]=gsl_spline_eval_integ(splineOut,start,end,accOut);
311 stat.initSpline(splineOut,&(wavelength_fine[0]),&(wavelengthOut[0]),wavelength_fine.size());
312 if(centreWavelength>0);
314 centreWavelength=gsl_spline_eval_integ(splineOut,start,end,accOut)/norm;
316 gsl_spline_free(splineOut);
317 gsl_interp_accel_free(accOut);
319 return(centreWavelength);
322 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){
324 std::vector<double> stddev(fwhm.size());
325 for(
int index=0;index<fwhm.size();++index)
326 stddev[index]=fwhm[index]/2.0/sqrt(2*log(2.0));
327 assert(wavelengthOut.size()==fwhm.size());
328 assert(wavelengthIn.size()==input.size());
329 assert(wavelengthIn[0]<=wavelengthOut[0]);
330 assert(wavelengthIn.back()>=wavelengthOut.back());
332 std::vector<double> input_fine;
333 std::vector<double> wavelength_fine;
334 for(
double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta)
335 wavelength_fine.push_back(win);
337 for(
int index=0;index<wavelength_fine.size();++index)
338 std::cout <<
" " << wavelength_fine[index];
339 std::cout << std::endl;
340 std::cout <<
"interpolate input wavelength to " << delta <<
" nm resolution (size=" << wavelength_fine.size() <<
")" << std::endl;
342 stat.interpolateUp(wavelengthIn,input,wavelength_fine,interpolationType,input_fine,verbose);
343 int nbandIn=wavelength_fine.size();
345 int nbandOut=wavelengthOut.size();
346 output.resize(nbandOut);
348 for(
int indexOut=0;indexOut<nbandOut;++indexOut){
350 for(
int indexIn=0;indexIn<nbandIn;++indexIn){
352 tf[indexIn][indexOut]=
353 exp((wavelengthOut[indexOut]-wavelength_fine[indexIn])
354 *(wavelength_fine[indexIn]-wavelengthOut[indexOut])
355 /2.0/stddev[indexOut]
357 tf[indexIn][indexOut]/=sqrt(2.0*M_PI);
358 tf[indexIn][indexOut]/=stddev[indexOut];
359 norm+=tf[indexIn][indexOut];
362 for(
int indexIn=0;indexIn<nbandIn;++indexIn)
363 output[indexOut]+=input_fine[indexIn]*tf[indexIn][indexOut]/norm;
369 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){
371 std::vector<double> stddev(fwhm.size());
372 for(
int index=0;index<fwhm.size();++index)
373 stddev[index]=fwhm[index]/2.0/sqrt(2*log(2.0));
375 std::vector<double> wavelength_fine;
376 for(
double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta)
377 wavelength_fine.push_back(win);
378 assert(wavelengthOut.size()==fwhm.size());
379 assert(wavelengthIn[0]<=wavelengthOut[0]);
380 assert(wavelengthIn.back()>=wavelengthOut.back());
382 for(
int index=0;index<wavelength_fine.size();++index)
383 std::cout <<
" " << wavelength_fine[index];
384 std::cout << std::endl;
385 std::cout <<
"interpolate input wavelength to " << delta <<
" nm resolution (size=" << wavelength_fine.size() <<
")" << std::endl;
387 int nbandIn=wavelength_fine.size();
388 int nbandOut=wavelengthOut.size();
389 output.resize(nbandOut,(input[0].size()+down-1)/down);
392 std::vector<double> norm(nbandOut);
393 for(
int indexOut=0;indexOut<nbandOut;++indexOut){
395 for(
int indexIn=0;indexIn<nbandIn;++indexIn){
396 tf[indexIn][indexOut]=
397 exp((wavelengthOut[indexOut]-wavelength_fine[indexIn])
398 *(wavelength_fine[indexIn]-wavelengthOut[indexOut])
399 /2.0/stddev[indexOut]
401 tf[indexIn][indexOut]/=sqrt(2.0*M_PI);
402 tf[indexIn][indexOut]/=stddev[indexOut];
403 norm[indexOut]+=tf[indexIn][indexOut];
407 for(
int isample=0;isample<input[0].size();++isample){
408 if((isample+1+down/2)%down)
410 std::vector<T> inputValues;
411 input.selectCol(isample,inputValues);
412 assert(wavelengthIn.size()==inputValues.size());
413 for(
int indexOut=0;indexOut<nbandOut;++indexOut){
414 std::vector<double> input_fine;
415 stat.interpolateUp(wavelengthIn,inputValues,wavelength_fine,interpolationType,input_fine,verbose);
416 output[indexOut][(isample+down-1)/down]=0;
417 for(
int indexIn=0;indexIn<nbandIn;++indexIn){
418 output[indexOut][(isample+down-1)/down]+=input_fine[indexIn]*tf[indexIn][indexOut]/norm[indexOut];
424 template<
class T>
void Filter::smooth(
const std::vector<T>& input, std::vector<T>& output,
short dim)
428 for(
int itap=0;itap<dim;++itap)
429 m_taps[itap]=1.0/dim;
430 filter(input,output);
433 template<
class T>
void Filter::smoothNoData(
const std::vector<T>& input,
const std::string& interpolationType, std::vector<T>& output)
436 stat.setNoDataValues(m_noDataValues);
437 std::vector<double> abscis(input.size());
438 for(
int i=0;i<abscis.size();++i)
440 stat.interpolateNoData(abscis,input,interpolationType,output);
443 template<
class T>
void Filter::filter(
const std::vector<T>& input, std::vector<T>& output)
445 assert(input.size()>=m_taps.size());
446 output.resize(input.size());
449 for(i=0;i<m_taps.size()/2;++i){
451 output[i]=m_taps[m_taps.size()/2]*input[i];
452 for(
int t=1;t<=m_taps.size()/2;++t){
453 output[i]+=m_taps[m_taps.size()/2+t]*input[i+t];
455 output[i]+=m_taps[m_taps.size()/2-t]*input[i-t];
457 switch(getPadding(m_padding)){
459 output[i]+=m_taps[m_taps.size()/2-t]*input[0];
462 output[i]+=m_taps[m_taps.size()/2-t]*input[input.size()+i-t];
465 output[i]+=m_taps[m_taps.size()/2-t]*0;
469 output[i]+=m_taps[m_taps.size()/2-t]*input[t-i];
476 for(i=m_taps.size()/2;i<input.size()-m_taps.size()/2;++i){
478 T leaveOut=(*(m_taps.begin()))*input[i-m_taps.size()/2];
479 T include=(m_taps.back())*input[i+m_taps.size()/2];
481 for(
int t=0;t<m_taps.size();++t)
482 output[i]+=input[i-m_taps.size()/2+t]*m_taps[t];
485 for(i=input.size()-m_taps.size()/2;i<input.size();++i){
487 output[i]=m_taps[m_taps.size()/2]*input[i];
489 for(
int t=1;t<=m_taps.size()/2;++t){
490 output[i]+=m_taps[m_taps.size()/2-t]*input[i-t];
492 output[i]+=m_taps[m_taps.size()/2+t]*input[i+t];
494 switch(getPadding(m_padding)){
496 output[i]+=m_taps[m_taps.size()/2+t]*input.back();
499 output[i]+=m_taps[m_taps.size()/2+t]*input[t-1];
502 output[i]+=m_taps[m_taps.size()/2+t]*0;
506 output[i]+=m_taps[m_taps.size()/2+t]*input[i-t];
516 template<
class T>
void Filter::filter(
const std::vector<T>& input, std::vector<T>& output,
const std::string& method,
int dim)
520 output.resize(input.size());
523 stat.setNoDataValues(m_noDataValues);
524 std::vector<T> statBuffer;
527 for(i=0;i<dim/2;++i){
529 for(
int iclass=0;iclass<m_class.size();++iclass){
530 if(input[i]==m_class[iclass]){
536 statBuffer.push_back(binValue);
538 statBuffer.push_back(input[i]);
540 for(
int t=1;t<=dim/2;++t){
541 T theValue=input[i+t];
542 for(
int iclass=0;iclass<m_class.size();++iclass){
543 if(theValue==m_class[iclass]){
549 statBuffer.push_back(binValue);
551 statBuffer.push_back(theValue);
557 switch(getPadding(m_padding)){
562 theValue=input[input.size()+i-t];
573 for(
int iclass=0;iclass<m_class.size();++iclass){
574 if(theValue==m_class[iclass]){
580 statBuffer.push_back(binValue);
582 statBuffer.push_back(theValue);
585 switch(getFilterType(method)){
586 case(filter::median):
587 output[i]=stat.median(statBuffer);
591 output[i]=stat.mymin(statBuffer);
594 case(filter::dilate):
595 output[i]=stat.mymax(statBuffer);
598 output[i]=sqrt(stat.sum(statBuffer));
601 output[i]=stat.var(statBuffer);
604 output[i]=sqrt(stat.var(statBuffer));
607 output[i]=stat.mean(statBuffer);
609 case(filter::percentile):
610 assert(m_threshold.size());
611 output[i]=stat.percentile(statBuffer,statBuffer.begin(),statBuffer.end(),m_threshold[0]);
614 std::ostringstream ess;
615 ess <<
"method " << method <<
" (" << getFilterType(method) <<
") not supported";
623 for(i=dim/2;i<input.size()-dim/2;++i){
625 for(
int t=0;t<dim;++t){
626 for(
int iclass=0;iclass<m_class.size();++iclass){
627 if(input[i-dim/2+t]==m_class[iclass]){
633 statBuffer.push_back(binValue);
635 statBuffer.push_back(input[i-dim/2+t]);
637 switch(getFilterType(method)){
638 case(filter::median):
639 output[i]=stat.median(statBuffer);
643 output[i]=stat.mymin(statBuffer);
646 case(filter::dilate):
647 output[i]=stat.mymax(statBuffer);
650 output[i]=sqrt(stat.sum(statBuffer));
653 output[i]=stat.var(statBuffer);
656 output[i]=stat.mean(statBuffer);
658 case(filter::percentile):
659 assert(m_threshold.size());
660 output[i]=stat.percentile(statBuffer,statBuffer.begin(),statBuffer.end(),m_threshold[0]);
663 std::string errorString=
"method not supported";
670 for(i=input.size()-dim/2;i<input.size();++i){
672 for(
int iclass=0;iclass<m_class.size();++iclass){
673 if(input[i]==m_class[iclass]){
679 statBuffer.push_back(binValue);
681 statBuffer.push_back(input[i]);
683 for(
int t=1;t<=dim/2;++t){
684 T theValue=input[i-t];
685 for(
int iclass=0;iclass<m_class.size();++iclass){
686 if(theValue==m_class[iclass]){
692 statBuffer.push_back(binValue);
694 statBuffer.push_back(theValue);
698 switch(getPadding(m_padding)){
700 theValue=input.back();
714 for(
int iclass=0;iclass<m_class.size();++iclass){
715 if(theValue==m_class[iclass]){
721 statBuffer.push_back(binValue);
723 statBuffer.push_back(theValue);
725 switch(getFilterType(method)){
726 case(filter::median):
727 output[i]=stat.median(statBuffer);
731 output[i]=stat.mymin(statBuffer);
734 case(filter::dilate):
735 output[i]=stat.mymax(statBuffer);
738 output[i]=sqrt(stat.sum(statBuffer));
741 output[i]=stat.var(statBuffer);
744 output[i]=stat.mean(statBuffer);
746 case(filter::percentile):
747 assert(m_threshold.size());
748 output[i]=stat.percentile(statBuffer,statBuffer.begin(),statBuffer.end(),m_threshold[0]);
751 std::string errorString=
"method not supported";
758 template<
class T>
void Filter::smooth(T* input,
int inputSize, std::vector<T>& output,
short dim)
762 for(
int itap=0;itap<dim;++itap)
763 m_taps[itap]=1.0/dim;
764 filter(input,output);
767 template<
class T>
void Filter::filter(T* input,
int inputSize, std::vector<T>& output)
769 assert(inputSize>=m_taps.size());
770 output.resize(inputSize);
774 for(i=0;i<m_taps.size()/2;++i){
776 output[i]=m_taps[m_taps.size()/2]*input[i];
778 for(
int t=1;t<=m_taps.size()/2;++t){
779 output[i]+=m_taps[m_taps.size()/2+t]*input[i+t];
781 output[i]+=m_taps[m_taps.size()/2-t]*input[i-t];
783 switch(getPadding(m_padding)){
785 output[i]+=m_taps[m_taps.size()/2-t]*input[0];
788 output[i]+=m_taps[m_taps.size()/2-t]*input[input.size()+i-t];
791 output[i]+=m_taps[m_taps.size()/2-t]*0;
795 output[i]+=m_taps[m_taps.size()/2-t]*input[t-i];
802 for(i=m_taps.size()/2;i<input.size()-m_taps.size()/2;++i){
804 T leaveOut=(*(m_taps.begin()))*input[i-m_taps.size()/2];
805 T include=(m_taps.back())*input[i+m_taps.size()/2];
807 for(
int t=0;t<m_taps.size();++t)
808 output[i]+=input[i-m_taps.size()/2+t]*m_taps[t];
811 for(i=input.size()-m_taps.size()/2;i<input.size();++i){
813 output[i]=m_taps[m_taps.size()/2]*input[i];
815 for(
int t=1;t<=m_taps.size()/2;++t){
816 output[i]+=m_taps[m_taps.size()/2-t]*input[i-t];
818 output[i]+=m_taps[m_taps.size()/2+t]*input[i+t];
820 switch(getPadding(m_padding)){
822 output[i]+=m_taps[m_taps.size()/2+t]*input.back();
825 output[i]+=m_taps[m_taps.size()/2+t]*input[t-1];
828 output[i]+=m_taps[m_taps.size()/2+t]*0;
832 output[i]+=m_taps[m_taps.size()/2+t]*input[i-t];