gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / filter / segmentation / FirstDerivativeFilter.java @ 2438
History | View | Annotate | Download (5.53 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.raster.impl.grid.filter.segmentation; |
23 |
|
24 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
25 |
import org.gvsig.fmap.dal.coverage.datastruct.Params; |
26 |
import org.gvsig.fmap.dal.coverage.exception.FilterAddException; |
27 |
import org.gvsig.fmap.dal.coverage.grid.filter.BaseRasterFilter; |
28 |
import org.gvsig.raster.impl.buffer.RasterBuffer; |
29 |
import org.gvsig.raster.impl.datastruct.Kernel; |
30 |
import org.gvsig.raster.impl.store.ParamsImpl; |
31 |
/**
|
32 |
* Clase base para los filtros de primera derivada.
|
33 |
*
|
34 |
* @author Diego Guerrero Sevilla <diego.guerrero@uclm.es>
|
35 |
*/
|
36 |
public class FirstDerivativeFilter extends BaseRasterFilter { |
37 |
public static String[] names = new String[] {"sobel", "roberts", "prewitt", "freichen"}; |
38 |
public static final int TYPE_SOBEL = 0; |
39 |
public static final int TYPE_ROBERTS = 1; |
40 |
public static final int TYPE_PREWITT = 2; |
41 |
public static final int TYPE_FREICHEN = 3; |
42 |
|
43 |
protected int umbral = 0; |
44 |
protected boolean compare = false; |
45 |
protected Kernel operatorH; |
46 |
protected Kernel operatorV; |
47 |
private int operator = 0; |
48 |
|
49 |
// Kernels:------------------------------------------
|
50 |
static final double sobelH[][] = {{-1,0,1},{-2,0,2},{-1,0,1}}; |
51 |
static final double sobelV[][] = {{-1,-2,-1},{0,0,0},{1,2,1}}; |
52 |
static final Kernel sobelHKernel = new Kernel(sobelH); |
53 |
static final Kernel sobelVKernel = new Kernel(sobelV); |
54 |
|
55 |
static final double robertsH[][] = {{0,0,-1},{0,1,0},{0,0,0}}; |
56 |
static final double robertsV[][] = {{-1,0,0},{0,1,0},{0,0,0}}; |
57 |
static final Kernel robertsHKernel = new Kernel(robertsH); |
58 |
static final Kernel robertsVKernel = new Kernel(robertsV); |
59 |
|
60 |
static final double prewittH[][] = {{1,0,-1},{1,0,-1},{1,0,-1}}; |
61 |
static final double prewittV[][] = {{-1,-1,-1},{0,0,0},{1,1,1}}; |
62 |
static final Kernel prewittHKernel = new Kernel(prewittH); |
63 |
static final Kernel prewittVKernel = new Kernel(prewittV); |
64 |
|
65 |
static final double freiChenH[][] = {{-1,-1.4D,-1},{0,0,0},{1,1.4D,1}}; |
66 |
static final double freiChenV[][] = {{-1,0,1},{-1.4D,0,1.4D},{-1,0,1}}; |
67 |
//static final double freiChenH[][]= {{-1,-1.4142D,-1},{0,0,0},{1,1.4142D,1}};
|
68 |
//static final double freiChenV[][]= {{-1,0,1},{-1.4142D,0,1.4142D},{-1,0,1}};
|
69 |
static final Kernel freiChenHKernel = new Kernel(freiChenH); |
70 |
static final Kernel freiChenVKernel = new Kernel(freiChenV); |
71 |
|
72 |
protected int nBandsToProcess = 0; |
73 |
protected boolean hasTransparency = false; |
74 |
// --------------------------------------------------
|
75 |
|
76 |
/**
|
77 |
* Constructor
|
78 |
*/
|
79 |
public FirstDerivativeFilter() {
|
80 |
setName(names[0]);
|
81 |
} |
82 |
|
83 |
public void pre() throws FilterAddException { |
84 |
super.pre();
|
85 |
|
86 |
if(transparency != null && transparency.existAlphaBand()) { |
87 |
nBandsToProcess = raster.getBandCount() - 1;
|
88 |
hasTransparency = true;
|
89 |
} else
|
90 |
nBandsToProcess = raster.getBandCount(); |
91 |
|
92 |
if (params.get("umbral") != null) |
93 |
umbral = ((Integer) params.get("umbral")).intValue(); |
94 |
else
|
95 |
umbral = 0;
|
96 |
|
97 |
if (params.get("compare") != null) |
98 |
compare = ((Boolean) params.get("compare")).booleanValue(); |
99 |
else
|
100 |
compare = false;
|
101 |
|
102 |
operator = 0;
|
103 |
for (int i = 0; i < names.length; i++) { |
104 |
if (names[i].equals(getName())) {
|
105 |
operator = i; |
106 |
break;
|
107 |
} |
108 |
} |
109 |
|
110 |
switch (operator) {
|
111 |
case TYPE_SOBEL:
|
112 |
operatorH = sobelHKernel; |
113 |
operatorV = sobelVKernel; |
114 |
break;
|
115 |
case TYPE_ROBERTS:
|
116 |
operatorH = robertsHKernel; |
117 |
operatorV = robertsVKernel; |
118 |
break;
|
119 |
case TYPE_PREWITT:
|
120 |
operatorH = prewittHKernel; |
121 |
operatorV = prewittVKernel; |
122 |
break;
|
123 |
case TYPE_FREICHEN:
|
124 |
operatorH = freiChenHKernel; |
125 |
operatorV = freiChenVKernel; |
126 |
break;
|
127 |
} |
128 |
|
129 |
createBufferResult(Buffer.TYPE_BYTE, raster.getBandCount());
|
130 |
} |
131 |
|
132 |
/**
|
133 |
* Obtiene el umbral
|
134 |
* @return entero que representa el umbral
|
135 |
*/
|
136 |
public int getUmbral() { |
137 |
return umbral;
|
138 |
} |
139 |
|
140 |
public String getGroup() { |
141 |
return "deteccion_bordes"; |
142 |
} |
143 |
|
144 |
public void post() { |
145 |
// En caso de que nadie apunte a raster, se liberar? su memoria.
|
146 |
raster = null;
|
147 |
} |
148 |
|
149 |
public String[] getNames() { |
150 |
return names;
|
151 |
} |
152 |
|
153 |
public Params getUIParams(String nameFilter) { |
154 |
Params params = new ParamsImpl();
|
155 |
params.setParam("Umbral",
|
156 |
new Integer(umbral), |
157 |
Params.SLIDER, |
158 |
new String[] {"0", "255", "0", "1", "25" }); //min, max, valor defecto, intervalo peque?o, intervalo grande; |
159 |
params.setParam("Compare",
|
160 |
new Boolean(compare), |
161 |
Params.CHECK, |
162 |
null);
|
163 |
params.setParam("FilterName",
|
164 |
getName(), |
165 |
-1,
|
166 |
null);
|
167 |
return params;
|
168 |
} |
169 |
|
170 |
public int getInRasterDataType() { |
171 |
return 0; |
172 |
} |
173 |
|
174 |
public int getOutRasterDataType() { |
175 |
return RasterBuffer.TYPE_BYTE;
|
176 |
} |
177 |
|
178 |
|
179 |
public void process(int x, int y) { |
180 |
} |
181 |
} |