Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / convolution / ConvolutionFilter.java @ 21803

History | View | Annotate | Download (7 KB)

1
/* gvSIG. Sistema de Informaci?n Geogrfica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Instituto de Desarrollo Regional and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Iba?ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
 *   Campus Universitario s/n
35
 *   02071 Alabacete
36
 *   Spain
37
 *
38
 *   +34 967 599 200
39
 */
40

    
41
package org.gvsig.raster.grid.filter.convolution;
42

    
43
import org.gvsig.raster.buffer.RasterBuffer;
44
import org.gvsig.raster.dataset.Params;
45
import org.gvsig.raster.grid.filter.RasterFilter;
46

    
47
/**
48
 * Filtros de  Convolucion
49
 * @author  Diego Guerrero Sevilla  <diego.guerrero@uclm.es>
50
 * @author        Alejandro Mu?oz S?nchez  < alejandro.munoz@uclm.es>
51
 * */
52

    
53
public class ConvolutionFilter extends RasterFilter {
54

    
55
        public static String[]     names        = new String[] {"media", "pasobajo", "sharpen", "gauss", "personalizado"};
56
        public int                 ladoVentana  = 0;
57
        protected Kernel           kernel       = null;
58

    
59
        /** Tipos de filtros de convolucion */
60
        public static final int TYPE_MEDIA  = 0;
61
        public static final int TYPE_LOWPASS  = 1;
62
        public static final int TYPE_HIGHPASS  = 2;
63
        public static final int TYPE_GAUSS  = 3;
64
        public static final int TYPE_OTHER  = 4;
65

    
66
        /**Definicion de los filtros basicos */
67
        static final double gauss3x3[][]= {{1,4,1},{4,12,4},{1,4,1}};
68
        static final double gauss5x5[][]= {{1,2,3,2,1},{2,7,11,7,2},{3,11,17,11,3},{2,7,11,7,2},{1,2,3,2,1}};
69
        static final double gauss7x7[][]= {{1,1,2,2,2,1,1},{1,2,2,4,2,2,1},{2,2,4,8,4,2,2},{2,4,8,16,8,4,2},{2,2,4,8,4,2,2},{1,2,2,4,2,2,1},{1,1,2,2,2,1,1}};
70
        static final double gauss7x7aux[][]= {{0,0,0.0003,0.0006,0.0003,0,0},{0,0.0011,0.0079,0.0153,0.0079,0.0011,0},{0.0003,0.0079,0.0563,0.1082,0.0563,0.0079,0.0003},{0.0006,0.0153,0.1082,0.2079,0.1082,0.0153,0.0006},{0.0003,0.0079,0.0563,0.1082,0.0563,0.0079,0.0003},{0,0.0011,0.0079,0.0153,0.0079,0.0011,0},{0,0,0.0003,0.0006,0.0003,0,0}};
71
        static final double lowpass3x3[][]= {{0,1,0},{1,6,1},{0,1,0}};
72
        static final double lowpass5x5[][]= {{1,1,1,1,1},{1,4,4,4,1},{1,4,12,4,1},{1,4,4,4,1},{1,1,1,1,1}};
73
        static final double media3x3[][]={{1,1,1},{1,1,1},{1,1,1}};
74
        static final double media5x5[][]={{1,1,1,1,1},{1,1,1,1,1},{1,1,1,1,1},{1,1,1,1,1},{1,1,1,1,1}};
75
        static final double media7x7[][]={{1,1,1,1,1,1,1},{1,1,1,1,1,1,1},{1,1,1,1,1,1,1},{1,1,1,1,1,1,1},{1,1,1,1,1,1,1},{1,1,1,1,1,1,1},{1,1,1,1,1,1,1}};
76
        static final double highpass3x3[][]= {{-1,-1,-1},{-1,9,-1},{-1,-1,-1}};
77

    
78
        static final Kernel kGauss3x3 = new Kernel(gauss3x3,34);
79
        static final Kernel kGauss5x5 = new Kernel(gauss5x5,121);
80
        static final Kernel kGauss7x7 = new Kernel (gauss7x7aux);
81
        static final Kernel kLowpass3x3 = new Kernel(lowpass3x3,10);
82
        static final Kernel kLowpass5x5 = new Kernel(lowpass5x5);
83
        static final Kernel kMedia3x3 = new  Kernel(media3x3,9);
84
        static final Kernel kMedia5x5 = new Kernel (media5x5,25);
85
        static final Kernel kMedia7x7 = new Kernel (media7x7,49);
86
        static final Kernel kHighpass3x3 = new Kernel (highpass3x3);
87
        private int                        operator = 0;
88
        private double      agudeza = 1;         
89

    
90

    
91

    
92

    
93
        /** Filtro de Convolucion*/
94
        public ConvolutionFilter() {
95
                super();
96
                setName(names[0]);
97
        }
98

    
99

    
100
        public String getGroup() {
101
                return "espaciales";
102
        }
103

    
104
        public int getInRasterDataType() {
105
                return 0;
106
        }
107

    
108
        public String[] getNames() {
109
                return names;
110
        }
111

    
112
        public int getOutRasterDataType() {
113
                return 0;
114
        }
115

    
116
        public Object getResult(String name) {
117
                if (name.equals("raster")) {
118
                        if (!exec)
119
                                return this.raster;
120
                        return this.rasterResult;
121
                }
122
                return null;
123
        }
124

    
125

    
126
        public void post() {
127
                //        En caso de que nadie apunte a raster, se liberara su memoria.
128
                raster = null;
129

    
130
        }
131

    
132
        public void pre() {
133
                exec = true;
134
                raster = (RasterBuffer) params.get("raster");
135
                height = raster.getHeight();
136
                width = raster.getWidth();
137
                ladoVentana = ((Integer) params.get("ladoVentana")).intValue();
138
                if(params.get("Agudeza") != null)
139
                        agudeza = ((Double) params.get("Agudeza")).doubleValue();
140
                Kernel userKernel = (Kernel) params.get("kernel");
141
                String name = ((String)params.get("filterName"));
142
                operator = 0;
143
                for (int i = 0; i < names.length; i++) {
144
                        if (names[i].equals(name)) {
145
                                operator = i;
146
                                setName(names[i]);
147
                                break;
148
                        }
149
                }
150

    
151
                switch (operator) {
152
                        case TYPE_MEDIA:
153
                                setName(names[0]);
154
                                switch (ladoVentana) {
155
                                        case 2:
156
                                                kernel = kMedia7x7;
157
                                                break;
158
                                        case 1:
159
                                                kernel = kMedia5x5;
160
                                                break;
161
                                        default:
162
                                                kernel = kMedia3x3;
163
                                                break;
164
                                }
165
                                break;
166
                        case TYPE_LOWPASS:
167
                                setName(names[1]);
168
                                switch (ladoVentana) {
169
                                        case 1:
170
                                                kernel = kLowpass5x5;
171
                                                break;
172
                                        default:
173
                                                kernel = kLowpass3x3;
174
                                                break;
175
                                }
176
                                break;
177
                        case TYPE_HIGHPASS:
178
                                setName(names[2]);
179
                                double[][] h = new double[3][3];
180
                                for (int i = 0; i < h.length; i++) {
181
                                        for (int j = 0; j < h[i].length; j++) {
182
                                                h[i][j] = highpass3x3[i][j];
183
                                        }
184
                                }
185
                                h[1][1] = 29 - (agudeza * 20.9) / 100;
186
                                kernel = new Kernel (h);
187
                                break;
188
                        case TYPE_GAUSS:
189
                                setName(names[3]);
190
                                switch (ladoVentana) {
191
                                        case 2:
192
                                                kernel = kGauss7x7;
193
                                                break;
194
                                        case 1:
195
                                                kernel = kGauss5x5;
196
                                                break;
197
                                        default:
198
                                                kernel = kGauss3x3;
199
                                                break;
200
                                }
201
                                break;
202
                        case TYPE_OTHER:
203
                                setName(names[4]);
204
                                kernel = userKernel;
205
                                break;
206
                }
207

    
208
                if (kernel == null)
209
                        kernel = kMedia3x3;
210
        }
211

    
212
        public void process(int x, int y) {
213
        }
214

    
215
        public Params getUIParams(String nameFilter) {
216
                Params params = new Params();
217
                if(nameFilter == "personalizado") {
218
                        params.setParam("Panel", new ConvolutionUI(kernel), -1, null);
219
                } else if (nameFilter != "pasobajo" && nameFilter != "sharpen") {
220
                        params.setParam("LadoVentana",
221
                                        new Integer(ladoVentana),
222
                                        Params.CHOICE,
223
                                        new String[] {"3", "5", "7"});
224
                } else if (nameFilter == "sharpen") {
225
                        params.setParam("Agudeza",
226
                                        new Double(agudeza),
227
                                        Params.SLIDER,
228
                                        new String[] {"0", "100", "0", "1", "25" }); //min, max, valor defecto, intervalo peque?o, intervalo grande;
229
                } else {
230
                        params.setParam("LadoVentana",
231
                                        new Integer(ladoVentana),
232
                                        Params.CHOICE,
233
                                        new String[] {"3", "5"});
234
                }
235

    
236
                params.setParam("FilterName",
237
                                nameFilter,
238
                                -1,
239
                                null);
240
                return params;
241
        }
242
}