Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / segmentation / FirstDerivativeFilter.java @ 21803

History | View | Annotate | Download (5.85 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2004 IVER T.I. 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
package org.gvsig.raster.grid.filter.segmentation;
20

    
21
import org.gvsig.raster.buffer.RasterBuffer;
22
import org.gvsig.raster.dataset.IBuffer;
23
import org.gvsig.raster.dataset.Params;
24
import org.gvsig.raster.grid.filter.RasterFilter;
25
/**
26
 * Clase base para los filtros de primera derivada.
27
 *
28
 * @version 04/06/2007
29
 * @author Diego Guerrero Sevilla  <diego.guerrero@uclm.es>
30
 */
31
public class FirstDerivativeFilter extends RasterFilter {
32
        public static String[] names = new String[] {"sobel", "roberts", "prewitt", "freichen"};
33
        public static final int TYPE_SOBEL  = 0;
34
        public static final int TYPE_ROBERTS  = 1;
35
        public static final int TYPE_PREWITT  = 2;
36
        public static final int TYPE_FREICHEN  = 3;
37

    
38
        protected int                         umbral = 0;
39
        protected boolean         compare = false;
40
        protected Kernel                 operatorH;
41
        protected Kernel                 operatorV;
42
        private int                                        operator = 0;
43

    
44
        // Kernels:------------------------------------------
45
        static final double sobelH[][]= {{-1,0,1},{-2,0,2},{-1,0,1}};
46
        static final double sobelV[][]= {{-1,-2,-1},{0,0,0},{1,2,1}};
47
        static final Kernel sobelHKernel = new Kernel(sobelH);
48
        static final Kernel sobelVKernel = new Kernel(sobelV);
49

    
50
        static final double robertsH[][]= {{0,0,-1},{0,1,0},{0,0,0}};
51
        static final double robertsV[][]= {{-1,0,0},{0,1,0},{0,0,0}};
52
        static final Kernel robertsHKernel = new Kernel(robertsH);
53
        static final Kernel robertsVKernel = new Kernel(robertsV);
54

    
55
        static final double prewittH[][]= {{1,0,-1},{1,0,-1},{1,0,-1}};
56
        static final double prewittV[][] = {{-1,-1,-1},{0,0,0},{1,1,1}};
57
        static final Kernel prewittHKernel = new Kernel(prewittH);
58
        static final Kernel prewittVKernel = new Kernel(prewittV);
59

    
60
        static final double freiChenH[][]= {{-1,-1.4D,-1},{0,0,0},{1,1.4D,1}};
61
        static final double freiChenV[][]= {{-1,0,1},{-1.4D,0,1.4D},{-1,0,1}};
62
        //static final double freiChenH[][]= {{-1,-1.4142D,-1},{0,0,0},{1,1.4142D,1}};
63
        //static final double freiChenV[][]= {{-1,0,1},{-1.4142D,0,1.4142D},{-1,0,1}};
64
        static final Kernel freiChenHKernel = new Kernel(freiChenH);
65
        static final Kernel freiChenVKernel = new Kernel(freiChenV);
66
        // --------------------------------------------------
67

    
68
        /**
69
         * Constructor
70
         */
71
        public FirstDerivativeFilter() {
72
                setName(names[0]);
73
        }
74

    
75
        public void pre() {
76
                exec = true;
77
                raster = (RasterBuffer) params.get("raster");
78
                height = raster.getHeight();
79
                width = raster.getWidth();
80

    
81
                if (params.get("umbral") != null)
82
                        umbral = ((Integer) params.get("umbral")).intValue();
83
                else
84
                        umbral = 0;
85

    
86
                if (params.get("compare") != null)
87
                        compare = ((Boolean) params.get("compare")).booleanValue();
88
                else
89
                        compare = false;
90

    
91
                operator = 0;
92
                for (int i = 0; i < names.length; i++) {
93
                        if (names[i].equals(getName())) {
94
                                operator = i;
95
                                break;
96
                        }
97
                }
98

    
99
                switch (operator) {
100
                        case TYPE_SOBEL:
101
                                operatorH = sobelHKernel;
102
                                operatorV = sobelVKernel;
103
                                break;
104
                        case TYPE_ROBERTS:
105
                                operatorH = robertsHKernel;
106
                                operatorV = robertsVKernel;
107
                                break;
108
                        case TYPE_PREWITT:
109
                                operatorH = prewittHKernel;
110
                                operatorV = prewittVKernel;
111
                                break;
112
                        case TYPE_FREICHEN:
113
                                operatorH = freiChenHKernel;
114
                                operatorV = freiChenVKernel;
115
                                break;
116
                }
117

    
118
                rasterResult = RasterBuffer.getBuffer(IBuffer.TYPE_BYTE, raster.getWidth(), raster.getHeight(), raster.getBandCount(), true);
119
        }
120

    
121
        /**
122
         * Obtiene el umbral
123
         * @return entero que representa el umbral
124
         */
125
        public int getUmbral() {
126
                return umbral;
127
        }
128

    
129
        /*
130
         * (non-Javadoc)
131
         * @see org.gvsig.raster.grid.filter.RasterFilter#getGroup()
132
         */
133
        public String getGroup() {
134
                return "deteccion_bordes";
135
        }
136

    
137
        /*
138
         * (non-Javadoc)
139
         * @see org.gvsig.raster.grid.filter.RasterFilter#post()
140
         */
141
        public void post() {
142
                // En caso de que nadie apunte a raster, se liberar? su memoria.
143
                raster = null;
144
        }
145

    
146
        /*
147
         * (non-Javadoc)
148
         * @see org.gvsig.raster.grid.filter.RasterFilter#getNames()
149
         */
150
        public String[] getNames() {
151
                return names;
152
        }
153

    
154
        /*
155
         * (non-Javadoc)
156
         * @see org.gvsig.raster.grid.filter.IRasterFilter#getParams()
157
         */
158
        public Params getUIParams(String nameFilter) {
159
                Params params = new Params();
160
                params.setParam("Umbral",
161
                                new Integer(umbral),
162
                                Params.SLIDER,
163
                                new String[] {"0", "255", "0", "1", "25" }); //min, max, valor defecto, intervalo peque?o, intervalo grande;
164
                params.setParam("Compare",
165
                                new Boolean(compare),
166
                                Params.CHECK,
167
                                null);
168
                params.setParam("FilterName",
169
                                getName(),
170
                                -1,
171
                                null);
172
                return params;
173
        }
174

    
175
        /*
176
         * (non-Javadoc)
177
         * @see org.gvsig.raster.grid.filter.RasterFilter#getInRasterDataType()
178
         */
179
        public int getInRasterDataType() {
180
                return 0;
181
        }
182

    
183
        /*
184
         * (non-Javadoc)
185
         * @see org.gvsig.raster.grid.filter.RasterFilter#getOutRasterDataType()
186
         */
187
        public int getOutRasterDataType() {
188
                return RasterBuffer.TYPE_BYTE;
189
        }
190

    
191
        /*
192
         * (non-Javadoc)
193
         * @see org.gvsig.raster.grid.filter.RasterFilter#getResult(java.lang.String)
194
         */
195
        public Object getResult(String name) {
196
                if (name.equals("raster")) {
197
                        if (!exec)
198
                                return this.raster;
199
                        return this.rasterResult;
200
                }
201
                return null;
202
        }
203

    
204
        /*
205
         * (non-Javadoc)
206
         * @see org.gvsig.raster.grid.filter.RasterFilter#process(int, int)
207
         */
208
        public void process(int x, int y) {
209
        }
210
}