| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#ifndef _JLFILTER1D_LIB_H_ |
| 22 |
#define _JLFILTER1D_LIB_H_ |
| 23 |
|
| 24 |
#include "imageclasses/Jim.h" |
| 25 |
#include "algorithms/Filter.h" |
| 26 |
#include "apps/AppFactory.h" |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
template<typename T> void Jim::firfilter1d_t(Jim& imgWriter, app::AppFactory& app){ |
| 54 |
Optionjl<double> taps_opt("taps", "taps", "taps used for spectral filtering"); |
| 55 |
Optionjl<std::string> padding_opt("pad","pad", "Padding method for filtering (how to handle edge effects). Choose between: symmetric, replicate, circular, zero (pad with 0).", "symmetric"); |
| 56 |
Optionjl<short> verbose_opt("v", "verbose", "verbose mode if > 0", 0,2); |
| 57 |
|
| 58 |
bool doProcess; |
| 59 |
try{ |
| 60 |
doProcess=taps_opt.retrieveOption(app); |
| 61 |
padding_opt.retrieveOption(app); |
| 62 |
verbose_opt.retrieveOption(app); |
| 63 |
if(!doProcess){ |
| 64 |
std::cout << std::endl; |
| 65 |
std::ostringstream helpStream; |
| 66 |
helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 67 |
throw(helpStream.str()); |
| 68 |
} |
| 69 |
|
| 70 |
#if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 71 |
#pragma omp parallel for |
| 72 |
#else |
| 73 |
#endif |
| 74 |
for(size_t iband=0;iband<nrOfBand();++iband){ |
| 75 |
T* pin=static_cast<T*>(getDataPointer(iband)); |
| 76 |
double* pout=static_cast<double*>(imgWriter.getDataPointer(iband)); |
| 77 |
|
| 78 |
filter::Filter filter1d; |
| 79 |
if(verbose_opt[0]) |
| 80 |
std::cout << "set padding" << std::endl; |
| 81 |
filter1d.setPadding(padding_opt[0]); |
| 82 |
if(taps_opt.size()>nrOfPlane()){ |
| 83 |
std::ostringstream errorStream; |
| 84 |
errorStream << "Error: number of taps must be < " << nrOfPlane() << std::endl; |
| 85 |
throw(errorStream.str()); |
| 86 |
} |
| 87 |
|
| 88 |
if(verbose_opt[0]){ |
| 89 |
std::cout << "taps: " << std::endl; |
| 90 |
std::cout << taps_opt << std::endl; |
| 91 |
} |
| 92 |
for(size_t index=0;index<nrOfCol()*nrOfRow();++index){ |
| 93 |
size_t i=0; |
| 94 |
|
| 95 |
for(i=0;i<taps_opt.size()/2;++i){ |
| 96 |
pout[index+i*nrOfCol()*nrOfRow()]=taps_opt[taps_opt.size()/2]*pin[index+i*nrOfCol()*nrOfRow()]; |
| 97 |
|
| 98 |
|
| 99 |
for(int t=1;t<taps_opt.size()/2;++t){ |
| 100 |
pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2+t]*pin[index+(i+t)*nrOfCol()*nrOfRow()]; |
| 101 |
if(i>=t) |
| 102 |
pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2-t]*pin[index+(i-t)*nrOfCol()*nrOfRow()]; |
| 103 |
else{ |
| 104 |
switch(filter1d.getPadding(padding_opt[0])){ |
| 105 |
case(filter::replicate): |
| 106 |
pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2-t]*pin[index]; |
| 107 |
break; |
| 108 |
case(filter::circular): |
| 109 |
pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2-t]*pin[index+(nrOfPlane()+i-t)*nrOfCol()*nrOfRow()]; |
| 110 |
break; |
| 111 |
case(filter::zero): |
| 112 |
pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2-t]*0; |
| 113 |
break; |
| 114 |
case(filter::symmetric): |
| 115 |
default: |
| 116 |
pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2-t]*pin[index+(t-i)*nrOfCol()*nrOfRow()]; |
| 117 |
break; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
for(i=taps_opt.size()/2;i<nrOfPlane()-taps_opt.size()/2;++i){ |
| 124 |
|
| 125 |
pout[index+i*nrOfCol()*nrOfRow()]=0; |
| 126 |
for(int t=0;t<taps_opt.size();++t) |
| 127 |
pout[index+i*nrOfCol()*nrOfRow()]+=pin[index+(i-taps_opt.size()/2+t)*nrOfCol()*nrOfRow()]*taps_opt[t]; |
| 128 |
} |
| 129 |
|
| 130 |
for(i=nrOfPlane()-taps_opt.size()/2;i<nrOfPlane();++i){ |
| 131 |
|
| 132 |
pout[index+i*nrOfCol()*nrOfRow()]=taps_opt[taps_opt.size()/2]*pin[index+i*nrOfCol()*nrOfRow()]; |
| 133 |
|
| 134 |
|
| 135 |
for(int t=1;t<taps_opt.size()/2;++t){ |
| 136 |
pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2-t]*pin[index+(i-t)*nrOfCol()*nrOfRow()]; |
| 137 |
if(i+t<nrOfPlane()) |
| 138 |
pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2+t]*pin[index+(i+t)*nrOfCol()*nrOfRow()]; |
| 139 |
else{ |
| 140 |
switch(filter1d.getPadding(padding_opt[0])){ |
| 141 |
case(filter::replicate): |
| 142 |
pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2+t]*pin[index+(nrOfPlane()-1)*nrOfCol()*nrOfRow()]; |
| 143 |
break; |
| 144 |
case(filter::circular): |
| 145 |
pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2+t]*pin[index+(t-1)*nrOfCol()*nrOfRow()]; |
| 146 |
break; |
| 147 |
case(filter::zero): |
| 148 |
pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2+t]*0; |
| 149 |
break; |
| 150 |
case(filter::symmetric): |
| 151 |
default: |
| 152 |
pout[index+i*nrOfCol()*nrOfRow()]+=taps_opt[taps_opt.size()/2+t]*pin[index+(i-t)*nrOfCol()*nrOfRow()]; |
| 153 |
break; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
catch(std::string predefinedString){ |
| 162 |
std::cout << predefinedString << std::endl; |
| 163 |
throw; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
template<typename T> void Jim::smoothNoData1d_t(Jim& imgWriter, app::AppFactory& app){ |
| 168 |
Optionjl<double> nodata_opt("nodata", "nodata", "nodata to interpolate",0); |
| 169 |
Optionjl<std::string> interpolationType_opt("interp", "interp", "type of interpolation for spectral filtering (see http://www.gnu.org/software/gsl/manual/html_node/Interpolation-Types.html)","akima"); |
| 170 |
|
| 171 |
bool doProcess; |
| 172 |
try{ |
| 173 |
doProcess=interpolationType_opt.retrieveOption(app); |
| 174 |
nodata_opt.retrieveOption(app); |
| 175 |
if(!doProcess){ |
| 176 |
std::cout << std::endl; |
| 177 |
std::ostringstream helpStream; |
| 178 |
helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 179 |
throw(helpStream.str()); |
| 180 |
} |
| 181 |
if(nrOfPlane()<2){ |
| 182 |
std::ostringstream errorStream; |
| 183 |
errorStream << "Error: not a 3D object, consider band2plane" << std::endl; |
| 184 |
throw(errorStream.str()); |
| 185 |
} |
| 186 |
statfactory::StatFactory stat; |
| 187 |
stat.setNoDataValues(nodata_opt); |
| 188 |
std::vector<double> abscis(nrOfPlane()); |
| 189 |
for(int i=0;i<abscis.size();++i) |
| 190 |
abscis[i]=i; |
| 191 |
#if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 192 |
#pragma omp parallel for |
| 193 |
#else |
| 194 |
#endif |
| 195 |
for(size_t iband=0;iband<nrOfBand();++iband){ |
| 196 |
T* pin=static_cast<T*>(getDataPointer(iband)); |
| 197 |
T* pout=static_cast<T*>(imgWriter.getDataPointer(iband)); |
| 198 |
for(size_t index=0;index<nrOfCol()*nrOfRow();++index){ |
| 199 |
std::vector<double> input(nrOfPlane()); |
| 200 |
std::vector<double> output(imgWriter.nrOfPlane()); |
| 201 |
for(size_t iplane=0;iplane<nrOfPlane();++iplane) |
| 202 |
input[iplane]=static_cast<double>(pin[index+iplane*nrOfCol()*nrOfRow()]); |
| 203 |
stat.interpolateNoData(abscis,input,interpolationType_opt[0],output); |
| 204 |
for(size_t iplane=0;iplane<nrOfPlane();++iplane) |
| 205 |
pout[index+iplane*nrOfCol()*nrOfRow()]=static_cast<T>(output[iplane]); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
catch(std::string predefinedString ){ |
| 210 |
std::cout << predefinedString << std::endl; |
| 211 |
throw; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
template<typename T> void Jim::stats1d_t(Jim& imgWriter, app::AppFactory& app){ |
| 216 |
Optionjl<double> nodata_opt("nodata", "nodata", "nodata to interpolate",0); |
| 217 |
Optionjl<std::string> methods_opt("method", "method", "statistical method"); |
| 218 |
Optionjl<double> threshold_opt("t", "threshold", "threshold value(s) to use for threshold filter (one for each class), or threshold to cut for dwt_cut (use 0 to keep all) or dwt_cut_from", 0); |
| 219 |
|
| 220 |
bool doProcess; |
| 221 |
try{ |
| 222 |
doProcess=methods_opt.retrieveOption(app); |
| 223 |
nodata_opt.retrieveOption(app); |
| 224 |
threshold_opt.retrieveOption(app); |
| 225 |
if(!doProcess){ |
| 226 |
std::cout << std::endl; |
| 227 |
std::ostringstream helpStream; |
| 228 |
helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 229 |
throw(helpStream.str()); |
| 230 |
} |
| 231 |
if(methods_opt.empty()){ |
| 232 |
std::ostringstream errorStream; |
| 233 |
errorStream << "Error: no method provided" << std::endl; |
| 234 |
throw(errorStream.str()); |
| 235 |
} |
| 236 |
if(nrOfBand()>1){ |
| 237 |
std::ostringstream errorStream; |
| 238 |
errorStream << "Error: multi-band input not supported"<< std::endl; |
| 239 |
throw(errorStream.str()); |
| 240 |
} |
| 241 |
if(nrOfPlane()<2){ |
| 242 |
std::ostringstream errorStream; |
| 243 |
errorStream << "Error: not a 3D object, consider band2plane" << std::endl; |
| 244 |
throw(errorStream.str()); |
| 245 |
} |
| 246 |
imgWriter.open(nrOfCol(),nrOfRow(),methods_opt.size(),1,getGDALDataType()); |
| 247 |
imgWriter.setProjection(this->getProjection()); |
| 248 |
double gt[6]; |
| 249 |
this->getGeoTransform(gt); |
| 250 |
imgWriter.setGeoTransform(gt); |
| 251 |
statfactory::StatFactory stat; |
| 252 |
stat.setNoDataValues(nodata_opt); |
| 253 |
std::vector<double> abscis(nrOfPlane()); |
| 254 |
for(int i=0;i<abscis.size();++i) |
| 255 |
abscis[i]=i; |
| 256 |
T* pin=static_cast<T*>(getDataPointer(0)); |
| 257 |
std::vector<T*> pout(methods_opt.size()); |
| 258 |
#if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 259 |
#pragma omp parallel for |
| 260 |
#else |
| 261 |
#endif |
| 262 |
for(size_t imethod=0;imethod<methods_opt.size();++imethod){ |
| 263 |
pout[imethod]=static_cast<T*>(imgWriter.getDataPointer(imethod)); |
| 264 |
for(size_t index=0;index<nrOfCol()*nrOfRow();++index){ |
| 265 |
std::vector<double> input(nrOfPlane()); |
| 266 |
for(size_t iplane=0;iplane<nrOfPlane();++iplane) |
| 267 |
input[iplane]=static_cast<double>(pin[index+iplane*nrOfCol()*nrOfRow()]); |
| 268 |
switch(filter::Filter::getFilterType(methods_opt[imethod])){ |
| 269 |
case(filter::first): |
| 270 |
pout[imethod][index]=input[0]; |
| 271 |
break; |
| 272 |
case(filter::last): |
| 273 |
pout[imethod][index]=input.back(); |
| 274 |
break; |
| 275 |
case(filter::nvalid): |
| 276 |
pout[imethod][index]=stat.nvalid(input); |
| 277 |
break; |
| 278 |
case(filter::median): |
| 279 |
pout[imethod][index]=stat.median(input); |
| 280 |
break; |
| 281 |
case(filter::min): |
| 282 |
pout[imethod][index]=stat.mymin(input); |
| 283 |
break; |
| 284 |
case(filter::max): |
| 285 |
pout[imethod][index]=stat.mymax(input); |
| 286 |
break; |
| 287 |
case(filter::sum): |
| 288 |
pout[imethod][index]=stat.sum(input); |
| 289 |
break; |
| 290 |
case(filter::var): |
| 291 |
pout[imethod][index]=stat.var(input); |
| 292 |
break; |
| 293 |
case(filter::stdev): |
| 294 |
pout[imethod][index]=sqrt(stat.var(input)); |
| 295 |
break; |
| 296 |
case(filter::mean): |
| 297 |
pout[imethod][index]=stat.mean(input); |
| 298 |
break; |
| 299 |
case(filter::percentile):{ |
| 300 |
|
| 301 |
pout[imethod][index]=stat.percentile(input,input.begin(),input.end(),threshold_opt[0]); |
| 302 |
break; |
| 303 |
} |
| 304 |
default: |
| 305 |
std::string errorString="method not supported"; |
| 306 |
throw(errorString); |
| 307 |
break; |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
catch(std::string predefinedString ){ |
| 313 |
std::cout << predefinedString << std::endl; |
| 314 |
throw; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
#endif |