Statistics
| Revision:

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

History | View | Annotate | Download (6.42 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 java.util.ArrayList;
44

    
45
import org.gvsig.raster.buffer.RasterBuffer;
46
import org.gvsig.raster.dataset.IBuffer;
47
import org.gvsig.raster.dataset.Params;
48
import org.gvsig.raster.grid.filter.RasterFilter;
49

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

    
56
public class ConvolutionFilter extends RasterFilter {
57

    
58
        public static String[] names = new String[] {"media","paso bajo", "gauss","personalizado"};
59
        public int                                        ladoVentana                        = 0;
60
        protected IBuffer                                rasterResult                 = null;
61
        protected Kernel kernel                                                         = null;
62
        
63
        /** Tipos de filtros de convolucion */
64
        public static final int TYPE_MEDIA  = 0;
65
        public static final int TYPE_LOWPASS  = 1;
66
        public static final int TYPE_GAUSS  = 2;
67
        public static final int TYPE_OTHER  = 3;
68
        
69
        /**Definicion de los filtros basicos */        
70
        double gauss3x3[][]= {{1,4,1},{4,12,4},{1,4,1}};
71
        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}};
72
        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}};
73
        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}};
74
        double lowpass3x3[][]= {{0,1,0},{1,6,1},{0,1,0}};
75
        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}};
76
        double media3x3[][]={{1,1,1},{1,1,1},{1,1,1}};
77
        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}};
78
        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}};
79
        
80
        Kernel kGauss3x3 = new Kernel(gauss3x3,34); 
81
        Kernel kGaus5x5 = new Kernel(gauss5x5,121);
82
        Kernel kGauss7x7 = new Kernel (gauss7x7aux);
83
        Kernel kLowpass3x3= new Kernel(lowpass3x3,10);
84
        Kernel kLowpass5x5= new Kernel(lowpass5x5);
85
        Kernel kMedia3x3=new  Kernel(media3x3,9);
86
        Kernel kMedia5x5= new Kernel (media5x5,25);
87
        Kernel kMedia7x7= new Kernel (media7x7,49);
88
        private int                                        operator = 0;
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 "suavizado";
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
                        return (Object) this.rasterResult;
119
                } else {
120
                        return null;
121
                }
122
        }
123

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

    
131
        public void pre() {
132
                
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(ladoVentana==0)ladoVentana=3;
139
                else if (ladoVentana==1) ladoVentana=5;
140
                else ladoVentana=7;
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
                switch (operator) {
151
                        case TYPE_MEDIA:
152
                                if(ladoVentana==3)
153
                                        kernel= kMedia3x3;
154
                                else if(ladoVentana==5)
155
                                        kernel=kMedia5x5;
156
                                else if(ladoVentana==7)
157
                                        kernel=kMedia7x7;
158
                                break;
159
                        
160
                        case TYPE_GAUSS:
161
                                // Dependiendo del tama?o de la ventana tomar un kernel u otro
162
                                if(ladoVentana==3)
163
                                        kernel= kGauss3x3;
164
                                else if(ladoVentana==5)
165
                                        kernel=kGaus5x5;
166
                                else if(ladoVentana==7)
167
                                        kernel=kGauss7x7;
168
                                break;
169
                        
170
                        case TYPE_LOWPASS:
171
                                if(ladoVentana==3)
172
                                        kernel= kLowpass3x3;
173
                                else if(ladoVentana==5)
174
                                        kernel= kLowpass5x5;
175
                                        
176
                                /*else if (ladoVentana==7)
177
                                        kernel= lowpass7x7;
178
                                 */
179
                                break;
180
                                
181
                        case TYPE_OTHER:
182
                                
183
                                if (kernel== null)
184
                                        // No hay kenel para operar
185
                                break;
186
                                
187
                }
188
        }
189

    
190
        public void process(int x, int y) {
191
        }
192
        
193
        
194
        public Params getUIParams() {
195
                Params params = new Params();
196
                params.setParam("LadoVentana",
197
                                ladoVentana + "",
198
                                Params.CHOICE,
199
                                new String[] {"3","3","5", "7"});
200
                return params;
201
        }
202

    
203
        
204
        public Params getUIParams(String nameFilter) {
205
                Params params = new Params();
206
                
207
                        params.setParam("LadoVentana",
208
                                ladoVentana + "",
209
                                Params.CHOICE,
210
                                new String[] {"3","5","7"});
211
                
212
                        params.setParam("FilterName",
213
                                        nameFilter,
214
                                        -1,
215
                                        null);
216
                
217
                        if (nameFilter=="personalizado" ){
218
                                
219
                                
220
                                /*params.setParam("LadoVentana",
221
                                                ladoVentana+ "",
222
                                                Params.MATRIX,
223
                                                new String[] {"0","1","2"});*/
224
                                /*if(ladoVentana==0){
225
                                        
226
                                System.out.print("0");        
227
                                }
228
                                if(ladoVentana==1){
229
                                        
230
                                System.out.print("1");        
231
                                }
232
                                if(ladoVentana==2){
233
                                        
234
                                        System.out.print("2");
235
                                }*/
236
                        
237
                        }
238
                return params;
239
        }
240
        
241
        
242

    
243
        public ArrayList getRasterFilterList() {
244
                ArrayList filters = new ArrayList();
245
                filters.add(ConvolutionFilter.class);
246
                return filters;
247
        }
248
        
249

    
250
                
251
        
252
        
253
        
254

    
255
}
256

    
257