/********************************************************************** Filter.h: class for filtering Author(s): Pieter.Kempeneers@ec.europa.eu Copyright (C) 2016-2020 European Union (Joint Research Centre) This file is part of jiplib. jiplib is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. jiplib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with jiplib. If not, see . ***********************************************************************/ #ifndef _MYFILTER_H_ #define _MYFILTER_H_ #include #include extern "C" { #include #include } #include "StatFactory.h" #include "imageclasses/Jim.h" namespace filter { 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}; enum PADDING { symmetric=0, replicate=1, circular=2, zero=3}; class Filter { public: Filter(void); Filter(const std::vector &taps); virtual ~Filter(){}; void setPadding(const std::string& padString){ m_padding=padString; }; static const gsl_wavelet_type* getWaveletType(const std::string waveletType){ if(waveletType=="daubechies") return(gsl_wavelet_daubechies); else if(waveletType=="daubechies_centered") return(gsl_wavelet_daubechies_centered); else if(waveletType=="haar") return(gsl_wavelet_haar); else if(waveletType=="haar_centered") return(gsl_wavelet_haar_centered); else if(waveletType=="bspline") return(gsl_wavelet_bspline); else if(waveletType=="bspline_centered") return(gsl_wavelet_bspline_centered); else{ std::string errorString="Error: wavelet type not supported"; throw(errorString); } } static FILTER_TYPE getFilterType(const std::string filterType){ std::map m_filterMap; initFilterMap(m_filterMap); return m_filterMap[filterType]; }; void setTaps(const std::vector &taps, bool normalize=true); void pushClass(short theClass=1){m_class.push_back(theClass);}; void pushMask(short theMask=0){m_mask.push_back(theMask);}; unsigned int pushNoDataValue(double noDataValue); unsigned int setNoDataValues(std::vector vnodata); void pushThreshold(double theThreshold){m_threshold.push_back(theThreshold);}; void setThresholds(const std::vector& theThresholds){m_threshold=theThresholds;}; template void filter(const std::vector& input, std::vector& output); template void filter(const std::vector& input, std::vector& output, const std::string& method, int dim); template void smooth(const std::vector& input, std::vector& output, short dim); template void smoothNoData(const std::vector& input, const std::string& interpolationType, std::vector& output); template void filter(T* input, int inputSize, std::vector& output); template void smooth(T* input, int inputSize, std::vector& output, short dim); //template void morphology(const std::vector& input, std::vector& output, const std::string& method, int dim, bool verbose=false); void morphology(Jim& input, Jim& output, const std::string& method, int dim, short verbose=0); void filter(Jim& input, Jim& output); void stat(Jim& input, Jim& output, const std::string& method); void stats(Jim& input, Jim& output, const std::vector& methods); void filter(Jim& input, Jim& output, const std::string& method, int dim); void getSavGolayCoefficients(std::vector &c, int np, int nl, int nr, int ld, int m); void ludcmp(std::vector &a, std::vector &indx, double &d); void lubksb(std::vector &a, std::vector &indx, std::vector &b); /* void savgolay(const std::shared_ptr input, std::shared_ptr output, int np, int nl, int nr, int m); */ void smooth(Jim& input, Jim& output, short dim); void smoothNoData(Jim& input, const std::string& interpolationType, Jim& output); double getCentreWavelength(const std::vector &wavelengthIn, const Vector2d& srf, const std::string& interpolationType, double delta=1.0, bool verbose=false); template double applySrf(const std::vector &wavelengthIn, const std::vector& input, const Vector2d& srf, const std::string& interpolationType, T& output, double delta=1.0, bool normalize=false, bool verbose=false); template double applySrf(const std::vector &wavelengthIn, const Vector2d& input, const Vector2d& srf, const std::string& interpolationType, std::vector& output, double delta=1.0, bool normalize=false, int down=1, bool transposeInput=false, bool verbose=false); template void applyFwhm(const std::vector &wavelengthIn, const std::vector& input, const std::vector &wavelengthOut, const std::vector &fwhm, const std::string& interpolationType, std::vector& output, bool verbose=false); template void applyFwhm(const std::vector &wavelengthIn, const Vector2d& input, const std::vector &wavelengthOut, const std::vector &fwhm, const std::string& interpolationType, Vector2d& output, int down=1, bool verbose=false); void dwtForward(Jim& input, Jim& output, const std::string& wavelet_type, int family); void dwtInverse(Jim& input, Jim& output, const std::string& wavelet_type, int family); void dwtCut(Jim& input, Jim& output, const std::string& wavelet_type, int family, double cut); void dwtForward(std::vector& data, const std::string& wavelet_type, int family); void dwtInverse(std::vector& data, const std::string& wavelet_type, int family); void dwtCut(std::vector& data, const std::string& wavelet_type, int family, double cut); void dwtCutFrom(Jim& input, Jim& output, const std::string& wavelet_type, int family, int band); static PADDING getPadding(const std::string& padString){ std::map padMap; padMap["zero"]=filter::zero; padMap["symmetric"]=filter::symmetric; padMap["replicate"]=filter::replicate; padMap["circular"]=filter::circular; return(padMap[padString]); }; private: static void initFilterMap(std::map& m_filterMap){ //initialize Map m_filterMap["median"]=filter::median; m_filterMap["var"]=filter::var; m_filterMap["min"]=filter::min; m_filterMap["max"]=filter::max; m_filterMap["sum"]=filter::sum; m_filterMap["mean"]=filter::mean; m_filterMap["minmax"]=filter::minmax; m_filterMap["dilate"]=filter::dilate; m_filterMap["erode"]=filter::erode; m_filterMap["close"]=filter::close; m_filterMap["open"]=filter::open; m_filterMap["homog"]=filter::homog; m_filterMap["sobelx"]=filter::sobelx; m_filterMap["sobely"]=filter::sobely; m_filterMap["sobelxy"]=filter::sobelxy; m_filterMap["sobelyx"]=filter::sobelyx; m_filterMap["smooth"]=filter::smooth; m_filterMap["density"]=filter::density; m_filterMap["mode"]=filter::mode; m_filterMap["mixed"]=filter::mixed; m_filterMap["threshold"]=filter::threshold; m_filterMap["ismin"]=filter::ismin; m_filterMap["ismax"]=filter::ismax; m_filterMap["heterog"]=filter::heterog; m_filterMap["order"]=filter::order; m_filterMap["stdev"]=filter::stdev; m_filterMap["mrf"]=filter::mrf; m_filterMap["dwt"]=filter::dwt; m_filterMap["dwti"]=filter::dwti; m_filterMap["dwt_cut"]=filter::dwt_cut; m_filterMap["scramble"]=filter::scramble; m_filterMap["shift"]=filter::shift; m_filterMap["linearfeature"]=filter::linearfeature; m_filterMap["smoothnodata"]=filter::smoothnodata; m_filterMap["countid"]=filter::countid; m_filterMap["dwt_cut_from"]=filter::dwt_cut_from; m_filterMap["savgolay"]=filter::savgolay; m_filterMap["percentile"]=filter::percentile; m_filterMap["proportion"]=filter::proportion; m_filterMap["nvalid"]=filter::nvalid; m_filterMap["sauvola"]=filter::sauvola; m_filterMap["first"]=filter::first; m_filterMap["last"]=filter::last; m_filterMap["minindex"]=filter::minindex; m_filterMap["maxindex"]=filter::maxindex; } std::vector m_taps; std::vector m_class; std::vector m_mask; std::string m_padding; std::vector m_noDataValues; std::vector m_threshold; }; //input[band], output //returns wavelength for which srf is maximum template double Filter::applySrf(const std::vector &wavelengthIn, const std::vector& input, const Vector2d& srf, const std::string& interpolationType, T& output, double delta, bool normalize, bool verbose) { assert(srf.size()==2);//[0]: wavelength, [1]: response function int nband=srf[0].size(); double start=floor(wavelengthIn[0]); double end=ceil(wavelengthIn.back()); if(verbose) std::cout << "wavelengths in [" << start << "," << end << "]" << std::endl << std::flush; statfactory::StatFactory stat; gsl_interp_accel *acc; stat.allocAcc(acc); gsl_spline *spline; stat.getSpline(interpolationType,nband,spline); stat.initSpline(spline,&(srf[0][0]),&(srf[1][0]),nband); if(verbose) std::cout << "calculating norm of srf" << std::endl << std::flush; double norm=0; norm=gsl_spline_eval_integ(spline,srf[0].front(),srf[0].back(),acc); if(verbose) std::cout << "norm of srf: " << norm << std::endl << std::flush; gsl_spline_free(spline); gsl_interp_accel_free(acc); //interpolate input and srf to delta std::vector wavelength_fine; for(double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta) wavelength_fine.push_back(win); if(verbose) std::cout << "interpolate wavelengths to " << wavelength_fine.size() << " entries " << std::endl; std::vector srf_fine;//spectral response function, interpolated for wavelength_fine stat.interpolateUp(srf[0],srf[1],wavelength_fine,interpolationType,srf_fine,verbose); assert(srf_fine.size()==wavelength_fine.size()); gsl_interp_accel *accOut; stat.allocAcc(accOut); gsl_spline *splineOut; stat.getSpline(interpolationType,wavelength_fine.size(),splineOut); assert(splineOut); assert(wavelengthIn.size()==input.size()); std::vector input_fine; std::vector product(wavelength_fine.size()); std::vector wavelengthOut(wavelength_fine.size()); stat.interpolateUp(wavelengthIn,input,wavelength_fine,interpolationType,input_fine,verbose); if(verbose) std::cout << "input_fine.size(): " << input_fine.size() << std::endl; for(int iband=0;iband double Filter::applySrf(const std::vector &wavelengthIn, const Vector2d& input, const Vector2d& srf, const std::string& interpolationType, std::vector& output, double delta, bool normalize, int down, bool transposeInput, bool verbose) { assert(srf.size()==2);//[0]: wavelength, [1]: response function int nband=srf[0].size(); unsigned int nsample=(transposeInput)? input.size():input[0].size(); output.resize((nsample+down-1)/down); double start=floor(wavelengthIn[0]); double end=ceil(wavelengthIn.back()); if(verbose) std::cout << "wavelengths in [" << start << "," << end << "]" << std::endl << std::flush; statfactory::StatFactory stat; gsl_interp_accel *acc; stat.allocAcc(acc); gsl_spline *spline; stat.getSpline(interpolationType,nband,spline); stat.initSpline(spline,&(srf[0][0]),&(srf[1][0]),nband); if(verbose) std::cout << "calculating norm of srf" << std::endl << std::flush; double norm=0; norm=gsl_spline_eval_integ(spline,srf[0].front(),srf[0].back(),acc); if(verbose) std::cout << "norm of srf: " << norm << std::endl << std::flush; gsl_spline_free(spline); gsl_interp_accel_free(acc); //interpolate input and srf to delta std::vector wavelength_fine; for(double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta) wavelength_fine.push_back(win); if(verbose) std::cout << "interpolate wavelengths to " << wavelength_fine.size() << " entries " << std::endl; std::vector srf_fine;//spectral response function, interpolated for wavelength_fine stat.interpolateUp(srf[0],srf[1],wavelength_fine,interpolationType,srf_fine,verbose); assert(srf_fine.size()==wavelength_fine.size()); gsl_interp_accel *accOut; stat.allocAcc(accOut); gsl_spline *splineOut; stat.getSpline(interpolationType,wavelength_fine.size(),splineOut); assert(splineOut); std::vector wavelengthOut; double centreWavelength=0; for(int isample=0;isample inputValues; if(transposeInput) inputValues=input[isample]; else input.selectCol(isample,inputValues); assert(wavelengthIn.size()==inputValues.size()); std::vector input_fine; std::vector product(wavelength_fine.size()); stat.interpolateUp(wavelengthIn,inputValues,wavelength_fine,interpolationType,input_fine,verbose); for(int iband=0;iband0); else centreWavelength=gsl_spline_eval_integ(splineOut,start,end,accOut)/norm; } gsl_spline_free(splineOut); gsl_interp_accel_free(accOut); return(centreWavelength); } template void Filter::applyFwhm(const std::vector &wavelengthIn, const std::vector& input, const std::vector &wavelengthOut, const std::vector &fwhm, const std::string& interpolationType, std::vector& output, bool verbose){ double delta=1;//1 nm resolution std::vector stddev(fwhm.size()); for(int index=0;index=wavelengthOut.back()); statfactory::StatFactory stat; std::vector input_fine; std::vector wavelength_fine; for(double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta) wavelength_fine.push_back(win); if(verbose){ for(int index=0;index tf(nbandIn,nbandOut); for(int indexOut=0;indexOut void Filter::applyFwhm(const std::vector &wavelengthIn, const Vector2d& input, const std::vector &wavelengthOut, const std::vector &fwhm, const std::string& interpolationType, Vector2d& output, int down, bool verbose){ double delta=1;//1 nm resolution std::vector stddev(fwhm.size()); for(int index=0;index wavelength_fine; for(double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta) wavelength_fine.push_back(win); assert(wavelengthOut.size()==fwhm.size()); assert(wavelengthIn[0]<=wavelengthOut[0]); assert(wavelengthIn.back()>=wavelengthOut.back()); if(verbose){ for(int index=0;index tf(nbandIn,nbandOut); std::vector norm(nbandOut); for(int indexOut=0;indexOut inputValues; input.selectCol(isample,inputValues); assert(wavelengthIn.size()==inputValues.size()); for(int indexOut=0;indexOut input_fine; stat.interpolateUp(wavelengthIn,inputValues,wavelength_fine,interpolationType,input_fine,verbose); output[indexOut][(isample+down-1)/down]=0; for(int indexIn=0;indexIn void Filter::smooth(const std::vector& input, std::vector& output, short dim) { assert(dim>0); m_taps.resize(dim); for(int itap=0;itap void Filter::smoothNoData(const std::vector& input, const std::string& interpolationType, std::vector& output) { statfactory::StatFactory stat; stat.setNoDataValues(m_noDataValues); std::vector abscis(input.size()); for(int i=0;i void Filter::filter(const std::vector& input, std::vector& output) { assert(input.size()>=m_taps.size()); output.resize(input.size()); int i=0; //start: extend input by padding for(i=0;i=t) output[i]+=m_taps[m_taps.size()/2-t]*input[i-t]; else{ switch(getPadding(m_padding)){ case(replicate): output[i]+=m_taps[m_taps.size()/2-t]*input[0]; break; case(circular): output[i]+=m_taps[m_taps.size()/2-t]*input[input.size()+i-t]; break; case(zero): output[i]+=m_taps[m_taps.size()/2-t]*0; break; case(symmetric): default: output[i]+=m_taps[m_taps.size()/2-t]*input[t-i]; break; } } } } //main for(i=m_taps.size()/2;i void Filter::filter(const std::vector& input, std::vector& output, const std::string& method, int dim) { assert(dim); output.resize(input.size()); int i=0; statfactory::StatFactory stat; stat.setNoDataValues(m_noDataValues); std::vector statBuffer; short binValue=0; //start: extend input by padding for(i=0;i=t){ theValue=input[i-t]; } else{ switch(getPadding(m_padding)){ case(replicate): theValue=input[0]; break; case(circular): theValue=input[input.size()+i-t]; break; case(zero): theValue=0; break; case(symmetric): default: theValue=input[t-i]; break; } } for(int iclass=0;iclass void Filter::smooth(T* input, int inputSize, std::vector& output, short dim) { assert(dim>0); m_taps.resize(dim); for(int itap=0;itap void Filter::filter(T* input, int inputSize, std::vector& output) { assert(inputSize>=m_taps.size()); output.resize(inputSize); int i=0; //start: extend input by padding for(i=0;i=t) output[i]+=m_taps[m_taps.size()/2-t]*input[i-t]; else{ switch(getPadding(m_padding)){ case(replicate): output[i]+=m_taps[m_taps.size()/2-t]*input[0]; break; case(circular): output[i]+=m_taps[m_taps.size()/2-t]*input[input.size()+i-t]; break; case(zero): output[i]+=m_taps[m_taps.size()/2-t]*0; break; case(symmetric): default: output[i]+=m_taps[m_taps.size()/2-t]*input[t-i]; break; } } } } //main for(i=m_taps.size()/2;i