Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / statistics / TailTrimFilter.java @ 12333

History | View | Annotate | Download (7.48 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 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.statistics;
20

    
21
import java.util.Arrays;
22

    
23
import org.gvsig.raster.dataset.IBuffer;
24
import org.gvsig.raster.dataset.Params;
25
import org.gvsig.raster.grid.filter.RasterFilter;
26
import org.gvsig.raster.shared.IStatistics;
27
/**
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
 * @author Nacho Brodin (nachobrodin@gmail.com)
39
 */
40
public class TailTrimFilter extends RasterFilter {
41
        public static String[] names = new String[] {"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

    
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
        /**
57
         * Array con el resultado. La primera dimensi?n es el n?mero de bandas y la segunda son dos elementos
58
         * el m?ximo y el m?nimo para esa banda.
59
         */
60
        protected double[][]        result = null;
61

    
62

    
63
        public TailTrimFilter() {
64
                setName(names[0]);
65
        }
66

    
67
        /**
68
         * Calcula el incremento de X y de Y para la toma de muestras en el calculo de
69
         * valores para el recorte
70
         */
71
        public void pre() {
72
                raster = (IBuffer) params.get("raster");
73
                height = raster.getHeight();
74
                width = raster.getWidth();
75
                tailPercent = ((Double) params.get("tail")).doubleValue();
76
                percentSamples = ((Double) params.get("samples")).doubleValue();
77
                removeMaxValue = ((Boolean) params.get("remove")).booleanValue();
78
                stats = ((IStatistics) params.get("stats"));
79

    
80
                if (exec) {
81
                        count = 0;
82

    
83
                        if (this.percentSamples == 0) { // Se toman todos los pixeles de la imagen
84
                                nSamples = height * width;
85
                                tailSize = (int) Math.round(this.tailPercent * nSamples);
86
                        } else { // Se toma un porcentaje de pixeles de la imagen para el calculo
87
                                incX = (int) Math.round(width / (int) Math.round(this.percentSamples * width));
88
                                incY = (int) Math.round(height / (int) Math.round(this.percentSamples * height));
89
                                nSamples = (int) ((Math.round(width / incX) + 1) * (Math.round(height / incY) + 1));
90
                                tailSize = (int) (nSamples * this.tailPercent);
91
                        }
92
                }
93
        }
94

    
95
        protected int posInit = 0;
96
        protected int posFin = 0;
97

    
98
        /**
99
         * Ordena las muestras , recorta y asigna m?ximo y m?nimo dependiendo del
100
         * porcentaje de recorte
101
         */
102
        public void post() {
103
                if (exec) {
104
                        // Ordenamos los vectores
105
                        if (sample != null) {
106
                                posInit = 0;
107
                                posFin = sample[0].length - 1;
108
                                for (int i = 0; i < raster.getBandCount(); i++)
109
                                        Arrays.sort(sample[i]);
110
                        } else {
111
                                posInit = 0;
112
                                posFin = sampleDec[0].length - 1;
113
                                for (int i = 0; i < raster.getBandCount(); i++)
114
                                        Arrays.sort(sampleDec[i]);
115
                        }
116

    
117
                        // Si est? marcada la opci?n removeMaxValue se calcula la posici?n en la que el m?ximo valor
118
                        // y el m?nimo ya no estan, teniendo as? un subconjunto del vector que elimina estos valores
119
                        if (removeMaxValue) {
120
                                if (sample != null)
121
                                        this.calcPosInitEnd();
122

    
123
                                if (sampleDec != null)
124
                                        this.calcPosInitEndDec();
125
                        }
126

    
127
                        // Calculamos de nuevo el n?mero de muestras ya que hemos quitado los valores m?ximo y m?nimo
128
                        nSamples = posFin - posInit;
129

    
130
                        // Como ha podido cambiar nSamples recalculamos tailsize
131
                        tailSize = (int) (nSamples * this.tailPercent);
132
                }
133
        }
134

    
135
        /**
136
         * Calcula la posici?n de inicio y final donde el m?ximo y el m?nimo ya no
137
         * est?n para valores enteros.
138
         */
139
        private void calcPosInitEnd() {
140
                for (int i = 0; i < sample[0].length; i++) {
141
                        for (int iBand = 0; iBand < raster.getBandCount(); iBand++) {
142
                                if (sample[iBand][i] != sample[iBand][0]) {
143
                                        posInit = i;
144
                                        break;
145
                                }
146
                        }
147
                        if (posInit != 0)
148
                                break;
149
                }
150

    
151
                for (int i = sample[0].length - 1; i > 0; i--) {
152
                        for (int iBand = 0; iBand < raster.getBandCount(); iBand++) {
153
                                if (sample[0][i] != sample[0][sample[0].length - 1]) {
154
                                        posFin = i;
155
                                        break;
156
                                }
157
                        }
158
                        if (posFin != sample[0].length - 1)
159
                                break;
160
                }
161
        }
162

    
163
        /**
164
         * Calcula la posici?n de inicio y final donde el m?ximo y el m?nimo ya no
165
         * est?n para valores decimal.
166
         */
167
        private void calcPosInitEndDec() {
168
                for (int i = 0; i < sampleDec[0].length; i++) {
169
                        for (int iBand = 0; iBand < raster.getBandCount(); iBand++) {
170
                                if (sampleDec[iBand][i] != sampleDec[iBand][0]) {
171
                                        posInit = i;
172
                                        break;
173
                                }
174
                        }
175
                        if (posInit != 0)
176
                                break;
177
                }
178

    
179
                for (int i = sampleDec[0].length - 1; i > 0; i--) {
180
                        for (int iBand = 0; iBand < raster.getBandCount(); iBand++) {
181
                                if (sampleDec[0][i] != sampleDec[0][sampleDec[0].length - 1]) {
182
                                        posFin = i;
183
                                        break;
184
                                }
185
                        }
186
                        if (posFin != sampleDec[0].length - 1)
187
                                break;
188
                }
189
        }
190

    
191
        /**
192
         * Obtiene el porcentaje de recorte
193
         * @return porcentaje de recorte
194
         */
195
        public double getTailPercent() {
196
                return tailPercent;
197
        }
198

    
199
        /**
200
         * Devuelve true si se eliminan los extremos de la serie antes del calculo del recorte de colas
201
         * o false si no se eliminan.
202
         * @return
203
         */
204
        public boolean removeMaxValue() {
205
                return this.removeMaxValue;
206
        }
207

    
208
        /*
209
         * (non-Javadoc)
210
         * @see org.gvsig.raster.grid.filter.RasterFilter#getGroup()
211
         */
212
        public String getGroup() {
213
                return "basics";
214
        }
215

    
216
        /*
217
         * (non-Javadoc)
218
         * @see org.gvsig.raster.grid.filter.RasterFilter#getUIParams()
219
         */
220
        public Params getUIParams(String nameFilter) {
221
                Params params = new Params();
222
                return params;
223
        }
224

    
225
        /*
226
         * (non-Javadoc)
227
         * @see org.gvsig.raster.grid.filter.RasterFilter#getInRasterDataType()
228
         */
229
        public int getInRasterDataType() {
230
                return 0;
231
        }
232

    
233
        /*
234
         * (non-Javadoc)
235
         * @see org.gvsig.raster.grid.filter.RasterFilter#getOutRasterDataType()
236
         */
237
        public int getOutRasterDataType() {
238
                return 0;
239
        }
240

    
241
        /*
242
         * (non-Javadoc)
243
         * @see org.gvsig.raster.grid.filter.RasterFilter#getResult(java.lang.String)
244
         */
245
        public Object getResult(String name) {
246
                return null;
247
        }
248

    
249
        /*
250
         * (non-Javadoc)
251
         * @see org.gvsig.raster.grid.filter.RasterFilter#process(int, int)
252
         */
253
        public void process(int x, int y) {
254
        }
255

    
256
        /*
257
         * (non-Javadoc)
258
         * @see org.gvsig.raster.grid.filter.RasterFilter#getNames()
259
         */
260
        public String[] getNames() {
261
                return names;
262
        }
263

    
264
        /*
265
         * (non-Javadoc)
266
         * @see org.gvsig.raster.grid.filter.RasterFilter#isVisible()
267
         */
268
        public boolean isVisible() {
269
                return false;
270
        }
271
}