Revision 11921 trunk/libraries/libRaster/src/org/gvsig/raster/grid/filter/statistics/TailTrimFilter.java
TailTrimFilter.java | ||
---|---|---|
24 | 24 |
import org.gvsig.raster.dataset.IStatistics; |
25 | 25 |
import org.gvsig.raster.dataset.Params; |
26 | 26 |
import org.gvsig.raster.grid.filter.RasterFilter; |
27 |
|
|
28 |
|
|
29 | 27 |
/** |
30 |
* Filtro de recorte de colas. Este filtro toma pixels de la imagen (todos o algunas muestras |
|
31 |
* dependiendo de la variable percentSample) y los ordena. Recorta un porcentaje controlado |
|
32 |
* por tailPercenten ambos extremos del vector ordenado. El nuevo m?ximo y m?nimo coinciden |
|
33 |
* con el valor de la posici?n del vector recortado. Por arriba para el m?ximo y por abajo |
|
34 |
* para el m?nimo. |
|
35 |
* El execute de este filtro no recorre toda la imagen sino que lo |
|
36 |
* hace en funci?n del porcentaje de muestras que quieren tomarse y |
|
37 |
* calculando a partir de este porcentaje un incremento. |
|
28 |
* Filtro de recorte de colas. Este filtro toma pixels de la imagen (todos o |
|
29 |
* algunas muestras dependiendo de la variable percentSample) y los ordena. |
|
30 |
* Recorta un porcentaje controlado por tailPercenten ambos extremos del vector |
|
31 |
* ordenado. El nuevo m?ximo y m?nimo coinciden con el valor de la posici?n del |
|
32 |
* vector recortado. Por arriba para el m?ximo y por abajo para el m?nimo. |
|
33 |
* El execute de este filtro no recorre toda la imagen sino que lo hace en |
|
34 |
* funci?n del porcentaje de muestras que quieren tomarse y calculando a partir |
|
35 |
* de este porcentaje un incremento. |
|
36 |
* |
|
37 |
* @version 31/05/2007 |
|
38 | 38 |
* @author Nacho Brodin (nachobrodin@gmail.com) |
39 | 39 |
*/ |
40 | 40 |
public class TailTrimFilter extends RasterFilter { |
41 | 41 |
public static String genericName = "tailTrim"; |
42 |
|
|
43 |
protected int count = 0; |
|
44 |
protected int tailSize = 0; |
|
45 |
protected int nSamples = 0; |
|
46 |
protected boolean removeMaxValue = false; |
|
47 |
protected int incX , incY; |
|
48 | 42 |
|
49 |
//Par?metros del filtro |
|
50 |
protected double tailPercent = 0D; |
|
51 |
public double percentSamples = 0D; |
|
52 |
|
|
53 |
protected int[][] sample = null; |
|
54 |
protected double[][] sampleDec = null; |
|
55 |
protected IStatistics stats = null; |
|
43 |
protected int count = 0; |
|
44 |
protected int tailSize = 0; |
|
45 |
protected int nSamples = 0; |
|
46 |
protected boolean removeMaxValue = false; |
|
47 |
protected int incX , incY; |
|
48 |
|
|
49 |
//Par?metros del filtro |
|
50 |
protected double tailPercent = 0D; |
|
51 |
public double percentSamples = 0D; |
|
52 |
|
|
53 |
protected int[][] sample = null; |
|
54 |
protected double[][] sampleDec = null; |
|
55 |
protected IStatistics stats = null; |
|
56 | 56 |
/** |
57 | 57 |
* Array con el resultado. La primera dimensi?n es el n?mero de bandas y la segunda son dos elementos |
58 | 58 |
* el m?ximo y el m?nimo para esa banda. |
... | ... | |
60 | 60 |
protected double[][] result = null; |
61 | 61 |
|
62 | 62 |
|
63 |
public TailTrimFilter() {
|
|
64 |
super();
|
|
65 |
super.fName = genericName;
|
|
66 |
}
|
|
63 |
public TailTrimFilter() {
|
|
64 |
super();
|
|
65 |
super.fName = genericName;
|
|
66 |
}
|
|
67 | 67 |
|
68 |
/**
|
|
69 |
* Calcula el incremento de X y de Y para la toma de muestras en el calculo de
|
|
70 |
* valores para el recorte
|
|
71 |
*/
|
|
72 |
public void pre() {
|
|
73 |
raster = (IBuffer) params.get("raster");
|
|
74 |
height = raster.getHeight();
|
|
75 |
width = raster.getWidth();
|
|
76 |
tailPercent = ((Double) params.get("tail")).doubleValue();
|
|
77 |
percentSamples = ((Double) params.get("samples")).doubleValue();
|
|
78 |
removeMaxValue = ((Boolean) params.get("remove")).booleanValue();
|
|
79 |
stats = ((IStatistics) params.get("stats"));
|
|
68 |
/**
|
|
69 |
* Calcula el incremento de X y de Y para la toma de muestras en el calculo de
|
|
70 |
* valores para el recorte
|
|
71 |
*/
|
|
72 |
public void pre() {
|
|
73 |
raster = (IBuffer) params.get("raster");
|
|
74 |
height = raster.getHeight();
|
|
75 |
width = raster.getWidth();
|
|
76 |
tailPercent = ((Double) params.get("tail")).doubleValue();
|
|
77 |
percentSamples = ((Double) params.get("samples")).doubleValue();
|
|
78 |
removeMaxValue = ((Boolean) params.get("remove")).booleanValue();
|
|
79 |
stats = ((IStatistics) params.get("stats"));
|
|
80 | 80 |
|
81 |
if (exec) {
|
|
82 |
count = 0;
|
|
81 |
if (exec) {
|
|
82 |
count = 0;
|
|
83 | 83 |
|
84 |
if (this.percentSamples == 0) { //Se toman todos los pixeles de la imagen
|
|
85 |
nSamples = height * width;
|
|
86 |
tailSize = (int) Math.round(this.tailPercent * nSamples);
|
|
87 |
} else { //Se toma un porcentaje de pixeles de la imagen para el calculo
|
|
88 |
incX = (int) Math.round(width / (int) Math.round(this.percentSamples * width));
|
|
89 |
incY = (int) Math.round(height / (int) Math.round(this.percentSamples * height));
|
|
90 |
nSamples = (int) ((Math.round(width / incX) + 1) * (Math.round(height / incY) + 1));
|
|
91 |
tailSize = (int) (nSamples * this.tailPercent);
|
|
92 |
}
|
|
93 |
}
|
|
94 |
}
|
|
84 |
if (this.percentSamples == 0) { // Se toman todos los pixeles de la imagen
|
|
85 |
nSamples = height * width;
|
|
86 |
tailSize = (int) Math.round(this.tailPercent * nSamples);
|
|
87 |
} else { // Se toma un porcentaje de pixeles de la imagen para el calculo
|
|
88 |
incX = (int) Math.round(width / (int) Math.round(this.percentSamples * width));
|
|
89 |
incY = (int) Math.round(height / (int) Math.round(this.percentSamples * height));
|
|
90 |
nSamples = (int) ((Math.round(width / incX) + 1) * (Math.round(height / incY) + 1));
|
|
91 |
tailSize = (int) (nSamples * this.tailPercent);
|
|
92 |
}
|
|
93 |
}
|
|
94 |
}
|
|
95 | 95 |
|
96 |
protected int posInit = 0; |
|
97 |
protected int posFin = 0; |
|
98 |
|
|
99 |
/** |
|
100 |
* Ordena las muestras , recorta y asigna m?ximo y m?nimo dependiendo del porcentaje |
|
101 |
* de recorte |
|
102 |
*/ |
|
103 |
public void post() { |
|
104 |
if (exec) { |
|
105 |
//Ordenamos los vectores |
|
106 |
if(sample != null){ |
|
107 |
posInit = 0; |
|
108 |
posFin = sample[0].length - 1; |
|
109 |
for (int i = 0; i < raster.getBandCount(); i++) |
|
110 |
Arrays.sort(sample[i]); |
|
111 |
}else{ |
|
112 |
posInit = 0; |
|
113 |
posFin = sampleDec[0].length - 1; |
|
114 |
for (int i = 0; i < raster.getBandCount(); i++) |
|
115 |
Arrays.sort(sampleDec[i]); |
|
116 |
} |
|
96 |
protected int posInit = 0; |
|
97 |
protected int posFin = 0; |
|
117 | 98 |
|
118 |
//Si est? marcada la opci?n removeMaxValue se calcula la posici?n en la que el m?ximo valor |
|
119 |
//y el m?nimo ya no estan, teniendo as? un subconjunto del vector que elimina estos valores |
|
120 |
if (removeMaxValue) { |
|
121 |
if(sample != null) |
|
122 |
this.calcPosInitEnd(); |
|
123 |
|
|
124 |
if(sampleDec != null) |
|
125 |
this.calcPosInitEndDec(); |
|
126 |
} |
|
99 |
/** |
|
100 |
* Ordena las muestras , recorta y asigna m?ximo y m?nimo dependiendo del |
|
101 |
* porcentaje de recorte |
|
102 |
*/ |
|
103 |
public void post() { |
|
104 |
if (exec) { |
|
105 |
// Ordenamos los vectores |
|
106 |
if (sample != null) { |
|
107 |
posInit = 0; |
|
108 |
posFin = sample[0].length - 1; |
|
109 |
for (int i = 0; i < raster.getBandCount(); i++) |
|
110 |
Arrays.sort(sample[i]); |
|
111 |
} else { |
|
112 |
posInit = 0; |
|
113 |
posFin = sampleDec[0].length - 1; |
|
114 |
for (int i = 0; i < raster.getBandCount(); i++) |
|
115 |
Arrays.sort(sampleDec[i]); |
|
116 |
} |
|
127 | 117 |
|
128 |
//Calculamos de nuevo el n?mero de muestras ya que hemos quitado los valores m?ximo y m?nimo |
|
129 |
nSamples = posFin - posInit; |
|
118 |
// Si est? marcada la opci?n removeMaxValue se calcula la posici?n en la que el m?ximo valor |
|
119 |
// y el m?nimo ya no estan, teniendo as? un subconjunto del vector que elimina estos valores |
|
120 |
if (removeMaxValue) { |
|
121 |
if (sample != null) |
|
122 |
this.calcPosInitEnd(); |
|
130 | 123 |
|
131 |
//Como ha podido cambiar nSamples recalculamos tailsize |
|
132 |
tailSize = (int) (nSamples * this.tailPercent); |
|
133 |
} |
|
134 |
} |
|
124 |
if (sampleDec != null) |
|
125 |
this.calcPosInitEndDec(); |
|
126 |
} |
|
135 | 127 |
|
136 |
/** |
|
137 |
* Calcula la posici?n de inicio y final donde el m?ximo y el m?nimo ya no est?n |
|
138 |
* para valores enteros. |
|
139 |
*/ |
|
140 |
private void calcPosInitEnd(){ |
|
141 |
for (int i = 0; i < sample[0].length; i++) { |
|
142 |
for(int iBand = 0; iBand < raster.getBandCount(); iBand ++){ |
|
143 |
if (sample[iBand][i] != sample[iBand][0]){ |
|
144 |
posInit = i; |
|
145 |
break; |
|
146 |
} |
|
147 |
} |
|
148 |
if(posInit != 0) |
|
149 |
break; |
|
150 |
} |
|
128 |
// Calculamos de nuevo el n?mero de muestras ya que hemos quitado los valores m?ximo y m?nimo |
|
129 |
nSamples = posFin - posInit; |
|
151 | 130 |
|
152 |
for (int i = sample[0].length - 1; i > 0; i--) { |
|
153 |
for(int iBand = 0; iBand < raster.getBandCount(); iBand ++){ |
|
154 |
if (sample[0][i] != sample[0][sample[0].length - 1]){ |
|
155 |
posFin = i; |
|
156 |
break; |
|
157 |
} |
|
158 |
} |
|
159 |
if(posFin != sample[0].length - 1) |
|
160 |
break; |
|
161 |
} |
|
162 |
} |
|
163 |
|
|
164 |
/** |
|
165 |
* Calcula la posici?n de inicio y final donde el m?ximo y el m?nimo ya no est?n |
|
166 |
* para valores decimal. |
|
167 |
*/ |
|
168 |
private void calcPosInitEndDec(){ |
|
169 |
for (int i = 0; i < sampleDec[0].length; i++) { |
|
170 |
for(int iBand = 0; iBand < raster.getBandCount(); iBand ++){ |
|
171 |
if (sampleDec[iBand][i] != sampleDec[iBand][0]){ |
|
172 |
posInit = i; |
|
173 |
break; |
|
174 |
} |
|
175 |
} |
|
176 |
if(posInit != 0) |
|
177 |
break; |
|
178 |
} |
|
131 |
// Como ha podido cambiar nSamples recalculamos tailsize |
|
132 |
tailSize = (int) (nSamples * this.tailPercent); |
|
133 |
} |
|
134 |
} |
|
179 | 135 |
|
180 |
for (int i = sampleDec[0].length - 1; i > 0; i--) { |
|
181 |
for(int iBand = 0; iBand < raster.getBandCount(); iBand ++){ |
|
182 |
if (sampleDec[0][i] != sampleDec[0][sampleDec[0].length - 1]){ |
|
183 |
posFin = i; |
|
184 |
break; |
|
185 |
} |
|
186 |
} |
|
187 |
if(posFin != sampleDec[0].length - 1) |
|
188 |
break; |
|
189 |
} |
|
190 |
} |
|
191 |
|
|
192 |
/** |
|
193 |
* Obtiene el porcentaje de recorte |
|
194 |
* @return porcentaje de recorte |
|
195 |
*/ |
|
196 |
public double getTailPercent() { |
|
197 |
return tailPercent; |
|
198 |
} |
|
136 |
/** |
|
137 |
* Calcula la posici?n de inicio y final donde el m?ximo y el m?nimo ya no |
|
138 |
* est?n para valores enteros. |
|
139 |
*/ |
|
140 |
private void calcPosInitEnd() { |
|
141 |
for (int i = 0; i < sample[0].length; i++) { |
|
142 |
for (int iBand = 0; iBand < raster.getBandCount(); iBand++) { |
|
143 |
if (sample[iBand][i] != sample[iBand][0]) { |
|
144 |
posInit = i; |
|
145 |
break; |
|
146 |
} |
|
147 |
} |
|
148 |
if (posInit != 0) |
|
149 |
break; |
|
150 |
} |
|
199 | 151 |
|
200 |
/** |
|
201 |
* Devuelve true si se eliminan los extremos de la serie antes del calculo del recorte de colas |
|
202 |
* o false si no se eliminan. |
|
203 |
* @return |
|
204 |
*/ |
|
205 |
public boolean removeMaxValue() { |
|
206 |
return this.removeMaxValue; |
|
207 |
} |
|
208 |
|
|
209 |
/* |
|
210 |
* (non-Javadoc) |
|
211 |
* @see org.gvsig.raster.grid.filter.RasterFilter#getName() |
|
212 |
*/ |
|
213 |
public String getName() { |
|
214 |
return genericName; |
|
215 |
} |
|
216 |
|
|
217 |
/* |
|
218 |
* (non-Javadoc) |
|
219 |
* @see org.gvsig.raster.grid.filter.IRasterFilter#getGroup() |
|
220 |
*/ |
|
221 |
public String getGroup() { |
|
222 |
return "basics"; |
|
223 |
} |
|
224 |
|
|
225 |
/* |
|
226 |
* (non-Javadoc) |
|
227 |
* @see org.gvsig.raster.grid.filter.IRasterFilter#getParams() |
|
228 |
*/ |
|
229 |
public Params getUIParams() { |
|
230 |
Params params = new Params(); |
|
231 |
return params; |
|
232 |
} |
|
233 |
|
|
234 |
/* |
|
235 |
* (non-Javadoc) |
|
236 |
* @see org.gvsig.raster.grid.filter.RasterFilter#getInRasterDataType() |
|
237 |
*/ |
|
238 |
public int getInRasterDataType() { |
|
239 |
return 0; |
|
152 |
for (int i = sample[0].length - 1; i > 0; i--) { |
|
153 |
for (int iBand = 0; iBand < raster.getBandCount(); iBand++) { |
|
154 |
if (sample[0][i] != sample[0][sample[0].length - 1]) { |
|
155 |
posFin = i; |
|
156 |
break; |
|
157 |
} |
|
158 |
} |
|
159 |
if (posFin != sample[0].length - 1) |
|
160 |
break; |
|
240 | 161 |
} |
162 |
} |
|
241 | 163 |
|
242 |
/* |
|
243 |
* (non-Javadoc) |
|
244 |
* @see org.gvsig.raster.grid.filter.RasterFilter#getOutRasterDataType() |
|
245 |
*/ |
|
246 |
public int getOutRasterDataType() { |
|
247 |
return 0; |
|
164 |
/** |
|
165 |
* Calcula la posici?n de inicio y final donde el m?ximo y el m?nimo ya no |
|
166 |
* est?n para valores decimal. |
|
167 |
*/ |
|
168 |
private void calcPosInitEndDec() { |
|
169 |
for (int i = 0; i < sampleDec[0].length; i++) { |
|
170 |
for (int iBand = 0; iBand < raster.getBandCount(); iBand++) { |
|
171 |
if (sampleDec[iBand][i] != sampleDec[iBand][0]) { |
|
172 |
posInit = i; |
|
173 |
break; |
|
174 |
} |
|
175 |
} |
|
176 |
if (posInit != 0) |
|
177 |
break; |
|
248 | 178 |
} |
249 | 179 |
|
250 |
/* |
|
251 |
* (non-Javadoc) |
|
252 |
* @see org.gvsig.raster.grid.filter.RasterFilter#getResult(java.lang.String) |
|
253 |
*/ |
|
254 |
public Object getResult(String name) { |
|
255 |
return null; |
|
180 |
for (int i = sampleDec[0].length - 1; i > 0; i--) { |
|
181 |
for (int iBand = 0; iBand < raster.getBandCount(); iBand++) { |
|
182 |
if (sampleDec[0][i] != sampleDec[0][sampleDec[0].length - 1]) { |
|
183 |
posFin = i; |
|
184 |
break; |
|
185 |
} |
|
186 |
} |
|
187 |
if (posFin != sampleDec[0].length - 1) |
|
188 |
break; |
|
256 | 189 |
} |
190 |
} |
|
257 | 191 |
|
258 |
/* |
|
259 |
* (non-Javadoc) |
|
260 |
* @see org.gvsig.raster.grid.filter.RasterFilter#process(int, int) |
|
261 |
*/ |
|
262 |
public void process(int x, int y) { |
|
263 |
} |
|
192 |
/** |
|
193 |
* Obtiene el porcentaje de recorte |
|
194 |
* @return porcentaje de recorte |
|
195 |
*/ |
|
196 |
public double getTailPercent() { |
|
197 |
return tailPercent; |
|
198 |
} |
|
199 |
|
|
200 |
/** |
|
201 |
* Devuelve true si se eliminan los extremos de la serie antes del calculo del recorte de colas |
|
202 |
* o false si no se eliminan. |
|
203 |
* @return |
|
204 |
*/ |
|
205 |
public boolean removeMaxValue() { |
|
206 |
return this.removeMaxValue; |
|
207 |
} |
|
208 |
|
|
209 |
/* |
|
210 |
* (non-Javadoc) |
|
211 |
* @see org.gvsig.raster.grid.filter.RasterFilter#getName() |
|
212 |
*/ |
|
213 |
public String getName() { |
|
214 |
return genericName; |
|
215 |
} |
|
216 |
|
|
217 |
/* |
|
218 |
* (non-Javadoc) |
|
219 |
* @see org.gvsig.raster.grid.filter.RasterFilter#getGroup() |
|
220 |
*/ |
|
221 |
public String getGroup() { |
|
222 |
return "basics"; |
|
223 |
} |
|
224 |
|
|
225 |
/* |
|
226 |
* (non-Javadoc) |
|
227 |
* @see org.gvsig.raster.grid.filter.RasterFilter#getUIParams() |
|
228 |
*/ |
|
229 |
public Params getUIParams() { |
|
230 |
Params params = new Params(); |
|
231 |
return params; |
|
232 |
} |
|
233 |
|
|
234 |
/* |
|
235 |
* (non-Javadoc) |
|
236 |
* @see org.gvsig.raster.grid.filter.RasterFilter#getInRasterDataType() |
|
237 |
*/ |
|
238 |
public int getInRasterDataType() { |
|
239 |
return 0; |
|
240 |
} |
|
241 |
|
|
242 |
/* |
|
243 |
* (non-Javadoc) |
|
244 |
* @see org.gvsig.raster.grid.filter.RasterFilter#getOutRasterDataType() |
|
245 |
*/ |
|
246 |
public int getOutRasterDataType() { |
|
247 |
return 0; |
|
248 |
} |
|
249 |
|
|
250 |
/* |
|
251 |
* (non-Javadoc) |
|
252 |
* @see org.gvsig.raster.grid.filter.RasterFilter#getResult(java.lang.String) |
|
253 |
*/ |
|
254 |
public Object getResult(String name) { |
|
255 |
return null; |
|
256 |
} |
|
257 |
|
|
258 |
/* |
|
259 |
* (non-Javadoc) |
|
260 |
* @see org.gvsig.raster.grid.filter.RasterFilter#process(int, int) |
|
261 |
*/ |
|
262 |
public void process(int x, int y) { |
|
263 |
} |
|
264 | 264 |
} |
Also available in: Unified diff