Statistics
| Revision:

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

History | View | Annotate | Download (8.43 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.hierarchy.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[]                        tailSizeList     = null;
46
        protected int                         nSamples         = 0;
47
        protected boolean                 removeMaxValue   = false;
48
        protected int                         incX , incY;
49

    
50
        //Par?metros del filtro
51
        protected double                 tailPercent      = 0D;
52
        protected double[]                 tailPercentList  = null;
53
        public double                         percentSamples   = 0D;
54

    
55
        protected int[][]                 sample           = null;
56
        protected double[][]         sampleDec        = null;
57
        protected IStatistics        stats            = null;
58
        /**
59
         * Array con el resultado. La primera dimensi?n es el n?mero de bandas y la segunda son dos elementos
60
         * el m?ximo y el m?nimo para esa banda.
61
         */
62
        protected double[][]        result = null;
63

    
64

    
65
        public TailTrimFilter() {
66
                setName(names[0]);
67
        }
68

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

    
83
                if(tailPercentList != null)
84
                        tailSizeList = new int[tailPercentList.length];
85
                
86
                if (exec) {
87
                        count = 0;
88

    
89
                        if (this.percentSamples == 0) { // Se toman todos los pixeles de la imagen
90
                                nSamples = height * width;
91
                                tailSize = (int) Math.round(this.tailPercent * nSamples);
92
                                if(tailPercentList != null) {
93
                                        for (int i = 0; i < tailPercentList.length; i++)
94
                                                tailSizeList[i] = (int) Math.round(this.tailPercentList[i] * nSamples);
95
                                }                
96
                        } else { // Se toma un porcentaje de pixeles de la imagen para el calculo
97
                                incX = (int) Math.round(width / (int) Math.round(this.percentSamples * width));
98
                                incY = (int) Math.round(height / (int) Math.round(this.percentSamples * height));
99
                                nSamples = (int) ((Math.round(width / incX) + 1) * (Math.round(height / incY) + 1));
100
                                tailSize = (int) (nSamples * this.tailPercent);
101
                                if(tailPercentList != null) {
102
                                        for (int i = 0; i < tailPercentList.length; i++)
103
                                                tailSizeList[i] = (int) (nSamples * this.tailPercentList[i]);
104
                                }
105
                        }
106
                }
107
        }
108

    
109
        protected int posInit = 0;
110
        protected int posFin = 0;
111

    
112
        /**
113
         * Ordena las muestras , recorta y asigna m?ximo y m?nimo dependiendo del
114
         * porcentaje de recorte
115
         */
116
        public void post() {
117
                if (exec) {
118
                        // Ordenamos los vectores
119
                        if (sample != null) {
120
                                posInit = 0;
121
                                posFin = sample[0].length - 1;
122
                                for (int i = 0; i < raster.getBandCount(); i++)
123
                                        Arrays.sort(sample[i]);
124
                        } else {
125
                                posInit = 0;
126
                                posFin = sampleDec[0].length - 1;
127
                                for (int i = 0; i < raster.getBandCount(); i++)
128
                                        Arrays.sort(sampleDec[i]);
129
                        }
130

    
131
                        // Si est? marcada la opci?n removeMaxValue se calcula la posici?n en la que el m?ximo valor
132
                        // y el m?nimo ya no estan, teniendo as? un subconjunto del vector que elimina estos valores
133
                        if (removeMaxValue) {
134
                                if (sample != null)
135
                                        this.calcPosInitEnd();
136

    
137
                                if (sampleDec != null)
138
                                        this.calcPosInitEndDec();
139
                        }
140

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

    
144
                        // Como ha podido cambiar nSamples recalculamos tailsize
145
                        tailSize = (int) (nSamples * this.tailPercent);
146
                        if(tailPercentList != null) {
147
                                for (int i = 0; i < tailPercentList.length; i++)
148
                                        tailSizeList[i] = (int) (nSamples * this.tailPercentList[i]);
149
                        }
150
                }
151
        }
152

    
153
        /**
154
         * Calcula la posici?n de inicio y final donde el m?ximo y el m?nimo ya no
155
         * est?n para valores enteros.
156
         */
157
        private void calcPosInitEnd() {
158
                for (int i = 0; i < sample[0].length; i++) {
159
                        for (int iBand = 0; iBand < raster.getBandCount(); iBand++) {
160
                                if (sample[iBand][i] != sample[iBand][0]) {
161
                                        posInit = i;
162
                                        break;
163
                                }
164
                        }
165
                        if (posInit != 0)
166
                                break;
167
                }
168

    
169
                for (int i = sample[0].length - 1; i > 0; i--) {
170
                        for (int iBand = 0; iBand < raster.getBandCount(); iBand++) {
171
                                if (sample[0][i] != sample[0][sample[0].length - 1]) {
172
                                        posFin = i;
173
                                        break;
174
                                }
175
                        }
176
                        if (posFin != sample[0].length - 1)
177
                                break;
178
                }
179
        }
180

    
181
        /**
182
         * Calcula la posici?n de inicio y final donde el m?ximo y el m?nimo ya no
183
         * est?n para valores decimal.
184
         */
185
        private void calcPosInitEndDec() {
186
                for (int i = 0; i < sampleDec[0].length; i++) {
187
                        for (int iBand = 0; iBand < raster.getBandCount(); iBand++) {
188
                                if (sampleDec[iBand][i] != sampleDec[iBand][0]) {
189
                                        posInit = i;
190
                                        break;
191
                                }
192
                        }
193
                        if (posInit != 0)
194
                                break;
195
                }
196

    
197
                for (int i = sampleDec[0].length - 1; i > 0; i--) {
198
                        for (int iBand = 0; iBand < raster.getBandCount(); iBand++) {
199
                                if (sampleDec[0][i] != sampleDec[0][sampleDec[0].length - 1]) {
200
                                        posFin = i;
201
                                        break;
202
                                }
203
                        }
204
                        if (posFin != sampleDec[0].length - 1)
205
                                break;
206
                }
207
        }
208

    
209
        /**
210
         * Obtiene el porcentaje de recorte
211
         * @return porcentaje de recorte
212
         */
213
        public double getTailPercent() {
214
                return tailPercent;
215
        }
216

    
217
        /**
218
         * Obtiene la lista de porcentajes de recorte 
219
         * @return porcentajes de recorte
220
         */
221
        public double[] getTailPercentList() {
222
                return tailPercentList;
223
        }
224
        
225
        /**
226
         * Devuelve true si se eliminan los extremos de la serie antes del calculo del recorte de colas
227
         * o false si no se eliminan.
228
         * @return
229
         */
230
        public boolean removeMaxValue() {
231
                return this.removeMaxValue;
232
        }
233

    
234
        /*
235
         * (non-Javadoc)
236
         * @see org.gvsig.raster.grid.filter.RasterFilter#getGroup()
237
         */
238
        public String getGroup() {
239
                return "basics";
240
        }
241

    
242
        /*
243
         * (non-Javadoc)
244
         * @see org.gvsig.raster.grid.filter.RasterFilter#getUIParams()
245
         */
246
        public Params getUIParams(String nameFilter) {
247
                Params params = new Params();
248
                return params;
249
        }
250

    
251
        /*
252
         * (non-Javadoc)
253
         * @see org.gvsig.raster.grid.filter.RasterFilter#getInRasterDataType()
254
         */
255
        public int getInRasterDataType() {
256
                return 0;
257
        }
258

    
259
        /*
260
         * (non-Javadoc)
261
         * @see org.gvsig.raster.grid.filter.RasterFilter#getOutRasterDataType()
262
         */
263
        public int getOutRasterDataType() {
264
                return 0;
265
        }
266

    
267
        /*
268
         * (non-Javadoc)
269
         * @see org.gvsig.raster.grid.filter.RasterFilter#getResult(java.lang.String)
270
         */
271
        public Object getResult(String name) {
272
                return null;
273
        }
274

    
275
        /*
276
         * (non-Javadoc)
277
         * @see org.gvsig.raster.grid.filter.RasterFilter#process(int, int)
278
         */
279
        public void process(int x, int y) {
280
        }
281

    
282
        /*
283
         * (non-Javadoc)
284
         * @see org.gvsig.raster.grid.filter.RasterFilter#getNames()
285
         */
286
        public String[] getNames() {
287
                return names;
288
        }
289

    
290
        /*
291
         * (non-Javadoc)
292
         * @see org.gvsig.raster.grid.filter.RasterFilter#isVisible()
293
         */
294
        public boolean isVisible() {
295
                return false;
296
        }
297
}