| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#ifndef _MYFILTER2D_H_ |
| 22 |
#define _MYFILTER2D_H_ |
| 23 |
|
| 24 |
#ifndef PI |
| 25 |
#define PI 3.1415926535897932384626433832795 |
| 26 |
#endif |
| 27 |
|
| 28 |
#ifndef DEG2RAD |
| 29 |
#define DEG2RAD(DEG) (DEG/180.0*PI) |
| 30 |
#endif |
| 31 |
|
| 32 |
#ifndef RAD2DEG |
| 33 |
#define RAD2DEG(RAD) (RAD/PI*180) |
| 34 |
#endif |
| 35 |
|
| 36 |
#ifdef WIN32 |
| 37 |
#include <process.h> |
| 38 |
#define getpid _getpid |
| 39 |
#endif |
| 40 |
|
| 41 |
#include <assert.h> |
| 42 |
|
| 43 |
#include <cmath> |
| 44 |
#include <limits> |
| 45 |
#include <vector> |
| 46 |
#include <string> |
| 47 |
#include <map> |
| 48 |
extern "C" { |
| 49 |
#include <gsl/gsl_sort.h> |
| 50 |
#include <gsl/gsl_wavelet.h> |
| 51 |
#include <gsl/gsl_wavelet2d.h> |
| 52 |
#include <gsl/gsl_rng.h> |
| 53 |
#include <gsl/gsl_randist.h> |
| 54 |
} |
| 55 |
#include "base/Vector2d.h" |
| 56 |
#include "Filter.h" |
| 57 |
#include "imageclasses/Jim.h" |
| 58 |
#include "algorithms/StatFactory.h" |
| 59 |
|
| 60 |
namespace filter2d |
| 61 |
{ |
| 62 |
enum FILTER_TYPE { median=200, var=201 , min=202, max=203, sum=204, mean=205, minmax=206, dilate=207, erode=208, close=209, open=210, homog=211, sobelx=212, sobely=213, sobelxy=214, sobelyx=215, smooth=216, density=217, mode=218, mixed=219, threshold=220, ismin=221, ismax=222, heterog=223, order=224, stdev=225, mrf=226, dwt=227, dwti=228, dwt_cut=229, scramble=230, shift=231, linearfeature=232, smoothnodata=233, countid=234, dwt_cut_from=235, savgolay=236, percentile=237, proportion=238, nvalid=239, sauvola=240}; |
| 63 |
|
| 64 |
class Filter2d |
| 65 |
{ |
| 66 |
public: |
| 67 |
enum RESAMPLE { NEAR = 0, BILINEAR = 1, BICUBIC = 2 }; |
| 68 |
Filter2d(void); |
| 69 |
Filter2d(const Vector2d<double> &taps, bool flip=true); |
| 70 |
virtual ~Filter2d(){}; |
| 71 |
static FILTER_TYPE getFilterType(const std::string filterType){ |
| 72 |
std::map<std::string, FILTER_TYPE> m_filterMap; |
| 73 |
initMap(m_filterMap); |
| 74 |
return m_filterMap[filterType]; |
| 75 |
}; |
| 76 |
static const RESAMPLE getResampleType(const std::string resampleType){ |
| 77 |
if(resampleType=="near") return(NEAR); |
| 78 |
else if(resampleType=="bilinear") return(BILINEAR); |
| 79 |
else{ |
| 80 |
std::string errorString="resampling type not supported: "; |
| 81 |
errorString+=resampleType; |
| 82 |
errorString+=" use near or bilinear"; |
| 83 |
throw(errorString); |
| 84 |
} |
| 85 |
}; |
| 86 |
|
| 87 |
void setTaps(const Vector2d<double> &taps, bool flip=true); |
| 88 |
|
| 89 |
void pushClass(short theClass=1){m_class.push_back(theClass);}; |
| 90 |
int pushNoDataValue(double noDataValue=0); |
| 91 |
void pushThreshold(double theThreshold){m_threshold.push_back(theThreshold);}; |
| 92 |
void setThresholds(const std::vector<double>& theThresholds){m_threshold=theThresholds;}; |
| 93 |
void setClasses(const std::vector<short>& theClasses){m_class=theClasses;}; |
| 94 |
void filter(Jim& input, Jim& output, bool absolute=false, bool normalize=false); |
| 95 |
|
| 96 |
void filterLB(Jim& input, Jim& output, bool absolute=false, bool normalize=false, bool noData=false); |
| 97 |
void smooth(Jim& input, Jim& output,int dim); |
| 98 |
void smooth(Jim& input, Jim& output,int dimX, int dimY); |
| 99 |
void smoothNoData(Jim& input, Jim& output,int dim); |
| 100 |
void smoothNoData(Jim& input, Jim& output,int dimX, int dimY); |
| 101 |
|
| 102 |
template<class T> void filter(T* inputVector, T* outputVector, size_t dimx, size_t dimy, bool absolute=false, bool normalize=false); |
| 103 |
template<class T1, class T2> void filter(const Vector2d<T1>& inputVector, Vector2d<T2>& outputVector); |
| 104 |
template<class T1, class T2> void smooth(const Vector2d<T1>& inputVector, Vector2d<T2>& outputVector,int dim); |
| 105 |
template<class T1, class T2> void smooth(const Vector2d<T1>& inputVector, Vector2d<T2>& outputVector,int dimX, int dimY); |
| 106 |
void dwtForward(Jim& input, Jim& output, const std::string& wavelet_type, int family); |
| 107 |
void dwtInverse(Jim& input, Jim& output, const std::string& wavelet_type, int family); |
| 108 |
void dwtCut(Jim& input, Jim& output, const std::string& wavelet_type, int family, double cut, bool verbose=false); |
| 109 |
template<class T> void dwtForward(Vector2d<T>& data, const std::string& wavelet_type, int family); |
| 110 |
template<class T> void dwtInverse(Vector2d<T>& data, const std::string& wavelet_type, int family); |
| 111 |
template<class T> void dwtCut(Vector2d<T>& data, const std::string& wavelet_type, int family, double cut); |
| 112 |
void majorVoting(Jim& input, Jim& output, int dim=0,const std::vector<int> &prior=std::vector<int>()); |
| 113 |
|
| 114 |
void doit(Jim& input, Jim& output, const std::string& method, int dim, short down=1, bool disc=false); |
| 115 |
void doit(Jim& input, Jim& output, const std::string& method, int dimX, int dimY, short down=1, bool disc=false); |
| 116 |
void mrf(Jim& input, Jim& output, int dimX, int dimY, double beta, bool eightConnectivity=true, short down=1, bool verbose=false); |
| 117 |
void mrf(Jim& input, Jim& output, int dimX, int dimY, Vector2d<double> beta, bool eightConnectivity=true, short down=1, bool verbose=false); |
| 118 |
template<class T1, class T2> void doit(const Vector2d<T1>& inputVector, Vector2d<T2>& outputVector, const std::string& method, int dimX, int dimY, short down=1, bool disc=false); |
| 119 |
void median(Jim& input, Jim& output, int dim, bool disc=false); |
| 120 |
void var(Jim& input, Jim& output, int dim, bool disc=false); |
| 121 |
void morphology(Jim& input, Jim& output, const std::string& method, int dimX, int dimY, const std::vector<double> &angle=std::vector<double>(), bool disc=false); |
| 122 |
template<class T> unsigned long int morphology(const Vector2d<T>& input, Vector2d<T>& output, const std::string& method, int dimX, int dimY, bool disc=false, double hThreshold=0); |
| 123 |
template<class T> unsigned long int dsm2dtm_nwse(const Vector2d<T>& inputDSM, Vector2d<T>& outputMask, double hThreshold, int nlimit, int dim=3); |
| 124 |
template<class T> unsigned long int dsm2dtm_nesw(const Vector2d<T>& inputDSM, Vector2d<T>& outputMask, double hThreshold, int nlimit, int dim=3); |
| 125 |
template<class T> unsigned long int dsm2dtm_senw(const Vector2d<T>& inputDSM, Vector2d<T>& outputMask, double hThreshold, int nlimit, int dim=3); |
| 126 |
template<class T> unsigned long int dsm2dtm_swne(const Vector2d<T>& inputDSM, Vector2d<T>& outputMask, double hThreshold, int nlimit, int dim=3); |
| 127 |
template<class T> void dsm2shadow(const Vector2d<T>& input, Vector2d<T>& output, double sza, double saa, double pixelSize, short shadowFlag=1); |
| 128 |
void dsm2shadow(Jim& input, Jim& output, double sza, double saa, double pixelSize, short shadowFlag=1); |
| 129 |
|
| 130 |
void shift(Jim& input, Jim& output, double offsetX=0, double offsetY=0, double randomSigma=0, RESAMPLE resample=BILINEAR, bool verbose=false); |
| 131 |
template<class T> void shift(const Vector2d<T>& input, Vector2d<T>& output, double offsetX=0, double offsetY=0, double randomSigma=0, RESAMPLE resample=NEAR, bool verbose=false); |
| 132 |
void linearFeature(const Vector2d<float>& input, std::vector< Vector2d<float> >& output, float angle=361, float angleStep=1, float maxDistance=0, float eps=0, bool l1=true, bool a1=true, bool l2=true, bool a2=true, bool verbose=false); |
| 133 |
void linearFeature(Jim& input, Jim& output, float angle=361, float angleStep=1, float maxDistance=0, float eps=0, bool l1=true, bool a1=true, bool l2=true, bool a2=true, int band=0, bool verbose=false); |
| 134 |
|
| 135 |
private: |
| 136 |
static void initMap(std::map<std::string, FILTER_TYPE>& m_filterMap){ |
| 137 |
|
| 138 |
m_filterMap["median"]=filter2d::median; |
| 139 |
m_filterMap["nvalid"]=filter2d::nvalid; |
| 140 |
m_filterMap["var"]=filter2d::var; |
| 141 |
m_filterMap["min"]=filter2d::min; |
| 142 |
m_filterMap["max"]=filter2d::max; |
| 143 |
m_filterMap["sum"]=filter2d::sum; |
| 144 |
m_filterMap["mean"]=filter2d::mean; |
| 145 |
m_filterMap["minmax"]=filter2d::minmax; |
| 146 |
m_filterMap["dilate"]=filter2d::dilate; |
| 147 |
m_filterMap["erode"]=filter2d::erode; |
| 148 |
m_filterMap["close"]=filter2d::close; |
| 149 |
m_filterMap["open"]=filter2d::open; |
| 150 |
m_filterMap["homog"]=filter2d::homog; |
| 151 |
m_filterMap["sobelx"]=filter2d::sobelx; |
| 152 |
m_filterMap["sobely"]=filter2d::sobely; |
| 153 |
m_filterMap["sobelxy"]=filter2d::sobelxy; |
| 154 |
m_filterMap["sobelyx"]=filter2d::sobelyx; |
| 155 |
m_filterMap["smooth"]=filter2d::smooth; |
| 156 |
m_filterMap["density"]=filter2d::density; |
| 157 |
m_filterMap["mode"]=filter2d::mode; |
| 158 |
m_filterMap["mixed"]=filter2d::mixed; |
| 159 |
m_filterMap["smoothnodata"]=filter2d::smoothnodata; |
| 160 |
m_filterMap["threshold"]=filter2d::threshold; |
| 161 |
m_filterMap["ismin"]=filter2d::ismin; |
| 162 |
m_filterMap["ismax"]=filter2d::ismax; |
| 163 |
m_filterMap["heterog"]=filter2d::heterog; |
| 164 |
m_filterMap["sauvola"]=filter2d::sauvola; |
| 165 |
m_filterMap["order"]=filter2d::order; |
| 166 |
m_filterMap["stdev"]=filter2d::stdev; |
| 167 |
m_filterMap["mrf"]=filter2d::mrf; |
| 168 |
m_filterMap["dwt"]=filter2d::dwt; |
| 169 |
m_filterMap["dwti"]=filter2d::dwti; |
| 170 |
m_filterMap["dwt_cut"]=filter2d::dwt_cut; |
| 171 |
m_filterMap["dwt_cut_from"]=filter2d::dwt_cut_from; |
| 172 |
m_filterMap["scramble"]=filter2d::scramble; |
| 173 |
m_filterMap["shift"]=filter2d::shift; |
| 174 |
m_filterMap["linearfeature"]=filter2d::linearfeature; |
| 175 |
m_filterMap["countid"]=filter2d::countid; |
| 176 |
m_filterMap["savgolay"]=filter2d::savgolay; |
| 177 |
m_filterMap["percentile"]=filter2d::percentile; |
| 178 |
m_filterMap["proportion"]=filter2d::proportion; |
| 179 |
} |
| 180 |
|
| 181 |
Vector2d<double> m_taps; |
| 182 |
|
| 183 |
std::vector<short> m_class; |
| 184 |
|
| 185 |
std::vector<double> m_noDataValues; |
| 186 |
std::vector<double> m_threshold; |
| 187 |
}; |
| 188 |
|
| 189 |
|
| 190 |
template<class T1, class T2> void Filter2d::smooth(const Vector2d<T1>& inputVector, Vector2d<T2>& outputVector,int dim) |
| 191 |
{ |
| 192 |
smooth(inputVector,outputVector,dim,dim); |
| 193 |
} |
| 194 |
|
| 195 |
template<class T1, class T2> void Filter2d::smooth(const Vector2d<T1>& inputVector, Vector2d<T2>& outputVector,int dimX, int dimY) |
| 196 |
{ |
| 197 |
m_taps.resize(dimY); |
| 198 |
for(int j=0;j<dimY;++j){ |
| 199 |
m_taps[j].resize(dimX); |
| 200 |
for(int i=0;i<dimX;++i) |
| 201 |
m_taps[j][i]=1.0/dimX/dimY; |
| 202 |
} |
| 203 |
filter(inputVector,outputVector); |
| 204 |
} |
| 205 |
|
| 206 |
template<class T> void Filter2d::filter(T* inputVector, T* outputVector, size_t dimx, size_t dimy, bool absolute, bool normalize) |
| 207 |
{ |
| 208 |
int dimX=m_taps[0].size(); |
| 209 |
int dimY=m_taps.size(); |
| 210 |
int indexI=0; |
| 211 |
int indexJ=0; |
| 212 |
for(int y=0;y<dimy;++y){ |
| 213 |
for(int x=0;x<dimx;++x){ |
| 214 |
double norm=0; |
| 215 |
double accumulator=0; |
| 216 |
for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 217 |
for(int i=-(dimX-1)/2;i<=dimX/2;++i){ |
| 218 |
indexI=x+i; |
| 219 |
|
| 220 |
indexJ=y+j; |
| 221 |
|
| 222 |
if(x<(dimX-1)/2) |
| 223 |
indexI=x+abs(i); |
| 224 |
else if(x>=dimx-(dimX-1)/2) |
| 225 |
indexI=x-abs(i); |
| 226 |
if(y<(dimY-1)/2) |
| 227 |
indexJ=y+abs(j); |
| 228 |
|
| 229 |
else if(y>=dimy-(dimY-1)/2) |
| 230 |
indexJ=y-abs(j); |
| 231 |
|
| 232 |
|
| 233 |
bool masked=false; |
| 234 |
for(int imask=0;imask<m_noDataValues.size();++imask){ |
| 235 |
if(inputVector[indexJ*dimx+indexI]==m_noDataValues[imask]){ |
| 236 |
masked=true; |
| 237 |
break; |
| 238 |
} |
| 239 |
} |
| 240 |
if(!masked){ |
| 241 |
outputVector[y*dimx+x]+=(m_taps[(dimY-1)/2+j][(dimX-1)/2+i]*inputVector[indexJ*dimx+indexI]); |
| 242 |
norm+=abs(static_cast<double>(m_taps[(dimY-1)/2+j][(dimX-1)/2+i])); |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
if(absolute) |
| 248 |
outputVector[y*dimx+x]=(normalize&&norm)? abs(static_cast<double>(outputVector[y*dimx+x]))/norm : abs(static_cast<double>(outputVector[y*dimx+x])); |
| 249 |
else if(normalize&&norm!=0) |
| 250 |
outputVector[y*dimx+x]=outputVector[y*dimx+x]/norm; |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
template<class T1, class T2> void Filter2d::doit(const Vector2d<T1>& inputVector, Vector2d<T2>& outputVector, const std::string& method, int dimX, int dimY, short down, bool disc) |
| 256 |
{ |
| 257 |
const char* pszMessage; |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
statfactory::StatFactory stat; |
| 264 |
double noDataValue=0; |
| 265 |
if(m_noDataValues.size()){ |
| 266 |
stat.setNoDataValues(m_noDataValues); |
| 267 |
noDataValue=m_noDataValues[0]; |
| 268 |
} |
| 269 |
assert(dimX); |
| 270 |
assert(dimY); |
| 271 |
|
| 272 |
outputVector.resize((inputVector.size()+down-1)/down); |
| 273 |
Vector2d<T1> inBuffer(dimY); |
| 274 |
std::vector<T2> outBuffer((inputVector[0].size()+down-1)/down); |
| 275 |
|
| 276 |
int indexI=0; |
| 277 |
int indexJ=0; |
| 278 |
|
| 279 |
for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 280 |
inBuffer[indexJ]=inputVector[abs(j)]; |
| 281 |
++indexJ; |
| 282 |
} |
| 283 |
for(int y=0;y<inputVector.size();++y){ |
| 284 |
|
| 285 |
if(y){ |
| 286 |
|
| 287 |
inBuffer.erase(inBuffer.begin()); |
| 288 |
|
| 289 |
if(y+dimY/2<inputVector.size()) |
| 290 |
inBuffer.push_back(inputVector[y+dimY/2]); |
| 291 |
else{ |
| 292 |
int over=y+dimY/2-inputVector.size(); |
| 293 |
int index=(inBuffer.size()-1)-over; |
| 294 |
assert(index>=0); |
| 295 |
assert(index<inBuffer.size()); |
| 296 |
inBuffer.push_back(inBuffer[index]); |
| 297 |
} |
| 298 |
} |
| 299 |
if((y+1+down/2)%down) |
| 300 |
continue; |
| 301 |
for(int x=0;x<inputVector[0].size();++x){ |
| 302 |
if((x+1+down/2)%down) |
| 303 |
continue; |
| 304 |
outBuffer[x/down]=0; |
| 305 |
|
| 306 |
std::vector<T1> windowBuffer; |
| 307 |
std::map<int,int> occurrence; |
| 308 |
int centre=dimX*(dimY-1)/2+(dimX-1)/2; |
| 309 |
for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 310 |
for(int i=-(dimX-1)/2;i<=dimX/2;++i){ |
| 311 |
indexI=x+i; |
| 312 |
|
| 313 |
if(indexI<0) |
| 314 |
indexI=-indexI; |
| 315 |
else if(indexI>=inputVector[0].size()) |
| 316 |
indexI=inputVector[0].size()-i; |
| 317 |
if(y+j<0) |
| 318 |
indexJ=-j; |
| 319 |
else if(y+j>=inputVector.size()) |
| 320 |
indexJ=(dimY>2) ? (dimY-1)/2-j : 0; |
| 321 |
else |
| 322 |
indexJ=(dimY-1)/2+j; |
| 323 |
windowBuffer.push_back(inBuffer[indexJ][indexI]); |
| 324 |
if(!stat.isNoData(inBuffer[indexJ][indexI])){ |
| 325 |
std::vector<short>::const_iterator vit=m_class.begin(); |
| 326 |
|
| 327 |
if(!m_class.size()) |
| 328 |
++occurrence[inBuffer[indexJ][indexI]]; |
| 329 |
else{ |
| 330 |
while(vit!=m_class.end()){ |
| 331 |
if(inBuffer[indexJ][indexI]==*(vit++)) |
| 332 |
++occurrence[inBuffer[indexJ][indexI]]; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
switch(getFilterType(method)){ |
| 339 |
case(filter2d::nvalid): |
| 340 |
outBuffer[x/down]=stat.nvalid(windowBuffer); |
| 341 |
break; |
| 342 |
case(filter2d::median): |
| 343 |
outBuffer[x/down]=stat.median(windowBuffer); |
| 344 |
break; |
| 345 |
case(filter2d::var):{ |
| 346 |
outBuffer[x/down]=stat.var(windowBuffer); |
| 347 |
break; |
| 348 |
} |
| 349 |
case(filter2d::stdev):{ |
| 350 |
T2 varValue=stat.var(windowBuffer); |
| 351 |
if(stat.isNoData(varValue)) |
| 352 |
outBuffer[x/down]=noDataValue; |
| 353 |
else |
| 354 |
outBuffer[x/down]=sqrt(varValue); |
| 355 |
break; |
| 356 |
} |
| 357 |
case(filter2d::mean):{ |
| 358 |
if(windowBuffer.empty()) |
| 359 |
outBuffer[x/down]=noDataValue; |
| 360 |
else |
| 361 |
outBuffer[x/down]=stat.mean(windowBuffer); |
| 362 |
break; |
| 363 |
} |
| 364 |
case(filter2d::min):{ |
| 365 |
outBuffer[x/down]=stat.mymin(windowBuffer); |
| 366 |
break; |
| 367 |
} |
| 368 |
case(filter2d::ismin):{ |
| 369 |
T1 minValue=stat.mymin(windowBuffer); |
| 370 |
if(stat.isNoData(minValue)) |
| 371 |
outBuffer[x/down]=noDataValue; |
| 372 |
else |
| 373 |
outBuffer[x/down]=(windowBuffer[centre]==minValue)? 1:0; |
| 374 |
break; |
| 375 |
} |
| 376 |
case(filter2d::minmax):{ |
| 377 |
T1 min=0; |
| 378 |
T1 max=0; |
| 379 |
stat.minmax(windowBuffer,windowBuffer.begin(),windowBuffer.end(),min,max); |
| 380 |
if(min!=max) |
| 381 |
outBuffer[x/down]=0; |
| 382 |
else |
| 383 |
outBuffer[x/down]=windowBuffer[centre]; |
| 384 |
break; |
| 385 |
} |
| 386 |
case(filter2d::max):{ |
| 387 |
outBuffer[x/down]=stat.mymax(windowBuffer); |
| 388 |
break; |
| 389 |
} |
| 390 |
case(filter2d::ismax):{ |
| 391 |
T1 maxValue=stat.mymax(windowBuffer); |
| 392 |
if(stat.isNoData(maxValue)) |
| 393 |
outBuffer[x/down]=noDataValue; |
| 394 |
else |
| 395 |
outBuffer[x/down]=(windowBuffer[centre]==maxValue)? 1:0; |
| 396 |
break; |
| 397 |
} |
| 398 |
case(filter2d::order):{ |
| 399 |
stat.eraseNoData(windowBuffer); |
| 400 |
if(windowBuffer.empty()) |
| 401 |
outBuffer[x/down]=noDataValue; |
| 402 |
else{ |
| 403 |
double lbound=0; |
| 404 |
double ubound=dimX*dimY; |
| 405 |
double theMin=stat.mymin(windowBuffer); |
| 406 |
double theMax=stat.mymax(windowBuffer); |
| 407 |
double scale=(ubound-lbound)/(theMax-theMin); |
| 408 |
outBuffer[x/down]=static_cast<short>(scale*(windowBuffer[centre]-theMin)+lbound); |
| 409 |
} |
| 410 |
break; |
| 411 |
} |
| 412 |
case(filter2d::sum):{ |
| 413 |
outBuffer[x/down]=stat.sum(windowBuffer); |
| 414 |
break; |
| 415 |
} |
| 416 |
case(filter2d::percentile):{ |
| 417 |
assert(m_threshold.size()); |
| 418 |
outBuffer[x/down]=stat.percentile(windowBuffer,windowBuffer.begin(),windowBuffer.end(),m_threshold[0]); |
| 419 |
break; |
| 420 |
} |
| 421 |
case(filter2d::proportion):{ |
| 422 |
stat.eraseNoData(windowBuffer); |
| 423 |
T2 sum=stat.sum(windowBuffer); |
| 424 |
if(sum) |
| 425 |
outBuffer[x/down]=windowBuffer[centre]/sum; |
| 426 |
else |
| 427 |
outBuffer[x/down]=noDataValue; |
| 428 |
break; |
| 429 |
} |
| 430 |
case(filter2d::homog):{ |
| 431 |
T1 centreValue=inBuffer[(dimY-1)/2][x]; |
| 432 |
bool isHomog=true; |
| 433 |
stat.eraseNoData(windowBuffer); |
| 434 |
typename std::vector<T1>::const_iterator wit; |
| 435 |
for(wit=windowBuffer.begin();wit!=windowBuffer.end();++wit){ |
| 436 |
if(*wit==centreValue) |
| 437 |
continue; |
| 438 |
else{ |
| 439 |
isHomog=false; |
| 440 |
break; |
| 441 |
} |
| 442 |
} |
| 443 |
if(isHomog) |
| 444 |
outBuffer[x/down]=1; |
| 445 |
else |
| 446 |
outBuffer[x/down]=noDataValue; |
| 447 |
break; |
| 448 |
} |
| 449 |
case(filter2d::heterog):{ |
| 450 |
T1 centreValue=inBuffer[(dimY-1)/2][x]; |
| 451 |
bool isHeterog=true; |
| 452 |
stat.eraseNoData(windowBuffer); |
| 453 |
typename std::vector<T1>::const_iterator wit; |
| 454 |
for(wit=windowBuffer.begin();wit!=windowBuffer.end();++wit){ |
| 455 |
if(*wit!=centreValue) |
| 456 |
continue; |
| 457 |
else{ |
| 458 |
isHeterog=false; |
| 459 |
break; |
| 460 |
} |
| 461 |
} |
| 462 |
if(isHeterog) |
| 463 |
outBuffer[x/down]=1; |
| 464 |
else |
| 465 |
outBuffer[x/down]=noDataValue; |
| 466 |
break; |
| 467 |
} |
| 468 |
case(filter2d::sauvola):{ |
| 469 |
try{ |
| 470 |
double theMean=0; |
| 471 |
double theStdev=0; |
| 472 |
bool invalid=false; |
| 473 |
T1 centreValue=inBuffer[(dimY-1)/2][x]; |
| 474 |
if(windowBuffer.empty()||stat.isNoData(centreValue)){ |
| 475 |
invalid=true; |
| 476 |
throw(invalid); |
| 477 |
} |
| 478 |
stat.meanVar(windowBuffer,theMean,theStdev); |
| 479 |
theStdev=sqrt(theStdev); |
| 480 |
double kValue=0.5; |
| 481 |
double rValue=128; |
| 482 |
if(m_threshold.size()==2){ |
| 483 |
kValue=m_threshold[0]; |
| 484 |
rValue=m_threshold[1]; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
double theThreshold=theMean*(1+kValue*(theStdev/rValue - 1)); |
| 489 |
|
| 490 |
outBuffer[x/down]=(centreValue>theThreshold) ? 1 : noDataValue; |
| 491 |
} |
| 492 |
catch(bool invalid){ |
| 493 |
outBuffer[x/down]=noDataValue; |
| 494 |
} |
| 495 |
break; |
| 496 |
} |
| 497 |
case(filter2d::density):{ |
| 498 |
int nvalid=stat.nvalid(windowBuffer); |
| 499 |
if(nvalid){ |
| 500 |
std::vector<short>::const_iterator vit=m_class.begin(); |
| 501 |
while(vit!=m_class.end()) |
| 502 |
outBuffer[x/down]+=100.0*occurrence[*(vit++)]/nvalid; |
| 503 |
} |
| 504 |
else |
| 505 |
outBuffer[x/down]=noDataValue; |
| 506 |
break; |
| 507 |
} |
| 508 |
case(filter2d::countid):{ |
| 509 |
if(occurrence.size()) |
| 510 |
outBuffer[x/down]=occurrence.size(); |
| 511 |
else |
| 512 |
outBuffer[x/down]=noDataValue; |
| 513 |
break; |
| 514 |
} |
| 515 |
case(filter2d::mode):{ |
| 516 |
if(occurrence.size()){ |
| 517 |
std::map<int,int>::const_iterator maxit=occurrence.begin(); |
| 518 |
for(std::map<int,int>::const_iterator mit=occurrence.begin();mit!=occurrence.end();++mit){ |
| 519 |
if(mit->second>maxit->second) |
| 520 |
maxit=mit; |
| 521 |
} |
| 522 |
if(occurrence[inBuffer[(dimY-1)/2][x]]<maxit->second) |
| 523 |
outBuffer[x/down]=maxit->first; |
| 524 |
else |
| 525 |
outBuffer[x/down]=inBuffer[(dimY-1)/2][x]; |
| 526 |
} |
| 527 |
else |
| 528 |
outBuffer[x/down]=noDataValue; |
| 529 |
break; |
| 530 |
} |
| 531 |
case(filter2d::threshold):{ |
| 532 |
assert(m_class.size()==m_threshold.size()); |
| 533 |
int nvalid=stat.nvalid(windowBuffer); |
| 534 |
if(nvalid>0){ |
| 535 |
outBuffer[x/down]=inBuffer[(dimY-1)/2][x]; |
| 536 |
for(int iclass=0;iclass<m_class.size();++iclass){ |
| 537 |
if(100.0*(occurrence[m_class[iclass]])/nvalid>m_threshold[iclass]) |
| 538 |
outBuffer[x/down]=m_class[iclass]; |
| 539 |
} |
| 540 |
} |
| 541 |
else |
| 542 |
outBuffer[x/down]=noDataValue; |
| 543 |
break; |
| 544 |
} |
| 545 |
case(filter2d::scramble):{ |
| 546 |
if(windowBuffer.size()){ |
| 547 |
int randomIndex=std::rand()%windowBuffer.size(); |
| 548 |
if(randomIndex>=windowBuffer.size()) |
| 549 |
outBuffer[x/down]=windowBuffer.back(); |
| 550 |
else if(randomIndex<0) |
| 551 |
outBuffer[x/down]=windowBuffer[0]; |
| 552 |
else |
| 553 |
outBuffer[x/down]=windowBuffer[randomIndex]; |
| 554 |
} |
| 555 |
else |
| 556 |
outBuffer[x/down]=noDataValue; |
| 557 |
break; |
| 558 |
} |
| 559 |
case(filter2d::mixed):{ |
| 560 |
enum MixType { BF=11, CF=12, MF=13, NF=20, W=30 }; |
| 561 |
double nBF=occurrence[BF]; |
| 562 |
double nCF=occurrence[CF]; |
| 563 |
double nMF=occurrence[MF]; |
| 564 |
double nNF=occurrence[NF]; |
| 565 |
double nW=occurrence[W]; |
| 566 |
if(windowBuffer.size()){ |
| 567 |
if((nBF+nCF+nMF)&&(nBF+nCF+nMF>=nNF+nW)){ |
| 568 |
if(nBF/(nBF+nCF)>=0.75) |
| 569 |
outBuffer[x/down]=BF; |
| 570 |
else if(nCF/(nBF+nCF)>=0.75) |
| 571 |
outBuffer[x/down]=CF; |
| 572 |
else |
| 573 |
outBuffer[x/down]=MF; |
| 574 |
} |
| 575 |
else{ |
| 576 |
if(nW&&(nW>=nNF)) |
| 577 |
outBuffer[x/down]=W; |
| 578 |
else |
| 579 |
outBuffer[x/down]=NF; |
| 580 |
} |
| 581 |
} |
| 582 |
else |
| 583 |
outBuffer[x/down]=inBuffer[indexJ][indexI]; |
| 584 |
break; |
| 585 |
} |
| 586 |
default: |
| 587 |
break; |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
outputVector[y/down]=outBuffer; |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
template<class T> void Filter2d::shift(const Vector2d<T>& input, Vector2d<T>& output, double offsetX, double offsetY, double randomSigma, RESAMPLE resample, bool verbose){ |
| 608 |
output.resize(input.nRows(),input.nCols()); |
| 609 |
const gsl_rng_type *rangenType; |
| 610 |
gsl_rng *rangen; |
| 611 |
gsl_rng_env_setup(); |
| 612 |
rangenType=gsl_rng_default; |
| 613 |
rangen=gsl_rng_alloc(rangenType); |
| 614 |
long seed=time(NULL)*getpid(); |
| 615 |
gsl_rng_set(rangen,seed); |
| 616 |
const char* pszMessage; |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
for(int j=0;j<input.nRows();++j){ |
| 622 |
for(int i=0;i<input.nCols();++i){ |
| 623 |
T theValue=0; |
| 624 |
double randomX=0; |
| 625 |
double randomY=0; |
| 626 |
if(randomSigma>0){ |
| 627 |
randomX=gsl_ran_gaussian(rangen,randomSigma); |
| 628 |
randomY=gsl_ran_gaussian(rangen,randomSigma); |
| 629 |
} |
| 630 |
double readCol=i+offsetX+randomX; |
| 631 |
double readRow=j+offsetY+randomY; |
| 632 |
if(readRow<0) |
| 633 |
readRow=0; |
| 634 |
if(readRow>input.nRows()-1) |
| 635 |
readRow=input.nRows()-1; |
| 636 |
if(readCol<0) |
| 637 |
readCol=0; |
| 638 |
if(readCol>input.nCols()-1) |
| 639 |
readCol=input.nCols()-1; |
| 640 |
switch(resample){ |
| 641 |
case(BILINEAR):{ |
| 642 |
double lowerRow=readRow-0.5; |
| 643 |
double upperRow=readRow+0.5; |
| 644 |
lowerRow=static_cast<int>(lowerRow); |
| 645 |
upperRow=static_cast<int>(upperRow); |
| 646 |
double lowerCol=readCol-0.5; |
| 647 |
double upperCol=readCol+0.5; |
| 648 |
lowerCol=static_cast<int>(lowerCol); |
| 649 |
upperCol=static_cast<int>(upperCol); |
| 650 |
assert(lowerRow>=0); |
| 651 |
assert(lowerRow<input.nRows()); |
| 652 |
assert(lowerCol>=0); |
| 653 |
assert(lowerCol<input.nCols()); |
| 654 |
assert(upperRow>=0); |
| 655 |
assert(upperRow<input.nRows()); |
| 656 |
assert(upperCol>=0); |
| 657 |
if(upperCol>=input.nCols()){ |
| 658 |
std::cout << "upperCol: " << upperCol << std::endl; |
| 659 |
std::cout << "readCol: " << readCol << std::endl; |
| 660 |
std::cout << "readCol+0.5: " << readCol+0.5 << std::endl; |
| 661 |
std::cout << "static_cast<int>(readCol+0.5): " << static_cast<int>(readCol+0.5) << std::endl; |
| 662 |
} |
| 663 |
assert(upperCol<input.nCols()); |
| 664 |
double c00=input[lowerRow][lowerCol]; |
| 665 |
double c11=input[upperRow][upperCol]; |
| 666 |
double c01=input[lowerRow][upperCol]; |
| 667 |
double c10=input[upperRow][lowerCol]; |
| 668 |
double a=(upperCol-readCol)*c00+(readCol-lowerCol)*c01; |
| 669 |
double b=(upperCol-readCol)*c10+(readCol-lowerCol)*c11; |
| 670 |
theValue=(upperRow-readRow)*a+(readRow-lowerRow)*b; |
| 671 |
break; |
| 672 |
} |
| 673 |
default: |
| 674 |
theValue=input[static_cast<int>(readRow)][static_cast<int>(readCol)]; |
| 675 |
break; |
| 676 |
} |
| 677 |
assert(j>=0); |
| 678 |
assert(j<output.nRows()); |
| 679 |
assert(i>=0); |
| 680 |
assert(i<output.nCols()); |
| 681 |
output[j][i]=theValue; |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
} |
| 687 |
gsl_rng_free(rangen); |
| 688 |
} |
| 689 |
|
| 690 |
template<class T> unsigned long int Filter2d::morphology(const Vector2d<T>& input, Vector2d<T>& output, const std::string& method, int dimX, int dimY, bool disc, double hThreshold) |
| 691 |
{ |
| 692 |
const char* pszMessage; |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
double noDataValue=0; |
| 699 |
if(m_noDataValues.size()) |
| 700 |
noDataValue=m_noDataValues[0]; |
| 701 |
|
| 702 |
unsigned long int nchange=0; |
| 703 |
assert(dimX); |
| 704 |
assert(dimY); |
| 705 |
statfactory::StatFactory stat; |
| 706 |
Vector2d<T> inBuffer(dimY,input.nCols()); |
| 707 |
output.clear(); |
| 708 |
output.resize(input.nRows(),input.nCols()); |
| 709 |
int indexI=0; |
| 710 |
int indexJ=0; |
| 711 |
|
| 712 |
for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 713 |
for(int i=0;i<input.nCols();++i) |
| 714 |
inBuffer[indexJ][i]=input[abs(j)][i]; |
| 715 |
++indexJ; |
| 716 |
} |
| 717 |
for(int y=0;y<input.nRows();++y){ |
| 718 |
if(y){ |
| 719 |
|
| 720 |
inBuffer.erase(inBuffer.begin()); |
| 721 |
|
| 722 |
if(y+dimY/2<input.nRows()){ |
| 723 |
|
| 724 |
inBuffer.push_back(inBuffer.back()); |
| 725 |
for(int i=0;i<input.nCols();++i) |
| 726 |
inBuffer[inBuffer.size()-1][i]=input[y+dimY/2][i]; |
| 727 |
} |
| 728 |
else{ |
| 729 |
int over=y+dimY/2-input.nRows(); |
| 730 |
int index=(inBuffer.size()-1)-over; |
| 731 |
assert(index>=0); |
| 732 |
assert(index<inBuffer.size()); |
| 733 |
inBuffer.push_back(inBuffer[index]); |
| 734 |
} |
| 735 |
} |
| 736 |
for(int x=0;x<input.nCols();++x){ |
| 737 |
output[y][x]=0; |
| 738 |
double currentValue=inBuffer[(dimY-1)/2][x]; |
| 739 |
std::vector<double> statBuffer; |
| 740 |
bool currentMasked=false; |
| 741 |
for(int imask=0;imask<m_noDataValues.size();++imask){ |
| 742 |
if(currentValue==m_noDataValues[imask]){ |
| 743 |
currentMasked=true; |
| 744 |
break; |
| 745 |
} |
| 746 |
} |
| 747 |
output[y][x]=currentValue; |
| 748 |
if(currentMasked){ |
| 749 |
output[y][x]=currentValue; |
| 750 |
} |
| 751 |
else{ |
| 752 |
for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 753 |
for(int i=-(dimX-1)/2;i<=dimX/2;++i){ |
| 754 |
double d2=i*i+j*j; |
| 755 |
if(disc&&(d2>(dimX/2)*(dimY/2))) |
| 756 |
continue; |
| 757 |
indexI=x+i; |
| 758 |
|
| 759 |
if(indexI<0) |
| 760 |
indexI=-indexI; |
| 761 |
else if(indexI>=input.nCols()) |
| 762 |
indexI=input.nCols()-i; |
| 763 |
if(y+j<0) |
| 764 |
indexJ=-j; |
| 765 |
else if(y+j>=input.nRows()) |
| 766 |
indexJ=(dimY>2) ? (dimY-1)/2-j : 0; |
| 767 |
else |
| 768 |
indexJ=(dimY-1)/2+j; |
| 769 |
if(inBuffer[indexJ][indexI]==noDataValue) |
| 770 |
continue; |
| 771 |
bool masked=false; |
| 772 |
for(int imask=0;imask<m_noDataValues.size();++imask){ |
| 773 |
if(inBuffer[indexJ][indexI]==m_noDataValues[imask]){ |
| 774 |
masked=true; |
| 775 |
break; |
| 776 |
} |
| 777 |
} |
| 778 |
if(!masked){ |
| 779 |
short binValue=0; |
| 780 |
for(int iclass=0;iclass<m_class.size();++iclass){ |
| 781 |
if(inBuffer[indexJ][indexI]==m_class[iclass]){ |
| 782 |
binValue=1; |
| 783 |
break; |
| 784 |
} |
| 785 |
} |
| 786 |
if(m_class.size()) |
| 787 |
statBuffer.push_back(binValue); |
| 788 |
else |
| 789 |
statBuffer.push_back(inBuffer[indexJ][indexI]); |
| 790 |
} |
| 791 |
} |
| 792 |
} |
| 793 |
if(statBuffer.size()){ |
| 794 |
switch(getFilterType(method)){ |
| 795 |
case(filter2d::dilate): |
| 796 |
if(output[y][x]<stat.mymax(statBuffer)-hThreshold){ |
| 797 |
output[y][x]=stat.mymax(statBuffer); |
| 798 |
++nchange; |
| 799 |
} |
| 800 |
break; |
| 801 |
case(filter2d::erode): |
| 802 |
if(output[y][x]<stat.mymin(statBuffer)+hThreshold){ |
| 803 |
output[y][x]=stat.mymin(statBuffer); |
| 804 |
++nchange; |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
break; |
| 819 |
default: |
| 820 |
std::ostringstream ess; |
| 821 |
ess << "Error: morphology method " << method << " not supported, choose " << filter2d::dilate << " (dilate) or " << filter2d::erode << " (erode)" << std::endl; |
| 822 |
throw(ess.str()); |
| 823 |
break; |
| 824 |
} |
| 825 |
if(output[y][x]&&m_class.size()) |
| 826 |
output[y][x]=m_class[0]; |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
} |
| 832 |
else |
| 833 |
output[y][x]=noDataValue; |
| 834 |
} |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
} |
| 840 |
return nchange; |
| 841 |
} |
| 842 |
|
| 843 |
template<class T> unsigned long int Filter2d::dsm2dtm_nwse(const Vector2d<T>& inputDSM, Vector2d<T>& outputMask, double hThreshold, int nlimit, int dim) |
| 844 |
{ |
| 845 |
const char* pszMessage; |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
Vector2d<T> tmpDSM(inputDSM); |
| 852 |
double noDataValue=0; |
| 853 |
if(m_noDataValues.size()) |
| 854 |
noDataValue=m_noDataValues[0]; |
| 855 |
|
| 856 |
unsigned long int nchange=0; |
| 857 |
int dimX=dim; |
| 858 |
int dimY=dim; |
| 859 |
assert(dimX); |
| 860 |
assert(dimY); |
| 861 |
statfactory::StatFactory stat; |
| 862 |
Vector2d<T> inBuffer(dimY,inputDSM.nCols()); |
| 863 |
if(outputMask.size()!=inputDSM.nRows()) |
| 864 |
outputMask.resize(inputDSM.nRows()); |
| 865 |
int indexI=0; |
| 866 |
int indexJ=0; |
| 867 |
|
| 868 |
for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 869 |
for(int i=0;i<inputDSM.nCols();++i) |
| 870 |
inBuffer[indexJ][i]=tmpDSM[abs(j)][i]; |
| 871 |
++indexJ; |
| 872 |
} |
| 873 |
for(int y=0;y<tmpDSM.nRows();++y){ |
| 874 |
if(y){ |
| 875 |
|
| 876 |
inBuffer.erase(inBuffer.begin()); |
| 877 |
|
| 878 |
if(y+dimY/2<tmpDSM.nRows()){ |
| 879 |
|
| 880 |
inBuffer.push_back(inBuffer.back()); |
| 881 |
for(int i=0;i<tmpDSM.nCols();++i) |
| 882 |
inBuffer[inBuffer.size()-1][i]=tmpDSM[y+dimY/2][i]; |
| 883 |
} |
| 884 |
else{ |
| 885 |
int over=y+dimY/2-tmpDSM.nRows(); |
| 886 |
int index=(inBuffer.size()-1)-over; |
| 887 |
assert(index>=0); |
| 888 |
assert(index<inBuffer.size()); |
| 889 |
inBuffer.push_back(inBuffer[index]); |
| 890 |
} |
| 891 |
} |
| 892 |
for(int x=0;x<tmpDSM.nCols();++x){ |
| 893 |
double centerValue=inBuffer[(dimY-1)/2][x]; |
| 894 |
short nmasked=0; |
| 895 |
std::vector<T> neighbors; |
| 896 |
for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 897 |
for(int i=-(dimX-1)/2;i<=dimX/2;++i){ |
| 898 |
indexI=x+i; |
| 899 |
|
| 900 |
if(indexI<0) |
| 901 |
indexI=-indexI; |
| 902 |
else if(indexI>=tmpDSM.nCols()) |
| 903 |
indexI=tmpDSM.nCols()-i; |
| 904 |
if(y+j<0) |
| 905 |
indexJ=-j; |
| 906 |
else if(y+j>=tmpDSM.nRows()) |
| 907 |
indexJ=(dimY>2) ? (dimY-1)/2-j : 0; |
| 908 |
else |
| 909 |
indexJ=(dimY-1)/2+j; |
| 910 |
double difference=(centerValue-inBuffer[indexJ][indexI]); |
| 911 |
if(i||j) |
| 912 |
neighbors.push_back(inBuffer[indexJ][indexI]); |
| 913 |
if(difference>hThreshold) |
| 914 |
++nmasked; |
| 915 |
} |
| 916 |
} |
| 917 |
if(nmasked<=nlimit){ |
| 918 |
++nchange; |
| 919 |
|
| 920 |
outputMask[y][x]=0; |
| 921 |
} |
| 922 |
else{ |
| 923 |
|
| 924 |
sort(neighbors.begin(),neighbors.end()); |
| 925 |
assert(neighbors.size()>1); |
| 926 |
inBuffer[(dimY-1)/2][x]=neighbors[1]; |
| 927 |
|
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
} |
| 934 |
return nchange; |
| 935 |
} |
| 936 |
|
| 937 |
template<class T> unsigned long int Filter2d::dsm2dtm_nesw(const Vector2d<T>& inputDSM, Vector2d<T>& outputMask, double hThreshold, int nlimit, int dim) |
| 938 |
{ |
| 939 |
const char* pszMessage; |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
Vector2d<T> tmpDSM(inputDSM); |
| 946 |
double noDataValue=0; |
| 947 |
if(m_noDataValues.size()) |
| 948 |
noDataValue=m_noDataValues[0]; |
| 949 |
|
| 950 |
unsigned long int nchange=0; |
| 951 |
int dimX=dim; |
| 952 |
int dimY=dim; |
| 953 |
assert(dimX); |
| 954 |
assert(dimY); |
| 955 |
statfactory::StatFactory stat; |
| 956 |
Vector2d<T> inBuffer(dimY,inputDSM.nCols()); |
| 957 |
if(outputMask.size()!=inputDSM.nRows()) |
| 958 |
outputMask.resize(inputDSM.nRows()); |
| 959 |
int indexI=0; |
| 960 |
int indexJ=0; |
| 961 |
|
| 962 |
for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 963 |
for(int i=0;i<inputDSM.nCols();++i) |
| 964 |
inBuffer[indexJ][i]=tmpDSM[abs(j)][i]; |
| 965 |
++indexJ; |
| 966 |
} |
| 967 |
for(int y=0;y<tmpDSM.nRows();++y){ |
| 968 |
if(y){ |
| 969 |
|
| 970 |
inBuffer.erase(inBuffer.begin()); |
| 971 |
|
| 972 |
if(y+dimY/2<tmpDSM.nRows()){ |
| 973 |
|
| 974 |
inBuffer.push_back(inBuffer.back()); |
| 975 |
for(int i=0;i<tmpDSM.nCols();++i) |
| 976 |
inBuffer[inBuffer.size()-1][i]=tmpDSM[y+dimY/2][i]; |
| 977 |
} |
| 978 |
else{ |
| 979 |
int over=y+dimY/2-tmpDSM.nRows(); |
| 980 |
int index=(inBuffer.size()-1)-over; |
| 981 |
assert(index>=0); |
| 982 |
assert(index<inBuffer.size()); |
| 983 |
inBuffer.push_back(inBuffer[index]); |
| 984 |
} |
| 985 |
} |
| 986 |
for(int x=tmpDSM.nCols()-1;x>=0;--x){ |
| 987 |
double centerValue=inBuffer[(dimY-1)/2][x]; |
| 988 |
short nmasked=0; |
| 989 |
std::vector<T> neighbors; |
| 990 |
for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 991 |
for(int i=-(dimX-1)/2;i<=dimX/2;++i){ |
| 992 |
indexI=x+i; |
| 993 |
|
| 994 |
if(indexI<0) |
| 995 |
indexI=-indexI; |
| 996 |
else if(indexI>=tmpDSM.nCols()) |
| 997 |
indexI=tmpDSM.nCols()-i; |
| 998 |
if(y+j<0) |
| 999 |
indexJ=-j; |
| 1000 |
else if(y+j>=tmpDSM.nRows()) |
| 1001 |
indexJ=(dimY>2) ? (dimY-1)/2-j : 0; |
| 1002 |
else |
| 1003 |
indexJ=(dimY-1)/2+j; |
| 1004 |
double difference=(centerValue-inBuffer[indexJ][indexI]); |
| 1005 |
if(i||j) |
| 1006 |
neighbors.push_back(inBuffer[indexJ][indexI]); |
| 1007 |
if(difference>hThreshold) |
| 1008 |
++nmasked; |
| 1009 |
} |
| 1010 |
} |
| 1011 |
if(nmasked<=nlimit){ |
| 1012 |
++nchange; |
| 1013 |
|
| 1014 |
outputMask[y][x]=0; |
| 1015 |
} |
| 1016 |
else{ |
| 1017 |
|
| 1018 |
sort(neighbors.begin(),neighbors.end()); |
| 1019 |
assert(neighbors.size()>1); |
| 1020 |
inBuffer[(dimY-1)/2][x]=neighbors[1]; |
| 1021 |
|
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
} |
| 1028 |
return nchange; |
| 1029 |
} |
| 1030 |
|
| 1031 |
template<class T> unsigned long int Filter2d::dsm2dtm_senw(const Vector2d<T>& inputDSM, Vector2d<T>& outputMask, double hThreshold, int nlimit, int dim) |
| 1032 |
{ |
| 1033 |
const char* pszMessage; |
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
Vector2d<T> tmpDSM(inputDSM); |
| 1040 |
double noDataValue=0; |
| 1041 |
if(m_noDataValues.size()) |
| 1042 |
noDataValue=m_noDataValues[0]; |
| 1043 |
|
| 1044 |
unsigned long int nchange=0; |
| 1045 |
int dimX=dim; |
| 1046 |
int dimY=dim; |
| 1047 |
assert(dimX); |
| 1048 |
assert(dimY); |
| 1049 |
statfactory::StatFactory stat; |
| 1050 |
Vector2d<T> inBuffer(dimY,inputDSM.nCols()); |
| 1051 |
if(outputMask.size()!=inputDSM.nRows()) |
| 1052 |
outputMask.resize(inputDSM.nRows()); |
| 1053 |
int indexI=0; |
| 1054 |
int indexJ=inputDSM.nRows()-1; |
| 1055 |
|
| 1056 |
for(int j=inputDSM.nRows()-dimY/2;j<inputDSM.nRows();--j){ |
| 1057 |
for(int i=0;i<inputDSM.nCols();++i) |
| 1058 |
inBuffer[indexJ][i]=tmpDSM[abs(j)][i]; |
| 1059 |
++indexJ; |
| 1060 |
} |
| 1061 |
for(int y=tmpDSM.nRows()-1;y>=0;--y){ |
| 1062 |
if(y<tmpDSM.nRows()-1){ |
| 1063 |
|
| 1064 |
inBuffer.erase(inBuffer.end()-1); |
| 1065 |
|
| 1066 |
if(y-dimY/2>0){ |
| 1067 |
|
| 1068 |
inBuffer.insert(inBuffer.begin(),inBuffer.back()); |
| 1069 |
for(int i=0;i<tmpDSM.nCols();++i) |
| 1070 |
inBuffer[0][i]=tmpDSM[y-dimY/2][i]; |
| 1071 |
} |
| 1072 |
else{ |
| 1073 |
inBuffer.insert(inBuffer.begin(),inBuffer[abs(y-dimY/2)]); |
| 1074 |
} |
| 1075 |
} |
| 1076 |
for(int x=tmpDSM.nCols()-1;x>=0;--x){ |
| 1077 |
double centerValue=inBuffer[(dimY-1)/2][x]; |
| 1078 |
short nmasked=0; |
| 1079 |
std::vector<T> neighbors; |
| 1080 |
for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 1081 |
for(int i=-(dimX-1)/2;i<=dimX/2;++i){ |
| 1082 |
indexI=x+i; |
| 1083 |
|
| 1084 |
if(indexI<0) |
| 1085 |
indexI=-indexI; |
| 1086 |
else if(indexI>=tmpDSM.nCols()) |
| 1087 |
indexI=tmpDSM.nCols()-i; |
| 1088 |
if(y+j<0) |
| 1089 |
indexJ=-j; |
| 1090 |
else if(y+j>=tmpDSM.nRows()) |
| 1091 |
indexJ=(dimY>2) ? (dimY-1)/2-j : 0; |
| 1092 |
else |
| 1093 |
indexJ=(dimY-1)/2+j; |
| 1094 |
double difference=(centerValue-inBuffer[indexJ][indexI]); |
| 1095 |
if(i||j) |
| 1096 |
neighbors.push_back(inBuffer[indexJ][indexI]); |
| 1097 |
if(difference>hThreshold) |
| 1098 |
++nmasked; |
| 1099 |
} |
| 1100 |
} |
| 1101 |
if(nmasked<=nlimit){ |
| 1102 |
++nchange; |
| 1103 |
|
| 1104 |
outputMask[y][x]=0; |
| 1105 |
} |
| 1106 |
else{ |
| 1107 |
|
| 1108 |
sort(neighbors.begin(),neighbors.end()); |
| 1109 |
assert(neighbors.size()>1); |
| 1110 |
inBuffer[(dimY-1)/2][x]=neighbors[1]; |
| 1111 |
|
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
} |
| 1118 |
return nchange; |
| 1119 |
} |
| 1120 |
|
| 1121 |
template<class T> unsigned long int Filter2d::dsm2dtm_swne(const Vector2d<T>& inputDSM, Vector2d<T>& outputMask, double hThreshold, int nlimit, int dim) |
| 1122 |
{ |
| 1123 |
const char* pszMessage; |
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
Vector2d<T> tmpDSM(inputDSM); |
| 1130 |
double noDataValue=0; |
| 1131 |
if(m_noDataValues.size()) |
| 1132 |
noDataValue=m_noDataValues[0]; |
| 1133 |
|
| 1134 |
unsigned long int nchange=0; |
| 1135 |
int dimX=dim; |
| 1136 |
int dimY=dim; |
| 1137 |
assert(dimX); |
| 1138 |
assert(dimY); |
| 1139 |
statfactory::StatFactory stat; |
| 1140 |
Vector2d<T> inBuffer(dimY,inputDSM.nCols()); |
| 1141 |
if(outputMask.size()!=inputDSM.nRows()) |
| 1142 |
outputMask.resize(inputDSM.nRows()); |
| 1143 |
int indexI=0; |
| 1144 |
int indexJ=0; |
| 1145 |
|
| 1146 |
for(int j=inputDSM.nRows()-dimY/2;j<inputDSM.nRows();--j){ |
| 1147 |
for(int i=0;i<inputDSM.nCols();++i) |
| 1148 |
inBuffer[indexJ][i]=tmpDSM[abs(j)][i]; |
| 1149 |
++indexJ; |
| 1150 |
} |
| 1151 |
for(int y=tmpDSM.nRows()-1;y>=0;--y){ |
| 1152 |
if(y<tmpDSM.nRows()-1){ |
| 1153 |
|
| 1154 |
inBuffer.erase(inBuffer.end()-1); |
| 1155 |
|
| 1156 |
if(y-dimY/2>0){ |
| 1157 |
|
| 1158 |
inBuffer.insert(inBuffer.begin(),inBuffer.back()); |
| 1159 |
for(int i=0;i<tmpDSM.nCols();++i) |
| 1160 |
inBuffer[0][i]=tmpDSM[y-dimY/2][i]; |
| 1161 |
} |
| 1162 |
else{ |
| 1163 |
inBuffer.insert(inBuffer.begin(),inBuffer[abs(y-dimY/2)]); |
| 1164 |
} |
| 1165 |
} |
| 1166 |
for(int x=0;x<tmpDSM.nCols();++x){ |
| 1167 |
double centerValue=inBuffer[(dimY-1)/2][x]; |
| 1168 |
short nmasked=0; |
| 1169 |
std::vector<T> neighbors; |
| 1170 |
for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 1171 |
for(int i=-(dimX-1)/2;i<=dimX/2;++i){ |
| 1172 |
indexI=x+i; |
| 1173 |
|
| 1174 |
if(indexI<0) |
| 1175 |
indexI=-indexI; |
| 1176 |
else if(indexI>=tmpDSM.nCols()) |
| 1177 |
indexI=tmpDSM.nCols()-i; |
| 1178 |
if(y+j<0) |
| 1179 |
indexJ=-j; |
| 1180 |
else if(y+j>=tmpDSM.nRows()) |
| 1181 |
indexJ=(dimY>2) ? (dimY-1)/2-j : 0; |
| 1182 |
else |
| 1183 |
indexJ=(dimY-1)/2+j; |
| 1184 |
double difference=(centerValue-inBuffer[indexJ][indexI]); |
| 1185 |
if(i||j) |
| 1186 |
neighbors.push_back(inBuffer[indexJ][indexI]); |
| 1187 |
if(difference>hThreshold) |
| 1188 |
++nmasked; |
| 1189 |
} |
| 1190 |
} |
| 1191 |
if(nmasked<=nlimit){ |
| 1192 |
++nchange; |
| 1193 |
|
| 1194 |
outputMask[y][x]=0; |
| 1195 |
} |
| 1196 |
else{ |
| 1197 |
|
| 1198 |
sort(neighbors.begin(),neighbors.end()); |
| 1199 |
assert(neighbors.size()>1); |
| 1200 |
inBuffer[(dimY-1)/2][x]=neighbors[1]; |
| 1201 |
|
| 1202 |
} |
| 1203 |
} |
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
} |
| 1208 |
return nchange; |
| 1209 |
} |
| 1210 |
|
| 1211 |
template<class T> void Filter2d::dsm2shadow(const Vector2d<T>& input, Vector2d<T>& output, double sza, double saa, double pixelSize, short shadowFlag) |
| 1212 |
{ |
| 1213 |
unsigned int ncols=input.nCols(); |
| 1214 |
output.clear(); |
| 1215 |
output.resize(input.nRows(),ncols); |
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
int indexI=0; |
| 1221 |
int indexJ=0; |
| 1222 |
const char* pszMessage; |
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
for(int y=0;y<input.nRows();++y){ |
| 1228 |
for(int x=0;x<input.nCols();++x){ |
| 1229 |
double currentValue=input[y][x]; |
| 1230 |
int theDist=static_cast<int>(sqrt((currentValue*tan(DEG2RAD(sza))/pixelSize)*(currentValue*tan(DEG2RAD(sza))/pixelSize))); |
| 1231 |
double theDir=DEG2RAD(saa)+PI/2.0; |
| 1232 |
if(theDir<0) |
| 1233 |
theDir+=2*PI; |
| 1234 |
for(int d=0;d<theDist;++d){ |
| 1235 |
indexI=x+d*cos(theDir); |
| 1236 |
indexJ=y+d*sin(theDir); |
| 1237 |
if(indexJ<0||indexJ>=input.size()) |
| 1238 |
continue; |
| 1239 |
if(indexI<0||indexI>=input[indexJ].size()) |
| 1240 |
continue; |
| 1241 |
if(input[indexJ][indexI]<currentValue-d*pixelSize/tan(DEG2RAD(sza))){ |
| 1242 |
output[indexJ][indexI]=shadowFlag; |
| 1243 |
} |
| 1244 |
} |
| 1245 |
} |
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
} |
| 1250 |
} |
| 1251 |
|
| 1252 |
template<class T> void Filter2d::dwtForward(Vector2d<T>& theBuffer, const std::string& wavelet_type, int family){ |
| 1253 |
const char* pszMessage; |
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
int nRow=theBuffer.size(); |
| 1260 |
assert(nRow); |
| 1261 |
int nCol=theBuffer[0].size(); |
| 1262 |
assert(nCol); |
| 1263 |
|
| 1264 |
while(theBuffer.size()&(theBuffer.size()-1)) |
| 1265 |
theBuffer.push_back(theBuffer.back()); |
| 1266 |
for(int irow=0;irow<theBuffer.size();++irow) |
| 1267 |
while(theBuffer[irow].size()&(theBuffer[irow].size()-1)) |
| 1268 |
theBuffer[irow].push_back(theBuffer[irow].back()); |
| 1269 |
std::vector<double> vdata(theBuffer.size()*theBuffer[0].size()); |
| 1270 |
double* data=&(vdata[0]); |
| 1271 |
for(int irow=0;irow<theBuffer.size();++irow){ |
| 1272 |
for(int icol=0;icol<theBuffer[0].size();++icol){ |
| 1273 |
int index=irow*theBuffer[0].size()+icol; |
| 1274 |
data[index]=theBuffer[irow][icol]; |
| 1275 |
} |
| 1276 |
} |
| 1277 |
int nsize=theBuffer.size()*theBuffer[0].size(); |
| 1278 |
gsl_wavelet *w; |
| 1279 |
gsl_wavelet_workspace *work; |
| 1280 |
assert(nsize); |
| 1281 |
w=gsl_wavelet_alloc(filter::Filter::getWaveletType(wavelet_type),family); |
| 1282 |
work=gsl_wavelet_workspace_alloc(nsize); |
| 1283 |
gsl_wavelet2d_nstransform_forward (w, data, theBuffer.size(), theBuffer.size(),theBuffer[0].size(), work); |
| 1284 |
theBuffer.erase(theBuffer.begin()+nRow,theBuffer.end()); |
| 1285 |
for(int irow=0;irow<theBuffer.size();++irow){ |
| 1286 |
theBuffer[irow].erase(theBuffer[irow].begin()+nCol,theBuffer[irow].end()); |
| 1287 |
for(int icol=0;icol<theBuffer[irow].size();++icol){ |
| 1288 |
int index=irow*theBuffer[irow].size()+icol; |
| 1289 |
theBuffer[irow][icol]=data[index]; |
| 1290 |
} |
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
} |
| 1295 |
gsl_wavelet_free (w); |
| 1296 |
gsl_wavelet_workspace_free (work); |
| 1297 |
} |
| 1298 |
|
| 1299 |
template<class T> void Filter2d::dwtInverse(Vector2d<T>& theBuffer, const std::string& wavelet_type, int family){ |
| 1300 |
const char* pszMessage; |
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
int nRow=theBuffer.size(); |
| 1307 |
assert(nRow); |
| 1308 |
int nCol=theBuffer[0].size(); |
| 1309 |
assert(nCol); |
| 1310 |
|
| 1311 |
while(theBuffer.size()&(theBuffer.size()-1)) |
| 1312 |
theBuffer.push_back(theBuffer.back()); |
| 1313 |
for(int irow=0;irow<theBuffer.size();++irow) |
| 1314 |
while(theBuffer[irow].size()&(theBuffer[irow].size()-1)) |
| 1315 |
theBuffer[irow].push_back(theBuffer[irow].back()); |
| 1316 |
std::vector<double> vdata(theBuffer.size()*theBuffer[0].size()); |
| 1317 |
double* data=&(vdata[0]); |
| 1318 |
|
| 1319 |
for(int irow=0;irow<theBuffer.size();++irow){ |
| 1320 |
for(int icol=0;icol<theBuffer[0].size();++icol){ |
| 1321 |
int index=irow*theBuffer[0].size()+icol; |
| 1322 |
data[index]=theBuffer[irow][icol]; |
| 1323 |
} |
| 1324 |
} |
| 1325 |
int nsize=theBuffer.size()*theBuffer[0].size(); |
| 1326 |
gsl_wavelet *w; |
| 1327 |
gsl_wavelet_workspace *work; |
| 1328 |
assert(nsize); |
| 1329 |
w=gsl_wavelet_alloc(filter::Filter::getWaveletType(wavelet_type),family); |
| 1330 |
work=gsl_wavelet_workspace_alloc(nsize); |
| 1331 |
gsl_wavelet2d_nstransform_inverse (w, data, theBuffer.size(), theBuffer.size(),theBuffer[0].size(), work); |
| 1332 |
theBuffer.erase(theBuffer.begin()+nRow,theBuffer.end()); |
| 1333 |
for(int irow=0;irow<theBuffer.size();++irow){ |
| 1334 |
theBuffer[irow].erase(theBuffer[irow].begin()+nCol,theBuffer[irow].end()); |
| 1335 |
for(int icol=0;icol<theBuffer[irow].size();++icol){ |
| 1336 |
int index=irow*theBuffer[irow].size()+icol; |
| 1337 |
theBuffer[irow][icol]=data[index]; |
| 1338 |
} |
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
} |
| 1343 |
gsl_wavelet_free (w); |
| 1344 |
gsl_wavelet_workspace_free (work); |
| 1345 |
} |
| 1346 |
|
| 1347 |
template<class T> void Filter2d::dwtCut(Vector2d<T>& theBuffer, const std::string& wavelet_type, int family, double cut){ |
| 1348 |
const char* pszMessage; |
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
int nRow=theBuffer.size(); |
| 1355 |
assert(nRow); |
| 1356 |
int nCol=theBuffer[0].size(); |
| 1357 |
assert(nCol); |
| 1358 |
|
| 1359 |
while(theBuffer.size()&(theBuffer.size()-1)) |
| 1360 |
theBuffer.push_back(theBuffer.back()); |
| 1361 |
for(int irow=0;irow<theBuffer.size();++irow) |
| 1362 |
while(theBuffer[irow].size()&(theBuffer[irow].size()-1)) |
| 1363 |
theBuffer[irow].push_back(theBuffer[irow].back()); |
| 1364 |
double* data=new double[theBuffer.size()*theBuffer[0].size()]; |
| 1365 |
double* abscoeff=new double[theBuffer.size()*theBuffer[0].size()]; |
| 1366 |
size_t* p=new size_t[theBuffer.size()*theBuffer[0].size()]; |
| 1367 |
for(int irow=0;irow<theBuffer.size();++irow){ |
| 1368 |
for(int icol=0;icol<theBuffer[0].size();++icol){ |
| 1369 |
int index=irow*theBuffer[0].size()+icol; |
| 1370 |
assert(index<theBuffer.size()*theBuffer[0].size()); |
| 1371 |
data[index]=theBuffer[irow][icol]; |
| 1372 |
} |
| 1373 |
} |
| 1374 |
int nsize=theBuffer.size()*theBuffer[0].size(); |
| 1375 |
gsl_wavelet *w; |
| 1376 |
gsl_wavelet_workspace *work; |
| 1377 |
assert(nsize); |
| 1378 |
w=gsl_wavelet_alloc(filter::Filter::getWaveletType(wavelet_type),family); |
| 1379 |
work=gsl_wavelet_workspace_alloc(nsize); |
| 1380 |
gsl_wavelet2d_nstransform_forward (w, data, theBuffer.size(), theBuffer[0].size(),theBuffer[0].size(), work); |
| 1381 |
for(int irow=0;irow<theBuffer.size();++irow){ |
| 1382 |
for(int icol=0;icol<theBuffer[0].size();++icol){ |
| 1383 |
int index=irow*theBuffer[0].size()+icol; |
| 1384 |
abscoeff[index]=fabs(data[index]); |
| 1385 |
} |
| 1386 |
} |
| 1387 |
int nc=(100-cut)/100.0*nsize; |
| 1388 |
gsl_sort_index(p,abscoeff,1,nsize); |
| 1389 |
for(int i=0;(i+nc)<nsize;i++) |
| 1390 |
data[p[i]]=0; |
| 1391 |
gsl_wavelet2d_nstransform_inverse (w, data, theBuffer.size(), theBuffer[0].size(),theBuffer[0].size(), work); |
| 1392 |
for(int irow=0;irow<theBuffer.size();++irow){ |
| 1393 |
for(int icol=0;icol<theBuffer[irow].size();++icol){ |
| 1394 |
int index=irow*theBuffer[irow].size()+icol; |
| 1395 |
theBuffer[irow][icol]=data[index]; |
| 1396 |
} |
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
} |
| 1401 |
theBuffer.erase(theBuffer.begin()+nRow,theBuffer.end()); |
| 1402 |
for(int irow=0;irow<theBuffer.size();++irow) |
| 1403 |
theBuffer[irow].erase(theBuffer[irow].begin()+nCol,theBuffer[irow].end()); |
| 1404 |
delete[] data; |
| 1405 |
delete[] abscoeff; |
| 1406 |
delete[] p; |
| 1407 |
gsl_wavelet_free (w); |
| 1408 |
gsl_wavelet_workspace_free (work); |
| 1409 |
|
| 1410 |
} |
| 1411 |
|
| 1412 |
} |
| 1413 |
|
| 1414 |
#endif |