| 1 | /********************************************************************** |
| 2 | jlfilter_lib.cc: program to filter raster images: median, min/max, morphological, filtering |
| 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 <assert.h> |
| 22 | #include <iostream> |
| 23 | #include <string> |
| 24 | #include <fstream> |
| 25 | #include <memory> |
| 26 | // #include <math.h> |
| 27 | #include <cmath> |
| 28 | #include <sys/types.h> |
| 29 | #include <stdio.h> |
| 30 | #include "base/Optionjl.h" |
| 31 | #include "fileclasses/FileReaderAscii.h" |
| 32 | #include "imageclasses/Jim.h" |
| 33 | #include "algorithms/StatFactory.h" |
| 34 | #include "algorithms/Filter.h" |
| 35 | #include "algorithms/Filter2d.h" |
| 36 | #include "apps/AppFactory.h" |
| 37 | |
| 38 | using namespace std; |
| 39 | using namespace app; |
| 40 | using namespace filter; |
| 41 | |
| 42 | /** |
| 43 | * @param app application specific option arguments |
| 44 | * @return output filtered raster dataset |
| 45 | **/ |
| 46 | shared_ptr<Jim> Jim::filter2dFast(const app::AppFactory& app){ |
| 47 | try{ |
| 48 | shared_ptr<Jim> imgWriter=createImg(); |
| 49 | filter2dFast(*imgWriter, app); |
| 50 | return(imgWriter); |
| 51 | } |
| 52 | catch(string helpString){ |
| 53 | cerr << helpString << endl; |
| 54 | throw; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param app application specific option arguments |
| 60 | * @return output filtered raster dataset |
| 61 | **/ |
| 62 | shared_ptr<Jim> Jim::filter(app::AppFactory& app){ |
| 63 | try{ |
| 64 | shared_ptr<Jim> imgWriter=createImg(); |
| 65 | filter(*imgWriter, app); |
| 66 | return(imgWriter); |
| 67 | } |
| 68 | catch(string helpString){ |
| 69 | cerr << helpString << endl; |
| 70 | throw; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @param imgWriter output filtered raster dataset |
| 76 | * @param app application specific option arguments |
| 77 | **/ |
| 78 | void Jim::filter(Jim& imgWriter, app::AppFactory& app){ |
| 79 | Optionjl<bool> disc_opt("circ", "circular", "circular disc kernel for dilation and erosion", false); |
| 80 | // Optionjl<double> angle_opt("a", "angle", "angle used for directional filtering in dilation (North=0, East=90, South=180, West=270)."); |
| 81 | Optionjl<std::string> method_opt("f", "filter", "filter function (nvalid, median, var, min, max, sum, mean, dilate, erode, close, open, homog (central pixel must be identical to all other pixels within window), heterog (central pixel must be different than all other pixels within window), sauvola, sobelx (horizontal edge detection), sobely (vertical edge detection), sobelxy (diagonal edge detection NE-SW),sobelyx (diagonal edge detection NW-SE), density, countid, mode (majority voting), only for classes), smooth, smoothnodata (smooth nodata values only) values, ismin, ismax, order (rank pixels in order), stdev, mrf, dwt, dwti, dwt_cut, dwt_cut_from, scramble, shift, savgolay, percentile, proportion)"); |
| 82 | Optionjl<std::string> resample_opt("r", "resampling-method", "Resampling method for shifting operation (near: nearest neighbour, bilinear: bi-linear interpolation).", "near"); |
| 83 | Optionjl<double> dimX_opt("dx", "dx", "filter kernel size in x, use odd values only", 3); |
| 84 | Optionjl<double> dimY_opt("dy", "dy", "filter kernel size in y, use odd values only", 3); |
| 85 | Optionjl<int> dimZ_opt("dz", "dz", "filter kernel size in z (spectral/temporal dimension), must be odd (example: 3).. Set dz>0 if 1-D filter must be used in band domain"); |
| 86 | Optionjl<std::string> wavelet_type_opt("wt", "wavelet", "wavelet type: daubechies,daubechies_centered, haar, haar_centered, bspline, bspline_centered", "daubechies"); |
| 87 | Optionjl<int> family_opt("wf", "family", "wavelet family (vanishing moment, see also http://www.gnu.org/software/gsl/manual/html_node/DWT-Initialization.html)", 4); |
| 88 | Optionjl<int> savgolay_nl_opt("nl", "nl", "Number of leftward (past) data points used in Savitzky-Golay filter)", 2); |
| 89 | Optionjl<int> savgolay_nr_opt("nr", "nr", "Number of rightward (future) data points used in Savitzky-Golay filter)", 2); |
| 90 | Optionjl<int> savgolay_ld_opt("ld", "ld", "order of the derivative desired in Savitzky-Golay filter (e.g., ld=0 for smoothed function)", 0); |
| 91 | Optionjl<int> savgolay_m_opt("m", "m", "order of the smoothing polynomial in Savitzky-Golay filter, also equal to the highest conserved moment; usual values are m = 2 or m = 4)", 2); |
| 92 | Optionjl<short> class_opt("class", "class", "class value(s) to use for density, erosion, dilation, openening and closing, thresholding"); |
| 93 | Optionjl<double> threshold_opt("t", "threshold", "threshold value(s) to use for threshold filter (one for each class), or threshold to cut for dwt_cut (use 0 to keep all) or dwt_cut_from, or sigma for shift", 0); |
| 94 | Optionjl<double> nodata_opt("nodata", "nodata", "nodata value(s) (e.g., used for smoothnodata filter)"); |
| 95 | Optionjl<std::string> tap_opt("tap", "tap", "text file containing taps used for spatial filtering (from ul to lr). Use dimX and dimY to specify tap dimensions in x and y. Leave empty for not using taps"); |
| 96 | Optionjl<double> tapz_opt("tapz", "tapz", "taps used for spectral filtering"); |
| 97 | Optionjl<string> padding_opt("pad","pad", "Padding method for filtering (how to handle edge effects). Choose between: symmetric, replicate, circular, zero (pad with 0).", "symmetric"); |
| 98 | Optionjl<double> fwhm_opt("fwhm", "fwhm", "list of full width half to apply spectral filtering (-fwhm band1 -fwhm band2 ...)"); |
| 99 | Optionjl<std::string> srf_opt("srf", "srf", "list of ASCII files containing spectral response functions (two columns: wavelength response)"); |
| 100 | Optionjl<double> wavelengthIn_opt("win", "wavelengthIn", "list of wavelengths in input spectrum (-win band1 -win band2 ...)"); |
| 101 | Optionjl<double> wavelengthOut_opt("wout", "wavelengthOut", "list of wavelengths in output spectrum (-wout band1 -wout band2 ...)"); |
| 102 | Optionjl<std::string> interpolationType_opt("interp", "interp", "type of interpolation for spectral filtering (see http://www.gnu.org/software/gsl/manual/html_node/Interpolation-Types.html)","akima"); |
| 103 | Optionjl<std::string> otype_opt("ot", "otype", "Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image"); |
| 104 | Optionjl<string> colorTable_opt("ct", "ct", "color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid). Use none to omit color table"); |
| 105 | Optionjl<short> down_opt("d", "down", "down sampling factor. Use value 1 for no downsampling. Use value n>1 for downsampling (aggregation)", 1); |
| 106 | Optionjl<string> beta_opt("beta", "beta", "ASCII file with beta for each class transition in Markov Random Field"); |
| 107 | // Optionjl<double> eps_opt("eps","eps", "error marging for linear feature",0); |
| 108 | // Optionjl<bool> l1_opt("l1","l1", "obtain longest object length for linear feature",false); |
| 109 | // Optionjl<bool> l2_opt("l2","l2", "obtain shortest object length for linear feature",false,2); |
| 110 | // Optionjl<bool> a1_opt("a1","a1", "obtain angle found for longest object length for linear feature",false); |
| 111 | // Optionjl<bool> a2_opt("a2","a2", "obtain angle found for shortest object length for linear feature",false); |
| 112 | Optionjl<short> verbose_opt("v", "verbose", "verbose mode if > 0", 0,2); |
| 113 | |
| 114 | resample_opt.setHide(1); |
| 115 | wavelet_type_opt.setHide(1); |
| 116 | family_opt.setHide(1); |
| 117 | savgolay_nl_opt.setHide(1); |
| 118 | savgolay_nr_opt.setHide(1); |
| 119 | savgolay_ld_opt.setHide(1); |
| 120 | savgolay_m_opt.setHide(1); |
| 121 | class_opt.setHide(1); |
| 122 | threshold_opt.setHide(1); |
| 123 | tap_opt.setHide(1); |
| 124 | tapz_opt.setHide(1); |
| 125 | padding_opt.setHide(1); |
| 126 | wavelengthIn_opt.setHide(1); |
| 127 | wavelengthOut_opt.setHide(1); |
| 128 | down_opt.setHide(1); |
| 129 | beta_opt.setHide(1); |
| 130 | // eps_opt.setHide(1); |
| 131 | // l1_opt.setHide(1); |
| 132 | // l2_opt.setHide(1); |
| 133 | // a1_opt.setHide(1); |
| 134 | // a2_opt.setHide(1); |
| 135 | interpolationType_opt.setHide(1); |
| 136 | otype_opt.setHide(1); |
| 137 | colorTable_opt.setHide(1); |
| 138 | disc_opt.setHide(1); |
| 139 | |
| 140 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 141 | try{ |
| 142 | doProcess=method_opt.retrieveOption(app); |
| 143 | // angle_opt.retrieveOption(app); |
| 144 | srf_opt.retrieveOption(app); |
| 145 | fwhm_opt.retrieveOption(app); |
| 146 | dimX_opt.retrieveOption(app); |
| 147 | dimY_opt.retrieveOption(app); |
| 148 | dimZ_opt.retrieveOption(app); |
| 149 | nodata_opt.retrieveOption(app); |
| 150 | resample_opt.retrieveOption(app); |
| 151 | wavelet_type_opt.retrieveOption(app); |
| 152 | family_opt.retrieveOption(app); |
| 153 | savgolay_nl_opt.retrieveOption(app); |
| 154 | savgolay_nr_opt.retrieveOption(app); |
| 155 | savgolay_ld_opt.retrieveOption(app); |
| 156 | savgolay_m_opt.retrieveOption(app); |
| 157 | class_opt.retrieveOption(app); |
| 158 | threshold_opt.retrieveOption(app); |
| 159 | tap_opt.retrieveOption(app); |
| 160 | tapz_opt.retrieveOption(app); |
| 161 | padding_opt.retrieveOption(app); |
| 162 | wavelengthIn_opt.retrieveOption(app); |
| 163 | wavelengthOut_opt.retrieveOption(app); |
| 164 | down_opt.retrieveOption(app); |
| 165 | beta_opt.retrieveOption(app); |
| 166 | // eps_opt.retrieveOption(app); |
| 167 | // l1_opt.retrieveOption(app); |
| 168 | // l2_opt.retrieveOption(app); |
| 169 | // a1_opt.retrieveOption(app); |
| 170 | // a2_opt.retrieveOption(app); |
| 171 | interpolationType_opt.retrieveOption(app); |
| 172 | otype_opt.retrieveOption(app); |
| 173 | colorTable_opt.retrieveOption(app); |
| 174 | disc_opt.retrieveOption(app); |
| 175 | verbose_opt.retrieveOption(app); |
| 176 | if(!doProcess){ |
| 177 | cout << endl; |
| 178 | std::ostringstream helpStream; |
| 179 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 180 | throw(helpStream.str());//help was invoked, stop processing |
| 181 | } |
| 182 | |
| 183 | //not implemented yet, must debug first... |
| 184 | vector<double> angle_opt; |
| 185 | |
| 186 | GDALDataType theType=GDT_Unknown; |
| 187 | if(otype_opt.size()){ |
| 188 | theType=string2GDAL(otype_opt[0]); |
| 189 | if(theType==GDT_Unknown) |
| 190 | std::cout << "Warning: unknown output pixel type: " << otype_opt[0] << ", using input type as default" << std::endl; |
| 191 | } |
| 192 | |
| 193 | if(theType==GDT_Unknown) |
| 194 | theType=this->getGDALDataType(); |
| 195 | |
| 196 | if(verbose_opt[0]) |
| 197 | std::cout << "Output pixel type: " << GDALGetDataTypeName(theType) << endl; |
| 198 | |
| 199 | string errorString; |
| 200 | unsigned int nband=this->nrOfBand(); |
| 201 | |
| 202 | if(fwhm_opt.size()) |
| 203 | nband=fwhm_opt.size(); |
| 204 | else if(srf_opt.size()) |
| 205 | nband=srf_opt.size(); |
| 206 | else if(tap_opt.size()||tapz_opt.size()) |
| 207 | nband=this->nrOfBand(); |
| 208 | else{ |
| 209 | if(method_opt.empty()){ |
| 210 | errorString="Error: no filter selected, use option -f"; |
| 211 | throw(errorString); |
| 212 | } |
| 213 | else if(verbose_opt[0]) |
| 214 | std::cout << "filter method: " << method_opt[0] << "=" << filter2d::Filter2d::getFilterType(method_opt[0]) << std::endl; |
| 215 | // std::cout << "filter method: "<< filter2d::Filter2d::getFilterType(method_opt[0]) << std::endl; |
| 216 | switch(filter2d::Filter2d::getFilterType(method_opt[0])){ |
| 217 | case(filter2d::dilate): |
| 218 | case(filter2d::erode): |
| 219 | case(filter2d::close): |
| 220 | case(filter2d::open): |
| 221 | case(filter2d::smooth): |
| 222 | //implemented in spectral/temporal domain (dimZ>1) and spatial domain |
| 223 | if(dimZ_opt.size()) |
| 224 | assert(dimZ_opt[0]>1); |
| 225 | nband=this->nrOfBand(); |
| 226 | break; |
| 227 | case(filter2d::dwt): |
| 228 | case(filter2d::dwti): |
| 229 | case(filter2d::dwt_cut): |
| 230 | case(filter2d::smoothnodata): |
| 231 | //implemented in spectral/temporal/spatial domain and nband always this->nrOfBand() |
| 232 | nband=this->nrOfBand(); |
| 233 | break; |
| 234 | case(filter2d::savgolay): |
| 235 | nband=this->nrOfBand(); |
| 236 | if(dimZ_opt.empty()) |
| 237 | dimZ_opt.push_back(1); |
| 238 | case(filter2d::dwt_cut_from): |
| 239 | //only implemented in spectral/temporal domain |
| 240 | if(dimZ_opt.size()){ |
| 241 | nband=this->nrOfBand(); |
| 242 | assert(threshold_opt.size()); |
| 243 | } |
| 244 | else{ |
| 245 | errorString="filter not implemented in spatial domain"; |
| 246 | throw(errorString); |
| 247 | } |
| 248 | break; |
| 249 | case(filter2d::mrf)://deliberate fall through |
| 250 | assert(class_opt.size()>1); |
| 251 | if(verbose_opt[0]) |
| 252 | std::cout << "opening output image" << std::endl; |
| 253 | nband=class_opt.size(); |
| 254 | case(filter2d::ismin): |
| 255 | case(filter2d::ismax): |
| 256 | case(filter2d::shift): |
| 257 | case(filter2d::scramble): |
| 258 | case(filter2d::mode): |
| 259 | case(filter2d::sobelx): |
| 260 | case(filter2d::sobely): |
| 261 | case(filter2d::sobelxy): |
| 262 | case(filter2d::countid): |
| 263 | case(filter2d::order): |
| 264 | case(filter2d::density): |
| 265 | case(filter2d::homog): |
| 266 | case(filter2d::heterog): |
| 267 | case(filter2d::sauvola): |
| 268 | //only implemented in spatial domain |
| 269 | if(dimZ_opt.size()){ |
| 270 | errorString="filter not implemented in spectral/temporal domain"; |
| 271 | throw(errorString); |
| 272 | } |
| 273 | break; |
| 274 | // case(filter2d::percentile): |
| 275 | // //implemented in spectral/temporal/spatial domain and nband 1 if dimZ>0 |
| 276 | // if(dimZ_opt.size()){ |
| 277 | // dimZ_opt[0]=1; |
| 278 | // nband=1; |
| 279 | // } |
| 280 | // else |
| 281 | // nband=this->nrOfBand(); |
| 282 | // break; |
| 283 | case(filter2d::sum): |
| 284 | case(filter2d::mean): |
| 285 | case(filter2d::min): |
| 286 | case(filter2d::max): |
| 287 | case(filter2d::var): |
| 288 | case(filter2d::stdev): |
| 289 | case(filter2d::nvalid): |
| 290 | case(filter2d::median): |
| 291 | case(filter2d::percentile): |
| 292 | case(filter2d::proportion): |
| 293 | //implemented in spectral/temporal/spatial domain and nband 1 if dimZ==1 |
| 294 | if(dimZ_opt.size()==1) |
| 295 | if(dimZ_opt[0]==1) |
| 296 | nband=1; |
| 297 | else |
| 298 | nband=this->nrOfBand(); |
| 299 | break; |
| 300 | default:{ |
| 301 | cout << endl; |
| 302 | std::ostringstream errorStream; |
| 303 | errorStream << "filter method: " << method_opt[0] << "=" << filter2d::Filter2d::getFilterType(method_opt[0]) << " not implemented"<< std::endl; |
| 304 | // errorStream << "filter " << method_opt[0] << " (" << )"<< " not implemented"; |
| 305 | throw(errorStream.str()); |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | // imgWriter.open(output_opt[0],(this->nrOfCol()+down_opt[0]-1)/down_opt[0],(this->nrOfRow()+down_opt[0]-1)/down_opt[0],nband,theType,imageType,memory_opt[0],option_opt); |
| 311 | imgWriter.open((this->nrOfCol()+down_opt[0]-1)/down_opt[0],(this->nrOfRow()+down_opt[0]-1)/down_opt[0],nband,theType); |
| 312 | imgWriter.setProjection(this->getProjection()); |
| 313 | double gt[6]; |
| 314 | this->getGeoTransform(gt); |
| 315 | gt[1]*=down_opt[0];//dx |
| 316 | gt[5]*=down_opt[0];//dy |
| 317 | imgWriter.setGeoTransform(gt); |
| 318 | |
| 319 | if(colorTable_opt.size()){ |
| 320 | if(colorTable_opt[0]!="none"){ |
| 321 | if(verbose_opt[0]) |
| 322 | cout << "set colortable " << colorTable_opt[0] << endl; |
| 323 | assert(imgWriter.getDataType()==GDT_Byte); |
| 324 | imgWriter.setColorTable(colorTable_opt[0]); |
| 325 | } |
| 326 | } |
| 327 | else if(this->getColorTable()!=NULL) |
| 328 | imgWriter.setColorTable(this->getColorTable()); |
| 329 | |
| 330 | if(nodata_opt.size()){ |
| 331 | for(unsigned int iband=0;iband<imgWriter.nrOfBand();++iband) |
| 332 | imgWriter.GDALSetNoDataValue(nodata_opt[0],iband); |
| 333 | } |
| 334 | |
| 335 | filter2d::Filter2d filter2d; |
| 336 | filter::Filter filter1d; |
| 337 | if(verbose_opt[0]) |
| 338 | cout << "Set padding to " << padding_opt[0] << endl; |
| 339 | filter1d.setPadding(padding_opt[0]); |
| 340 | if(class_opt.size()){ |
| 341 | if(verbose_opt[0]) |
| 342 | std::cout<< "class values: "; |
| 343 | for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 344 | if(!dimZ_opt.size()) |
| 345 | filter2d.pushClass(class_opt[iclass]); |
| 346 | else |
| 347 | filter1d.pushClass(class_opt[iclass]); |
| 348 | if(verbose_opt[0]) |
| 349 | std::cout<< class_opt[iclass] << " "; |
| 350 | } |
| 351 | if(verbose_opt[0]) |
| 352 | std::cout<< std::endl; |
| 353 | } |
| 354 | |
| 355 | if(nodata_opt.size()){ |
| 356 | if(verbose_opt[0]) |
| 357 | std::cout<< "mask values: "; |
| 358 | for(unsigned int imask=0;imask<nodata_opt.size();++imask){ |
| 359 | if(verbose_opt[0]) |
| 360 | std::cout<< nodata_opt[imask] << " "; |
| 361 | filter1d.pushNoDataValue(nodata_opt[imask]); |
| 362 | filter2d.pushNoDataValue(nodata_opt[imask]); |
| 363 | } |
| 364 | if(verbose_opt[0]) |
| 365 | std::cout<< std::endl; |
| 366 | } |
| 367 | filter1d.setThresholds(threshold_opt); |
| 368 | filter2d.setThresholds(threshold_opt); |
| 369 | |
| 370 | if(tap_opt.size()){ |
| 371 | ifstream tapfile(tap_opt[0].c_str()); |
| 372 | assert(tapfile); |
| 373 | Vector2d<double> taps(dimY_opt[0],dimX_opt[0]); |
| 374 | |
| 375 | for(unsigned int j=0;j<dimY_opt[0];++j){ |
| 376 | for(unsigned int i=0;i<dimX_opt[0];++i){ |
| 377 | tapfile >> taps[j][i]; |
| 378 | } |
| 379 | } |
| 380 | if(verbose_opt[0]){ |
| 381 | std::cout << "taps: "; |
| 382 | for(unsigned int j=0;j<dimY_opt[0];++j){ |
| 383 | for(unsigned int i=0;i<dimX_opt[0];++i){ |
| 384 | std::cout<< taps[j][i] << " "; |
| 385 | } |
| 386 | std::cout<< std::endl; |
| 387 | } |
| 388 | } |
| 389 | filter2d.setTaps(taps); |
| 390 | //todo: this->filter2D.filter(imgWriter); |
| 391 | filter2d.filter(*this,imgWriter); |
| 392 | // filter2d.filter(input,output); |
| 393 | tapfile.close(); |
| 394 | } |
| 395 | else if(tapz_opt.size()){ |
| 396 | if(verbose_opt[0]){ |
| 397 | std::cout << "taps: "; |
| 398 | for(int itap=0;itap<tapz_opt.size();++itap) |
| 399 | std::cout<< tapz_opt[itap] << " "; |
| 400 | std::cout<< std::endl; |
| 401 | } |
| 402 | filter1d.setTaps(tapz_opt); |
| 403 | //todo:this->filter3D.filter(imgWriter); |
| 404 | filter1d.filter(*this,imgWriter); |
| 405 | // filter1d.filter(input,output); |
| 406 | } |
| 407 | else if(fwhm_opt.size()){ |
| 408 | if(verbose_opt[0]) |
| 409 | std::cout << "spectral filtering to " << fwhm_opt.size() << " bands with provided fwhm " << std::endl; |
| 410 | assert(wavelengthOut_opt.size()==fwhm_opt.size()); |
| 411 | assert(wavelengthIn_opt.size()); |
| 412 | |
| 413 | Vector2d<double> lineInput(this->nrOfBand(),this->nrOfCol()); |
| 414 | Vector2d<double> lineOutput(wavelengthOut_opt.size(),this->nrOfCol()); |
| 415 | const char* pszMessage; |
| 416 | // void* pProgressArg=NULL; |
| 417 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 418 | // double progress=0; |
| 419 | // pfnProgress(progress,pszMessage,pProgressArg); |
| 420 | for(unsigned int y=0;y<this->nrOfRow();++y){ |
| 421 | if((y+1+down_opt[0]/2)%down_opt[0]) |
| 422 | continue; |
| 423 | for(unsigned int iband=0;iband<this->nrOfBand();++iband) |
| 424 | this->readData(lineInput[iband],y,iband); |
| 425 | filter1d.applyFwhm<double>(wavelengthIn_opt,lineInput,wavelengthOut_opt,fwhm_opt, interpolationType_opt[0], lineOutput, down_opt[0], verbose_opt[0]); |
| 426 | for(unsigned int iband=0;iband<imgWriter.nrOfBand();++iband){ |
| 427 | imgWriter.writeData(lineOutput[iband],y/down_opt[0],iband); |
| 428 | } |
| 429 | // progress=(1.0+y)/imgWriter.nrOfRow(); |
| 430 | // pfnProgress(progress,pszMessage,pProgressArg); |
| 431 | } |
| 432 | } |
| 433 | else if(srf_opt.size()){ |
| 434 | if(verbose_opt[0]) |
| 435 | std::cout << "spectral filtering to " << srf_opt.size() << " bands with provided SRF " << std::endl; |
| 436 | assert(wavelengthIn_opt.size()); |
| 437 | vector< Vector2d<double> > srf(srf_opt.size());//[0] srf_nr, [1]: wavelength, [2]: response |
| 438 | ifstream srfFile; |
| 439 | for(int isrf=0;isrf<srf_opt.size();++isrf){ |
| 440 | srf[isrf].resize(2); |
| 441 | srfFile.open(srf_opt[isrf].c_str()); |
| 442 | double v; |
| 443 | //add 0 to make sure srf is 0 at boundaries after interpolation step |
| 444 | srf[isrf][0].push_back(0); |
| 445 | srf[isrf][1].push_back(0); |
| 446 | srf[isrf][0].push_back(1); |
| 447 | srf[isrf][1].push_back(0); |
| 448 | while(srfFile >> v){ |
| 449 | srf[isrf][0].push_back(v); |
| 450 | srfFile >> v; |
| 451 | srf[isrf][1].push_back(v); |
| 452 | } |
| 453 | srfFile.close(); |
| 454 | //add 0 to make sure srf[isrf] is 0 at boundaries after interpolation step |
| 455 | srf[isrf][0].push_back(srf[isrf][0].back()+1); |
| 456 | srf[isrf][1].push_back(0); |
| 457 | srf[isrf][0].push_back(srf[isrf][0].back()+1); |
| 458 | srf[isrf][1].push_back(0); |
| 459 | if(verbose_opt[0]) |
| 460 | cout << "srf file details: " << srf[isrf][0].size() << " wavelengths defined" << endl; |
| 461 | } |
| 462 | assert(imgWriter.nrOfBand()==srf.size()); |
| 463 | double centreWavelength=0; |
| 464 | Vector2d<double> lineInput(this->nrOfBand(),this->nrOfCol()); |
| 465 | const char* pszMessage; |
| 466 | // void* pProgressArg=NULL; |
| 467 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 468 | // double progress=0; |
| 469 | // pfnProgress(progress,pszMessage,pProgressArg); |
| 470 | for(unsigned int y=0;y<this->nrOfRow();++y){ |
| 471 | if((y+1+down_opt[0]/2)%down_opt[0]) |
| 472 | continue; |
| 473 | for(unsigned int iband=0;iband<this->nrOfBand();++iband) |
| 474 | this->readData(lineInput[iband],y,iband); |
| 475 | for(unsigned int isrf=0;isrf<srf.size();++isrf){ |
| 476 | vector<double> lineOutput(imgWriter.nrOfCol()); |
| 477 | double delta=1.0; |
| 478 | bool normalize=true; |
| 479 | centreWavelength=filter1d.applySrf<double>(wavelengthIn_opt,lineInput,srf[isrf], interpolationType_opt[0], lineOutput, delta, normalize); |
| 480 | if(verbose_opt[0]) |
| 481 | std::cout << "centre wavelength srf " << isrf << ": " << centreWavelength << std::endl; |
| 482 | imgWriter.writeData(lineOutput,y/down_opt[0],isrf); |
| 483 | } |
| 484 | // progress=(1.0+y)/imgWriter.nrOfRow(); |
| 485 | // pfnProgress(progress,pszMessage,pProgressArg); |
| 486 | } |
| 487 | |
| 488 | } |
| 489 | else{ |
| 490 | switch(filter2d::Filter2d::getFilterType(method_opt[0])){ |
| 491 | case(filter2d::dilate): |
| 492 | if(down_opt[0]!=1){ |
| 493 | std::cerr << "Error: down option not supported for morphological operator" << std::endl; |
| 494 | exit(1); |
| 495 | } |
| 496 | if(dimZ_opt.size()){ |
| 497 | if(verbose_opt[0]) |
| 498 | std::cout<< "1-D filtering: dilate" << std::endl; |
| 499 | //todo:this->filter3D.morphology(imgWriter,"dilate",dimZ_opt[0],verbose_opt[0]); |
| 500 | filter1d.morphology(*this,imgWriter,"dilate",dimZ_opt[0],verbose_opt[0]); |
| 501 | // filter1d.morphology(input,output,"dilate",dimZ_opt[0],verbose_opt[0]); |
| 502 | } |
| 503 | else{ |
| 504 | //todo:this=>filter2D.morphology(imgWriter,"dilate",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 505 | filter2d.morphology(*this,imgWriter,"dilate",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 506 | // filter2d.morphology(input,output,"dilate",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 507 | } |
| 508 | break; |
| 509 | case(filter2d::erode): |
| 510 | if(down_opt[0]!=1){ |
| 511 | std::cerr << "Error: down option not supported for morphological operator" << std::endl; |
| 512 | exit(1); |
| 513 | } |
| 514 | if(dimZ_opt.size()>0){ |
| 515 | if(verbose_opt[0]) |
| 516 | std::cout<< "1-D filtering: erode" << std::endl; |
| 517 | //todo:this->filter3D.morphology(imgRaster,"erode",dimZ_opt[0],verbose_opt[0]); |
| 518 | filter1d.morphology(*this,imgWriter,"erode",dimZ_opt[0],verbose_opt[0]); |
| 519 | // filter1d.morphology(input,output,"erode",dimZ_opt[0],verbose_opt[0]); |
| 520 | } |
| 521 | else{ |
| 522 | //todo:this->filter2D.morphology(imgRaster,"erode",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 523 | filter2d.morphology(*this,imgWriter,"erode",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 524 | // filter2d.morphology(input,output,"erode",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 525 | } |
| 526 | break; |
| 527 | case(filter2d::close):{//closing |
| 528 | if(down_opt[0]!=1){ |
| 529 | std::cerr << "Error: down option not supported for morphological operator" << std::endl; |
| 530 | exit(1); |
| 531 | } |
| 532 | |
| 533 | if(dimZ_opt.size()){ |
| 534 | //todo:this->filter3D.morphology(imgRaster,"dilate",dimZ_opt[0]); |
| 535 | filter1d.morphology(*this,imgWriter,"dilate",dimZ_opt[0]); |
| 536 | // filter1d.morphology(input,output,"dilate",dimZ_opt[0]); |
| 537 | } |
| 538 | else{ |
| 539 | //todo:this->filter2D.morphology(imgRaster,"dilate",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 540 | filter2d.morphology(*this,imgWriter,"dilate",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 541 | // filter2d.morphology(input,output,"dilate",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 542 | } |
| 543 | if(dimZ_opt.size()){ |
| 544 | filter1d.morphology(imgWriter,imgWriter,"erode",dimZ_opt[0]); |
| 545 | // filter1d.morphology(output,output,"erode",dimZ_opt[0]); |
| 546 | } |
| 547 | else{ |
| 548 | filter2d.morphology(imgWriter,imgWriter,"erode",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 549 | // filter2d.morphology(output,output,"erode",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 550 | } |
| 551 | break; |
| 552 | } |
| 553 | case(filter2d::open):{//opening |
| 554 | if(down_opt[0]!=1){ |
| 555 | std::cerr << "Error: down option not supported for morphological operator" << std::endl; |
| 556 | exit(1); |
| 557 | } |
| 558 | if(dimZ_opt.size()){ |
| 559 | filter1d.morphology(*this,imgWriter,"erode",dimZ_opt[0],verbose_opt[0]); |
| 560 | // filter1d.morphology(input,output,"erode",dimZ_opt[0],verbose_opt[0]); |
| 561 | } |
| 562 | else{ |
| 563 | filter2d.morphology(*this,imgWriter,"erode",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 564 | // filter2d.morphology(input,output,"erode",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 565 | } |
| 566 | if(dimZ_opt.size()){ |
| 567 | filter1d.morphology(imgWriter,imgWriter,"dilate",dimZ_opt[0]); |
| 568 | // filter1d.morphology(output,output,"dilate",dimZ_opt[0]); |
| 569 | } |
| 570 | else{ |
| 571 | filter2d.morphology(imgWriter,imgWriter,"dilate",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 572 | // filter2d.morphology(output,output,"dilate",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 573 | } |
| 574 | break; |
| 575 | } |
| 576 | case(filter2d::homog):{//spatially homogeneous |
| 577 | filter2d.doit(*this,imgWriter,"homog",dimX_opt[0],dimY_opt[0],down_opt[0],disc_opt[0]); |
| 578 | // filter2d.doit(input,output,"homog",dimX_opt[0],dimY_opt[0],down_opt[0],disc_opt[0]); |
| 579 | break; |
| 580 | } |
| 581 | case(filter2d::heterog):{//spatially heterogeneous |
| 582 | filter2d.doit(*this,imgWriter,"heterog",dimX_opt[0],dimY_opt[0],down_opt[0],disc_opt[0]); |
| 583 | // filter2d.doit(input,output,"heterog",dimX_opt[0],dimY_opt[0],down_opt[0],disc_opt[0]); |
| 584 | break; |
| 585 | } |
| 586 | case(filter2d::sauvola):{//Implements Sauvola's thresholding method (http://fiji.sc/Auto_Local_Threshold) |
| 587 | |
| 588 | //test |
| 589 | Vector2d<unsigned short> inBuffer; |
| 590 | for(unsigned int iband=0;iband<this->nrOfBand();++iband){ |
| 591 | this->readDataBlock(inBuffer,0,this->nrOfCol()-1,0,this->nrOfRow()-1,iband); |
| 592 | } |
| 593 | filter2d.doit(*this,imgWriter,"sauvola",dimX_opt[0],dimY_opt[0],down_opt[0],disc_opt[0]); |
| 594 | // filter2d.doit(input,output,"sauvola",dimX_opt[0],dimY_opt[0],down_opt[0],disc_opt[0]); |
| 595 | break; |
| 596 | } |
| 597 | case(filter2d::shift):{//shift |
| 598 | if(down_opt[0]!=1){ |
| 599 | std::cerr << "Error: down option not supported for shift operator" << std::endl; |
| 600 | exit(1); |
| 601 | } |
| 602 | assert(this->nrOfBand()); |
| 603 | assert(this->nrOfCol()); |
| 604 | assert(this->nrOfRow()); |
| 605 | filter2d.shift(*this,imgWriter,dimX_opt[0],dimY_opt[0],threshold_opt[0],filter2d::Filter2d::getResampleType(resample_opt[0])); |
| 606 | // filter2d.shift(*this,imgWriter,dimX_opt[0],dimY_opt[0],threshold_opt[0],filter2d::Filter2d::getResResampleType(resample_opt[0])); |
| 607 | break; |
| 608 | } |
| 609 | // case(filter2d::linearfeature):{ |
| 610 | // if(down_opt[0]!=1){ |
| 611 | // std::cerr << "Error: down option not supported for linear feature" << std::endl; |
| 612 | // exit(1); |
| 613 | // } |
| 614 | // assert(this->nrOfBand()); |
| 615 | // assert(this->nrOfCol()); |
| 616 | // assert(this->nrOfRow()); |
| 617 | // float theAngle=361; |
| 618 | // if(angle_opt.size()) |
| 619 | // theAngle=angle_opt[0]; |
| 620 | // if(verbose_opt[0]) |
| 621 | // std::cout << "using angle " << theAngle << std::endl; |
| 622 | // try{ |
| 623 | // //using an angle step of 5 degrees and no maximum distance |
| 624 | // filter2d.linearFeature(input,output,theAngle,5,0,eps_opt[0],l1_opt[0],a1_opt[0],l2_opt[0],a2_opt[0],0,verbose_opt[0]); |
| 625 | // } |
| 626 | // catch(string errorstring){ |
| 627 | // cerr << errorstring << endl; |
| 628 | // } |
| 629 | // break; |
| 630 | // } |
| 631 | case(filter2d::mrf):{//Markov Random Field |
| 632 | if(verbose_opt[0]) |
| 633 | std::cout << "Markov Random Field filtering" << std::endl; |
| 634 | if(beta_opt.size()){ |
| 635 | //in file: classFrom classTo |
| 636 | //in variable: beta[classTo][classFrom] |
| 637 | FileReaderAscii betaReader(beta_opt[0]); |
| 638 | Vector2d<double> beta(class_opt.size(),class_opt.size()); |
| 639 | vector<int> cols(class_opt.size()); |
| 640 | for(int iclass=0;iclass<class_opt.size();++iclass) |
| 641 | cols[iclass]=iclass; |
| 642 | betaReader.readData(beta,cols); |
| 643 | if(verbose_opt[0]){ |
| 644 | std::cout << "using values for beta:" << std::endl; |
| 645 | for(int iclass1=0;iclass1<class_opt.size();++iclass1) |
| 646 | std::cout << " " << iclass1 << " (" << class_opt[iclass1] << ")"; |
| 647 | std::cout << std::endl; |
| 648 | for(int iclass1=0;iclass1<class_opt.size();++iclass1){ |
| 649 | std::cout << iclass1 << " (" << class_opt[iclass1] << ")"; |
| 650 | for(int iclass2=0;iclass2<class_opt.size();++iclass2) |
| 651 | std::cout << " " << beta[iclass2][iclass1] << " (" << class_opt[iclass2] << ")"; |
| 652 | std::cout << std::endl; |
| 653 | } |
| 654 | } |
| 655 | filter2d.mrf(*this, imgWriter, dimX_opt[0], dimY_opt[0], beta, true, down_opt[0], verbose_opt[0]); |
| 656 | //filter2d.mrf(input, output, dimX_opt[0], dimY_opt[0], beta, true, down_opt[0], verbose_opt[0]); |
| 657 | } |
| 658 | else |
| 659 | filter2d.mrf(*this, imgWriter, dimX_opt[0], dimY_opt[0], 1, true, down_opt[0], verbose_opt[0]); |
| 660 | // filter2d.mrf(input, output, dimX_opt[0], dimY_opt[0], 1, true, down_opt[0], verbose_opt[0]); |
| 661 | break; |
| 662 | } |
| 663 | case(filter2d::sobelx):{//Sobel edge detection in X |
| 664 | if(down_opt[0]!=1){ |
| 665 | std::cerr << "Error: down option not supported for sobel edge detection" << std::endl; |
| 666 | exit(1); |
| 667 | } |
| 668 | Vector2d<double> theTaps(3,3); |
| 669 | theTaps[0][0]=-1.0; |
| 670 | theTaps[0][1]=0.0; |
| 671 | theTaps[0][2]=1.0; |
| 672 | theTaps[1][0]=-2.0; |
| 673 | theTaps[1][1]=0.0; |
| 674 | theTaps[1][2]=2.0; |
| 675 | theTaps[2][0]=-1.0; |
| 676 | theTaps[2][1]=0.0; |
| 677 | theTaps[2][2]=1.0; |
| 678 | filter2d.setTaps(theTaps); |
| 679 | filter2d.filter(*this,imgWriter,true,true);//absolute and normalize |
| 680 | // filter2d.filter(input,output,true,true);//absolute and normalize |
| 681 | break; |
| 682 | } |
| 683 | case(filter2d::sobely):{//Sobel edge detection in Y |
| 684 | if(down_opt[0]!=1){ |
| 685 | std::cerr << "Error: down option not supported for sobel edge detection" << std::endl; |
| 686 | exit(1); |
| 687 | } |
| 688 | Vector2d<double> theTaps(3,3); |
| 689 | theTaps[0][0]=1.0; |
| 690 | theTaps[0][1]=2.0; |
| 691 | theTaps[0][2]=1.0; |
| 692 | theTaps[1][0]=0.0; |
| 693 | theTaps[1][1]=0.0; |
| 694 | theTaps[1][2]=0.0; |
| 695 | theTaps[2][0]=-1.0; |
| 696 | theTaps[2][1]=-2.0; |
| 697 | theTaps[2][2]=-1.0; |
| 698 | filter2d.setTaps(theTaps); |
| 699 | filter2d.filter(*this,imgWriter,true,true);//absolute and normalize |
| 700 | // filter2d.filter(input,output,true,true);//absolute and normalize |
| 701 | break; |
| 702 | } |
| 703 | case(filter2d::sobelxy):{//Sobel edge detection in XY |
| 704 | if(down_opt[0]!=1){ |
| 705 | std::cerr << "Error: down option not supported for sobel edge detection" << std::endl; |
| 706 | exit(1); |
| 707 | } |
| 708 | Vector2d<double> theTaps(3,3); |
| 709 | theTaps[0][0]=0.0; |
| 710 | theTaps[0][1]=1.0; |
| 711 | theTaps[0][2]=2.0; |
| 712 | theTaps[1][0]=-1.0; |
| 713 | theTaps[1][1]=0.0; |
| 714 | theTaps[1][2]=1.0; |
| 715 | theTaps[2][0]=-2.0; |
| 716 | theTaps[2][1]=-1.0; |
| 717 | theTaps[2][2]=0.0; |
| 718 | filter2d.setTaps(theTaps); |
| 719 | filter2d.filter(*this,imgWriter,true,true);//absolute and normalize |
| 720 | // filter2d.filter(input,output,true,true);//absolute and normalize |
| 721 | break; |
| 722 | } |
| 723 | case(filter2d::sobelyx):{//Sobel edge detection in XY |
| 724 | if(down_opt[0]!=1){ |
| 725 | std::cerr << "Error: down option not supported for sobel edge detection" << std::endl; |
| 726 | exit(1); |
| 727 | } |
| 728 | Vector2d<double> theTaps(3,3); |
| 729 | theTaps[0][0]=2.0; |
| 730 | theTaps[0][1]=1.0; |
| 731 | theTaps[0][2]=0.0; |
| 732 | theTaps[1][0]=1.0; |
| 733 | theTaps[1][1]=0.0; |
| 734 | theTaps[1][2]=-1.0; |
| 735 | theTaps[2][0]=0.0; |
| 736 | theTaps[2][1]=-1.0; |
| 737 | theTaps[2][2]=-2.0; |
| 738 | filter2d.setTaps(theTaps); |
| 739 | filter2d.filter(*this,imgWriter,true,true);//absolute and normalize |
| 740 | // filter2d.filter(input,output,true,true);//absolute and normalize |
| 741 | break; |
| 742 | } |
| 743 | case(filter2d::smooth):{//Smoothing filter |
| 744 | if(down_opt[0]!=1){ |
| 745 | std::cerr << "Error: down option not supported for this filter" << std::endl; |
| 746 | exit(1); |
| 747 | } |
| 748 | if(dimZ_opt.size()){ |
| 749 | if(verbose_opt[0]) |
| 750 | std::cout<< "1-D filtering: smooth" << std::endl; |
| 751 | filter1d.smooth(*this,imgWriter,dimZ_opt[0]); |
| 752 | } |
| 753 | else{ |
| 754 | filter2d.smooth(*this,imgWriter,dimX_opt[0],dimY_opt[0]); |
| 755 | } |
| 756 | break; |
| 757 | } |
| 758 | case(filter2d::smoothnodata):{//Smoothing filter |
| 759 | if(down_opt[0]!=1){ |
| 760 | std::cerr << "Error: down option not supported for this filter" << std::endl; |
| 761 | exit(1); |
| 762 | } |
| 763 | if(dimZ_opt.size()){ |
| 764 | if(verbose_opt[0]) |
| 765 | std::cout<< "1-D filtering: smooth" << std::endl; |
| 766 | filter1d.smoothNoData(*this,interpolationType_opt[0],imgWriter); |
| 767 | // filter1d.smoothNoData(input,interpolationType_opt[0],output); |
| 768 | } |
| 769 | else{ |
| 770 | if(verbose_opt[0]) |
| 771 | std::cout<< "2-D filtering: smooth" << std::endl; |
| 772 | filter2d.smoothNoData(*this,imgWriter,dimX_opt[0],dimY_opt[0]); |
| 773 | // filter2d.smoothNoData(input,output,dimX_opt[0],dimY_opt[0]); |
| 774 | } |
| 775 | break; |
| 776 | } |
| 777 | case(filter2d::dwt): |
| 778 | if(down_opt[0]!=1){ |
| 779 | std::cerr << "Error: down option not supported for this filter" << std::endl; |
| 780 | exit(1); |
| 781 | } |
| 782 | if(dimZ_opt.size()){ |
| 783 | if(verbose_opt[0]) |
| 784 | std::cout<< "DWT in spectral domain" << std::endl; |
| 785 | filter1d.dwtForward(*this, imgWriter, wavelet_type_opt[0], family_opt[0]); |
| 786 | // filter1d.dwtForward(input, output, wavelet_type_opt[0], family_opt[0]); |
| 787 | } |
| 788 | else |
| 789 | filter2d.dwtForward(*this, imgWriter, wavelet_type_opt[0], family_opt[0]); |
| 790 | // filter2d.dwtForward(input, output, wavelet_type_opt[0], family_opt[0]); |
| 791 | break; |
| 792 | case(filter2d::dwti): |
| 793 | if(down_opt[0]!=1){ |
| 794 | std::cerr << "Error: down option not supported for this filter" << std::endl; |
| 795 | exit(1); |
| 796 | } |
| 797 | if(dimZ_opt.size()){ |
| 798 | if(verbose_opt[0]) |
| 799 | std::cout<< "inverse DWT in spectral domain" << std::endl; |
| 800 | filter1d.dwtInverse(*this, imgWriter, wavelet_type_opt[0], family_opt[0]); |
| 801 | // filter1d.dwtInverse(input, output, wavelet_type_opt[0], family_opt[0]); |
| 802 | } |
| 803 | else |
| 804 | filter2d.dwtInverse(*this, imgWriter, wavelet_type_opt[0], family_opt[0]); |
| 805 | // filter2d.dwtInverse(input, output, wavelet_type_opt[0], family_opt[0]); |
| 806 | break; |
| 807 | case(filter2d::dwt_cut): |
| 808 | if(down_opt[0]!=1){ |
| 809 | std::cerr << "Error: down option not supported for this filter" << std::endl; |
| 810 | exit(1); |
| 811 | } |
| 812 | if(dimZ_opt.size()){ |
| 813 | if(verbose_opt[0]) |
| 814 | std::cout<< "DWT approximation in spectral domain" << std::endl; |
| 815 | filter1d.dwtCut(*this, imgWriter, wavelet_type_opt[0], family_opt[0], threshold_opt[0]); |
| 816 | // filter1d.dwtCut(input, output, wavelet_type_opt[0], family_opt[0], threshold_opt[0]); |
| 817 | } |
| 818 | else |
| 819 | filter2d.dwtCut(*this, imgWriter, wavelet_type_opt[0], family_opt[0], threshold_opt[0]); |
| 820 | // filter2d.dwtCut(input, output, wavelet_type_opt[0], family_opt[0], threshold_opt[0]); |
| 821 | break; |
| 822 | case(filter2d::dwt_cut_from): |
| 823 | if(down_opt[0]!=1){ |
| 824 | std::cerr << "Error: down option not supported for this filter" << std::endl; |
| 825 | exit(1); |
| 826 | } |
| 827 | if(dimZ_opt.size()){ |
| 828 | if(verbose_opt[0]) |
| 829 | std::cout<< "DWT approximation in spectral domain" << std::endl; |
| 830 | filter1d.dwtCutFrom(*this,imgWriter, wavelet_type_opt[0], family_opt[0], static_cast<int>(threshold_opt[0])); |
| 831 | //filter1d.dwtCutFrom(input, output, wavelet_type_opt[0], family_opt[0], static_cast<int>(thres |
| 832 | } |
| 833 | else{ |
| 834 | string errorString="Error: this filter is not supported in 2D"; |
| 835 | throw(errorString); |
| 836 | } |
| 837 | break; |
| 838 | case(filter2d::savgolay):{ |
| 839 | assert(savgolay_nl_opt.size()); |
| 840 | assert(savgolay_nr_opt.size()); |
| 841 | assert(savgolay_ld_opt.size()); |
| 842 | assert(savgolay_m_opt.size()); |
| 843 | if(verbose_opt[0]) |
| 844 | std::cout << "Calculating Savitzky-Golay coefficients: " << endl; |
| 845 | filter1d.getSavGolayCoefficients(tapz_opt, this->nrOfBand(), savgolay_nl_opt[0], savgolay_nr_opt[0], savgolay_ld_opt[0], savgolay_m_opt[0]); |
| 846 | if(verbose_opt[0]){ |
| 847 | std::cout << "taps (size is " << tapz_opt.size() << "): "; |
| 848 | for(int itap=0;itap<tapz_opt.size();++itap) |
| 849 | std::cout<< tapz_opt[itap] << " "; |
| 850 | std::cout<< std::endl; |
| 851 | } |
| 852 | filter1d.setTaps(tapz_opt); |
| 853 | filter1d.filter(*this,imgWriter); |
| 854 | // filter1d.filter(input,output); |
| 855 | break; |
| 856 | } |
| 857 | case(filter2d::percentile)://deliberate fall through |
| 858 | case(filter2d::threshold)://deliberate fall through |
| 859 | assert(threshold_opt.size()); |
| 860 | if(dimZ_opt.size()) |
| 861 | filter1d.setThresholds(threshold_opt); |
| 862 | else |
| 863 | filter2d.setThresholds(threshold_opt); |
| 864 | case(filter2d::density)://deliberate fall through |
| 865 | filter2d.setClasses(class_opt); |
| 866 | if(verbose_opt[0]) |
| 867 | std::cout << "classes set" << std::endl; |
| 868 | default: |
| 869 | if(dimZ_opt.size()){ |
| 870 | if(dimZ_opt[0]==1) |
| 871 | filter1d.stat(*this,imgWriter,method_opt[0]); |
| 872 | // filter1d.stat(input,output,method_opt[0]); |
| 873 | else{ |
| 874 | assert(down_opt[0]==1);//not implemented yet... |
| 875 | filter1d.filter(*this,imgWriter,method_opt[0],dimZ_opt[0]); |
| 876 | // filter1d.filter(input,output,method_opt[0],dimZ_opt[0]); |
| 877 | } |
| 878 | } |
| 879 | else if(getBlockSize()==nrOfRow()) |
| 880 | filter2d.doit(*this,imgWriter,method_opt[0],dimX_opt[0],dimY_opt[0],down_opt[0],disc_opt[0]); |
| 881 | else |
| 882 | filter2d.doit(*this,imgWriter,method_opt[0],dimX_opt[0],dimY_opt[0],down_opt[0],disc_opt[0]); |
| 883 | // filter2d.doit(input,output,method_opt[0],dimX_opt[0],dimY_opt[0],down_opt[0],disc_opt[0]); |
| 884 | break; |
| 885 | } |
| 886 | } |
| 887 | // this->close(); |
| 888 | // imgWriter.close(); |
| 889 | } |
| 890 | catch(string predefinedString){ |
| 891 | std::cout << predefinedString << std::endl; |
| 892 | throw; |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | ///Fast implementation of filter2d, not taking care of boundary effects (values at the boundary will be initialized as 0 as they will be cut of by caller) |
| 897 | void Jim::filter2dFast(Jim& imgWriter, const app::AppFactory& app) |
| 898 | { |
| 899 | Optionjl<unsigned int> band_opt("b", "band", "band index to crop (leave empty to retain all bands)"); |
| 900 | Optionjl<bool> disc_opt("circ", "circular", "circular disc kernel for dilation and erosion", false); |
| 901 | // Optionjl<double> angle_opt("a", "angle", "angle used for directional filtering in dilation (North=0, East=90, South=180, West=270)."); |
| 902 | Optionjl<std::string> method_opt("f", "filter", "filter function (nvalid, median, var, min, max, sum, mean, dilate, erode, close, open, homog (central pixel must be identical to all other pixels within window), heterog (central pixel must be different than all other pixels within window), sauvola, sobelx (horizontal edge detection), sobely (vertical edge detection), sobelxy (diagonal edge detection NE-SW),sobelyx (diagonal edge detection NW-SE), density, countid, mode (majority voting), only for classes), smooth, smoothnodata (smooth nodata values only) values, ismin, ismax, order (rank pixels in order), stdev, mrf, dwt, dwti, dwt_cut, dwt_cut_from, scramble, shift, savgolay, percentile, proportion)"); |
| 903 | Optionjl<std::string> resample_opt("r", "resampling-method_opt[0]", "Resampling method_opt[0] for shifting operation (near: nearest neighbour, bilinear: bi-linear interpolation).", "near"); |
| 904 | Optionjl<double> dimX_opt("dx", "dx", "filter kernel size in x, use odd values only", 3); |
| 905 | Optionjl<double> dimY_opt("dy", "dy", "filter kernel size in y, use odd values only", 3); |
| 906 | Optionjl<std::string> wavelet_type_opt("wt", "wavelet", "wavelet type: daubechies,daubechies_centered, haar, haar_centered, bspline, bspline_centered", "daubechies"); |
| 907 | Optionjl<int> family_opt("wf", "family", "wavelet family (vanishing moment, see also http://www.gnu.org/software/gsl/manual/html_node/DWT-Initialization.html)", 4); |
| 908 | Optionjl<short> class_opt("class", "class", "class value(s) to use for density, erosion, dilation, openening and closing, thresholding"); |
| 909 | Optionjl<double> threshold_opt("t", "threshold", "threshold value(s) to use for threshold filter (one for each class), or threshold to cut for dwt_cut (use 0 to keep all) or dwt_cut_from, or sigma for shift", 0); |
| 910 | Optionjl<double> nodata_opt("nodata", "nodata", "nodata value(s) (e.g., used for smoothnodata filter)"); |
| 911 | Optionjl<std::string> tap_opt("tap", "tap", "text file containing taps used for spatial filtering (from ul to lr). Use dimX and dimY to specify tap dimensions in x and y. Leave empty for not using taps"); |
| 912 | Optionjl<std::string> interpolationType_opt("interp", "interp", "type of interpolation for spectral filtering (see http://www.gnu.org/software/gsl/manual/html_node/Interpolation-Types.html)","akima"); |
| 913 | Optionjl<std::string> otype_opt("ot", "otype", "Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image"); |
| 914 | Optionjl<string> beta_opt("beta", "beta", "ASCII file with beta for each class transition in Markov Random Field"); |
| 915 | Optionjl<short> verbose_opt("v", "verbose", "verbose mode if > 0", 0,2); |
| 916 | |
| 917 | resample_opt.setHide(1); |
| 918 | wavelet_type_opt.setHide(1); |
| 919 | family_opt.setHide(1); |
| 920 | class_opt.setHide(1); |
| 921 | threshold_opt.setHide(1); |
| 922 | tap_opt.setHide(1); |
| 923 | beta_opt.setHide(1); |
| 924 | interpolationType_opt.setHide(1); |
| 925 | otype_opt.setHide(1); |
| 926 | disc_opt.setHide(1); |
| 927 | |
| 928 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 929 | try{ |
| 930 | doProcess=method_opt.retrieveOption(app); |
| 931 | band_opt.retrieveOption(app); |
| 932 | // angle_opt.retrieveOption(app); |
| 933 | dimX_opt.retrieveOption(app); |
| 934 | dimY_opt.retrieveOption(app); |
| 935 | nodata_opt.retrieveOption(app); |
| 936 | resample_opt.retrieveOption(app); |
| 937 | wavelet_type_opt.retrieveOption(app); |
| 938 | family_opt.retrieveOption(app); |
| 939 | class_opt.retrieveOption(app); |
| 940 | threshold_opt.retrieveOption(app); |
| 941 | tap_opt.retrieveOption(app); |
| 942 | beta_opt.retrieveOption(app); |
| 943 | interpolationType_opt.retrieveOption(app); |
| 944 | otype_opt.retrieveOption(app); |
| 945 | disc_opt.retrieveOption(app); |
| 946 | verbose_opt.retrieveOption(app); |
| 947 | if(!doProcess){ |
| 948 | cout << endl; |
| 949 | std::ostringstream helpStream; |
| 950 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 951 | throw(helpStream.str());//help was invoked, stop processing |
| 952 | } |
| 953 | const char* pszMessage; |
| 954 | // void* pProgressArg=NULL; |
| 955 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 956 | // double progress=0; |
| 957 | // // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 958 | // pfnProgress(progress,pszMessage,pProgressArg); |
| 959 | |
| 960 | statfactory::StatFactory stat; |
| 961 | double noDataValue=0; |
| 962 | if(m_noDataValues.size()){ |
| 963 | stat.setNoDataValues(m_noDataValues); |
| 964 | noDataValue=m_noDataValues[0]; |
| 965 | } |
| 966 | int iband=0; |
| 967 | if(!band_opt.size()) |
| 968 | while(band_opt.size()<nrOfBand()) |
| 969 | band_opt.push_back(iband++); |
| 970 | GDALDataType theType=GDT_Unknown; |
| 971 | if(otype_opt.size()){ |
| 972 | theType=string2GDAL(otype_opt[0]); |
| 973 | if(theType==GDT_Unknown) |
| 974 | std::cout << "Warning: unknown output pixel type: " << otype_opt[0] << ", using input type as default" << std::endl; |
| 975 | } |
| 976 | |
| 977 | if(theType==GDT_Unknown) |
| 978 | theType=this->getGDALDataType(); |
| 979 | |
| 980 | if(verbose_opt[0]) |
| 981 | std::cout << "Output pixel type: " << GDALGetDataTypeName(theType) << endl; |
| 982 | try{ |
| 983 | imgWriter.open(nrOfCol(),nrOfRow(),band_opt.size(),theType); |
| 984 | imgWriter.setProjection(this->getProjection()); |
| 985 | double gt[6]; |
| 986 | this->getGeoTransform(gt); |
| 987 | imgWriter.setGeoTransform(gt); |
| 988 | |
| 989 | imgWriter.setNoData(nodata_opt); |
| 990 | // if(nodata_opt.size()){ |
| 991 | // imgWriter.setNoData(nodata_opt); |
| 992 | // } |
| 993 | } |
| 994 | catch(string errorstring){ |
| 995 | cout << errorstring << endl; |
| 996 | throw; |
| 997 | } |
| 998 | |
| 999 | for(int iband=0;iband<band_opt.size();++iband){ |
| 1000 | double writeValue=0; |
| 1001 | for(int y=dimY_opt[0]/2;y<nrOfRow()-dimY_opt[0]/2;++y){ |
| 1002 | for(int x=dimX_opt[0]/2;x<nrOfCol()-dimX_opt[0]/2;++x){ |
| 1003 | std::vector<double> windowBuffer; |
| 1004 | std::map<int,int> occurrence; |
| 1005 | int centre=dimX_opt[0]*(dimY_opt[0]-1)/2+(dimX_opt[0]-1)/2; |
| 1006 | for(int j=-(dimY_opt[0]-1)/2;j<=dimY_opt[0]/2;++j){ |
| 1007 | for(int i=-(dimX_opt[0]-1)/2;i<=dimX_opt[0]/2;++i){ |
| 1008 | int indexJ=(dimY_opt[0]-1)/2+j; |
| 1009 | int indexI=x+i; |
| 1010 | //check if out of bounds |
| 1011 | if(indexI<0) |
| 1012 | indexI=-indexI; |
| 1013 | else if(indexI>=nrOfCol()) |
| 1014 | indexI=nrOfCol()-i; |
| 1015 | if(y+j<0) |
| 1016 | indexJ=-j; |
| 1017 | else if(y+j>=nrOfRow()) |
| 1018 | indexJ=(dimY_opt[0]>2) ? (dimY_opt[0]-1)/2-j : 0; |
| 1019 | // windowBuffer.push_back(inBuffer[indexJ][indexI]); |
| 1020 | windowBuffer.push_back(readData(x,y,band_opt[iband])); |
| 1021 | switch(filter2d::Filter2d::getFilterType(method_opt[0])){ |
| 1022 | case(filter2d::density): |
| 1023 | case(filter2d::countid): |
| 1024 | case(filter2d::mode): |
| 1025 | case(filter2d::threshold): |
| 1026 | if(!stat.isNoData(readData(x,y,band_opt[iband]))){ |
| 1027 | std::vector<short>::const_iterator vit=class_opt.begin(); |
| 1028 | //todo: test if this works (only add occurrence if within defined classes)! |
| 1029 | if(!class_opt.size()) |
| 1030 | ++occurrence[readData(x,y,band_opt[iband])]; |
| 1031 | else{ |
| 1032 | while(vit!=class_opt.end()){ |
| 1033 | if(readData(x,y,band_opt[iband])) |
| 1034 | ++occurrence[readData(x,y,band_opt[iband])]; |
| 1035 | } |
| 1036 | } |
| 1037 | break; |
| 1038 | } |
| 1039 | default: |
| 1040 | break; |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | switch(filter2d::Filter2d::getFilterType(method_opt[0])){ |
| 1045 | case(filter2d::nvalid): |
| 1046 | imgWriter.writeData(stat.nvalid(windowBuffer),x,y,iband); |
| 1047 | break; |
| 1048 | case(filter2d::median): |
| 1049 | imgWriter.writeData(stat.median(windowBuffer),x,y,iband); |
| 1050 | break; |
| 1051 | case(filter2d::var):{ |
| 1052 | imgWriter.writeData(stat.var(windowBuffer),x,y,iband); |
| 1053 | break; |
| 1054 | } |
| 1055 | case(filter2d::stdev):{ |
| 1056 | double varValue=stat.var(windowBuffer); |
| 1057 | if(stat.isNoData(varValue)) |
| 1058 | imgWriter.writeData(noDataValue,x,y,iband); |
| 1059 | else |
| 1060 | imgWriter.writeData(sqrt(varValue),x,y,iband); |
| 1061 | break; |
| 1062 | } |
| 1063 | case(filter2d::mean):{ |
| 1064 | if(windowBuffer.empty()) |
| 1065 | imgWriter.writeData(noDataValue,x,y,iband); |
| 1066 | else |
| 1067 | imgWriter.writeData(stat.mean(windowBuffer),x,y,iband); |
| 1068 | break; |
| 1069 | } |
| 1070 | case(filter2d::min):{ |
| 1071 | imgWriter.writeData(stat.mymin(windowBuffer),x,y,iband); |
| 1072 | break; |
| 1073 | } |
| 1074 | case(filter2d::ismin):{ |
| 1075 | double minValue=stat.mymin(windowBuffer); |
| 1076 | if(stat.isNoData(minValue)) |
| 1077 | imgWriter.writeData(noDataValue,x,y,iband); |
| 1078 | else |
| 1079 | imgWriter.writeData((windowBuffer[centre]==minValue)? 1:0,x,y,iband); |
| 1080 | break; |
| 1081 | } |
| 1082 | case(filter2d::minmax):{ |
| 1083 | double min=0; |
| 1084 | double max=0; |
| 1085 | stat.minmax(windowBuffer,windowBuffer.begin(),windowBuffer.end(),min,max); |
| 1086 | if(min!=max) |
| 1087 | imgWriter.writeData(0,x,y,iband); |
| 1088 | else |
| 1089 | imgWriter.writeData(windowBuffer[centre],x,y,iband);//centre pixels |
| 1090 | break; |
| 1091 | } |
| 1092 | case(filter2d::max):{ |
| 1093 | imgWriter.writeData(stat.mymax(windowBuffer),x,y,iband); |
| 1094 | break; |
| 1095 | } |
| 1096 | case(filter2d::ismax):{ |
| 1097 | double maxValue=stat.mymax(windowBuffer); |
| 1098 | if(stat.isNoData(maxValue)) |
| 1099 | imgWriter.writeData(noDataValue,x,y,iband); |
| 1100 | else |
| 1101 | imgWriter.writeData((windowBuffer[centre]==maxValue)? 1:0,x,y,iband); |
| 1102 | break; |
| 1103 | } |
| 1104 | case(filter2d::order):{ |
| 1105 | stat.eraseNoData(windowBuffer); |
| 1106 | if(windowBuffer.empty()) |
| 1107 | imgWriter.writeData(noDataValue,x,y,iband); |
| 1108 | else{ |
| 1109 | double lbound=0; |
| 1110 | double ubound=dimX_opt[0]*dimY_opt[0]; |
| 1111 | double theMin=stat.mymin(windowBuffer); |
| 1112 | double theMax=stat.mymax(windowBuffer); |
| 1113 | double scale=(ubound-lbound)/(theMax-theMin); |
| 1114 | imgWriter.writeData(static_cast<short>(scale*(windowBuffer[centre]-theMin)+lbound),x,y,iband); |
| 1115 | } |
| 1116 | break; |
| 1117 | } |
| 1118 | case(filter2d::sum):{ |
| 1119 | imgWriter.writeData(stat.sum(windowBuffer),x,y,iband); |
| 1120 | break; |
| 1121 | } |
| 1122 | case(filter2d::percentile):{ |
| 1123 | assert(threshold_opt.size()); |
| 1124 | imgWriter.writeData(stat.percentile(windowBuffer,windowBuffer.begin(),windowBuffer.end(),threshold_opt[0]),x,y,iband); |
| 1125 | break; |
| 1126 | } |
| 1127 | case(filter2d::proportion):{ |
| 1128 | stat.eraseNoData(windowBuffer); |
| 1129 | double sum=stat.sum(windowBuffer); |
| 1130 | if(sum) |
| 1131 | imgWriter.writeData(windowBuffer[centre]/sum,x,y,iband); |
| 1132 | else |
| 1133 | imgWriter.writeData(noDataValue,x,y,iband); |
| 1134 | break; |
| 1135 | } |
| 1136 | case(filter2d::homog):{ |
| 1137 | // double centreValue=inBuffer[(dimY-1)/2][x]; |
| 1138 | double centreValue=readData(x,y,band_opt[iband]); |
| 1139 | bool isHomog=true; |
| 1140 | stat.eraseNoData(windowBuffer); |
| 1141 | typename std::vector<double>::const_iterator wit; |
| 1142 | for(wit=windowBuffer.begin();wit!=windowBuffer.end();++wit){ |
| 1143 | if(*wit==centreValue) |
| 1144 | continue; |
| 1145 | else{ |
| 1146 | isHomog=false; |
| 1147 | break; |
| 1148 | } |
| 1149 | } |
| 1150 | if(isHomog) |
| 1151 | imgWriter.writeData(1,x,y,iband); |
| 1152 | else |
| 1153 | imgWriter.writeData(noDataValue,x,y,iband); |
| 1154 | break; |
| 1155 | } |
| 1156 | case(filter2d::heterog):{ |
| 1157 | // double centreValue=inBuffer[(dimY-1)/2][x]; |
| 1158 | double centreValue=readData(x,y,band_opt[iband]); |
| 1159 | bool isHeterog=true; |
| 1160 | stat.eraseNoData(windowBuffer); |
| 1161 | typename std::vector<double>::const_iterator wit; |
| 1162 | for(wit=windowBuffer.begin();wit!=windowBuffer.end();++wit){ |
| 1163 | if(*wit!=centreValue) |
| 1164 | continue; |
| 1165 | else{ |
| 1166 | isHeterog=false; |
| 1167 | break; |
| 1168 | } |
| 1169 | } |
| 1170 | if(isHeterog) |
| 1171 | imgWriter.writeData(1,x,y,iband); |
| 1172 | else |
| 1173 | imgWriter.writeData(noDataValue,x,y,iband); |
| 1174 | break; |
| 1175 | } |
| 1176 | case(filter2d::sauvola):{ |
| 1177 | try{ |
| 1178 | double theMean=0; |
| 1179 | double theStdev=0; |
| 1180 | bool invalid=false; |
| 1181 | // double centreValue=inBuffer[(dimY-1)/2][x]; |
| 1182 | double centreValue=readData(x,y,band_opt[iband]); |
| 1183 | if(windowBuffer.empty()||stat.isNoData(centreValue)){ |
| 1184 | invalid=true; |
| 1185 | throw(invalid); |
| 1186 | } |
| 1187 | stat.meanVar(windowBuffer,theMean,theStdev); |
| 1188 | theStdev=sqrt(theStdev); |
| 1189 | double kValue=0.5; |
| 1190 | double rValue=128; |
| 1191 | if(threshold_opt.size()==2){ |
| 1192 | kValue=threshold_opt[0]; |
| 1193 | rValue=threshold_opt[1]; |
| 1194 | } |
| 1195 | //from http://fiji.sc/Auto_Local_Threshold |
| 1196 | //pixel = ( pixel > mean * ( 1 + k * ( standard_deviation / r - 1 ) ) ) ? object : background |
| 1197 | double theThreshold=theMean*(1+kValue*(theStdev/rValue - 1)); |
| 1198 | //isdata value hardcoded as 1 for now |
| 1199 | imgWriter.writeData((centreValue>theThreshold) ? 1 : noDataValue,x,y,iband); |
| 1200 | } |
| 1201 | catch(bool invalid){ |
| 1202 | imgWriter.writeData(noDataValue,x,y,iband); |
| 1203 | } |
| 1204 | break; |
| 1205 | } |
| 1206 | case(filter2d::density):{ |
| 1207 | int nvalid=stat.nvalid(windowBuffer); |
| 1208 | if(nvalid){ |
| 1209 | std::vector<short>::const_iterator vit=class_opt.begin(); |
| 1210 | while(vit!=class_opt.end()) |
| 1211 | imgWriter.writeData(imgWriter.readData(x,y,iband)+100.0*occurrence[*(vit++)]/nvalid,x,y,iband); |
| 1212 | } |
| 1213 | else |
| 1214 | imgWriter.writeData(noDataValue,x,y,iband); |
| 1215 | break; |
| 1216 | } |
| 1217 | case(filter2d::countid):{ |
| 1218 | if(occurrence.size()) |
| 1219 | imgWriter.writeData(occurrence.size(),x,y,iband); |
| 1220 | else |
| 1221 | imgWriter.writeData(noDataValue,x,y,iband); |
| 1222 | break; |
| 1223 | } |
| 1224 | case(filter2d::mode):{ |
| 1225 | if(occurrence.size()){ |
| 1226 | std::map<int,int>::const_iterator maxit=occurrence.begin(); |
| 1227 | for(std::map<int,int>::const_iterator mit=occurrence.begin();mit!=occurrence.end();++mit){ |
| 1228 | if(mit->second>maxit->second) |
| 1229 | maxit=mit; |
| 1230 | } |
| 1231 | // if(occurrence[inBuffer[(dimY-1)/2][x]]<maxit->second)// |
| 1232 | if(occurrence[readData(x,y,band_opt[iband])]<maxit->second) |
| 1233 | imgWriter.writeData(maxit->first,x,y,iband); |
| 1234 | else//favorize original value in case of ties |
| 1235 | imgWriter.writeData(readData(x,y,band_opt[iband]),x,y,iband); |
| 1236 | // outBuffer[x/down_opt[0]+y/down_opt[0]*nrOfCol()]=inBuffer[(dimY-1)/2][x]; |
| 1237 | } |
| 1238 | else |
| 1239 | imgWriter.writeData(noDataValue,x,y,iband); |
| 1240 | break; |
| 1241 | } |
| 1242 | case(filter2d::threshold):{ |
| 1243 | assert(class_opt.size()==threshold_opt.size()); |
| 1244 | int nvalid=stat.nvalid(windowBuffer); |
| 1245 | if(nvalid>0){ |
| 1246 | // outBuffer[x/down_opt[0]+y/down_opt[0]*nrOfCol()]=inBuffer[(dimY-1)/2][x];//initialize with original value (in case thresholds not met) |
| 1247 | imgWriter.writeData(readData(x,y,band_opt[iband]),x,y,iband);//initialize with original value (in case thresholds not met) |
| 1248 | for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 1249 | if(100.0*(occurrence[class_opt[iclass]])/nvalid>threshold_opt[iclass]) |
| 1250 | imgWriter.writeData(class_opt[iclass],x,y,iband); |
| 1251 | } |
| 1252 | } |
| 1253 | else |
| 1254 | imgWriter.writeData(noDataValue,x,y,iband); |
| 1255 | break; |
| 1256 | } |
| 1257 | 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... |
| 1258 | if(windowBuffer.size()){ |
| 1259 | int randomIndex=std::rand()%windowBuffer.size(); |
| 1260 | if(randomIndex>=windowBuffer.size()) |
| 1261 | imgWriter.writeData(windowBuffer.back(),x,y,iband); |
| 1262 | else if(randomIndex<0) |
| 1263 | imgWriter.writeData(windowBuffer[0],x,y,iband); |
| 1264 | else |
| 1265 | imgWriter.writeData(windowBuffer[randomIndex],x,y,iband); |
| 1266 | } |
| 1267 | else |
| 1268 | imgWriter.writeData(noDataValue,x,y,iband); |
| 1269 | break; |
| 1270 | } |
| 1271 | default: |
| 1272 | break; |
| 1273 | } |
| 1274 | } |
| 1275 | // progress=(1.0+y); |
| 1276 | // progress/=nrOfRow(); |
| 1277 | // // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 1278 | // pfnProgress(progress,pszMessage,pProgressArg); |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | catch(string predefinedString){ |
| 1283 | std::cout << predefinedString << std::endl; |
| 1284 | throw; |
| 1285 | } |
| 1286 | } |