root / trunk / libraries / libCq_CMS_praster / src / org / cresques / filter / segmentation / FirstDerivativeShortFilter.java @ 8026
History | View | Annotate | Download (9.3 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 |
|
20 |
package org.cresques.filter.segmentation; |
21 |
|
22 |
import org.cresques.filter.RasterFilter; |
23 |
import org.cresques.io.data.RasterBuf; |
24 |
|
25 |
/**
|
26 |
* Filtro de Sobel que se aplica a un RasterBuf de tipo Short.
|
27 |
* Toma como entrada la imagen y el umbral (cero para no umbralizar).
|
28 |
*
|
29 |
* @author Diego Guerrero Sevilla <diego.guerrero@uclm.es>
|
30 |
*
|
31 |
*/
|
32 |
|
33 |
public class FirstDerivativeShortFilter extends FirstDerivativeFilter { |
34 |
|
35 |
/**
|
36 |
* Constructor sin par?metros. Este es usado principalmente desde controlTypes
|
37 |
* ya que se instancia con newInstance y sin par?metros. Poseriormente le aplica
|
38 |
* el nombre al filtro con la funci?n setFilterName de RasterFilter.
|
39 |
*/
|
40 |
public FirstDerivativeShortFilter() {}
|
41 |
|
42 |
/**
|
43 |
* Constructor para la asignaci?n del identificador del filtro
|
44 |
* @param fName Cadena que representa el identificador del filtro
|
45 |
*/
|
46 |
public FirstDerivativeShortFilter(String fName) { |
47 |
super(fName);
|
48 |
} |
49 |
|
50 |
public void process(int col, int line) { |
51 |
short[] pxOut = new short[4]; |
52 |
short[] px = new short[4]; |
53 |
short convoResult[][] = new short[3][4]; |
54 |
int ladoVentana = 3; |
55 |
int semiLado = (ladoVentana-1)>>1; |
56 |
|
57 |
double ventanaR[][]=new double[ladoVentana][ladoVentana]; |
58 |
double ventanaG[][]=new double[ladoVentana][ladoVentana]; |
59 |
double ventanaB[][]=new double[ladoVentana][ladoVentana]; |
60 |
|
61 |
Kernel kernelRGB[]=new Kernel[3]; |
62 |
|
63 |
raster.getElemShort(line, col, px); |
64 |
|
65 |
if((col-semiLado >= 0) && (line-semiLado >= 0) &&(col+semiLado < width)&&(line+semiLado < height)) |
66 |
{ |
67 |
// Obtener el vector con la ventanas de muestras (una por componente RGB)
|
68 |
for (int i=-semiLado;i<=semiLado;i++) |
69 |
for(int j=-semiLado;j<=semiLado;j++) |
70 |
{ |
71 |
raster.getElemShort(line+j, col+i,px); |
72 |
ventanaR[i+semiLado][j+semiLado] =px[0];
|
73 |
ventanaG[i+semiLado][j+semiLado] =px[1];
|
74 |
ventanaB[i+semiLado][j+semiLado] =px[2];
|
75 |
} |
76 |
kernelRGB[0]=new Kernel(ventanaR); |
77 |
kernelRGB[1]=new Kernel(ventanaG); |
78 |
kernelRGB[2]=new Kernel(ventanaB); |
79 |
|
80 |
convoResult[0][0]=(short)operatorH.convolution(kernelRGB[0]); |
81 |
convoResult[0][1]=(short)operatorV.convolution(kernelRGB[0]); |
82 |
convoResult[1][0]=(short)operatorH.convolution(kernelRGB[1]); |
83 |
convoResult[1][1]=(short)operatorV.convolution(kernelRGB[1]); |
84 |
convoResult[2][0]=(short)operatorH.convolution(kernelRGB[2]); |
85 |
convoResult[2][1]=(short)operatorV.convolution(kernelRGB[2]); |
86 |
|
87 |
if (compare){
|
88 |
if (convoResult[0][0]>convoResult[0][1]) pxOut[0]= convoResult[0][0]; |
89 |
else pxOut[0]= convoResult[0][1]; |
90 |
|
91 |
if (convoResult[1][0]>convoResult[1][1]) pxOut[1]= convoResult[1][0]; |
92 |
else pxOut[1]= convoResult[1][1]; |
93 |
|
94 |
if (convoResult[2][0]>convoResult[2][1]) pxOut[2]= convoResult[2][0]; |
95 |
else pxOut[2]= convoResult[2][1]; |
96 |
|
97 |
} |
98 |
else{
|
99 |
pxOut[0]=(short)Math.sqrt(Math.pow(convoResult[0][0],2)+Math.pow(convoResult[0][1],2)); |
100 |
pxOut[1]=(short)Math.sqrt(Math.pow(convoResult[1][0],2)+Math.pow(convoResult[1][1],2)); |
101 |
pxOut[2]=(short)Math.sqrt(Math.pow(convoResult[2][0],2)+Math.pow(convoResult[2][1],2)); |
102 |
} |
103 |
|
104 |
if (umbral>0){ |
105 |
if(pxOut[0]>=umbral)pxOut[0]=255; |
106 |
else pxOut[0]=0; |
107 |
if(pxOut[1]>=umbral)pxOut[1]=255; |
108 |
else pxOut[1]=0; |
109 |
if(pxOut[2]>=umbral)pxOut[2]=255; |
110 |
else pxOut[2]=0; |
111 |
} |
112 |
else{
|
113 |
if (pxOut[0]<0) pxOut[0]=0; |
114 |
else if (pxOut[0]>255) pxOut[0]=255; |
115 |
if (pxOut[1]<0) pxOut[1]=0; |
116 |
else if (pxOut[1]>255) pxOut[1]=255; |
117 |
if (pxOut[2]<0) pxOut[2]=0; |
118 |
else if (pxOut[2]>255) pxOut[2]=255; |
119 |
} |
120 |
|
121 |
rasterResult.setElemShort(line, col,pxOut); |
122 |
} |
123 |
else rasterResult.setElemShort(line, col,pxOut);
|
124 |
|
125 |
} |
126 |
|
127 |
public void processSuperSampling(int col, int line) { |
128 |
short[] px = new short[4]; |
129 |
short[] pxOut = new short[4]; |
130 |
short convoResult[][] = new short[3][4]; |
131 |
int ladoVentana = 3; |
132 |
RasterFilter.Kernel kernelRGB[];
|
133 |
|
134 |
raster.getElemShort(line, col,px); |
135 |
|
136 |
// Obtener el vector con la ventanas de muestras (una por componente RGB)
|
137 |
kernelRGB=extractSubRaster(ladoVentana,col,line); |
138 |
if(kernelRGB!=null) |
139 |
{ |
140 |
convoResult[0][0]=(short)operatorH.convolution(kernelRGB[0]); |
141 |
convoResult[0][1]=(short)operatorV.convolution(kernelRGB[0]); |
142 |
convoResult[1][0]=(short)operatorH.convolution(kernelRGB[1]); |
143 |
convoResult[1][1]=(short)operatorV.convolution(kernelRGB[1]); |
144 |
convoResult[2][0]=(short)operatorH.convolution(kernelRGB[2]); |
145 |
convoResult[2][1]=(short)operatorV.convolution(kernelRGB[2]); |
146 |
|
147 |
if (compare){
|
148 |
if (convoResult[0][0]>convoResult[0][1]) pxOut[0]= convoResult[0][0]; |
149 |
else pxOut[0]= convoResult[0][1]; |
150 |
|
151 |
if (convoResult[1][0]>convoResult[1][1]) pxOut[1]= convoResult[1][0]; |
152 |
else pxOut[1]= convoResult[1][1]; |
153 |
|
154 |
if (convoResult[2][0]>convoResult[2][1]) pxOut[2]= convoResult[2][0]; |
155 |
else pxOut[2]= convoResult[2][1]; |
156 |
|
157 |
} |
158 |
else{
|
159 |
pxOut[0]=(short)Math.sqrt(Math.pow(convoResult[0][0],2)+Math.pow(convoResult[0][1],2)); |
160 |
pxOut[1]=(short)Math.sqrt(Math.pow(convoResult[1][0],2)+Math.pow(convoResult[1][1],2)); |
161 |
pxOut[2]=(short)Math.sqrt(Math.pow(convoResult[2][0],2)+Math.pow(convoResult[2][1],2)); |
162 |
} |
163 |
|
164 |
if (umbral>0){ |
165 |
if(pxOut[0]>=umbral)pxOut[0]=255; |
166 |
else pxOut[0]=0; |
167 |
if(pxOut[1]>=umbral)pxOut[1]=255; |
168 |
else pxOut[1]=0; |
169 |
if(pxOut[2]>=umbral)pxOut[2]=255; |
170 |
else pxOut[2]=0; |
171 |
} |
172 |
else{
|
173 |
|
174 |
if (pxOut[0]<0) pxOut[0]=0; |
175 |
else if (pxOut[0]>255) pxOut[0]=255; |
176 |
if (pxOut[1]<0) pxOut[1]=0; |
177 |
else if (pxOut[1]>255) pxOut[1]=255; |
178 |
if (pxOut[2]<0) pxOut[2]=0; |
179 |
else if (pxOut[2]>255) pxOut[2]=255; |
180 |
} |
181 |
|
182 |
for(int i = col; i < width && i < (col + stepX[contX + 1]); i++) |
183 |
for(int j = line; j < height && j < (line + stepY[contY + 1]); j++) |
184 |
rasterResult.setElemShort(j, i,pxOut); |
185 |
} |
186 |
else
|
187 |
for(int i = col; i < rasterResult.getWidth() && i < (col + stepX[contX + 1]); i++) |
188 |
for(int j = line; j < rasterResult.getHeight() && j < (line + stepY[contY + 1]); j++) |
189 |
rasterResult.setElemShort(j, i,px); |
190 |
|
191 |
} |
192 |
|
193 |
public void processLine(int y) { |
194 |
// TODO Auto-generated method stub
|
195 |
|
196 |
} |
197 |
|
198 |
public int getInRasterDataType() { |
199 |
return RasterBuf.TYPE_SHORT;
|
200 |
} |
201 |
|
202 |
public int getOutRasterDataType() { |
203 |
return RasterBuf.TYPE_SHORT;
|
204 |
} |
205 |
|
206 |
public Object getResult(String name) { |
207 |
if (name.equals("raster")) { |
208 |
return (Object) this.rasterResult; |
209 |
} else {
|
210 |
return null; |
211 |
} |
212 |
} |
213 |
|
214 |
public void pre() { |
215 |
exec = true;
|
216 |
this.raster = (RasterBuf)params.get("raster"); |
217 |
height = raster.getHeight(); |
218 |
width = raster.getWidth(); |
219 |
rasterResult = new RasterBuf(raster.getDataType(),width,height,raster.getBandCount(),true); |
220 |
|
221 |
super.pre();
|
222 |
} |
223 |
|
224 |
/**
|
225 |
* @param ladoVentana Lado del subraster a extraer
|
226 |
* @param x Coordenada x del pixel central
|
227 |
* @param y Coordenada y del pixel central
|
228 |
* @return
|
229 |
*/
|
230 |
public Kernel[] extractSubRaster(int ladoVentana,int x,int y){ |
231 |
|
232 |
int k,indiceX,indiceY,origenX,origenY;
|
233 |
short[] px = new short[4]; |
234 |
int semiLado = (ladoVentana-1)>>1; |
235 |
int offsetX=0; |
236 |
int offsetY=0; |
237 |
|
238 |
Kernel kernelRGB[] = null; |
239 |
|
240 |
// Obtener el vector con las ventanas de muestras (una por componente RGB) **************************
|
241 |
// Me situo en la esquina superior izquierda del "kernel"
|
242 |
if ((contX+1-semiLado>=0) && (contY+1-semiLado>=0)&& (contX+1+semiLado<stepX.length) && (contY+1+semiLado<stepY.length)){ |
243 |
|
244 |
// Calcular el alcance del kernel cuyo centro es (x,y)
|
245 |
for (int i=0;i<semiLado;i++){ |
246 |
offsetX=offsetX+stepX[contX+i+1];
|
247 |
offsetY=offsetY+stepY[contY+i+1];
|
248 |
} |
249 |
if((x+offsetX<width)&&(y+offsetY<height)){
|
250 |
|
251 |
double ventanaR[][] = new double[ladoVentana][ladoVentana]; |
252 |
double ventanaG[][] = new double[ladoVentana][ladoVentana]; |
253 |
double ventanaB[][] = new double[ladoVentana][ladoVentana]; |
254 |
kernelRGB = new Kernel[3]; |
255 |
|
256 |
origenX=x; |
257 |
origenY=y; |
258 |
for (int i=0;i<semiLado;i++){ |
259 |
origenX=origenX-stepX[contX-i]; |
260 |
origenY=origenY-stepY[contY-i]; |
261 |
} |
262 |
//Recorro el kernel seg?n los step
|
263 |
k=0;
|
264 |
indiceX=origenX; |
265 |
for (int i=-semiLado;i<=semiLado;i++){ |
266 |
indiceY=origenY; |
267 |
for(int j=-semiLado;j<=semiLado;j++){ |
268 |
raster.getElemShort(indiceY,indiceX,px); |
269 |
ventanaR[i+semiLado][j+semiLado] =px[0];
|
270 |
ventanaG[i+semiLado][j+semiLado] =px[1];
|
271 |
ventanaB[i+semiLado][j+semiLado] =px[2];
|
272 |
indiceY=indiceY+stepY[contY+j+1];
|
273 |
k++; |
274 |
} |
275 |
indiceX=indiceX+stepX[contX+i+1];
|
276 |
} |
277 |
//**************************************************************************************************
|
278 |
kernelRGB[0]= new Kernel(ventanaR); |
279 |
kernelRGB[1]= new Kernel(ventanaG); |
280 |
kernelRGB[2]= new Kernel(ventanaB); |
281 |
} |
282 |
} |
283 |
return kernelRGB;
|
284 |
} |
285 |
|
286 |
} |