| 1 | /********************************************************************** |
| 2 | Filter2d.cc: class for filtering images |
| 3 | Author(s): [email protected] |
| 4 | Copyright (C) 2016-2022 European Union (Joint Research Centre) |
| 5 | |
| 6 | This file is part of jiplib. |
| 7 | |
| 8 | jiplib is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | jiplib is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with jiplib. If not, see <https://www.gnu.org/licenses/>. |
| 20 | ***********************************************************************/ |
| 21 | #include <sstream> |
| 22 | #include <iomanip> |
| 23 | #include <iostream> |
| 24 | #include <cmath> |
| 25 | #include "Filter2d.h" |
| 26 | #include "StatFactory.h" |
| 27 | |
| 28 | filter2d::Filter2d::Filter2d(void) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | filter2d::Filter2d::Filter2d(const Vector2d<double> &taps, bool flip) |
| 33 | { |
| 34 | setTaps(taps,flip); |
| 35 | } |
| 36 | |
| 37 | int filter2d::Filter2d::pushNoDataValue(double noDataValue) |
| 38 | { |
| 39 | if(find(m_noDataValues.begin(),m_noDataValues.end(),noDataValue)==m_noDataValues.end()) |
| 40 | m_noDataValues.push_back(noDataValue); |
| 41 | return(m_noDataValues.size()); |
| 42 | } |
| 43 | |
| 44 | void filter2d::Filter2d::setTaps(const Vector2d<double> &taps, bool flip) |
| 45 | { |
| 46 | size_t dimY=taps.size(); |
| 47 | if(dimY<=0){ |
| 48 | std::string errorString="Error: empty taps"; |
| 49 | throw(errorString); |
| 50 | } |
| 51 | size_t dimX=taps[0].size(); |
| 52 | m_taps.resize(dimY,dimX); |
| 53 | if(flip){ |
| 54 | for(size_t j=0;j<taps.size();++j){ |
| 55 | for(size_t i=0;i<taps[j].size();++i){ |
| 56 | m_taps[taps.size()-j-1][taps[0].size()-i-1]=taps[j][i]; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | else |
| 61 | m_taps=taps; |
| 62 | } |
| 63 | |
| 64 | void filter2d::Filter2d::smoothNoData(Jim& input, Jim& output, int dim) |
| 65 | { |
| 66 | smoothNoData(input, output,dim,dim); |
| 67 | } |
| 68 | |
| 69 | void filter2d::Filter2d::smooth(Jim& input, Jim& output, int dim) |
| 70 | { |
| 71 | smooth(input, output,dim,dim); |
| 72 | } |
| 73 | |
| 74 | void filter2d::Filter2d::smoothNoData(Jim& input, Jim& output, int dimX, int dimY) |
| 75 | { |
| 76 | m_taps.resize(dimY); |
| 77 | for(int j=0;j<dimY;++j){ |
| 78 | m_taps[j].resize(dimX); |
| 79 | for(int i=0;i<dimX;++i) |
| 80 | m_taps[j][i]=1.0; |
| 81 | } |
| 82 | filterLB(input,output,false,true,true); |
| 83 | } |
| 84 | |
| 85 | void filter2d::Filter2d::smooth(Jim& input, Jim& output, int dimX, int dimY) |
| 86 | { |
| 87 | m_taps.resize(dimY); |
| 88 | for(int j=0;j<dimY;++j){ |
| 89 | m_taps[j].resize(dimX); |
| 90 | for(int i=0;i<dimX;++i) |
| 91 | m_taps[j][i]=1.0; |
| 92 | } |
| 93 | filterLB(input,output,false,true,false); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | void filter2d::Filter2d::filter(Jim& input, Jim& output, bool absolute, bool normalize){ |
| 98 | if(!output.isInit()) |
| 99 | output.open(input,false); |
| 100 | output.setNoData(m_noDataValues); |
| 101 | |
| 102 | size_t dimx=input.nrOfCol(); |
| 103 | size_t dimy=input.nrOfRow(); |
| 104 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 105 | #pragma omp parallel for |
| 106 | #else |
| 107 | #endif |
| 108 | for(size_t iband=0;iband<input.nrOfBand();++iband){ |
| 109 | switch(input.getDataType()){ |
| 110 | case(GDT_Byte): |
| 111 | filter<unsigned char>(static_cast<unsigned char*>(input.getDataPointer(iband)),static_cast<unsigned char*>(output.getDataPointer(iband)), dimx, dimy, absolute,normalize); |
| 112 | break; |
| 113 | case(GDT_Int16): |
| 114 | filter<short>(static_cast<short*>(input.getDataPointer(iband)),static_cast<short*>(output.getDataPointer(iband)), dimx, dimy, absolute,normalize); |
| 115 | break; |
| 116 | case(GDT_UInt16): |
| 117 | filter<unsigned short>(static_cast<unsigned short*>(input.getDataPointer(iband)),static_cast<unsigned short*>(output.getDataPointer(iband)), dimx, dimy, absolute,normalize); |
| 118 | break; |
| 119 | case(GDT_Int32): |
| 120 | filter<int>(static_cast<int*>(input.getDataPointer(iband)),static_cast<int*>(output.getDataPointer(iband)), dimx, dimy, absolute,normalize); |
| 121 | break; |
| 122 | case(GDT_UInt32): |
| 123 | filter<unsigned int>(static_cast<unsigned int*>(input.getDataPointer(iband)),static_cast<unsigned int*>(output.getDataPointer(iband)), dimx, dimy, absolute,normalize); |
| 124 | break; |
| 125 | case(GDT_Float32): |
| 126 | filter<float>(static_cast<float*>(input.getDataPointer(iband)),static_cast<float*>(output.getDataPointer(iband)), dimx, dimy, absolute,normalize); |
| 127 | break; |
| 128 | case(GDT_Float64): |
| 129 | filter<double>(static_cast<double*>(input.getDataPointer(iband)),static_cast<double*>(output.getDataPointer(iband)), dimx, dimy, absolute,normalize); |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | //line based version |
| 136 | void filter2d::Filter2d::filterLB(Jim& input, Jim& output, bool absolute, bool normalize, bool noData) |
| 137 | { |
| 138 | if(!output.isInit()) |
| 139 | output.open(input); |
| 140 | output.setNoData(m_noDataValues); |
| 141 | |
| 142 | int dimX=m_taps[0].size();//horizontal!!! |
| 143 | int dimY=m_taps.size();//vertical!!! |
| 144 | const char* pszMessage; |
| 145 | // void* pProgressArg=NULL; |
| 146 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 147 | // double progress=0; |
| 148 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 149 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 150 | #pragma omp parallel for |
| 151 | #else |
| 152 | #endif |
| 153 | for(unsigned int iband=0;iband<input.nrOfBand();++iband){ |
| 154 | Vector2d<double> inBuffer(dimY,input.nrOfCol()); |
| 155 | std::vector<double> outBuffer(input.nrOfCol()); |
| 156 | int indexI=0; |
| 157 | int indexJ=0; |
| 158 | //initialize last half of inBuffer |
| 159 | for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 160 | try{ |
| 161 | input.readData(inBuffer[indexJ],static_cast<unsigned int>(abs(j)),iband); |
| 162 | } |
| 163 | catch(std::string errorstring){ |
| 164 | std::cerr << errorstring << "in line " << indexJ << std::endl; |
| 165 | } |
| 166 | ++indexJ; |
| 167 | } |
| 168 | |
| 169 | for(unsigned int y=0;y<input.nrOfRow();++y){ |
| 170 | if(y){//inBuffer already initialized for y=0 |
| 171 | //erase first line from inBuffer |
| 172 | if(dimY>1) |
| 173 | inBuffer.erase(inBuffer.begin()); |
| 174 | //read extra line and push back to inBuffer if not out of bounds |
| 175 | if(y+dimY/2<input.nrOfRow()){ |
| 176 | //allocate buffer |
| 177 | if(dimY>1) |
| 178 | inBuffer.push_back(inBuffer.back()); |
| 179 | try{ |
| 180 | input.readData(inBuffer[inBuffer.size()-1],y+dimY/2,iband); |
| 181 | } |
| 182 | catch(std::string errorstring){ |
| 183 | std::cerr << errorstring << "in band " << iband << ", line " << y << std::endl; |
| 184 | exit(1); |
| 185 | } |
| 186 | } |
| 187 | else{ |
| 188 | int over=y+dimY/2-input.nrOfRow(); |
| 189 | int index=(inBuffer.size()-1)-over; |
| 190 | assert(index>=0); |
| 191 | assert(index<inBuffer.size()); |
| 192 | inBuffer.push_back(inBuffer[index]); |
| 193 | } |
| 194 | } |
| 195 | for(int x=0;x<input.nrOfCol();++x){ |
| 196 | outBuffer[x]=0; |
| 197 | double norm=0; |
| 198 | bool masked=false; |
| 199 | if(noData){//only filter noData values |
| 200 | for(int imask=0;imask<m_noDataValues.size();++imask){ |
| 201 | if(inBuffer[(dimY-1)/2][x]==m_noDataValues[imask]){ |
| 202 | masked=true; |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | if(!masked){ |
| 207 | outBuffer[x]=inBuffer[(dimY-1)/2][x]; |
| 208 | continue; |
| 209 | } |
| 210 | } |
| 211 | assert(!noData||masked); |
| 212 | for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 213 | for(int i=-(dimX-1)/2;i<=dimX/2;++i){ |
| 214 | indexI=x+i; |
| 215 | indexJ=(dimY-1)/2+j; |
| 216 | //check if out of bounds |
| 217 | if(x<(dimX-1)/2) |
| 218 | indexI=x+abs(i); |
| 219 | else if(x>=input.nrOfCol()-(dimX-1)/2) |
| 220 | indexI=x-abs(i); |
| 221 | if(y<(dimY-1)/2) |
| 222 | indexJ=(dimY-1)/2+abs(j); |
| 223 | else if(y>=input.nrOfRow()-(dimY-1)/2) |
| 224 | indexJ=(dimY-1)/2-abs(j); |
| 225 | //do not take masked values into account |
| 226 | masked=false; |
| 227 | for(int imask=0;imask<m_noDataValues.size();++imask){ |
| 228 | if(inBuffer[indexJ][indexI]==m_noDataValues[imask]){ |
| 229 | masked=true; |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | if(!masked){ |
| 234 | outBuffer[x]+=(m_taps[(dimY-1)/2+j][(dimX-1)/2+i]*inBuffer[indexJ][indexI]); |
| 235 | norm+=m_taps[(dimY-1)/2+j][(dimX-1)/2+i]; |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | if(absolute) |
| 240 | outBuffer[x]=(normalize&&norm)? abs(outBuffer[x])/norm : abs(outBuffer[x]); |
| 241 | else if(normalize&&norm!=0) |
| 242 | outBuffer[x]=outBuffer[x]/norm; |
| 243 | } |
| 244 | //write outBuffer to file |
| 245 | try{ |
| 246 | output.writeData(outBuffer,y,iband); |
| 247 | } |
| 248 | catch(std::string errorstring){ |
| 249 | std::cerr << errorstring << " in band " << iband << ", line " << y << std::endl; |
| 250 | exit(1); |
| 251 | } |
| 252 | // progress=(1.0+y); |
| 253 | // progress+=(output.nrOfRow()*iband); |
| 254 | // progress/=output.nrOfBand()*output.nrOfRow(); |
| 255 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | |
| 261 | void filter2d::Filter2d::majorVoting(Jim& input, Jim& output, int dim, const std::vector<int> &prior) |
| 262 | { |
| 263 | if(!output.isInit()) |
| 264 | output.open(input); |
| 265 | output.setNoData(m_noDataValues); |
| 266 | |
| 267 | const char* pszMessage; |
| 268 | // void* pProgressArg=NULL; |
| 269 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 270 | // double progress=0; |
| 271 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 272 | |
| 273 | bool usePriors=true; |
| 274 | if(prior.empty()){ |
| 275 | std::cout << "no prior information" << std::endl; |
| 276 | usePriors=false; |
| 277 | } |
| 278 | else{ |
| 279 | std::cout << "using priors "; |
| 280 | for(int iclass=0;iclass<prior.size();++iclass) |
| 281 | std::cout << " " << static_cast<short>(prior[iclass]); |
| 282 | std::cout << std::endl; |
| 283 | } |
| 284 | |
| 285 | // Jim input; |
| 286 | // Jim output; |
| 287 | // input.open(inputFilename); |
| 288 | // output.open(outputFilename,input); |
| 289 | int dimX=0;//horizontal!!! |
| 290 | int dimY=0;//vertical!!! |
| 291 | if(dim){ |
| 292 | dimX=dim; |
| 293 | dimY=dim; |
| 294 | } |
| 295 | else{ |
| 296 | dimX=m_taps[0].size(); |
| 297 | dimY=m_taps.size(); |
| 298 | } |
| 299 | |
| 300 | assert(dimX); |
| 301 | assert(dimY); |
| 302 | |
| 303 | Vector2d<double> inBuffer(dimY,input.nrOfCol()); |
| 304 | std::vector<double> outBuffer(input.nrOfCol()); |
| 305 | int indexI=0; |
| 306 | int indexJ=0; |
| 307 | //initialize last half of inBuffer |
| 308 | for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 309 | try{ |
| 310 | input.readData(inBuffer[indexJ],static_cast<unsigned int>(abs(j))); |
| 311 | } |
| 312 | catch(std::string errorstring){ |
| 313 | std::cerr << errorstring << "in line " << indexJ << std::endl; |
| 314 | } |
| 315 | ++indexJ; |
| 316 | } |
| 317 | |
| 318 | for(unsigned int y=0;y<input.nrOfRow();++y){ |
| 319 | if(y){//inBuffer already initialized for y=0 |
| 320 | //erase first line from inBuffer |
| 321 | if(dimY>1) |
| 322 | inBuffer.erase(inBuffer.begin()); |
| 323 | //read extra line and push back to inBuffer if not out of bounds |
| 324 | if(y+dimY/2<input.nrOfRow()){ |
| 325 | //allocate buffer |
| 326 | if(dimY>1) |
| 327 | inBuffer.push_back(inBuffer.back()); |
| 328 | try{ |
| 329 | input.readData(inBuffer[inBuffer.size()-1],y+dimY/2); |
| 330 | } |
| 331 | catch(std::string errorstring){ |
| 332 | std::cerr << errorstring << "in line" << y << std::endl; |
| 333 | } |
| 334 | } |
| 335 | else{ |
| 336 | int over=y+dimY/2-input.nrOfRow(); |
| 337 | int index=(inBuffer.size()-1)-over; |
| 338 | assert(index>=0); |
| 339 | assert(index<inBuffer.size()); |
| 340 | inBuffer.push_back(inBuffer[index]); |
| 341 | } |
| 342 | } |
| 343 | for(unsigned int x=0;x<input.nrOfCol();++x){ |
| 344 | outBuffer[x]=0; |
| 345 | std::map<int,int> occurrence; |
| 346 | // int centre=dimX*(dimY-1)/2+(dimX-1)/2; |
| 347 | for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 348 | for(int i=-(dimX-1)/2;i<=dimX/2;++i){ |
| 349 | indexI=x+i; |
| 350 | //check if out of bounds |
| 351 | if(indexI<0) |
| 352 | indexI=-indexI; |
| 353 | else if(indexI>=input.nrOfCol()) |
| 354 | indexI=input.nrOfCol()-i; |
| 355 | if(y+j<0) |
| 356 | indexJ=-j; |
| 357 | else if(y+j>=input.nrOfRow()) |
| 358 | indexJ=(dimY>2) ? (dimY-1)/2-j : 0; |
| 359 | else |
| 360 | indexJ=(dimY-1)/2+j; |
| 361 | |
| 362 | // if(x<dimX/2) |
| 363 | // indexI=x+abs(i); |
| 364 | // else if(x>=input.nrOfCol()-dimX/2) |
| 365 | // indexI=x-abs(i); |
| 366 | // if(y<dimY/2) |
| 367 | // indexJ=dimY/2+abs(j); |
| 368 | // else if(y>=input.nrOfRow()-dimY/2) |
| 369 | // indexJ=dimY/2-abs(j); |
| 370 | if(usePriors){ |
| 371 | occurrence[inBuffer[indexJ][indexI]]+=prior[inBuffer[indexJ][indexI]-1]; |
| 372 | } |
| 373 | else |
| 374 | ++occurrence[inBuffer[indexJ][indexI]]; |
| 375 | } |
| 376 | } |
| 377 | std::map<int,int>::const_iterator maxit=occurrence.begin(); |
| 378 | for(std::map<int,int>::const_iterator mit=occurrence.begin();mit!=occurrence.end();++mit){ |
| 379 | if(mit->second>maxit->second) |
| 380 | maxit=mit; |
| 381 | } |
| 382 | if(occurrence[inBuffer[(dimY-1)/2][x]]<maxit->second)// |
| 383 | outBuffer[x]=maxit->first; |
| 384 | else//favorize original value in case of ties |
| 385 | outBuffer[x]=inBuffer[(dimY-1)/2][x]; |
| 386 | } |
| 387 | //write outBuffer to file |
| 388 | try{ |
| 389 | output.writeData(outBuffer,y); |
| 390 | } |
| 391 | catch(std::string errorstring){ |
| 392 | std::cerr << errorstring << "in line" << y << std::endl; |
| 393 | } |
| 394 | // progress=(1.0+y)/output.nrOfRow(); |
| 395 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 396 | } |
| 397 | input.close(); |
| 398 | output.close(); |
| 399 | } |
| 400 | |
| 401 | void filter2d::Filter2d::median(Jim& input, Jim& output, int dim, bool disc) |
| 402 | { |
| 403 | doit(input,output,"median",dim,disc); |
| 404 | } |
| 405 | |
| 406 | void filter2d::Filter2d::var(Jim& input, Jim& output, int dim, bool disc) |
| 407 | { |
| 408 | doit(input,output,"var",dim,disc); |
| 409 | } |
| 410 | |
| 411 | void filter2d::Filter2d::doit(Jim& input, Jim& output, const std::string& method, int dim, short down, bool disc) |
| 412 | { |
| 413 | doit(input,output,method,dim,dim,down,disc); |
| 414 | } |
| 415 | |
| 416 | void filter2d::Filter2d::doit(Jim& input, Jim& output, const std::string& method, int dimX, int dimY, short down, bool disc) |
| 417 | { |
| 418 | if(!output.isInit()) |
| 419 | output.open(input); |
| 420 | output.setNoData(m_noDataValues); |
| 421 | |
| 422 | const char* pszMessage; |
| 423 | // void* pProgressArg=NULL; |
| 424 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 425 | // double progress=0; |
| 426 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 427 | |
| 428 | assert(dimX); |
| 429 | assert(dimY); |
| 430 | |
| 431 | statfactory::StatFactory stat; |
| 432 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 433 | #pragma omp parallel for |
| 434 | #else |
| 435 | #endif |
| 436 | for(unsigned int iband=0;iband<input.nrOfBand();++iband){ |
| 437 | Vector2d<double> inBuffer(dimY,input.nrOfCol()); |
| 438 | std::vector<double> outBuffer((input.nrOfCol()+down-1)/down); |
| 439 | int indexI=0; |
| 440 | int indexJ=0; |
| 441 | //initialize last half of inBuffer |
| 442 | for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 443 | try{ |
| 444 | input.readData(inBuffer[indexJ],static_cast<unsigned int>(abs(j)),iband); |
| 445 | } |
| 446 | catch(std::string errorstring){ |
| 447 | std::cerr << errorstring << "in line " << indexJ << std::endl; |
| 448 | } |
| 449 | ++indexJ; |
| 450 | } |
| 451 | for(unsigned int y=0;y<input.nrOfRow();++y){ |
| 452 | if(y){//inBuffer already initialized for y=0 |
| 453 | //erase first line from inBuffer |
| 454 | if(dimY>1) |
| 455 | inBuffer.erase(inBuffer.begin()); |
| 456 | //read extra line and push back to inBuffer if not out of bounds |
| 457 | if(y+dimY/2<input.nrOfRow()){ |
| 458 | //allocate buffer |
| 459 | if(dimY>1) |
| 460 | inBuffer.push_back(inBuffer.back()); |
| 461 | try{ |
| 462 | input.readData(inBuffer[inBuffer.size()-1],y+dimY/2,iband); |
| 463 | } |
| 464 | catch(std::string errorstring){ |
| 465 | std::cerr << errorstring << "in band " << iband << ", line " << y << std::endl; |
| 466 | } |
| 467 | } |
| 468 | else{ |
| 469 | int over=y+dimY/2-input.nrOfRow(); |
| 470 | int index=(inBuffer.size()-1)-over; |
| 471 | assert(index>=0); |
| 472 | assert(index<inBuffer.size()); |
| 473 | inBuffer.push_back(inBuffer[index]); |
| 474 | } |
| 475 | } |
| 476 | if((y+1+down/2)%down) |
| 477 | continue; |
| 478 | for(unsigned int x=0;x<input.nrOfCol();++x){ |
| 479 | if((x+1+down/2)%down) |
| 480 | continue; |
| 481 | outBuffer[x/down]=0; |
| 482 | std::vector<double> windowBuffer; |
| 483 | std::map<long int,int> occurrence; |
| 484 | int centre=dimX*(dimY-1)/2+(dimX-1)/2; |
| 485 | for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 486 | for(int i=-(dimX-1)/2;i<=dimX/2;++i){ |
| 487 | double d2=i*i+j*j;//square distance |
| 488 | if(disc&&(d2>(dimX/2)*(dimY/2))) |
| 489 | continue; |
| 490 | indexI=x+i; |
| 491 | //check if out of bounds |
| 492 | if(indexI<0) |
| 493 | indexI=-indexI; |
| 494 | else if(indexI>=input.nrOfCol()) |
| 495 | indexI=input.nrOfCol()-i; |
| 496 | if(y+j<0) |
| 497 | indexJ=-j; |
| 498 | else if(y+j>=input.nrOfRow()) |
| 499 | indexJ=(dimY>2) ? (dimY-1)/2-j : 0; |
| 500 | else |
| 501 | indexJ=(dimY-1)/2+j; |
| 502 | bool masked=false; |
| 503 | for(int imask=0;imask<m_noDataValues.size();++imask){ |
| 504 | if(inBuffer[indexJ][indexI]==m_noDataValues[imask]){ |
| 505 | masked=true; |
| 506 | break; |
| 507 | } |
| 508 | } |
| 509 | if(!masked){ |
| 510 | std::vector<short>::const_iterator vit=m_class.begin(); |
| 511 | if(!m_class.size()) |
| 512 | ++occurrence[inBuffer[indexJ][indexI]]; |
| 513 | else{ |
| 514 | while(vit!=m_class.end()){ |
| 515 | if(inBuffer[indexJ][indexI]==*(vit++)) |
| 516 | ++occurrence[inBuffer[indexJ][indexI]]; |
| 517 | } |
| 518 | } |
| 519 | windowBuffer.push_back(inBuffer[indexJ][indexI]); |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | switch(getFilterType(method)){ |
| 524 | case(filter2d::nvalid): |
| 525 | outBuffer[x/down]=stat.nvalid(windowBuffer); |
| 526 | break; |
| 527 | case(filter2d::median): |
| 528 | if(windowBuffer.empty()) |
| 529 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 530 | else |
| 531 | outBuffer[x/down]=stat.median(windowBuffer); |
| 532 | break; |
| 533 | case(filter2d::var):{ |
| 534 | if(windowBuffer.empty()) |
| 535 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 536 | else |
| 537 | outBuffer[x/down]=stat.var(windowBuffer); |
| 538 | break; |
| 539 | } |
| 540 | case(filter2d::stdev):{ |
| 541 | if(windowBuffer.empty()) |
| 542 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 543 | else |
| 544 | outBuffer[x/down]=sqrt(stat.var(windowBuffer)); |
| 545 | break; |
| 546 | } |
| 547 | case(filter2d::mean):{ |
| 548 | if(windowBuffer.empty()) |
| 549 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 550 | else |
| 551 | outBuffer[x/down]=stat.mean(windowBuffer); |
| 552 | break; |
| 553 | } |
| 554 | case(filter2d::min):{ |
| 555 | if(windowBuffer.empty()) |
| 556 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 557 | else |
| 558 | outBuffer[x/down]=stat.mymin(windowBuffer); |
| 559 | break; |
| 560 | } |
| 561 | case(filter2d::ismin):{ |
| 562 | if(windowBuffer.empty()) |
| 563 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 564 | else |
| 565 | outBuffer[x/down]=(stat.mymin(windowBuffer)==windowBuffer[centre])? 1:0; |
| 566 | break; |
| 567 | } |
| 568 | case(filter2d::minmax):{//is the same as homog? |
| 569 | double min=0; |
| 570 | double max=0; |
| 571 | if(windowBuffer.empty()) |
| 572 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 573 | else{ |
| 574 | stat.minmax(windowBuffer,windowBuffer.begin(),windowBuffer.end(),min,max); |
| 575 | if(min!=max) |
| 576 | outBuffer[x/down]=0; |
| 577 | else |
| 578 | outBuffer[x/down]=windowBuffer[centre];//centre pixels |
| 579 | } |
| 580 | break; |
| 581 | } |
| 582 | case(filter2d::max):{ |
| 583 | if(windowBuffer.empty()) |
| 584 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 585 | else |
| 586 | outBuffer[x/down]=stat.mymax(windowBuffer); |
| 587 | break; |
| 588 | } |
| 589 | case(filter2d::ismax):{ |
| 590 | if(windowBuffer.empty()) |
| 591 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 592 | else |
| 593 | outBuffer[x/down]=(stat.mymax(windowBuffer)==windowBuffer[centre])? 1:0; |
| 594 | break; |
| 595 | } |
| 596 | case(filter2d::order):{ |
| 597 | if(windowBuffer.empty()) |
| 598 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 599 | else{ |
| 600 | double lbound=0; |
| 601 | double ubound=dimX*dimY; |
| 602 | double theMin=stat.mymin(windowBuffer); |
| 603 | double theMax=stat.mymax(windowBuffer); |
| 604 | double scale=(ubound-lbound)/(theMax-theMin); |
| 605 | outBuffer[x/down]=static_cast<short>(scale*(windowBuffer[centre]-theMin)+lbound); |
| 606 | } |
| 607 | break; |
| 608 | } |
| 609 | case(filter2d::sum):{ |
| 610 | outBuffer[x/down]=stat.sum(windowBuffer); |
| 611 | break; |
| 612 | } |
| 613 | case(filter2d::percentile):{ |
| 614 | assert(m_threshold.size()); |
| 615 | outBuffer[x/down]=stat.percentile(windowBuffer,windowBuffer.begin(),windowBuffer.end(),m_threshold[0]); |
| 616 | break; |
| 617 | } |
| 618 | case(filter2d::proportion):{ |
| 619 | if(windowBuffer.size()){ |
| 620 | double sum=stat.sum(windowBuffer); |
| 621 | if(sum) |
| 622 | outBuffer[x/down]=100.0*windowBuffer[centre]/stat.sum(windowBuffer); |
| 623 | else |
| 624 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 625 | } |
| 626 | else |
| 627 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 628 | break; |
| 629 | } |
| 630 | case(filter2d::homog): |
| 631 | if(occurrence.size()==1)//all values in window are the same |
| 632 | outBuffer[x/down]=inBuffer[(dimY-1)/2][x]; |
| 633 | else |
| 634 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 635 | break; |
| 636 | case(filter2d::heterog):{ |
| 637 | if(occurrence.size()==windowBuffer.size()) |
| 638 | outBuffer[x/down]=inBuffer[(dimY-1)/2][x]; |
| 639 | else |
| 640 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 641 | // if(occurrence.size()==1)//all values in window are the same |
| 642 | // outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 643 | // else |
| 644 | // outBuffer[x/down]=inBuffer[(dimY-1)/2][x]; |
| 645 | // break; |
| 646 | // for(std::vector<double>::const_iterator wit=windowBuffer.begin();wit!=windowBuffer.end();++wit){ |
| 647 | // if(wit==windowBuffer.begin()+windowBuffer.size()/2) |
| 648 | // continue; |
| 649 | // else if(*wit!=inBuffer[(dimY-1)/2][x]){ |
| 650 | // outBuffer[x/down]=1; |
| 651 | // break; |
| 652 | // } |
| 653 | // else if(*wit==inBuffer[(dimY-1)/2][x]){//todo:wit mag niet central pixel zijn |
| 654 | // outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 655 | // break; |
| 656 | // } |
| 657 | // } |
| 658 | // break; |
| 659 | } |
| 660 | case(filter2d::density):{ |
| 661 | if(windowBuffer.size()){ |
| 662 | std::vector<short>::const_iterator vit=m_class.begin(); |
| 663 | while(vit!=m_class.end()) |
| 664 | outBuffer[x/down]+=100.0*occurrence[*(vit++)]/windowBuffer.size(); |
| 665 | } |
| 666 | else |
| 667 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 668 | break; |
| 669 | } |
| 670 | case(filter2d::countid):{ |
| 671 | if(windowBuffer.size()) |
| 672 | outBuffer[x/down]=occurrence.size(); |
| 673 | else |
| 674 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 675 | break; |
| 676 | } |
| 677 | case(filter2d::mode):{ |
| 678 | if(occurrence.size()){ |
| 679 | std::map<long int,int>::const_iterator maxit=occurrence.begin(); |
| 680 | for(std::map<long int,int>::const_iterator mit=occurrence.begin();mit!=occurrence.end();++mit){ |
| 681 | if(mit->second>maxit->second) |
| 682 | maxit=mit; |
| 683 | } |
| 684 | if(occurrence[inBuffer[(dimY-1)/2][x]]<maxit->second)// |
| 685 | outBuffer[x/down]=maxit->first; |
| 686 | else//favorize original value in case of ties |
| 687 | outBuffer[x/down]=inBuffer[(dimY-1)/2][x]; |
| 688 | } |
| 689 | else |
| 690 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 691 | break; |
| 692 | } |
| 693 | case(filter2d::threshold):{ |
| 694 | assert(m_class.size()==m_threshold.size()); |
| 695 | if(windowBuffer.size()){ |
| 696 | outBuffer[x/down]=inBuffer[(dimY-1)/2][x];//initialize with original value (in case thresholds not met) |
| 697 | for(int iclass=0;iclass<m_class.size();++iclass){ |
| 698 | if(100.0*(occurrence[m_class[iclass]])/windowBuffer.size()>m_threshold[iclass]) |
| 699 | outBuffer[x/down]=m_class[iclass]; |
| 700 | } |
| 701 | } |
| 702 | else |
| 703 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 704 | break; |
| 705 | } |
| 706 | case(filter2d::scramble):{//could be done more efficiently window by window with random shuffling entire buffer and assigning entire buffer at once to output image... |
| 707 | if(windowBuffer.size()){ |
| 708 | int randomIndex=std::rand()%windowBuffer.size(); |
| 709 | if(randomIndex>=windowBuffer.size()) |
| 710 | outBuffer[x/down]=windowBuffer.back(); |
| 711 | else if(randomIndex<0) |
| 712 | outBuffer[x/down]=windowBuffer[0]; |
| 713 | else |
| 714 | outBuffer[x/down]=windowBuffer[randomIndex]; |
| 715 | } |
| 716 | else |
| 717 | outBuffer[x/down]=(m_noDataValues.size())? m_noDataValues[0] : 0; |
| 718 | break; |
| 719 | } |
| 720 | case(filter2d::mixed):{ |
| 721 | enum Type { BF=11, CF=12, MF=13, NF=20, W=30 }; |
| 722 | double nBF=occurrence[BF]; |
| 723 | double nCF=occurrence[CF]; |
| 724 | double nMF=occurrence[MF]; |
| 725 | double nNF=occurrence[NF]; |
| 726 | double nW=occurrence[W]; |
| 727 | if(windowBuffer.size()){ |
| 728 | if((nBF+nCF+nMF)&&(nBF+nCF+nMF>=nNF+nW)){//forest |
| 729 | if(nBF/(nBF+nCF)>=0.75) |
| 730 | outBuffer[x/down]=BF; |
| 731 | else if(nCF/(nBF+nCF)>=0.75) |
| 732 | outBuffer[x/down]=CF; |
| 733 | else |
| 734 | outBuffer[x/down]=MF; |
| 735 | } |
| 736 | else{//non-forest |
| 737 | if(nW&&(nW>=nNF)) |
| 738 | outBuffer[x/down]=W; |
| 739 | else |
| 740 | outBuffer[x/down]=NF; |
| 741 | } |
| 742 | } |
| 743 | else |
| 744 | outBuffer[x/down]=inBuffer[indexJ][indexI]; |
| 745 | break; |
| 746 | } |
| 747 | default:{ |
| 748 | std::ostringstream ess; |
| 749 | ess << "Error: filter method " << method << " not supported" << std::endl; |
| 750 | throw(ess.str()); |
| 751 | break; |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | // progress=(1.0+y/down); |
| 756 | // progress+=(output.nrOfRow()*iband); |
| 757 | // progress/=output.nrOfBand()*output.nrOfRow(); |
| 758 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 759 | //write outBuffer to file |
| 760 | try{ |
| 761 | output.writeData(outBuffer,y/down,iband); |
| 762 | } |
| 763 | catch(std::string errorstring){ |
| 764 | std::cerr << errorstring << "in band " << iband << ", line " << y << std::endl; |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | // MyProgressFunc(1.0,pszMessage,pProgressArg); |
| 769 | } |
| 770 | |
| 771 | void filter2d::Filter2d::mrf(Jim& input, Jim& output, int dimX, int dimY, double beta, bool eightConnectivity, short down, bool verbose){ |
| 772 | assert(m_class.size()>1); |
| 773 | Vector2d<double> fullBeta(m_class.size(),m_class.size()); |
| 774 | for(int iclass1=0;iclass1<m_class.size();++iclass1) |
| 775 | for(int iclass2=0;iclass2<m_class.size();++iclass2) |
| 776 | fullBeta[iclass1][iclass2]=beta; |
| 777 | mrf(input,output,dimX,dimY,fullBeta,eightConnectivity,down,verbose); |
| 778 | } |
| 779 | |
| 780 | //beta[classTo][classFrom] |
| 781 | void filter2d::Filter2d::mrf(Jim& input, Jim& output, int dimX, int dimY, Vector2d<double> beta, bool eightConnectivity, short down, bool verbose) |
| 782 | { |
| 783 | if(!output.isInit()) |
| 784 | output.open(input); |
| 785 | output.setNoData(m_noDataValues); |
| 786 | |
| 787 | const char* pszMessage; |
| 788 | // void* pProgressArg=NULL; |
| 789 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 790 | // double progress=0; |
| 791 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 792 | |
| 793 | assert(dimX); |
| 794 | assert(dimY); |
| 795 | |
| 796 | Vector2d<short> inBuffer(dimY,input.nrOfCol()); |
| 797 | Vector2d<double> outBuffer(m_class.size(),(input.nrOfCol()+down-1)/down); |
| 798 | assert(input.nrOfBand()==1); |
| 799 | assert(output.nrOfBand()==m_class.size()); |
| 800 | assert(m_class.size()>1); |
| 801 | assert(beta.size()==m_class.size()); |
| 802 | int indexI=0; |
| 803 | int indexJ=0; |
| 804 | //initialize last half of inBuffer |
| 805 | for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 806 | try{ |
| 807 | input.readData(inBuffer[indexJ],static_cast<unsigned int>(abs(j))); |
| 808 | } |
| 809 | catch(std::string errorstring){ |
| 810 | std::cerr << errorstring << "in line " << indexJ << std::endl; |
| 811 | } |
| 812 | ++indexJ; |
| 813 | } |
| 814 | for(unsigned int y=0;y<input.nrOfRow();++y){ |
| 815 | if(y){//inBuffer already initialized for y=0 |
| 816 | //erase first line from inBuffer |
| 817 | if(dimY>1) |
| 818 | inBuffer.erase(inBuffer.begin()); |
| 819 | //read extra line and push back to inBuffer if not out of bounds |
| 820 | if(y+dimY/2<input.nrOfRow()){ |
| 821 | //allocate buffer |
| 822 | if(dimY>1) |
| 823 | inBuffer.push_back(inBuffer.back()); |
| 824 | try{ |
| 825 | input.readData(inBuffer[inBuffer.size()-1],y+dimY/2); |
| 826 | } |
| 827 | catch(std::string errorstring){ |
| 828 | std::cerr << errorstring << "in line " << y << std::endl; |
| 829 | } |
| 830 | } |
| 831 | else{ |
| 832 | int over=y+dimY/2-input.nrOfRow(); |
| 833 | int index=(inBuffer.size()-1)-over; |
| 834 | assert(index>=0); |
| 835 | assert(index<inBuffer.size()); |
| 836 | inBuffer.push_back(inBuffer[index]); |
| 837 | } |
| 838 | } |
| 839 | if((y+1+down/2)%down) |
| 840 | continue; |
| 841 | for(unsigned int x=0;x<input.nrOfCol();++x){ |
| 842 | if((x+1+down/2)%down) |
| 843 | continue; |
| 844 | std::vector<short> potential(m_class.size()); |
| 845 | for(int iclass=0;iclass<m_class.size();++iclass){ |
| 846 | potential[iclass]=0; |
| 847 | outBuffer[iclass][x/down]=0; |
| 848 | } |
| 849 | std::vector<double> windowBuffer; |
| 850 | // int centre=dimX*(dimY-1)/2+(dimX-1)/2; |
| 851 | for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 852 | for(int i=-(dimX-1)/2;i<=dimX/2;++i){ |
| 853 | if(i!=0&&j!=0&&!eightConnectivity) |
| 854 | continue; |
| 855 | if(i==0&&j==0) |
| 856 | continue; |
| 857 | indexI=x+i; |
| 858 | //check if out of bounds |
| 859 | if(indexI<0) |
| 860 | indexI=-indexI; |
| 861 | else if(indexI>=input.nrOfCol()) |
| 862 | indexI=input.nrOfCol()-i; |
| 863 | if(y+j<0) |
| 864 | indexJ=-j; |
| 865 | else if(y+j>=input.nrOfRow()) |
| 866 | indexJ=(dimY>2) ? (dimY-1)/2-j : 0; |
| 867 | else |
| 868 | indexJ=(dimY-1)/2+j; |
| 869 | bool masked=false; |
| 870 | for(int imask=0;imask<m_noDataValues.size();++imask){ |
| 871 | if(inBuffer[indexJ][indexI]==m_noDataValues[imask]){ |
| 872 | masked=true; |
| 873 | break; |
| 874 | } |
| 875 | } |
| 876 | if(!masked){ |
| 877 | for(int iclass=0;iclass<m_class.size();++iclass){ |
| 878 | if(inBuffer[indexJ][indexI]==m_class[iclass]) |
| 879 | potential[iclass]+=1; |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | double norm=0; |
| 885 | for(int iclass1=0;iclass1<m_class.size();++iclass1){ |
| 886 | assert(beta[iclass1].size()==m_class.size()); |
| 887 | double pot=0; |
| 888 | for(int iclass2=0;iclass2<m_class.size();++iclass2) |
| 889 | if(iclass2!=iclass1) |
| 890 | pot+=potential[iclass2]*beta[iclass1][iclass2]; |
| 891 | double prior=exp(-pot); |
| 892 | outBuffer[iclass1][x/down]=prior; |
| 893 | norm+=prior; |
| 894 | } |
| 895 | if(norm){ |
| 896 | for(int iclass1=0;iclass1<m_class.size();++iclass1) |
| 897 | outBuffer[iclass1][x/down]/=norm; |
| 898 | } |
| 899 | } |
| 900 | // progress=(1.0+y/down)/output.nrOfRow(); |
| 901 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 902 | //write outBuffer to file |
| 903 | assert(outBuffer.size()==m_class.size()); |
| 904 | assert(y<output.nrOfRow()); |
| 905 | for(int iclass=0;iclass<m_class.size();++iclass){ |
| 906 | assert(outBuffer[iclass].size()==output.nrOfCol()); |
| 907 | try{ |
| 908 | output.writeData(outBuffer[iclass],y/down,iclass); |
| 909 | } |
| 910 | catch(std::string errorstring){ |
| 911 | std::cerr << errorstring << "in class " << iclass << ", line " << y << std::endl; |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | //todo: perform on Jim directly instead of copying into Vector2d |
| 918 | void filter2d::Filter2d::shift(Jim& input, Jim& output, double offsetX, double offsetY, double randomSigma, RESAMPLE resample, bool verbose) |
| 919 | { |
| 920 | if(!output.isInit()) |
| 921 | output.open(input); |
| 922 | output.setNoData(m_noDataValues); |
| 923 | |
| 924 | assert(input.nrOfCol()==output.nrOfCol()); |
| 925 | assert(input.nrOfRow()==output.nrOfRow()); |
| 926 | assert(input.nrOfBand()==output.nrOfBand()); |
| 927 | const char* pszMessage; |
| 928 | // void* pProgressArg=NULL; |
| 929 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 930 | // double progress=0; |
| 931 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 932 | //process band per band in memory |
| 933 | Vector2d<double> inBuffer(input.nrOfRow(),output.nrOfCol()); |
| 934 | Vector2d<double> outBuffer(input.nrOfRow(),output.nrOfCol()); |
| 935 | for(int iband=0;iband<input.nrOfBand();++iband){ |
| 936 | input.readDataBlock(inBuffer,0,inBuffer.nCols()-1,0,inBuffer.nRows()-1,iband); |
| 937 | shift(inBuffer,outBuffer,offsetX,offsetY,randomSigma,resample,verbose); |
| 938 | output.writeDataBlock(outBuffer,0,outBuffer.nCols()-1,0,outBuffer.nRows()-1,iband); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | //todo: re-implement without dependency of CImg and reg libraries |
| 943 | // void filter2d::Filter2d::dwt_texture(const std::string& inputFilename, const std::string& outputFilename,int dim, int scale, int down, int iband, bool verbose) |
| 944 | // { |
| 945 | // Jim input; |
| 946 | // Jim output; |
| 947 | // if(verbose) |
| 948 | // std::cout << "opening file " << inputFilename << std::endl; |
| 949 | // input.open(inputFilename); |
| 950 | // double magicX=1,magicY=1; |
| 951 | // output.open(outputFilename,(input.nrOfCol()+down-1)/down,(input.nrOfRow()+down-1)/down,scale*3,GDT_Float32,input.getImageType()); |
| 952 | // if(input.isGeoRef()){ |
| 953 | // output.setProjection(input.getProjection()); |
| 954 | // output.copyGeoTransform(input); |
| 955 | // } |
| 956 | // if(verbose) |
| 957 | // std::cout << "Dimension texture (row x col x band) = " << (input.nrOfCol()+down-1)/down << " x " << (input.nrOfRow()+down-1)/down << " x " << scale*3 << std::endl; |
| 958 | // assert(dim%2); |
| 959 | // int dimX=dim; |
| 960 | // int dimY=dim; |
| 961 | // Vector2d<float> inBuffer(dimY,input.nrOfCol()); |
| 962 | // Vector2d<float> outBuffer(scale*3,(input.nrOfCol()+down-1)/down); |
| 963 | // //initialize last half of inBuffer |
| 964 | // int indexI=0; |
| 965 | // int indexJ=0; |
| 966 | // for(int j=-dimY/2;j<(dimY+1)/2;++j){ |
| 967 | // try{ |
| 968 | // if(verbose) |
| 969 | // cout << "reading input line " << abs(j) << std::endl; |
| 970 | // input.readData(inBuffer[indexJ],GDT_Float32,abs(j),iband); |
| 971 | // ++indexJ; |
| 972 | // } |
| 973 | // catch(std::string errorstring){ |
| 974 | // std::cerr << errorstring << "in band " << iband << ", line " << indexJ << std::endl; |
| 975 | // } |
| 976 | // } |
| 977 | // const char* pszMessage; |
| 978 | // void* pProgressArg=NULL; |
| 979 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 980 | // double progress=0; |
| 981 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 982 | // for(int y=0;y<input.nrOfRow();y+=down){ |
| 983 | // if(verbose) |
| 984 | // std::cout << "calculating line " << y/down << std::endl; |
| 985 | // if(y){//inBuffer already initialized for y=0 |
| 986 | // //erase first line from inBuffer |
| 987 | // inBuffer.erase(inBuffer.begin()); |
| 988 | // //read extra line and push back to inBuffer if not out of bounds |
| 989 | // if(y+dimY/2<input.nrOfRow()){ |
| 990 | // //allocate buffer |
| 991 | // inBuffer.push_back(inBuffer.back()); |
| 992 | // try{ |
| 993 | // if(verbose) |
| 994 | // std::cout << "reading input line " << y+dimY/2 << std::endl; |
| 995 | // input.readData(inBuffer[inBuffer.size()-1],GDT_Float32,y+dimY/2,iband); |
| 996 | // } |
| 997 | // catch(std::string errorstring){ |
| 998 | // std::cerr << errorstring << "in band " << iband << ", line " << y << std::endl; |
| 999 | // } |
| 1000 | // } |
| 1001 | // } |
| 1002 | // for(int x=0;x<input.nrOfCol();x+=down){ |
| 1003 | // Vector2d<double> texture_feature(scale,3); |
| 1004 | // CImg<> texture_in(dimX,dimY); |
| 1005 | // int r=0;//index for row of texture_in |
| 1006 | // for(int j=-dimY/2;j<(dimY+1)/2;++j){ |
| 1007 | // int c=0; |
| 1008 | // for(int i=-dimX/2;i<(dimX+1)/2;++i){ |
| 1009 | // indexI=x+i; |
| 1010 | // //check if out of bounds |
| 1011 | // if(indexI<0) |
| 1012 | // indexI=-indexI; |
| 1013 | // else if(indexI>=input.nrOfCol()) |
| 1014 | // indexI=input.nrOfCol()-i; |
| 1015 | // if(y+j<0) |
| 1016 | // indexJ=-j; |
| 1017 | // else if(y+j>=input.nrOfRow()) |
| 1018 | // indexJ=dimY/2-j;//indexJ=inBuffer.size()-1-j; |
| 1019 | // else |
| 1020 | // indexJ=dimY/2+j; |
| 1021 | // assert(indexJ<inBuffer.size()); |
| 1022 | // assert(indexI<inBuffer[indexJ].size()); |
| 1023 | // texture_in(r,c)=inBuffer[indexJ][indexI]; |
| 1024 | // c++; |
| 1025 | // } |
| 1026 | // ++r; |
| 1027 | // } |
| 1028 | // texture_in.dwt_texture(texture_feature,scale); |
| 1029 | // for(int v=0;v<scale*3;++v) |
| 1030 | // outBuffer[v][x/down]=texture_feature[v/3][v%3]; |
| 1031 | // } |
| 1032 | // //write outBuffer to file |
| 1033 | // try{ |
| 1034 | // if(verbose) |
| 1035 | // std::cout << "writing line " << y/down << std::endl; |
| 1036 | // for(int v=0;v<scale*3;++v) |
| 1037 | // output.writeData(outBuffer[v],GDT_Float32,y/down,v); |
| 1038 | // } |
| 1039 | // catch(std::string errorstring){ |
| 1040 | // std::cerr << errorstring << "in band " << iband << ", line " << y << std::endl; |
| 1041 | // } |
| 1042 | // progress=(1.0+y)/output.nrOfRow(); |
| 1043 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 1044 | // } |
| 1045 | // input.close(); |
| 1046 | // output.close(); |
| 1047 | // } |
| 1048 | |
| 1049 | void filter2d::Filter2d::morphology(Jim& input, Jim& output, const std::string& method, int dimX, int dimY, const std::vector<double> &angle, bool disc) |
| 1050 | { |
| 1051 | if(!output.isInit()) |
| 1052 | output.open(input); |
| 1053 | output.setNoData(m_noDataValues); |
| 1054 | |
| 1055 | const char* pszMessage; |
| 1056 | // void* pProgressArg=NULL; |
| 1057 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 1058 | // double progress=0; |
| 1059 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 1060 | |
| 1061 | assert(dimX); |
| 1062 | assert(dimY); |
| 1063 | |
| 1064 | statfactory::StatFactory stat; |
| 1065 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 1066 | #pragma omp parallel for |
| 1067 | #else |
| 1068 | #endif |
| 1069 | for(unsigned int iband=0;iband<input.nrOfBand();++iband){ |
| 1070 | Vector2d<double> inBuffer(dimY,input.nrOfCol()); |
| 1071 | std::vector<double> outBuffer(input.nrOfCol()); |
| 1072 | int indexI=0; |
| 1073 | int indexJ=0; |
| 1074 | //initialize last half of inBuffer |
| 1075 | for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 1076 | try{ |
| 1077 | input.readData(inBuffer[indexJ],static_cast<unsigned int>(abs(j)),iband); |
| 1078 | ++indexJ; |
| 1079 | } |
| 1080 | catch(std::string errorstring){ |
| 1081 | std::cerr << errorstring << "in line " << indexJ << std::endl; |
| 1082 | } |
| 1083 | } |
| 1084 | for(unsigned int y=0;y<input.nrOfRow();++y){ |
| 1085 | if(y){//inBuffer already initialized for y=0 |
| 1086 | //erase first line from inBuffer |
| 1087 | if(dimY>1) |
| 1088 | inBuffer.erase(inBuffer.begin()); |
| 1089 | //read extra line and push back to inBuffer if not out of bounds |
| 1090 | if(y+dimY/2<input.nrOfRow()){ |
| 1091 | //allocate buffer |
| 1092 | if(dimY>1) |
| 1093 | inBuffer.push_back(inBuffer.back()); |
| 1094 | try{ |
| 1095 | input.readData(inBuffer[inBuffer.size()-1],y+dimY/2,iband); |
| 1096 | } |
| 1097 | catch(std::string errorstring){ |
| 1098 | std::cerr << errorstring << "in band " << iband << ", line " << y << std::endl; |
| 1099 | } |
| 1100 | } |
| 1101 | else{ |
| 1102 | int over=y+dimY/2-input.nrOfRow(); |
| 1103 | int index=(inBuffer.size()-1)-over; |
| 1104 | assert(index>=0); |
| 1105 | assert(index<inBuffer.size()); |
| 1106 | inBuffer.push_back(inBuffer[index]); |
| 1107 | } |
| 1108 | } |
| 1109 | for(unsigned int x=0;x<input.nrOfCol();++x){ |
| 1110 | double currentValue=inBuffer[(dimY-1)/2][x]; |
| 1111 | outBuffer[x]=currentValue; |
| 1112 | std::vector<double> statBuffer; |
| 1113 | bool currentMasked=false; |
| 1114 | // int centre=dimX*(dimY-1)/2+(dimX-1)/2; |
| 1115 | for(int imask=0;imask<m_noDataValues.size();++imask){ |
| 1116 | if(currentValue==m_noDataValues[imask]){ |
| 1117 | currentMasked=true; |
| 1118 | break; |
| 1119 | } |
| 1120 | } |
| 1121 | if(currentMasked){ |
| 1122 | outBuffer[x]=currentValue; |
| 1123 | } |
| 1124 | else{ |
| 1125 | for(int j=-(dimY-1)/2;j<=dimY/2;++j){ |
| 1126 | for(int i=-(dimX-1)/2;i<=dimX/2;++i){ |
| 1127 | double d2=i*i+j*j;//square distance |
| 1128 | if(disc&&(d2>(dimX/2)*(dimY/2))) |
| 1129 | continue; |
| 1130 | if(angle.size()){ |
| 1131 | double theta; |
| 1132 | //use polar coordinates in radians |
| 1133 | if(i>0){ |
| 1134 | if(j<0) |
| 1135 | theta=atan(static_cast<double>(-j)/(static_cast<double>(i))); |
| 1136 | else |
| 1137 | theta=-atan(static_cast<double>(j)/(static_cast<double>(i))); |
| 1138 | } |
| 1139 | else if(i<0){ |
| 1140 | if(j<0) |
| 1141 | theta=PI-atan(static_cast<double>(-j)/(static_cast<double>(-i))); |
| 1142 | else |
| 1143 | theta=PI+atan(static_cast<double>(j)/(static_cast<double>(-i))); |
| 1144 | } |
| 1145 | else if(j<0) |
| 1146 | theta=PI/2.0; |
| 1147 | else if(j>0) |
| 1148 | theta=3.0*PI/2.0; |
| 1149 | //convert to North (0), East (90), South (180), West (270) in degrees |
| 1150 | theta=360-(theta/PI*180)+90; |
| 1151 | if(theta<0) |
| 1152 | theta+=360; |
| 1153 | while(theta>360) |
| 1154 | theta-=360; |
| 1155 | bool alligned=false; |
| 1156 | for(int iangle=0;iangle<angle.size();++iangle){ |
| 1157 | if(sqrt((theta-angle[iangle])*(theta-angle[iangle]))<10){ |
| 1158 | alligned=true; |
| 1159 | break; |
| 1160 | } |
| 1161 | } |
| 1162 | if(!alligned) |
| 1163 | continue; |
| 1164 | } |
| 1165 | indexI=x+i; |
| 1166 | //check if out of bounds |
| 1167 | if(indexI<0) |
| 1168 | indexI=-indexI; |
| 1169 | else if(indexI>=input.nrOfCol()) |
| 1170 | indexI=input.nrOfCol()-i; |
| 1171 | if(y+j<0) |
| 1172 | indexJ=-j; |
| 1173 | else if(y+j>=input.nrOfRow()) |
| 1174 | indexJ=(dimY>2) ? (dimY-1)/2-j : 0; |
| 1175 | else |
| 1176 | indexJ=(dimY-1)/2+j; |
| 1177 | //todo: introduce novalue as this: ? |
| 1178 | // if(inBuffer[indexJ][indexI]==(m_noDataValues.size())? m_noDataValues[0] : 0) |
| 1179 | // continue; |
| 1180 | bool masked=false; |
| 1181 | for(int imask=0;imask<m_noDataValues.size();++imask){ |
| 1182 | if(inBuffer[indexJ][indexI]==m_noDataValues[imask]){ |
| 1183 | masked=true; |
| 1184 | break; |
| 1185 | } |
| 1186 | } |
| 1187 | if(!masked){ |
| 1188 | short binValue=0; |
| 1189 | for(int iclass=0;iclass<m_class.size();++iclass){ |
| 1190 | if(inBuffer[indexJ][indexI]==m_class[iclass]){ |
| 1191 | binValue=1; |
| 1192 | break; |
| 1193 | } |
| 1194 | } |
| 1195 | if(m_class.size()) |
| 1196 | statBuffer.push_back(binValue); |
| 1197 | else |
| 1198 | statBuffer.push_back(inBuffer[indexJ][indexI]); |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | if(statBuffer.size()){ |
| 1203 | switch(getFilterType(method)){ |
| 1204 | case(filter2d::dilate): |
| 1205 | outBuffer[x]=stat.mymax(statBuffer); |
| 1206 | break; |
| 1207 | case(filter2d::erode): |
| 1208 | outBuffer[x]=stat.mymin(statBuffer); |
| 1209 | break; |
| 1210 | default: |
| 1211 | std::ostringstream ess; |
| 1212 | ess << "Error: morphology method " << method << " not supported, choose " << filter2d::dilate << " (dilate) or " << filter2d::erode << " (erode)" << std::endl; |
| 1213 | throw(ess.str()); |
| 1214 | break; |
| 1215 | } |
| 1216 | } |
| 1217 | if(outBuffer[x]&&m_class.size()) |
| 1218 | outBuffer[x]=m_class[0]; |
| 1219 | } |
| 1220 | } |
| 1221 | //write outBuffer to file |
| 1222 | try{ |
| 1223 | output.writeData(outBuffer,y,iband); |
| 1224 | } |
| 1225 | catch(std::string errorstring){ |
| 1226 | std::cerr << errorstring << "in band " << iband << ", line " << y << std::endl; |
| 1227 | } |
| 1228 | // progress=(1.0+y); |
| 1229 | // progress+=(output.nrOfRow()*iband); |
| 1230 | // progress/=output.nrOfBand()*output.nrOfRow(); |
| 1231 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 1232 | } |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | //todo: perform on Jim directly instead of copying into Vector2d |
| 1237 | void filter2d::Filter2d::dsm2shadow(Jim& input, Jim& output, double sza, double saa, double pixelSize, short shadowFlag){ |
| 1238 | if(!output.isInit()) |
| 1239 | output.open(input); |
| 1240 | output.setNoData(m_noDataValues); |
| 1241 | |
| 1242 | Vector2d<float> inputBuffer; |
| 1243 | Vector2d<float> outputBuffer; |
| 1244 | input.readDataBlock(inputBuffer, 0, input.nrOfCol()-1, 0, input.nrOfRow()-1, 0); |
| 1245 | dsm2shadow(inputBuffer, outputBuffer, sza, saa, pixelSize, shadowFlag); |
| 1246 | output.writeDataBlock(outputBuffer,0,output.nrOfCol()-1,0,output.nrOfRow()-1,0); |
| 1247 | } |
| 1248 | |
| 1249 | void filter2d::Filter2d::dwtForward(Jim& input, Jim& output, const std::string& wavelet_type, int family){ |
| 1250 | if(!output.isInit()) |
| 1251 | output.open(input); |
| 1252 | output.setNoData(m_noDataValues); |
| 1253 | |
| 1254 | Vector2d<double> theBuffer; |
| 1255 | for(int iband=0;iband<input.nrOfBand();++iband){ |
| 1256 | std::cout << "filtering band " << iband << std::endl << std::flush; |
| 1257 | input.readDataBlock(theBuffer, 0, input.nrOfCol()-1, 0, input.nrOfRow()-1, iband); |
| 1258 | dwtForward(theBuffer, wavelet_type, family); |
| 1259 | output.writeDataBlock(theBuffer,0,output.nrOfCol()-1,0,output.nrOfRow()-1,iband); |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | void filter2d::Filter2d::dwtInverse(Jim& input, Jim& output, const std::string& wavelet_type, int family){ |
| 1264 | if(!output.isInit()) |
| 1265 | output.open(input); |
| 1266 | output.setNoData(m_noDataValues); |
| 1267 | |
| 1268 | Vector2d<float> theBuffer; |
| 1269 | for(int iband=0;iband<input.nrOfBand();++iband){ |
| 1270 | input.readDataBlock(theBuffer, 0, input.nrOfCol()-1, 0, input.nrOfRow()-1, iband); |
| 1271 | std::cout << "filtering band " << iband << std::endl << std::flush; |
| 1272 | dwtInverse(theBuffer, wavelet_type, family); |
| 1273 | output.writeDataBlock(theBuffer,0,output.nrOfCol()-1,0,output.nrOfRow()-1,iband); |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | void filter2d::Filter2d::dwtCut(Jim& input, Jim& output, const std::string& wavelet_type, int family, double cut, bool verbose){ |
| 1278 | if(!output.isInit()) |
| 1279 | output.open(input); |
| 1280 | output.setNoData(m_noDataValues); |
| 1281 | |
| 1282 | Vector2d<float> theBuffer; |
| 1283 | for(int iband=0;iband<input.nrOfBand();++iband){ |
| 1284 | input.readDataBlock(theBuffer, 0, input.nrOfCol()-1, 0, input.nrOfRow()-1, iband); |
| 1285 | std::cout << "filtering band " << iband << std::endl << std::flush; |
| 1286 | dwtCut(theBuffer, wavelet_type, family, cut); |
| 1287 | output.writeDataBlock(theBuffer,0,output.nrOfCol()-1,0,output.nrOfRow()-1,iband); |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | void filter2d::Filter2d::linearFeature(Jim& input, Jim& output, float angle, float angleStep, float maxDistance, float eps, bool l1, bool a1, bool l2, bool a2, int band, bool verbose){ |
| 1292 | if(!output.isInit()) |
| 1293 | output.open(input); |
| 1294 | output.setNoData(m_noDataValues); |
| 1295 | |
| 1296 | Vector2d<float> inputBuffer; |
| 1297 | std::vector< Vector2d<float> > outputBuffer; |
| 1298 | input.readDataBlock(inputBuffer, 0, input.nrOfCol()-1, 0, input.nrOfRow()-1, band); |
| 1299 | if(maxDistance<=0) |
| 1300 | maxDistance=sqrt(static_cast<float>(input.nrOfCol()*input.nrOfRow())); |
| 1301 | linearFeature(inputBuffer,outputBuffer,angle,angleStep,maxDistance,eps, l1, a1, l2, a2,verbose); |
| 1302 | for(int iband=0;iband<outputBuffer.size();++iband) |
| 1303 | output.writeDataBlock(outputBuffer[iband],0,output.nrOfCol()-1,0,output.nrOfRow()-1,iband); |
| 1304 | } |
| 1305 | |
| 1306 | void filter2d::Filter2d::linearFeature(const Vector2d<float>& input, std::vector< Vector2d<float> >& output, float angle, float angleStep, float maxDistance, float eps, bool l1, bool a1, bool l2, bool a2, bool verbose) |
| 1307 | { |
| 1308 | output.clear(); |
| 1309 | int nband=0;//linear feature |
| 1310 | if(l1) |
| 1311 | ++nband; |
| 1312 | if(a1) |
| 1313 | ++nband; |
| 1314 | if(l2) |
| 1315 | ++nband; |
| 1316 | if(a2) |
| 1317 | ++nband; |
| 1318 | output.resize(nband); |
| 1319 | for(int iband=0;iband<output.size();++iband) |
| 1320 | output[iband].resize(input.nRows(),input.nCols()); |
| 1321 | if(maxDistance<=0) |
| 1322 | maxDistance=sqrt(static_cast<float>(input.nRows()*input.nCols())); |
| 1323 | int indexI=0; |
| 1324 | int indexJ=0; |
| 1325 | const char* pszMessage; |
| 1326 | // void* pProgressArg=NULL; |
| 1327 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 1328 | // double progress=0; |
| 1329 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 1330 | for(unsigned int y=0;y<input.nRows();++y){ |
| 1331 | for(unsigned int x=0;x<input.nCols();++x){ |
| 1332 | float currentValue=input[y][x]; |
| 1333 | //find values equal to current value with some error margin |
| 1334 | //todo: add distance for two opposite directions |
| 1335 | float lineDistance1=0;//longest line of object |
| 1336 | float lineDistance2=maxDistance;//shortest line of object |
| 1337 | float lineAngle1=0;//angle to longest line (North=0) |
| 1338 | float lineAngle2=0;//angle to shortest line (North=0) |
| 1339 | float northAngle=0;//rotating angle |
| 1340 | for(northAngle=0;northAngle<180;northAngle+=angleStep){ |
| 1341 | if(angle<=360&&angle>=0&&angle!=northAngle) |
| 1342 | continue; |
| 1343 | float currentDistance=0; |
| 1344 | float theDir=0; |
| 1345 | for(short side=0;side<=1;side+=1){ |
| 1346 | theDir=PI/2.0-DEG2RAD(northAngle)+side*PI;//in radians |
| 1347 | if(theDir<0) |
| 1348 | theDir+=2*PI; |
| 1349 | float nextValue=currentValue; |
| 1350 | for(float currentRay=1;currentRay<maxDistance;++currentRay){ |
| 1351 | indexI=x+currentRay*cos(theDir); |
| 1352 | indexJ=y-currentRay*sin(theDir); |
| 1353 | if(indexJ<0||indexJ>=input.size()) |
| 1354 | break; |
| 1355 | if(indexI<0||indexI>=input[indexJ].size()) |
| 1356 | break; |
| 1357 | nextValue=input[indexJ][indexI]; |
| 1358 | if(verbose){ |
| 1359 | std::cout << "x: " << x << std::endl; |
| 1360 | std::cout << "y: " << y << std::endl; |
| 1361 | std::cout << "currentValue: " << currentValue << std::endl; |
| 1362 | std::cout << "theDir in degrees: " << RAD2DEG(theDir) << std::endl; |
| 1363 | std::cout << "cos(theDir): " << cos(theDir) << std::endl; |
| 1364 | std::cout << "sin(theDir): " << sin(theDir) << std::endl; |
| 1365 | std::cout << "currentRay: " << currentRay << std::endl; |
| 1366 | std::cout << "currentDistance: " << currentDistance << std::endl; |
| 1367 | std::cout << "indexI: " << indexI << std::endl; |
| 1368 | std::cout << "indexJ: " << indexJ << std::endl; |
| 1369 | std::cout << "nextValue: " << nextValue << std::endl; |
| 1370 | } |
| 1371 | if(fabs(currentValue-nextValue)<=eps){ |
| 1372 | ++currentDistance; |
| 1373 | } |
| 1374 | else{ |
| 1375 | break; |
| 1376 | } |
| 1377 | } |
| 1378 | } |
| 1379 | if(lineDistance1<currentDistance){ |
| 1380 | lineDistance1=currentDistance; |
| 1381 | lineAngle1=northAngle; |
| 1382 | } |
| 1383 | if(lineDistance2>currentDistance){ |
| 1384 | lineDistance2=currentDistance; |
| 1385 | lineAngle2=northAngle; |
| 1386 | } |
| 1387 | if(verbose){ |
| 1388 | std::cout << "lineDistance1: " << lineDistance1 << std::endl; |
| 1389 | std::cout << "lineAngle1: " << lineAngle1 << std::endl; |
| 1390 | std::cout << "lineDistance2: " << lineDistance2 << std::endl; |
| 1391 | std::cout << "lineAngle2: " << lineAngle2 << std::endl; |
| 1392 | } |
| 1393 | } |
| 1394 | int iband=0; |
| 1395 | if(l1) |
| 1396 | output[iband++][y][x]=lineDistance1; |
| 1397 | if(a1) |
| 1398 | output[iband++][y][x]=lineAngle1; |
| 1399 | if(l2) |
| 1400 | output[iband++][y][x]=lineDistance2; |
| 1401 | if(a2) |
| 1402 | output[iband++][y][x]=lineAngle2; |
| 1403 | assert(iband==nband); |
| 1404 | } |
| 1405 | // progress=(1.0+y); |
| 1406 | // progress/=input.nRows(); |
| 1407 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 1408 | } |
| 1409 | } |