File: pkfilter.cc

package info (click to toggle)
pktools 2.6.7.6%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,056 kB
  • sloc: cpp: 33,451; xml: 10,955; python: 1,601; makefile: 142; sh: 58; ansic: 24
file content (1171 lines) | stat: -rw-r--r-- 47,032 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
/**********************************************************************
pkfilter.cc: program to filter raster images: median, min/max, morphological, filtering
Copyright (C) 2008-2014 Pieter Kempeneers

This file is part of pktools

pktools is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

pktools is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with pktools.  If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/
#include <assert.h>
#include <iostream>
#include <string>
#include <fstream>
#include <math.h>
#include <sys/types.h>
#include <stdio.h>
#include "base/Optionpk.h"
#include "base/Vector2d.h"
#include "algorithms/Filter2d.h"
#include "algorithms/Filter.h"
#include "fileclasses/FileReaderAscii.h"
#include "imageclasses/ImgReaderGdal.h"
#include "imageclasses/ImgWriterGdal.h"
#include "algorithms/StatFactory.h"

/******************************************************************************/
/*! \page pkfilter pkfilter
 program to filter raster images
## SYNOPSIS

<code>
  Usage: pkfilter -i input -o output [-f filter | -perc value | -srf file [-srf file]* -win wavelength [-win wavelength]* | -wout wavelength -fwhm value [-wout wavelength -fwhm value]* -win wavelength [-win wavelength]*]
</code>

<code>
  Options: [-dx value [-dy value] | -dz value] [-nodata value]

  Advanced options: check table
</code>

\section pkfilter_description Description

This utility implements spatial and spectral filtering for raster data. In the spatial domain (X, Y), the filter typically involves a rectangular convolution kernel (moving window). To avoid image shifting, the size of the window should be odd (3, 5, 7, ...). You can set the window sizes in X and Y directions separately with the options -dx and -dy. A circular kernel (disc) is applied if option -circ is set. An overview of the supported filters (option -f|--filter) is given below. You can create customized filters by defining your own filter taps (multiplicative elements of the filter kernel) via an ascii file (option -tap). In the spectral/temporal domain (Z) you can filter multi-band raster inputs. The kernel filter size can be set with the option -dz (use odd values only).

\anchor pkfilter_functions

\subsection pkfilter_functions_1 Filters in spatial (dx, dy) and spectral/temporal (dz) domain

\subsubsection pkfilter_functions_1_1 Implemented as moving window: choose dx, dy or dz > 1 and odd (3, 5, 7, etc.)

The number of output bands equals number of input bands

|filter|description|
|-------------|-------------|
|dilate|morphological dilation|
|erode|morphological erosion|
|close|morpholigical closing (dilate+erode)|
|open|morpholigical opening (erode+dilate)|
|smoothnodata values|smooth nodata values (set nodata option!)|

Example: "Smooth" (interpolate) nodata in spectral/temporal domain (-dz 1), using a linear interpolation
\code
pkfilter -i input.tif -o smoothed.tif -dz 1 -f smoothnodata -interp linear
\endcode

Example: Filter input.tif in spatial domain with morphological dilation filter with kernel size 3x3.

\code
pkfilter -i input.tif -o dilated.tif -dx 3 -dy 3 -f dilate
\endcode

\subsubsection pkfilter_functions_1_2 Implemented as either moving window or statistical function in spectral/temporal domain (choose dz=1). 

In case of moving window, the number of output bands equals number of input bands. In case dz=1, the single output band is calculated as the result of the statistical function applied to all bands.

|filter | description|
|-------------|-------------|
nvalid | report number of valid (not nodata) values in window
median | perform a median filter in spatial (dx, dy) or spectral/temporal (dz) domain
var | calculate variance in window
min | calculate minimum in window
max | calculate maximum in window
sum | calculate sum in window
mean | calculate mean in window
stdev | calculate standard deviation in window
savgolay | Savitzky-Golay filter (check examples page!)
percentile | calculate percentile value in window
proportion | calculate proportion in windoww

Example: Median filter in spatial domain

\code
pkfilter -i input.tif -o median.tif -dx 3 -dy 3 -f median
\endcode

Example: Calculate statistical variance in spectral/temporal domain (single output band)

\code
pkfilter -i input.tif -o var.tif -dz 1 -f var
\endcode

\subsection pkfilter_functions_2 Wavelet filters 

\subsubsection pkfilter_functions_2_1 Wavelet filter in in spatial or spectral/temporal (set dz = 1) domain. 

The number of output bands equals number of input bands

|filter | description|
|-------------|-------------|
dwt | discrete wavelet transform
dwti | discrete inverse wavelet transform
dwt_cut | discrete wavelet + inverse transform, using threshold option to cut percentile of coefficients 

Example: Calculate discrete wavelet in spatial domain

\code
pkfilter -i lena.tif -o lena_dwt.tif -f dwt
\endcode

Example: Calculate discrete wavelet in spectral/temporal domain

\code
pkfilter -i timeseries.tif -o dwt.tif -f dwt -dz 1
\endcode

\subsubsection pkfilter_functions_2_2 Wavelet filter implemented in spectral/temporal domain only. 

The number of output bands equals number of input bands

|filter | description|
|-------------|-------------|
dwt_cut_from | discrete wavelet + inverse transform, setting all high frequence coefficients to zero (scale >= threshold)


Example: Calculate low frequency time series based on discrete wavelet + inverse transform in spectral/temporal domain, retaining only coefficients until scale 3

\code
pkfilter -i timeseries.tif -o lowfrequency.tif -f dwt_cut_from -dz 1 -t 4
\endcode

\subsection pkfilter_functions_3 Filters in spatial domain only (dx, dy > 1 and odd). 

The number of output bands equals number of input bands. 

|filter | description|
|-------------|-------------|
mrf | Markov random field
ismin | pixel is minimum?
ismax | pixel is maximum?
shift | perform a pixel shift in spatial window
scramble | scramble pixels in a spatial window
mode (majority voting) | perform a majority voring (set class option)
sobelx | horizontal edge detection
sobely | vertical edge detection 
sobelxy | diagonal edge detection (NE-SW)
sobelyx | diagonal edge detection (NW-SE)
countid | count digital numbers in window
order | rank pixels in order
density | calculated the density
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 | Sauvola's thresholding method

Example: Sobel edge detection in horizontal direction

\code
pkfilter -i lena.tif -o sobelx.tif -f sobelx -dx 5 -dy 5
\endcode

\section pkfilter_options Options
 - use either `-short` or `--long` options (both `--long=value` and `--long value` are supported)
 - short option `-h` shows basic options only, long option `--help` shows all options
|short|long|type|default|description|
|-----|----|----|-------|-----------|
 | i      | input                | std::string |       |input image file | 
 | o      | output               | std::string |       |Output image file | 
 | f      | filter               | 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), 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) | 
 | srf    | srf                  | std::string |       |list of ASCII files containing spectral response functions (two columns: wavelength response) | 
 | fwhm   | fwhm                 | double |       |list of full width half to apply spectral filtering (-fwhm band1 -fwhm band2 ...) | 
 | dx     | dx                   | double | 3     |filter kernel size in x, use odd values only | 
 | dy     | dy                   | double | 3     |filter kernel size in y, use odd values only | 
 | dz     | dz                   | int  |       |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 | 
 | nodata | nodata               | double |       |nodata value(s) (used for smoothnodata filter) | 
 | r      | resampling-method    | std::string | near  |Resampling method for shifting operation (near: nearest neighbour, bilinear: bi-linear interpolation). | 
 | co     | co                   | std::string |       |Creation option for output file. Multiple options can be specified. | 
 | wt     | wavelet              | std::string | daubechies |wavelet type: daubechies,daubechies_centered, haar, haar_centered, bspline, bspline_centered | 
 | wf     | family               | int  | 4     |wavelet family (vanishing moment, see also http://www.gnu.org/software/gsl/manual/html_node/DWT-Initialization.html) | 
 | nl     | nl                   | int  | 2     |Number of leftward (past) data points used in Savitzky-Golay filter) | 
 | nr     | nr                   | int  | 2     |Number of rightward (future) data points used in Savitzky-Golay filter) | 
 | ld     | ld                   | int  | 0     |order of the derivative desired in Savitzky-Golay filter (e.g., ld=0 for smoothed function) | 
 | m      | m                    | int  | 2     |order of the smoothing polynomial in Savitzky-Golay filter, also equal to the highest conserved moment; usual values are m = 2 or m = 4) | 
 | class  | class                | short |       |class value(s) to use for density, erosion, dilation, openening and closing, thresholding | 
 | t      | threshold            | double | 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 | 
 | tap    | tap                  | 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 | 
 | tapz   | tapz                 | double |       |taps used for spectral filtering | 
 | pad    | pad                  | std::string | symmetric |Padding method for filtering (how to handle edge effects). Choose between: symmetric, replicate, circular, zero (pad with 0). | 
 | win    | wavelengthIn         | double |       |list of wavelengths in input spectrum (-win band1 -win band2 ...) | 
 | wout   | wavelengthOut        | double |       |list of wavelengths in output spectrum (-wout band1 -wout band2 ...) | 
 | d      | down                 | short | 1     |down sampling factor. Use value 1 for no downsampling). Use value n>1 for downsampling (aggregation) | 
 | beta   | beta                 | std::string |       |ASCII file with beta for each class transition in Markov Random Field | 
 | interp | interp               | std::string | akima |type of interpolation for spectral filtering (see http://www.gnu.org/software/gsl/manual/html_node/Interpolation-Types.html) | 
 | ot     | otype                | 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 | 
 | of     | oformat              | std::string | GTiff |Output image format (see also gdal_translate).| 
 | ct     | ct                   | std::string |       |color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid). Use none to omit color table | 
 | circ   | circular             | bool | false |circular disc kernel for dilation and erosion | 

Usage: pkfilter -i input -o output [-f filter | -perc value | -srf file [-srf file]* -win wavelength [-win wavelength]* | -wout wavelength -fwhm value [-wout wavelength -fwhm value]* -win wavelength [-win wavelength]*]


Examples
========
Some examples how to use pkfilter can be found \ref examples_pkfilter "here"
**/

using namespace std;
/*------------------
  Main procedure
  ----------------*/
int main(int argc,char **argv) {
  Optionpk<std::string> input_opt("i","input","input image file");
  Optionpk<std::string> output_opt("o", "output", "Output image file");
  // Optionpk<std::string> tmpdir_opt("tmp", "tmp", "Temporary directory","/tmp",2);
  Optionpk<bool> disc_opt("circ", "circular", "circular disc kernel for dilation and erosion", false);
  // Optionpk<double> angle_opt("a", "angle", "angle used for directional filtering in dilation (North=0, East=90, South=180, West=270).");
  Optionpk<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), 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)");
  Optionpk<std::string> resample_opt("r", "resampling-method", "Resampling method for shifting operation (near: nearest neighbour, bilinear: bi-linear interpolation).", "near");
  Optionpk<double> dimX_opt("dx", "dx", "filter kernel size in x, use odd values only", 3);
  Optionpk<double> dimY_opt("dy", "dy", "filter kernel size in y, use odd values only", 3);
  Optionpk<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");
  Optionpk<std::string> wavelet_type_opt("wt", "wavelet", "wavelet type: daubechies,daubechies_centered, haar, haar_centered, bspline, bspline_centered", "daubechies");
  Optionpk<int> family_opt("wf", "family", "wavelet family (vanishing moment, see also http://www.gnu.org/software/gsl/manual/html_node/DWT-Initialization.html)", 4);
  Optionpk<int> savgolay_nl_opt("nl", "nl", "Number of leftward (past) data points used in Savitzky-Golay filter)", 2);
  Optionpk<int> savgolay_nr_opt("nr", "nr", "Number of rightward (future) data points used in Savitzky-Golay filter)", 2);
  Optionpk<int> savgolay_ld_opt("ld", "ld", "order of the derivative desired in Savitzky-Golay filter (e.g., ld=0 for smoothed function)", 0);
  Optionpk<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);
  Optionpk<short> class_opt("class", "class", "class value(s) to use for density, erosion, dilation, openening and closing, thresholding");
  Optionpk<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);
  Optionpk<double> nodata_opt("nodata", "nodata", "nodata value(s) (used for smoothnodata filter)");
  Optionpk<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");
  Optionpk<double> tapz_opt("tapz", "tapz", "taps used for spectral filtering");
  Optionpk<string> padding_opt("pad","pad", "Padding method for filtering (how to handle edge effects). Choose between: symmetric, replicate, circular, zero (pad with 0).", "symmetric");
  Optionpk<double> fwhm_opt("fwhm", "fwhm", "list of full width half to apply spectral filtering (-fwhm band1 -fwhm band2 ...)");
  Optionpk<std::string> srf_opt("srf", "srf", "list of ASCII files containing spectral response functions (two columns: wavelength response)");
  Optionpk<double> wavelengthIn_opt("win", "wavelengthIn", "list of wavelengths in input spectrum (-win band1 -win band2 ...)");
  Optionpk<double> wavelengthOut_opt("wout", "wavelengthOut", "list of wavelengths in output spectrum (-wout band1 -wout band2 ...)");
  Optionpk<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");
  Optionpk<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","");
  Optionpk<string>  oformat_opt("of", "oformat", "Output image format (see also gdal_translate).","GTiff");
  Optionpk<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");
  Optionpk<string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified.");
  Optionpk<short> down_opt("d", "down", "down sampling factor. Use value 1 for no downsampling). Use value n>1 for downsampling (aggregation)", 1);
  Optionpk<string> beta_opt("beta", "beta", "ASCII file with beta for each class transition in Markov Random Field");
  // Optionpk<double> eps_opt("eps","eps", "error marging for linear feature",0);
  // Optionpk<bool> l1_opt("l1","l1", "obtain longest object length for linear feature",false);
  // Optionpk<bool> l2_opt("l2","l2", "obtain shortest object length for linear feature",false,2);
  // Optionpk<bool> a1_opt("a1","a1", "obtain angle found for longest object length for linear feature",false);
  // Optionpk<bool> a2_opt("a2","a2", "obtain angle found for shortest object length for linear feature",false);
  Optionpk<short> verbose_opt("v", "verbose", "verbose mode if > 0", 0,2);

  resample_opt.setHide(1);
  option_opt.setHide(1);
  wavelet_type_opt.setHide(1);
  family_opt.setHide(1);
  savgolay_nl_opt.setHide(1);
  savgolay_nr_opt.setHide(1);
  savgolay_ld_opt.setHide(1);
  savgolay_m_opt.setHide(1);
  class_opt.setHide(1);
  threshold_opt.setHide(1);
  tap_opt.setHide(1);
  tapz_opt.setHide(1);
  padding_opt.setHide(1);
  wavelengthIn_opt.setHide(1);
  wavelengthOut_opt.setHide(1);
  down_opt.setHide(1);
  beta_opt.setHide(1);
  // eps_opt.setHide(1);
  // l1_opt.setHide(1);
  // l2_opt.setHide(1);
  // a1_opt.setHide(1);
  // a2_opt.setHide(1);
  interpolationType_opt.setHide(1);
  otype_opt.setHide(1);
  oformat_opt.setHide(1);
  colorTable_opt.setHide(1);
  disc_opt.setHide(1);

  bool doProcess;//stop process when program was invoked with help option (-h --help)
  try{
    doProcess=input_opt.retrieveOption(argc,argv);
    output_opt.retrieveOption(argc,argv);
    // tmpdir_opt.retrieveOption(argc,argv);
    // angle_opt.retrieveOption(argc,argv);
    method_opt.retrieveOption(argc,argv);
    srf_opt.retrieveOption(argc,argv);
    fwhm_opt.retrieveOption(argc,argv);
    dimX_opt.retrieveOption(argc,argv);
    dimY_opt.retrieveOption(argc,argv);
    dimZ_opt.retrieveOption(argc,argv);
    nodata_opt.retrieveOption(argc,argv);
    resample_opt.retrieveOption(argc,argv);
    option_opt.retrieveOption(argc,argv);
    wavelet_type_opt.retrieveOption(argc,argv);
    family_opt.retrieveOption(argc,argv);
    savgolay_nl_opt.retrieveOption(argc,argv);
    savgolay_nr_opt.retrieveOption(argc,argv);
    savgolay_ld_opt.retrieveOption(argc,argv);
    savgolay_m_opt.retrieveOption(argc,argv);
    class_opt.retrieveOption(argc,argv);
    threshold_opt.retrieveOption(argc,argv);
    tap_opt.retrieveOption(argc,argv);
    tapz_opt.retrieveOption(argc,argv);
    padding_opt.retrieveOption(argc,argv);
    wavelengthIn_opt.retrieveOption(argc,argv);
    wavelengthOut_opt.retrieveOption(argc,argv);
    down_opt.retrieveOption(argc,argv);
    beta_opt.retrieveOption(argc,argv);
    // eps_opt.retrieveOption(argc,argv);
    // l1_opt.retrieveOption(argc,argv);
    // l2_opt.retrieveOption(argc,argv);
    // a1_opt.retrieveOption(argc,argv);
    // a2_opt.retrieveOption(argc,argv);
    interpolationType_opt.retrieveOption(argc,argv);
    otype_opt.retrieveOption(argc,argv);
    oformat_opt.retrieveOption(argc,argv);
    colorTable_opt.retrieveOption(argc,argv);
    disc_opt.retrieveOption(argc,argv);
    verbose_opt.retrieveOption(argc,argv);
  }
  catch(string predefinedString){
    std::cout << predefinedString << std::endl;
    exit(0);
  }
  if(!doProcess){
    cout << endl;
    cout << "Usage: pkfilter -i input -o output [-f filter | -perc value | -srf file [-srf file]* -win wavelength [-win wavelength]* | -wout wavelength -fwhm value [-wout wavelength -fwhm value]* -win wavelength [-win wavelength]*]" << endl;
    cout << endl;
    std::cout << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
    exit(0);//help was invoked, stop processing
  }

  //not implemented yet, must debug first...
  vector<double> angle_opt;

  ImgReaderGdal input;
  ImgWriterGdal output;
  if(input_opt.empty()){
    cerr << "Error: no input file selected, use option -i" << endl;
    exit(1);
  }
  if(output_opt.empty()){
    cerr << "Error: no output file selected, use option -o" << endl;
    exit(1);
  }
  input.open(input_opt[0]);
  GDALDataType theType=GDT_Unknown;
  if(verbose_opt[0])
    cout << "possible output data types: ";
  for(int iType = 0; iType < GDT_TypeCount; ++iType){
    if(verbose_opt[0])
      cout << " " << GDALGetDataTypeName((GDALDataType)iType);
    if( GDALGetDataTypeName((GDALDataType)iType) != NULL
        && EQUAL(GDALGetDataTypeName((GDALDataType)iType),
                 otype_opt[0].c_str()))
      theType=(GDALDataType) iType;
  }
  if(theType==GDT_Unknown)
    theType=input.getDataType();

  if(verbose_opt[0])
    std::cout << std::endl << "Output pixel type:  " << GDALGetDataTypeName(theType) << endl;

  string imageType;//=input.getImageType();
  if(oformat_opt.size())
    imageType=oformat_opt[0];

  if(option_opt.findSubstring("INTERLEAVE=")==option_opt.end()){
    string theInterleave="INTERLEAVE=";
    theInterleave+=input.getInterleave();
    option_opt.push_back(theInterleave);
  }

  try{
    string errorString;
    int nband=input.nrOfBand();

    if(fwhm_opt.size())
      nband=fwhm_opt.size();
    else if(srf_opt.size())
      nband=srf_opt.size();
    else if(tap_opt.size()||tapz_opt.size())
      nband=input.nrOfBand();
    else{
      if(method_opt.empty()){
        errorString="Error: no filter selected, use option -f";
        throw(errorString);
      }
      switch(filter2d::Filter2d::getFilterType(method_opt[0])){
      case(filter2d::dilate):
      case(filter2d::erode):
      case(filter2d::close):
      case(filter2d::open):
      case(filter2d::smooth):
	//implemented in spectral/temporal domain (dimZ>1) and spatial domain
	if(dimZ_opt.size())
	  assert(dimZ_opt[0]>1);
      nband=input.nrOfBand();
      break;
      case(filter2d::dwt):
      case(filter2d::dwti):
      case(filter2d::dwt_cut):
      case(filter2d::smoothnodata):
	//implemented in spectral/temporal/spatial domain and nband always input.nrOfBand()
	nband=input.nrOfBand();
	break;
      case(filter2d::savgolay):
	nband=input.nrOfBand();
	if(dimZ_opt.empty())
	  dimZ_opt.push_back(1);
      case(filter2d::dwt_cut_from):
	//only implemented in spectral/temporal domain
	if(dimZ_opt.size()){
	  nband=input.nrOfBand();
	  assert(threshold_opt.size());
	}
	else{
          errorString="filter not implemented in spatial domain";
          throw(errorString);
	}
	break;
      case(filter2d::mrf)://deliberate fall through
	assert(class_opt.size()>1);
	if(verbose_opt[0])
	  std::cout << "opening output image " << output_opt[0] << std::endl;
	nband=class_opt.size();
      case(filter2d::ismin):
      case(filter2d::ismax):
      case(filter2d::shift):
      case(filter2d::scramble):
      case(filter2d::mode):
      case(filter2d::sobelx):
      case(filter2d::sobely):
      case(filter2d::sobelxy):
      case(filter2d::countid):
      case(filter2d::order):
      case(filter2d::density):
      case(filter2d::homog):
      case(filter2d::heterog):
      case(filter2d::sauvola):
	//only implemented in spatial domain
	if(dimZ_opt.size()){
          errorString="filter not implemented in spectral/temporal domain";
          throw(errorString);
	}
	break;
      // case(filter2d::percentile):
      // 	//implemented in spectral/temporal/spatial domain and nband 1 if dimZ>0
      // 	if(dimZ_opt.size()){
      // 	  dimZ_opt[0]=1;
      // 	  nband=1;
      // 	}
      // 	else
      // 	  nband=input.nrOfBand();
      // 	break;
      case(filter2d::sum):
      case(filter2d::mean):
      case(filter2d::min):
      case(filter2d::max):
      case(filter2d::var):
      case(filter2d::stdev):
      case(filter2d::nvalid):
      case(filter2d::median):
      case(filter2d::percentile):
      case(filter2d::proportion):
	//implemented in spectral/temporal/spatial domain and nband 1 if dimZ==1
	if(dimZ_opt.size()==1)
	  if(dimZ_opt[0]==1)
	    nband=1;
	else
	  nband=input.nrOfBand();
	break;
      default:
	errorString="filter not implemented";
        throw(errorString);
	break;
      }
    }
    std::cout << "opening output image " << output_opt[0] << " with " << nband << " bands" << std::endl;
    output.open(output_opt[0],(input.nrOfCol()+down_opt[0]-1)/down_opt[0],(input.nrOfRow()+down_opt[0]-1)/down_opt[0],nband,theType,imageType,option_opt);
  }
  catch(string errorstring){
    cerr << errorstring << endl;
    exit(1);
  }
  output.setProjection(input.getProjection());
  double gt[6];
  input.getGeoTransform(gt);
  gt[1]*=down_opt[0];//dx
  gt[5]*=down_opt[0];//dy
  output.setGeoTransform(gt);
  
  if(colorTable_opt.size()){
    if(colorTable_opt[0]!="none"){
      if(verbose_opt[0])
	cout << "set colortable " << colorTable_opt[0] << endl;
      assert(output.getDataType()==GDT_Byte);
      output.setColorTable(colorTable_opt[0]);
    }
  }
  else if(input.getColorTable()!=NULL)
    output.setColorTable(input.getColorTable());
  
  if(nodata_opt.size()){
      for(int iband=0;iband<output.nrOfBand();++iband)
	output.GDALSetNoDataValue(nodata_opt[0],iband);
  }

  filter2d::Filter2d filter2d;
  filter::Filter filter1d;
  if(verbose_opt[0])
    cout << "Set padding to " << padding_opt[0] << endl;
  filter1d.setPadding(padding_opt[0]);
  if(class_opt.size()){
    if(verbose_opt[0])
      std::cout<< "class values: ";
    for(int iclass=0;iclass<class_opt.size();++iclass){
      if(!dimZ_opt.size())
        filter2d.pushClass(class_opt[iclass]);
      else
        filter1d.pushClass(class_opt[iclass]);
      if(verbose_opt[0])
        std::cout<< class_opt[iclass] << " ";
    }
    if(verbose_opt[0])
      std::cout<< std::endl;
  }

  if(nodata_opt.size()){
    if(verbose_opt[0])
      std::cout<< "mask values: ";
    for(int imask=0;imask<nodata_opt.size();++imask){
      if(verbose_opt[0])
        std::cout<< nodata_opt[imask] << " ";
      filter1d.pushNoDataValue(nodata_opt[imask]);
      filter2d.pushNoDataValue(nodata_opt[imask]);
    }
    if(verbose_opt[0])
      std::cout<< std::endl;
  }
  filter1d.setThresholds(threshold_opt);
  filter2d.setThresholds(threshold_opt);

  if(tap_opt.size()){
    ifstream tapfile(tap_opt[0].c_str());
    assert(tapfile);
    Vector2d<double> taps(dimY_opt[0],dimX_opt[0]);

    for(int j=0;j<dimY_opt[0];++j){
      for(int i=0;i<dimX_opt[0];++i){
        tapfile >> taps[j][i];
      }
    }
    if(verbose_opt[0]){
      std::cout << "taps: ";
      for(int j=0;j<dimY_opt[0];++j){
        for(int i=0;i<dimX_opt[0];++i){
          std::cout<< taps[j][i] << " ";
        }
        std::cout<< std::endl;
      }
    }
    filter2d.setTaps(taps);
    try{
      filter2d.filter(input,output);
    }
    catch(string errorstring){
      cerr << errorstring << endl;
    }
    tapfile.close();
  }
  else if(tapz_opt.size()){
    if(verbose_opt[0]){
      std::cout << "taps: ";
      for(int itap=0;itap<tapz_opt.size();++itap)
	std::cout<< tapz_opt[itap] << " ";
      std::cout<< std::endl;
    }
    filter1d.setTaps(tapz_opt);
    filter1d.filter(input,output);
  }
  else if(fwhm_opt.size()){
    if(verbose_opt[0])
      std::cout << "spectral filtering to " << fwhm_opt.size() << " bands with provided fwhm " << std::endl;
    assert(wavelengthOut_opt.size()==fwhm_opt.size());
    assert(wavelengthIn_opt.size());

    Vector2d<double> lineInput(input.nrOfBand(),input.nrOfCol());
    Vector2d<double> lineOutput(wavelengthOut_opt.size(),input.nrOfCol());
    const char* pszMessage;
    void* pProgressArg=NULL;
    GDALProgressFunc pfnProgress=GDALTermProgress;
    double progress=0;
    pfnProgress(progress,pszMessage,pProgressArg);
    for(int y=0;y<input.nrOfRow();++y){
      if((y+1+down_opt[0]/2)%down_opt[0])
        continue;
      for(int iband=0;iband<input.nrOfBand();++iband)
        input.readData(lineInput[iband],y,iband);
      filter1d.applyFwhm<double>(wavelengthIn_opt,lineInput,wavelengthOut_opt,fwhm_opt, interpolationType_opt[0], lineOutput, down_opt[0], verbose_opt[0]);
      for(int iband=0;iband<output.nrOfBand();++iband){
        try{
          output.writeData(lineOutput[iband],y/down_opt[0],iband);
        }
        catch(string errorstring){
          cerr << errorstring << "in band " << iband << ", line " << y << endl;
        }
      }
      progress=(1.0+y)/output.nrOfRow();
      pfnProgress(progress,pszMessage,pProgressArg);
    }
  }
  else if(srf_opt.size()){
    if(verbose_opt[0])
      std::cout << "spectral filtering to " << srf_opt.size() << " bands with provided SRF " << std::endl;
    assert(wavelengthIn_opt.size());
    vector< Vector2d<double> > srf(srf_opt.size());//[0] srf_nr, [1]: wavelength, [2]: response
    ifstream srfFile;
    for(int isrf=0;isrf<srf_opt.size();++isrf){
      srf[isrf].resize(2);
      srfFile.open(srf_opt[isrf].c_str());
      double v;
      //add 0 to make sure srf is 0 at boundaries after interpolation step
      srf[isrf][0].push_back(0);
      srf[isrf][1].push_back(0);
      srf[isrf][0].push_back(1);
      srf[isrf][1].push_back(0);
      while(srfFile >> v){
        srf[isrf][0].push_back(v);
        srfFile >> v;
        srf[isrf][1].push_back(v);
      }
      srfFile.close();
      //add 0 to make sure srf[isrf] is 0 at boundaries after interpolation step
      srf[isrf][0].push_back(srf[isrf][0].back()+1);
      srf[isrf][1].push_back(0);    
      srf[isrf][0].push_back(srf[isrf][0].back()+1);
      srf[isrf][1].push_back(0);
      if(verbose_opt[0])
        cout << "srf file details: " << srf[isrf][0].size() << " wavelengths defined" << endl;    
    }
    assert(output.nrOfBand()==srf.size());
    double centreWavelength=0;
    Vector2d<double> lineInput(input.nrOfBand(),input.nrOfCol());
    const char* pszMessage;
    void* pProgressArg=NULL;
    GDALProgressFunc pfnProgress=GDALTermProgress;
    double progress=0;
    pfnProgress(progress,pszMessage,pProgressArg);
    for(int y=0;y<input.nrOfRow();++y){
      if((y+1+down_opt[0]/2)%down_opt[0])
        continue;
      for(int iband=0;iband<input.nrOfBand();++iband)
        input.readData(lineInput[iband],y,iband);
      for(int isrf=0;isrf<srf.size();++isrf){
        vector<double> lineOutput(output.nrOfCol());
        double delta=1.0;
        bool normalize=true;
	centreWavelength=filter1d.applySrf<double>(wavelengthIn_opt,lineInput,srf[isrf], interpolationType_opt[0], lineOutput, delta, normalize);
        if(verbose_opt[0])
          std::cout << "centre wavelength srf " << isrf << ": " << centreWavelength << std::endl;
        try{
          output.writeData(lineOutput,GDT_Float64,y/down_opt[0],isrf);
        }
        catch(string errorstring){
          cerr << errorstring << "in srf " << srf_opt[isrf] << ", line " << y << endl;
        }

      }
      progress=(1.0+y)/output.nrOfRow();
      pfnProgress(progress,pszMessage,pProgressArg);
    }

  }
  else{
    switch(filter2d::Filter2d::getFilterType(method_opt[0])){
    case(filter2d::dilate):
      if(down_opt[0]!=1){
	std::cerr << "Error: down option not supported for morphological operator" << std::endl;
	exit(1);
      }
      try{
	if(dimZ_opt.size()){
	  if(verbose_opt[0])
          std::cout<< "1-D filtering: dilate" << std::endl;
	  filter1d.morphology(input,output,"dilate",dimZ_opt[0],verbose_opt[0]);
	}
	else
	  filter2d.morphology(input,output,"dilate",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]);
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
      break;
    case(filter2d::erode):
      if(down_opt[0]!=1){
	std::cerr << "Error: down option not supported for morphological operator" << std::endl;
	exit(1);
      }
      try{
	if(dimZ_opt.size()>0){
	  if(verbose_opt[0])
	    std::cout<< "1-D filtering: dilate" << std::endl;
	  filter1d.morphology(input,output,"erode",dimZ_opt[0]);
	}
	else{
	  filter2d.morphology(input,output,"erode",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]);
	}
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
    break;
    case(filter2d::close):{//closing
      if(down_opt[0]!=1){
	std::cerr << "Error: down option not supported for morphological operator" << std::endl;
	exit(1);
      }

      ImgWriterGdal tmpout;
      tmpout.open("/vsimem/dilation.tif",input.nrOfCol(),input.nrOfRow(),input.nrOfBand(),input.getDataType(),input.getImageType());
      try{
        if(dimZ_opt.size()){
          filter1d.morphology(input,tmpout,"dilate",dimZ_opt[0]);
        }
        else{
	  filter2d.morphology(input,tmpout,"dilate",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]);
        }
      }
      catch(std::string errorString){
	std::cout<< errorString;
	exit(1);
      }
      tmpout.close();
      ImgReaderGdal tmpin;
      tmpin.open("/vsimem/dilation.tif");
      try{
	if(dimZ_opt.size()){
	  filter1d.morphology(tmpin,output,"erode",dimZ_opt[0]);
	}
	else{
	  filter2d.morphology(tmpin,output,"erode",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]);
	}
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
      tmpin.close();
      break;
    }
    case(filter2d::open):{//opening
      if(down_opt[0]!=1){
	std::cerr << "Error: down option not supported for morphological operator" << std::endl;
	exit(1);
      }
      ImgWriterGdal tmpout;
      tmpout.open("/vsimem/erosion.tif",input.nrOfCol(),input.nrOfRow(),input.nrOfBand(),input.getDataType(),input.getImageType());
      try{
	if(dimZ_opt.size()){
	  filter1d.morphology(input,tmpout,"erode",dimZ_opt[0]);
	}
	else{
	  filter2d.morphology(input,tmpout,"erode",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]);
	}
      }
      catch(std::string errorString){
	std::cout<< errorString;
	exit(1);
      }
      tmpout.close();
      ImgReaderGdal tmpin;
      try{
	tmpin.open("/vsimem/erosion.tif");
	if(dimZ_opt.size()){
	  filter1d.morphology(tmpin,output,"dilate",dimZ_opt[0]);
	}
	else{
	  filter2d.morphology(tmpin,output,"dilate",dimX_opt[0],dimY_opt[0],angle_opt,disc_opt[0]);
	}
	tmpin.close();
	tmpout.close();
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
      break;
    }
    case(filter2d::homog):{//spatially homogeneous
      try{
	filter2d.doit(input,output,"homog",dimX_opt[0],dimY_opt[0],down_opt[0],disc_opt[0]);
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
	break;
    }
    case(filter2d::heterog):{//spatially heterogeneous
      try{
	filter2d.doit(input,output,"heterog",dimX_opt[0],dimY_opt[0],down_opt[0],disc_opt[0]);
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
      break;
    }
    case(filter2d::sauvola):{//Implements Sauvola's thresholding method (http://fiji.sc/Auto_Local_Threshold)

  //test
      Vector2d<unsigned short> inBuffer;
      for(int iband=0;iband<input.nrOfBand();++iband){
        input.readDataBlock(inBuffer,0,input.nrOfCol()-1,0,input.nrOfRow()-1,iband);
      }
      try{
	filter2d.doit(input,output,"sauvola",dimX_opt[0],dimY_opt[0],down_opt[0],disc_opt[0]);
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
      break;
    }
    case(filter2d::shift):{//shift
      if(down_opt[0]!=1){
	std::cerr << "Error: down option not supported for shift operator" << std::endl;
	exit(1);
      }
      assert(input.nrOfBand());
      assert(input.nrOfCol());
      assert(input.nrOfRow());
      try{
        filter2d.shift(input,output,dimX_opt[0],dimY_opt[0],threshold_opt[0],filter2d::Filter2d::getResampleType(resample_opt[0]));
      }
      catch(string errorstring){
        cerr << errorstring << endl;
      }
      break;
    }
    // case(filter2d::linearfeature):{
    //   if(down_opt[0]!=1){
    // 	std::cerr << "Error: down option not supported for linear feature" << std::endl;
    // 	exit(1);
    //   }
    //   assert(input.nrOfBand());
    //   assert(input.nrOfCol());
    //   assert(input.nrOfRow());
    //   float theAngle=361;
    //   if(angle_opt.size())
    // 	theAngle=angle_opt[0];
    //   if(verbose_opt[0])
    // 	std::cout << "using angle " << theAngle << std::endl;
    //   try{
    // 	//using an angle step of 5 degrees and no maximum distance
    //     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]);
    //   }
    //   catch(string errorstring){
    //     cerr << errorstring << endl;
    //   }
    //   break;
    // }
    case(filter2d::mrf):{//Markov Random Field
      if(verbose_opt[0])
	std::cout << "Markov Random Field filtering" << std::endl;
      try{
	if(beta_opt.size()){
	  //in file: classFrom classTo
	  //in variable: beta[classTo][classFrom]
	  FileReaderAscii betaReader(beta_opt[0]);
	  Vector2d<double> beta(class_opt.size(),class_opt.size());
	  vector<int> cols(class_opt.size());
	  for(int iclass=0;iclass<class_opt.size();++iclass)
	    cols[iclass]=iclass;
	  betaReader.readData(beta,cols);
	  if(verbose_opt[0]){
	    std::cout << "using values for beta:" << std::endl;
	    for(int iclass1=0;iclass1<class_opt.size();++iclass1)
	      std::cout << "      " << iclass1 << " (" << class_opt[iclass1] << ")";
	    std::cout << std::endl;
	    for(int iclass1=0;iclass1<class_opt.size();++iclass1){
	      std::cout << iclass1 << " (" << class_opt[iclass1] << ")";
	      for(int iclass2=0;iclass2<class_opt.size();++iclass2)
		std::cout << " " << beta[iclass2][iclass1] << " (" << class_opt[iclass2] << ")";
	      std::cout << std::endl;
	    }
	  }
	  filter2d.mrf(input, output, dimX_opt[0], dimY_opt[0], beta, true, down_opt[0], verbose_opt[0]);
	}
	else
	  filter2d.mrf(input, output, dimX_opt[0], dimY_opt[0], 1, true, down_opt[0], verbose_opt[0]);
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
      break;
    }
    case(filter2d::sobelx):{//Sobel edge detection in X
      if(down_opt[0]!=1){
	std::cerr << "Error: down option not supported for sobel edge detection" << std::endl;
	exit(1);
      }
      Vector2d<double> theTaps(3,3);
      theTaps[0][0]=-1.0;
      theTaps[0][1]=0.0;
      theTaps[0][2]=1.0;
      theTaps[1][0]=-2.0;
      theTaps[1][1]=0.0;
      theTaps[1][2]=2.0;
      theTaps[2][0]=-1.0;
      theTaps[2][1]=0.0;
      theTaps[2][2]=1.0;
      filter2d.setTaps(theTaps);
      try{
	filter2d.filter(input,output,true,true);//absolute and normalize
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
      break;
    }
    case(filter2d::sobely):{//Sobel edge detection in Y
      if(down_opt[0]!=1){
	std::cerr << "Error: down option not supported for sobel edge detection" << std::endl;
	exit(1);
      }
      Vector2d<double> theTaps(3,3);
      theTaps[0][0]=1.0;
      theTaps[0][1]=2.0;
      theTaps[0][2]=1.0;
      theTaps[1][0]=0.0;
      theTaps[1][1]=0.0;
      theTaps[1][2]=0.0;
      theTaps[2][0]=-1.0;
      theTaps[2][1]=-2.0;
      theTaps[2][2]=-1.0;
      filter2d.setTaps(theTaps);
      try{
	filter2d.filter(input,output,true,true);//absolute and normalize
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
      break;
    }
    case(filter2d::sobelxy):{//Sobel edge detection in XY
      if(down_opt[0]!=1){
	std::cerr << "Error: down option not supported for sobel edge detection" << std::endl;
	exit(1);
      }
      Vector2d<double> theTaps(3,3);
      theTaps[0][0]=0.0;
      theTaps[0][1]=1.0;
      theTaps[0][2]=2.0;
      theTaps[1][0]=-1.0;
      theTaps[1][1]=0.0;
      theTaps[1][2]=1.0;
      theTaps[2][0]=-2.0;
      theTaps[2][1]=-1.0;
      theTaps[2][2]=0.0;
      filter2d.setTaps(theTaps);
      try{
	filter2d.filter(input,output,true,true);//absolute and normalize
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
      break;
    }
    case(filter2d::sobelyx):{//Sobel edge detection in XY
      if(down_opt[0]!=1){
	std::cerr << "Error: down option not supported for sobel edge detection" << std::endl;
	exit(1);
      }
      Vector2d<double> theTaps(3,3);
      theTaps[0][0]=2.0;
      theTaps[0][1]=1.0;
      theTaps[0][2]=0.0;
      theTaps[1][0]=1.0;
      theTaps[1][1]=0.0;
      theTaps[1][2]=-1.0;
      theTaps[2][0]=0.0;
      theTaps[2][1]=-1.0;
      theTaps[2][2]=-2.0;
      filter2d.setTaps(theTaps);
      try{
	filter2d.filter(input,output,true,true);//absolute and normalize
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
      break;
    }
    case(filter2d::smooth):{//Smoothing filter
      if(down_opt[0]!=1){
	std::cerr << "Error: down option not supported for this filter" << std::endl;
	exit(1);
      }
      try{
	if(dimZ_opt.size()){
	  if(verbose_opt[0])
	    std::cout<< "1-D filtering: smooth" << std::endl;
	  filter1d.smooth(input,output,dimZ_opt[0]);
	}
	else{
	  filter2d.smooth(input,output,dimX_opt[0],dimY_opt[0]);
	}
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
      break;
    }
    case(filter2d::smoothnodata):{//Smoothing filter
      if(down_opt[0]!=1){
	std::cerr << "Error: down option not supported for this filter" << std::endl;
	exit(1);
      }
      try{
	if(dimZ_opt.size()){
	  if(verbose_opt[0])
	    std::cout<< "1-D filtering: smooth" << std::endl;
	  filter1d.smoothNoData(input,interpolationType_opt[0],output);
	}
	else{
	  if(verbose_opt[0])
	    std::cout<< "2-D filtering: smooth" << std::endl;
	  filter2d.smoothNoData(input,output,dimX_opt[0],dimY_opt[0]);
	}
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
      break;
    }
    case(filter2d::dwt):
      if(down_opt[0]!=1){
	std::cerr << "Error: down option not supported for this filter" << std::endl;
	exit(1);
      }
      try{
	if(dimZ_opt.size()){
	  if(verbose_opt[0])
	    std::cout<< "DWT in spectral domain" << std::endl;
	  filter1d.dwtForward(input, output, wavelet_type_opt[0], family_opt[0]);
	}
	else
	  filter2d.dwtForward(input, output, wavelet_type_opt[0], family_opt[0]);
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
      break;
    case(filter2d::dwti):
      if(down_opt[0]!=1){
	std::cerr << "Error: down option not supported for this filter" << std::endl;
	exit(1);
      }
      try{
	if(dimZ_opt.size()){
	  if(verbose_opt[0])
	    std::cout<< "inverse DWT in spectral domain" << std::endl;
	  filter1d.dwtInverse(input, output, wavelet_type_opt[0], family_opt[0]);
	}
	else
	  filter2d.dwtInverse(input, output, wavelet_type_opt[0], family_opt[0]);
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
      break;
    case(filter2d::dwt_cut):
      if(down_opt[0]!=1){
	std::cerr << "Error: down option not supported for this filter" << std::endl;
	exit(1);
      }
      if(dimZ_opt.size()){
        if(verbose_opt[0])
          std::cout<< "DWT approximation in spectral domain" << std::endl;
	filter1d.dwtCut(input, output, wavelet_type_opt[0], family_opt[0], threshold_opt[0]);
      }
      else
	filter2d.dwtCut(input, output, wavelet_type_opt[0], family_opt[0], threshold_opt[0]);
      break;
    case(filter2d::dwt_cut_from):
      if(down_opt[0]!=1){
	std::cerr << "Error: down option not supported for this filter" << std::endl;
	exit(1);
      }
      try{
	if(dimZ_opt.size()){
	  if(verbose_opt[0])
	    std::cout<< "DWT approximation in spectral domain" << std::endl;
	  filter1d.dwtCutFrom(input, output, wavelet_type_opt[0], family_opt[0], static_cast<int>(threshold_opt[0]));
	}
	else{
	  string errorString="Error: this filter is not supported in 2D";
	  throw(errorString);
	}
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
      break;
    case(filter2d::savgolay):{
      assert(savgolay_nl_opt.size());
      assert(savgolay_nr_opt.size());
      assert(savgolay_ld_opt.size());
      assert(savgolay_m_opt.size());
      if(verbose_opt[0])
      	std::cout << "Calculating Savitzky-Golay coefficients: " << endl;
      filter1d.getSavGolayCoefficients(tapz_opt, input.nrOfBand(), savgolay_nl_opt[0], savgolay_nr_opt[0], savgolay_ld_opt[0], savgolay_m_opt[0]);
      if(verbose_opt[0]){
      	std::cout << "taps (size is " << tapz_opt.size() << "): ";
      	for(int itap=0;itap<tapz_opt.size();++itap)
      	  std::cout<< tapz_opt[itap] << " ";
      	std::cout<< std::endl;
      }
      filter1d.setTaps(tapz_opt);
      filter1d.filter(input,output);
      break;
    }
    case(filter2d::percentile)://deliberate fall through
    case(filter2d::threshold)://deliberate fall through
      assert(threshold_opt.size());
      if(dimZ_opt.size())
	filter1d.setThresholds(threshold_opt);
      else
	filter2d.setThresholds(threshold_opt);
    case(filter2d::density)://deliberate fall through
      filter2d.setClasses(class_opt);
      if(verbose_opt[0])
	std::cout << "classes set" << std::endl;
    default:
      try{
	if(dimZ_opt.size()){
	  if(dimZ_opt[0]==1)
	    filter1d.stat(input,output,method_opt[0]);
	  else{
	    assert(down_opt[0]==1);//not implemented yet...
	    filter1d.filter(input,output,method_opt[0],dimZ_opt[0]);
	  }
	}
	else
	  filter2d.doit(input,output,method_opt[0],dimX_opt[0],dimY_opt[0],down_opt[0],disc_opt[0]);
      }
      catch(string errorstring){
	cerr << errorstring << endl;
      }
      break;
    }
  }
  input.close();
  output.close();
  return 0;
}