| 1 | /********************************************************************** |
| 2 | jlfilter2d_lib.cc: program to filter raster images in spatial domain |
| 3 | Author(s): [email protected] |
| 4 | Copyright (C) 2016-2020 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 | #include "jlfilter2d_lib.h" |
| 38 | |
| 39 | using namespace std; |
| 40 | using namespace app; |
| 41 | using namespace filter; |
| 42 | |
| 43 | // shared_ptr<Jim> Jim::filter2dFast(const app::AppFactory& app){ |
| 44 | // try{ |
| 45 | // shared_ptr<Jim> imgWriter=createImg(); |
| 46 | // filter2dFast(*imgWriter, app); |
| 47 | // return(imgWriter); |
| 48 | // } |
| 49 | // catch(string helpString){ |
| 50 | // cerr << helpString << endl; |
| 51 | // return(0); |
| 52 | // } |
| 53 | // } |
| 54 | |
| 55 | /** |
| 56 | * @param filter (type: std::string) 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), 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, scramble, shift, percentile, proportion) |
| 57 | * @param dx (type: double) (default: 3) filter kernel size in x, use odd values only |
| 58 | * @param dy (type: double) (default: 3) filter kernel size in y, use odd values only |
| 59 | * @param nodata (type: double) nodata value(s) (e.g., used for smoothnodata filter) |
| 60 | * @param resampling-method (type: std::string) (default: near) Resampling method for shifting operation (near: nearest neighbour, bilinear: bi-linear interpolation). |
| 61 | * @param wavelet (type: std::string) (default: daubechies) wavelet type: daubechies,daubechies_centered, haar, haar_centered, bspline, bspline_centered |
| 62 | * @param family (type: int) (default: 4) wavelet family (vanishing moment, see also http://www.gnu.org/software/gsl/manual/html_node/DWT-Initialization.html) |
| 63 | * @param class (type: short) class value(s) to use for density, erosion, dilation, openening and closing, thresholding |
| 64 | * @param threshold (type: double) (default: 0) 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 |
| 65 | * @param tap (type: std::string) 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 |
| 66 | * @param pad (type: std::string) (default: symmetric) Padding method for filtering (how to handle edge effects). Choose between: symmetric, replicate, circular, zero (pad with 0). |
| 67 | * @param down (type: short) (default: 1) down sampling factor. Use value 1 for no downsampling. Use value n>1 for downsampling (aggregation) |
| 68 | * @param beta (type: std::string) ASCII file with beta for each class transition in Markov Random Field |
| 69 | * @param otype (type: std::string) Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image |
| 70 | * @param ct (type: std::string) color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid). Use none to omit color table |
| 71 | * @param circular (type: bool) (default: 0) circular disc kernel for dilation and erosion |
| 72 | * @return shared pointer to image object |
| 73 | **/ |
| 74 | shared_ptr<Jim> Jim::filter2d(const app::AppFactory& app){ |
| 75 | try{ |
| 76 | shared_ptr<Jim> imgWriter=createImg(); |
| 77 | filter2d(*imgWriter, app); |
| 78 | return(imgWriter); |
| 79 | } |
| 80 | catch(string helpString){ |
| 81 | cerr << helpString << endl; |
| 82 | throw; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * @param filter (type: std::string) 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), 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, scramble, shift, percentile, proportion) |
| 89 | * @param dx (type: double) (default: 3) filter kernel size in x, use odd values only |
| 90 | * @param dy (type: double) (default: 3) filter kernel size in y, use odd values only |
| 91 | * @param nodata (type: double) nodata value(s) (e.g., used for smoothnodata filter) |
| 92 | * @param resampling-method (type: std::string) (default: near) Resampling method for shifting operation (near: nearest neighbour, bilinear: bi-linear interpolation). |
| 93 | * @param wavelet (type: std::string) (default: daubechies) wavelet type: daubechies,daubechies_centered, haar, haar_centered, bspline, bspline_centered |
| 94 | * @param family (type: int) (default: 4) wavelet family (vanishing moment, see also http://www.gnu.org/software/gsl/manual/html_node/DWT-Initialization.html) |
| 95 | * @param class (type: short) class value(s) to use for density, erosion, dilation, openening and closing, thresholding |
| 96 | * @param threshold (type: double) (default: 0) 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 |
| 97 | * @param tap (type: std::string) 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 |
| 98 | * @param pad (type: std::string) (default: symmetric) Padding method for filtering (how to handle edge effects). Choose between: symmetric, replicate, circular, zero (pad with 0). |
| 99 | * @param down (type: short) (default: 1) down sampling factor. Use value 1 for no downsampling. Use value n>1 for downsampling (aggregation) |
| 100 | * @param beta (type: std::string) ASCII file with beta for each class transition in Markov Random Field |
| 101 | * @param otype (type: std::string) Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image |
| 102 | * @param ct (type: std::string) color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid). Use none to omit color table |
| 103 | * @param circular (type: bool) (default: 0) circular disc kernel for dilation and erosion |
| 104 | **/ |
| 105 | void Jim::filter2d(Jim& imgWriter, const app::AppFactory& app){ |
| 106 | Optionjl<bool> disc_opt("circ", "circular", "circular disc kernel for dilation and erosion", false); |
| 107 | // Optionjl<double> angle_opt("a", "angle", "angle used for directional filtering in dilation (North=0, East=90, South=180, West=270)."); |
| 108 | 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), 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, scramble, shift, percentile, proportion)"); |
| 109 | Optionjl<std::string> resample_opt("r", "resampling-method", "Resampling method for shifting operation (near: nearest neighbour, bilinear: bi-linear interpolation).", "near"); |
| 110 | Optionjl<double> dimX_opt("dx", "dx", "filter kernel size in x, use odd values only", 3); |
| 111 | Optionjl<double> dimY_opt("dy", "dy", "filter kernel size in y, use odd values only", 3); |
| 112 | Optionjl<std::string> wavelet_type_opt("wt", "wavelet", "wavelet type: daubechies,daubechies_centered, haar, haar_centered, bspline, bspline_centered", "daubechies"); |
| 113 | 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); |
| 114 | Optionjl<short> class_opt("class", "class", "class value(s) to use for density, erosion, dilation, openening and closing, thresholding"); |
| 115 | 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); |
| 116 | Optionjl<double> nodata_opt("nodata", "nodata", "nodata value(s) (e.g., used for smoothnodata filter)"); |
| 117 | Optionjl<double> tap_opt("tap", "tap", "taps used for spatial filtering (from ul to lr). Use dx and dy to specify tap dimensions in x and y. Leave empty for not using taps"); |
| 118 | Optionjl<bool> abs_opt("abs", "abs", "use absolute values when filtering",false); |
| 119 | Optionjl<bool> norm_opt("norm", "norm", "normalize tap values values when filtering",false); |
| 120 | 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"); |
| 121 | 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"); |
| 122 | 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"); |
| 123 | Optionjl<short> down_opt("d", "down", "down sampling factor. Use value 1 for no downsampling. Use value n>1 for downsampling (aggregation)", 1); |
| 124 | Optionjl<string> beta_opt("beta", "beta", "ASCII file with beta for each class transition in Markov Random Field"); |
| 125 | Optionjl<short> verbose_opt("v", "verbose", "verbose mode if > 0", 0,2); |
| 126 | |
| 127 | resample_opt.setHide(1); |
| 128 | wavelet_type_opt.setHide(1); |
| 129 | family_opt.setHide(1); |
| 130 | class_opt.setHide(1); |
| 131 | threshold_opt.setHide(1); |
| 132 | tap_opt.setHide(1); |
| 133 | abs_opt.setHide(1); |
| 134 | norm_opt.setHide(1); |
| 135 | padding_opt.setHide(1); |
| 136 | down_opt.setHide(1); |
| 137 | beta_opt.setHide(1); |
| 138 | otype_opt.setHide(1); |
| 139 | colorTable_opt.setHide(1); |
| 140 | disc_opt.setHide(1); |
| 141 | |
| 142 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 143 | try{ |
| 144 | doProcess=method_opt.retrieveOption(app); |
| 145 | // angle_opt.retrieveOption(app); |
| 146 | dimX_opt.retrieveOption(app); |
| 147 | dimY_opt.retrieveOption(app); |
| 148 | nodata_opt.retrieveOption(app); |
| 149 | resample_opt.retrieveOption(app); |
| 150 | wavelet_type_opt.retrieveOption(app); |
| 151 | family_opt.retrieveOption(app); |
| 152 | class_opt.retrieveOption(app); |
| 153 | threshold_opt.retrieveOption(app); |
| 154 | tap_opt.retrieveOption(app); |
| 155 | abs_opt.retrieveOption(app); |
| 156 | norm_opt.retrieveOption(app); |
| 157 | padding_opt.retrieveOption(app); |
| 158 | down_opt.retrieveOption(app); |
| 159 | beta_opt.retrieveOption(app); |
| 160 | otype_opt.retrieveOption(app); |
| 161 | colorTable_opt.retrieveOption(app); |
| 162 | disc_opt.retrieveOption(app); |
| 163 | verbose_opt.retrieveOption(app); |
| 164 | if(!doProcess){ |
| 165 | cout << endl; |
| 166 | std::ostringstream helpStream; |
| 167 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 168 | throw(helpStream.str());//help was invoked, stop processing |
| 169 | } |
| 170 | |
| 171 | //not implemented yet, must debug first... |
| 172 | vector<double> angle_opt; |
| 173 | |
| 174 | GDALDataType theType=GDT_Unknown; |
| 175 | if(otype_opt.size()){ |
| 176 | theType=string2GDAL(otype_opt[0]); |
| 177 | if(theType==GDT_Unknown) |
| 178 | std::cout << "Warning: unknown output pixel type: " << otype_opt[0] << ", using input type as default" << std::endl; |
| 179 | } |
| 180 | |
| 181 | if(theType==GDT_Unknown) |
| 182 | theType=this->getGDALDataType(); |
| 183 | |
| 184 | if(verbose_opt[0]) |
| 185 | std::cout << "Output pixel type: " << GDALGetDataTypeName(theType) << endl; |
| 186 | |
| 187 | string errorString; |
| 188 | unsigned int nband=this->nrOfBand(); |
| 189 | |
| 190 | if(tap_opt.size()) |
| 191 | nband=this->nrOfBand(); |
| 192 | else{ |
| 193 | if(method_opt.empty()){ |
| 194 | errorString="Error: no filter selected, use option -f"; |
| 195 | throw(errorString); |
| 196 | } |
| 197 | else if(verbose_opt[0]) |
| 198 | std::cout << "filter method: " << method_opt[0] << "=" << filter2d::Filter2d::getFilterType(method_opt[0]) << std::endl; |
| 199 | switch(filter2d::Filter2d::getFilterType(method_opt[0])){ |
| 200 | case(filter2d::dilate): |
| 201 | case(filter2d::erode): |
| 202 | case(filter2d::close): |
| 203 | case(filter2d::open): |
| 204 | case(filter2d::smooth): |
| 205 | //implemented in spectral/temporal domain (dimZ>1) and spatial domain |
| 206 | nband=this->nrOfBand(); |
| 207 | break; |
| 208 | case(filter2d::dwt): |
| 209 | case(filter2d::dwti): |
| 210 | case(filter2d::dwt_cut): |
| 211 | case(filter2d::smoothnodata): |
| 212 | //implemented in spectral/temporal/spatial domain and nband always this->nrOfBand() |
| 213 | nband=this->nrOfBand(); |
| 214 | break; |
| 215 | case(filter2d::mrf)://deliberate fall through |
| 216 | assert(class_opt.size()>1); |
| 217 | if(verbose_opt[0]) |
| 218 | std::cout << "opening output image" << std::endl; |
| 219 | nband=class_opt.size(); |
| 220 | case(filter2d::ismin): |
| 221 | case(filter2d::ismax): |
| 222 | case(filter2d::shift): |
| 223 | case(filter2d::scramble): |
| 224 | case(filter2d::mode): |
| 225 | case(filter2d::sobelx): |
| 226 | case(filter2d::sobely): |
| 227 | case(filter2d::sobelxy): |
| 228 | case(filter2d::countid): |
| 229 | case(filter2d::order): |
| 230 | case(filter2d::density): |
| 231 | case(filter2d::homog): |
| 232 | case(filter2d::heterog): |
| 233 | case(filter2d::sauvola): |
| 234 | //only implemented in spatial domain |
| 235 | break; |
| 236 | case(filter2d::sum): |
| 237 | case(filter2d::mean): |
| 238 | case(filter2d::min): |
| 239 | case(filter2d::max): |
| 240 | case(filter2d::var): |
| 241 | case(filter2d::stdev): |
| 242 | case(filter2d::nvalid): |
| 243 | case(filter2d::median): |
| 244 | case(filter2d::percentile): |
| 245 | case(filter2d::proportion): |
| 246 | //implemented in spectral/temporal/spatial domain |
| 247 | nband=this->nrOfBand(); |
| 248 | break; |
| 249 | default:{ |
| 250 | cout << endl; |
| 251 | std::ostringstream errorStream; |
| 252 | errorStream << "filter method: " << method_opt[0] << "=" << filter2d::Filter2d::getFilterType(method_opt[0]) << " not implemented"<< std::endl; |
| 253 | // errorStream << "filter " << method_opt[0] << " (" << )"<< " not implemented"; |
| 254 | throw(errorStream.str()); |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | imgWriter.open((this->nrOfCol()+down_opt[0]-1)/down_opt[0],(this->nrOfRow()+down_opt[0]-1)/down_opt[0],nband,theType); |
| 260 | imgWriter.setProjection(this->getProjection()); |
| 261 | double gt[6]; |
| 262 | this->getGeoTransform(gt); |
| 263 | gt[1]*=down_opt[0];//dx |
| 264 | gt[5]*=down_opt[0];//dy |
| 265 | imgWriter.setGeoTransform(gt); |
| 266 | |
| 267 | if(colorTable_opt.size()){ |
| 268 | if(colorTable_opt[0]!="none"){ |
| 269 | if(verbose_opt[0]) |
| 270 | cout << "set colortable " << colorTable_opt[0] << endl; |
| 271 | assert(imgWriter.getDataType()==GDT_Byte); |
| 272 | imgWriter.setColorTable(colorTable_opt[0]); |
| 273 | } |
| 274 | } |
| 275 | else if(this->getColorTable()!=NULL) |
| 276 | imgWriter.setColorTable(this->getColorTable()); |
| 277 | |
| 278 | if(nodata_opt.size()){ |
| 279 | for(unsigned int iband=0;iband<imgWriter.nrOfBand();++iband) |
| 280 | imgWriter.GDALSetNoDataValue(nodata_opt[0],iband); |
| 281 | } |
| 282 | |
| 283 | filter2d::Filter2d filter2d; |
| 284 | if(verbose_opt[0]) |
| 285 | cout << "Set padding to " << padding_opt[0] << endl; |
| 286 | if(class_opt.size()){ |
| 287 | if(verbose_opt[0]) |
| 288 | std::cout<< "class values: "; |
| 289 | for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 290 | filter2d.pushClass(class_opt[iclass]); |
| 291 | if(verbose_opt[0]) |
| 292 | std::cout<< class_opt[iclass] << " "; |
| 293 | } |
| 294 | if(verbose_opt[0]) |
| 295 | std::cout<< std::endl; |
| 296 | } |
| 297 | |
| 298 | if(nodata_opt.size()){ |
| 299 | if(verbose_opt[0]) |
| 300 | std::cout<< "mask values: "; |
| 301 | for(unsigned int imask=0;imask<nodata_opt.size();++imask){ |
| 302 | if(verbose_opt[0]) |
| 303 | std::cout<< nodata_opt[imask] << " "; |
| 304 | filter2d.pushNoDataValue(nodata_opt[imask]); |
| 305 | } |
| 306 | if(verbose_opt[0]) |
| 307 | std::cout<< std::endl; |
| 308 | } |
| 309 | filter2d.setThresholds(threshold_opt); |
| 310 | |
| 311 | if(tap_opt.size()){ |
| 312 | // ifstream tapfile(tap_opt[0].c_str()); |
| 313 | // assert(tapfile); |
| 314 | Vector2d<double> taps(dimY_opt[0],dimX_opt[0]); |
| 315 | if(tap_opt.size()!=dimX_opt[0]*dimY_opt[0]){ |
| 316 | std::string errorString="Error: wrong dimensions of taps"; |
| 317 | throw(errorString); |
| 318 | } |
| 319 | |
| 320 | for(unsigned int j=0;j<dimY_opt[0];++j){ |
| 321 | for(unsigned int i=0;i<dimX_opt[0];++i){ |
| 322 | taps[j][i]=tap_opt[j*dimX_opt[0]+i]; |
| 323 | // tapfile >> taps[j][i]; |
| 324 | } |
| 325 | } |
| 326 | if(verbose_opt[0]){ |
| 327 | std::cout << "taps: "; |
| 328 | for(unsigned int j=0;j<dimY_opt[0];++j){ |
| 329 | for(unsigned int i=0;i<dimX_opt[0];++i){ |
| 330 | std::cout<< taps[j][i] << " "; |
| 331 | } |
| 332 | std::cout<< std::endl; |
| 333 | } |
| 334 | } |
| 335 | filter2d.setTaps(taps); |
| 336 | filter2d.filter(*this,imgWriter,abs_opt[0],norm_opt[0]); |
| 337 | // filter2d.filterLB(*this,imgWriter,abs_opt[0],norm_opt[0]); |
| 338 | // tapfile.close(); |
| 339 | } |
| 340 | else{ |
| 341 | switch(filter2d::Filter2d::getFilterType(method_opt[0])){ |
| 342 | case(filter2d::dilate): |
| 343 | if(down_opt[0]!=1){ |
| 344 | std::cerr << "Error: down option not supported for morphological operator" << std::endl; |
| 345 | exit(1); |
| 346 | } |
| 347 | filter2d.morphology(*this,imgWriter,"dilate",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 348 | break; |
| 349 | case(filter2d::erode): |
| 350 | if(down_opt[0]!=1){ |
| 351 | std::cerr << "Error: down option not supported for morphological operator" << std::endl; |
| 352 | exit(1); |
| 353 | } |
| 354 | filter2d.morphology(*this,imgWriter,"erode",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 355 | break; |
| 356 | case(filter2d::close):{//closing |
| 357 | if(down_opt[0]!=1){ |
| 358 | std::cerr << "Error: down option not supported for morphological operator" << std::endl; |
| 359 | exit(1); |
| 360 | } |
| 361 | filter2d.morphology(*this,imgWriter,"dilate",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 362 | filter2d.morphology(imgWriter,imgWriter,"erode",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 363 | break; |
| 364 | } |
| 365 | case(filter2d::open):{//opening |
| 366 | if(down_opt[0]!=1){ |
| 367 | std::cerr << "Error: down option not supported for morphological operator" << std::endl; |
| 368 | exit(1); |
| 369 | } |
| 370 | filter2d.morphology(*this,imgWriter,"erode",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 371 | filter2d.morphology(imgWriter,imgWriter,"dilate",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]); |
| 372 | break; |
| 373 | } |
| 374 | case(filter2d::homog):{//spatially homogeneous |
| 375 | filter2d.doit(*this,imgWriter,"homog",dimX_opt[0],dimY_opt[0],down_opt[0],disc_opt[0]); |
| 376 | break; |
| 377 | } |
| 378 | case(filter2d::heterog):{//spatially heterogeneous |
| 379 | filter2d.doit(*this,imgWriter,"heterog",dimX_opt[0],dimY_opt[0],down_opt[0],disc_opt[0]); |
| 380 | break; |
| 381 | } |
| 382 | case(filter2d::sauvola):{//Implements Sauvola's thresholding method (http://fiji.sc/Auto_Local_Threshold) |
| 383 | //test |
| 384 | Vector2d<unsigned short> inBuffer; |
| 385 | for(unsigned int iband=0;iband<this->nrOfBand();++iband){ |
| 386 | this->readDataBlock(inBuffer,0,this->nrOfCol()-1,0,this->nrOfRow()-1,iband); |
| 387 | } |
| 388 | filter2d.doit(*this,imgWriter,"sauvola",dimX_opt[0],dimY_opt[0],down_opt[0],disc_opt[0]); |
| 389 | break; |
| 390 | } |
| 391 | case(filter2d::shift):{//shift |
| 392 | if(down_opt[0]!=1){ |
| 393 | std::cerr << "Error: down option not supported for shift operator" << std::endl; |
| 394 | exit(1); |
| 395 | } |
| 396 | assert(this->nrOfBand()); |
| 397 | assert(this->nrOfCol()); |
| 398 | assert(this->nrOfRow()); |
| 399 | filter2d.shift(*this,imgWriter,dimX_opt[0],dimY_opt[0],threshold_opt[0],filter2d::Filter2d::getResampleType(resample_opt[0])); |
| 400 | break; |
| 401 | } |
| 402 | case(filter2d::mrf):{//Markov Random Field |
| 403 | if(verbose_opt[0]) |
| 404 | std::cout << "Markov Random Field filtering" << std::endl; |
| 405 | if(beta_opt.size()){ |
| 406 | //in file: classFrom classTo |
| 407 | //in variable: beta[classTo][classFrom] |
| 408 | FileReaderAscii betaReader(beta_opt[0]); |
| 409 | Vector2d<double> beta(class_opt.size(),class_opt.size()); |
| 410 | vector<int> cols(class_opt.size()); |
| 411 | for(int iclass=0;iclass<class_opt.size();++iclass) |
| 412 | cols[iclass]=iclass; |
| 413 | betaReader.readData(beta,cols); |
| 414 | if(verbose_opt[0]){ |
| 415 | std::cout << "using values for beta:" << std::endl; |
| 416 | for(int iclass1=0;iclass1<class_opt.size();++iclass1) |
| 417 | std::cout << " " << iclass1 << " (" << class_opt[iclass1] << ")"; |
| 418 | std::cout << std::endl; |
| 419 | for(int iclass1=0;iclass1<class_opt.size();++iclass1){ |
| 420 | std::cout << iclass1 << " (" << class_opt[iclass1] << ")"; |
| 421 | for(int iclass2=0;iclass2<class_opt.size();++iclass2) |
| 422 | std::cout << " " << beta[iclass2][iclass1] << " (" << class_opt[iclass2] << ")"; |
| 423 | std::cout << std::endl; |
| 424 | } |
| 425 | } |
| 426 | filter2d.mrf(*this, imgWriter, dimX_opt[0], dimY_opt[0], beta, true, down_opt[0], verbose_opt[0]); |
| 427 | } |
| 428 | else |
| 429 | filter2d.mrf(*this, imgWriter, dimX_opt[0], dimY_opt[0], 1, true, down_opt[0], verbose_opt[0]); |
| 430 | break; |
| 431 | } |
| 432 | case(filter2d::sobelx):{//Sobel edge detection in X |
| 433 | if(down_opt[0]!=1){ |
| 434 | std::cerr << "Error: down option not supported for sobel edge detection" << std::endl; |
| 435 | exit(1); |
| 436 | } |
| 437 | Vector2d<double> theTaps(3,3); |
| 438 | theTaps[0][0]=-1.0; |
| 439 | theTaps[0][1]=0.0; |
| 440 | theTaps[0][2]=1.0; |
| 441 | theTaps[1][0]=-2.0; |
| 442 | theTaps[1][1]=0.0; |
| 443 | theTaps[1][2]=2.0; |
| 444 | theTaps[2][0]=-1.0; |
| 445 | theTaps[2][1]=0.0; |
| 446 | theTaps[2][2]=1.0; |
| 447 | filter2d.setTaps(theTaps); |
| 448 | filter2d.filter(*this,imgWriter,true,true);//absolute and normalize |
| 449 | break; |
| 450 | } |
| 451 | case(filter2d::sobely):{//Sobel edge detection in Y |
| 452 | if(down_opt[0]!=1){ |
| 453 | std::cerr << "Error: down option not supported for sobel edge detection" << std::endl; |
| 454 | exit(1); |
| 455 | } |
| 456 | Vector2d<double> theTaps(3,3); |
| 457 | theTaps[0][0]=1.0; |
| 458 | theTaps[0][1]=2.0; |
| 459 | theTaps[0][2]=1.0; |
| 460 | theTaps[1][0]=0.0; |
| 461 | theTaps[1][1]=0.0; |
| 462 | theTaps[1][2]=0.0; |
| 463 | theTaps[2][0]=-1.0; |
| 464 | theTaps[2][1]=-2.0; |
| 465 | theTaps[2][2]=-1.0; |
| 466 | filter2d.setTaps(theTaps); |
| 467 | filter2d.filter(*this,imgWriter,true,true);//absolute and normalize |
| 468 | break; |
| 469 | } |
| 470 | case(filter2d::sobelxy):{//Sobel edge detection in XY |
| 471 | if(down_opt[0]!=1){ |
| 472 | std::cerr << "Error: down option not supported for sobel edge detection" << std::endl; |
| 473 | exit(1); |
| 474 | } |
| 475 | Vector2d<double> theTaps(3,3); |
| 476 | theTaps[0][0]=0.0; |
| 477 | theTaps[0][1]=1.0; |
| 478 | theTaps[0][2]=2.0; |
| 479 | theTaps[1][0]=-1.0; |
| 480 | theTaps[1][1]=0.0; |
| 481 | theTaps[1][2]=1.0; |
| 482 | theTaps[2][0]=-2.0; |
| 483 | theTaps[2][1]=-1.0; |
| 484 | theTaps[2][2]=0.0; |
| 485 | filter2d.setTaps(theTaps); |
| 486 | filter2d.filter(*this,imgWriter,true,true);//absolute and normalize |
| 487 | break; |
| 488 | } |
| 489 | case(filter2d::sobelyx):{//Sobel edge detection in XY |
| 490 | if(down_opt[0]!=1){ |
| 491 | std::cerr << "Error: down option not supported for sobel edge detection" << std::endl; |
| 492 | exit(1); |
| 493 | } |
| 494 | Vector2d<double> theTaps(3,3); |
| 495 | theTaps[0][0]=2.0; |
| 496 | theTaps[0][1]=1.0; |
| 497 | theTaps[0][2]=0.0; |
| 498 | theTaps[1][0]=1.0; |
| 499 | theTaps[1][1]=0.0; |
| 500 | theTaps[1][2]=-1.0; |
| 501 | theTaps[2][0]=0.0; |
| 502 | theTaps[2][1]=-1.0; |
| 503 | theTaps[2][2]=-2.0; |
| 504 | filter2d.setTaps(theTaps); |
| 505 | filter2d.filter(*this,imgWriter,true,true);//absolute and normalize |
| 506 | break; |
| 507 | } |
| 508 | case(filter2d::smooth):{//Smoothing filter |
| 509 | if(down_opt[0]!=1){ |
| 510 | std::cerr << "Error: down option not supported for this filter" << std::endl; |
| 511 | exit(1); |
| 512 | } |
| 513 | filter2d.smooth(*this,imgWriter,dimX_opt[0],dimY_opt[0]); |
| 514 | break; |
| 515 | } |
| 516 | case(filter2d::smoothnodata):{//Smoothing filter |
| 517 | if(down_opt[0]!=1){ |
| 518 | std::cerr << "Error: down option not supported for this filter" << std::endl; |
| 519 | exit(1); |
| 520 | } |
| 521 | if(verbose_opt[0]) |
| 522 | std::cout<< "2-D filtering: smooth" << std::endl; |
| 523 | filter2d.smoothNoData(*this,imgWriter,dimX_opt[0],dimY_opt[0]); |
| 524 | break; |
| 525 | } |
| 526 | case(filter2d::dwt): |
| 527 | if(down_opt[0]!=1){ |
| 528 | std::cerr << "Error: down option not supported for this filter" << std::endl; |
| 529 | exit(1); |
| 530 | } |
| 531 | filter2d.dwtForward(*this, imgWriter, wavelet_type_opt[0], family_opt[0]); |
| 532 | break; |
| 533 | case(filter2d::dwti): |
| 534 | if(down_opt[0]!=1){ |
| 535 | std::cerr << "Error: down option not supported for this filter" << std::endl; |
| 536 | exit(1); |
| 537 | } |
| 538 | filter2d.dwtInverse(*this, imgWriter, wavelet_type_opt[0], family_opt[0]); |
| 539 | break; |
| 540 | case(filter2d::dwt_cut): |
| 541 | if(down_opt[0]!=1){ |
| 542 | std::cerr << "Error: down option not supported for this filter" << std::endl; |
| 543 | exit(1); |
| 544 | } |
| 545 | filter2d.dwtCut(*this, imgWriter, wavelet_type_opt[0], family_opt[0], threshold_opt[0]); |
| 546 | break; |
| 547 | case(filter2d::percentile)://deliberate fall through |
| 548 | case(filter2d::threshold)://deliberate fall through |
| 549 | assert(threshold_opt.size()); |
| 550 | filter2d.setThresholds(threshold_opt); |
| 551 | case(filter2d::density)://deliberate fall through |
| 552 | filter2d.setClasses(class_opt); |
| 553 | if(verbose_opt[0]) |
| 554 | std::cout << "classes set" << std::endl; |
| 555 | default: |
| 556 | filter2d.doit(*this,imgWriter,method_opt[0],dimX_opt[0],dimY_opt[0],down_opt[0],disc_opt[0]); |
| 557 | break; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | catch(string predefinedString){ |
| 562 | std::cout << predefinedString << std::endl; |
| 563 | throw; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | std::shared_ptr<Jim> Jim::firfilter2d(app::AppFactory& app){ |
| 568 | try{ |
| 569 | shared_ptr<Jim> imgWriter=createImg(); |
| 570 | firfilter2d(*imgWriter, app); |
| 571 | return(imgWriter); |
| 572 | } |
| 573 | catch(std::string helpString){ |
| 574 | cerr << helpString << endl; |
| 575 | throw; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | void Jim::firfilter2d(Jim& imgWriter, app::AppFactory& app){ |
| 580 | imgWriter.open(nrOfCol(),nrOfRow(),nrOfBand(),nrOfPlane(),getGDALDataType()); |
| 581 | imgWriter.setProjection(getProjection()); |
| 582 | double gt[6]; |
| 583 | this->getGeoTransform(gt); |
| 584 | imgWriter.setGeoTransform(gt); |
| 585 | switch(getDataType()){ |
| 586 | case(GDT_Byte): |
| 587 | firfilter2d_t<unsigned char>(imgWriter,app); |
| 588 | break; |
| 589 | case(GDT_Int16): |
| 590 | firfilter2d_t<short>(imgWriter,app); |
| 591 | break; |
| 592 | case(GDT_UInt16): |
| 593 | firfilter2d_t<unsigned short>(imgWriter,app); |
| 594 | break; |
| 595 | case(GDT_Int32): |
| 596 | firfilter2d_t<int>(imgWriter,app); |
| 597 | break; |
| 598 | case(GDT_UInt32): |
| 599 | firfilter2d_t<unsigned int>(imgWriter,app); |
| 600 | break; |
| 601 | case(GDT_Float32): |
| 602 | firfilter2d_t<float>(imgWriter,app); |
| 603 | break; |
| 604 | case(GDT_Float64): |
| 605 | firfilter2d_t<double>(imgWriter,app); |
| 606 | break; |
| 607 | default: |
| 608 | std::string errorString="Error: data type not supported"; |
| 609 | throw(errorString); |
| 610 | break; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | shared_ptr<Jim> Jim::dwt2d(app::AppFactory& app){ |
| 615 | try{ |
| 616 | shared_ptr<Jim> imgWriter=clone(); |
| 617 | imgWriter->d_dwt2d(app); |
| 618 | return(imgWriter); |
| 619 | } |
| 620 | catch(std::string helpString){ |
| 621 | cerr << helpString << endl; |
| 622 | throw; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | void Jim::d_dwt2d(app::AppFactory& app){ |
| 627 | Optionjl<std::string> wavelet_type_opt("wt", "wavelet", "wavelet type: daubechies,daubechies_centered, haar, haar_centered, bspline, bspline_centered", "daubechies"); |
| 628 | Optionjl<int> family_opt("wf", "family", "wavelet family (vanishing moment, see also https://www.gnu.org/software/gsl/doc/html/dwt.html)", 4); |
| 629 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 630 | try{ |
| 631 | doProcess=wavelet_type_opt.retrieveOption(app); |
| 632 | family_opt.retrieveOption(app); |
| 633 | if(!doProcess){ |
| 634 | cout << endl; |
| 635 | std::ostringstream helpStream; |
| 636 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 637 | throw(helpStream.str());//help was invoked, stop processing |
| 638 | } |
| 639 | |
| 640 | if(nrOfPlane()!=1){ |
| 641 | std::ostringstream errorStream; |
| 642 | errorStream << "Error: multi-plane images are not supported" << std::endl; |
| 643 | throw(errorStream.str()); |
| 644 | } |
| 645 | if(getDataType()!=GDT_Float64){ |
| 646 | std::ostringstream errorStream; |
| 647 | errorStream << "Error: data type must be GDT_Float64" << std::endl; |
| 648 | throw(errorStream.str()); |
| 649 | } |
| 650 | size_t oldrow=nrOfRow(); |
| 651 | size_t oldcol=nrOfCol(); |
| 652 | size_t newrow=nrOfRow(); |
| 653 | size_t newcol=nrOfCol(); |
| 654 | while(newrow&(newrow-1)) |
| 655 | ++newrow; |
| 656 | while(newcol&(newcol-1)) |
| 657 | ++newcol; |
| 658 | std::vector<int> box{0,static_cast<int>(newcol-oldcol),0,static_cast<int>(newrow-oldrow),0,0}; |
| 659 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 660 | #pragma omp parallel for |
| 661 | #else |
| 662 | #endif |
| 663 | for(size_t iband=0;iband<nrOfBand();++iband){ |
| 664 | //make sure data size if power of 2 |
| 665 | //dangerous, we are not adapting the ncol and nrow, do not use nrOfCol() nor nrOfRow() within this block (before d_imageFrameSubtract) |
| 666 | d_imageFrameAdd(box.data(),0.0); |
| 667 | int nsize=newrow*newcol; |
| 668 | gsl_wavelet *w; |
| 669 | gsl_wavelet_workspace *work; |
| 670 | w=gsl_wavelet_alloc(filter::Filter::getWaveletType(wavelet_type_opt[0]),family_opt[0]); |
| 671 | work=gsl_wavelet_workspace_alloc(nsize); |
| 672 | double* pin=static_cast<double*>(getDataPointer(iband)); |
| 673 | gsl_wavelet2d_nstransform_forward (w, pin, newrow, newrow, newcol, work); |
| 674 | gsl_wavelet_free (w); |
| 675 | gsl_wavelet_workspace_free (work); |
| 676 | d_imageFrameSubtract(box.data()); |
| 677 | } |
| 678 | } |
| 679 | catch(std::string predefinedString ){ |
| 680 | std::cout << predefinedString << std::endl; |
| 681 | throw; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | shared_ptr<Jim> Jim::dwti2d(app::AppFactory& app){ |
| 686 | try{ |
| 687 | shared_ptr<Jim> imgWriter=clone(); |
| 688 | imgWriter->d_dwti2d(app); |
| 689 | return(imgWriter); |
| 690 | } |
| 691 | catch(std::string helpString){ |
| 692 | cerr << helpString << endl; |
| 693 | throw; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | void Jim::d_dwti2d(app::AppFactory& app){ |
| 698 | Optionjl<std::string> wavelet_type_opt("wt", "wavelet", "wavelet type: daubechies,daubechies_centered, haar, haar_centered, bspline, bspline_centered", "daubechies"); |
| 699 | Optionjl<int> family_opt("wf", "family", "wavelet family (vanishing moment, see also https://www.gnu.org/software/gsl/doc/html/dwt.html)", 4); |
| 700 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 701 | try{ |
| 702 | doProcess=wavelet_type_opt.retrieveOption(app); |
| 703 | family_opt.retrieveOption(app); |
| 704 | if(!doProcess){ |
| 705 | cout << endl; |
| 706 | std::ostringstream helpStream; |
| 707 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 708 | throw(helpStream.str());//help was invoked, stop processing |
| 709 | } |
| 710 | |
| 711 | if(nrOfPlane()!=1){ |
| 712 | std::ostringstream errorStream; |
| 713 | errorStream << "Error: multi-plane images are not supported" << std::endl; |
| 714 | throw(errorStream.str()); |
| 715 | } |
| 716 | if(getDataType()!=GDT_Float64){ |
| 717 | std::ostringstream errorStream; |
| 718 | errorStream << "Error: data type must be GDT_Float64" << std::endl; |
| 719 | throw(errorStream.str()); |
| 720 | } |
| 721 | size_t oldrow=nrOfRow(); |
| 722 | size_t oldcol=nrOfCol(); |
| 723 | size_t newrow=nrOfRow(); |
| 724 | size_t newcol=nrOfCol(); |
| 725 | while(newrow&(newrow-1)) |
| 726 | ++newrow; |
| 727 | while(newcol&(newcol-1)) |
| 728 | ++newcol; |
| 729 | std::vector<int> box{0,static_cast<int>(newcol-oldcol),0,static_cast<int>(newrow-oldrow),0,0}; |
| 730 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 731 | #pragma omp parallel for |
| 732 | #else |
| 733 | #endif |
| 734 | for(size_t iband=0;iband<nrOfBand();++iband){ |
| 735 | //make sure data size if power of 2 |
| 736 | //dangerous, we are not adapting the ncol and nrow, do not use nrOfCol() nor nrOfRow() within this block (before d_imageFrameSubtract) |
| 737 | d_imageFrameAdd(box.data(),0.0); |
| 738 | int nsize=newrow*newcol; |
| 739 | gsl_wavelet *w; |
| 740 | gsl_wavelet_workspace *work; |
| 741 | w=gsl_wavelet_alloc(filter::Filter::getWaveletType(wavelet_type_opt[0]),family_opt[0]); |
| 742 | work=gsl_wavelet_workspace_alloc(nsize); |
| 743 | double* pin=static_cast<double*>(getDataPointer(iband)); |
| 744 | gsl_wavelet2d_nstransform_inverse (w, pin, newrow, newrow, newcol, work); |
| 745 | gsl_wavelet_free (w); |
| 746 | gsl_wavelet_workspace_free (work); |
| 747 | d_imageFrameSubtract(box.data()); |
| 748 | } |
| 749 | } |
| 750 | catch(std::string predefinedString ){ |
| 751 | std::cout << predefinedString << std::endl; |
| 752 | throw; |
| 753 | } |
| 754 | } |