Statistics
| Revision:

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 / statistics / TailTrimFilter.java @ 2438

History | View | Annotate | Download (8.22 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.statistics;
23

    
24
import java.util.Arrays;
25

    
26
import org.gvsig.fmap.dal.coverage.datastruct.Params;
27
import org.gvsig.fmap.dal.coverage.exception.FilterAddException;
28
import org.gvsig.fmap.dal.coverage.grid.filter.BaseRasterFilter;
29
import org.gvsig.fmap.dal.coverage.store.props.Statistics;
30
import org.gvsig.raster.impl.store.ParamsImpl;
31
/**
32
 * Filtro de recorte de colas. Este filtro toma pixels de la imagen (todos o
33
 * algunas muestras dependiendo de la variable percentSample) y los ordena.
34
 * Recorta un porcentaje controlado por tailPercenten ambos extremos del vector
35
 * ordenado. El nuevo m?ximo y m?nimo coinciden con el valor de la posici?n del
36
 * vector recortado. Por arriba para el m?ximo y por abajo para el m?nimo.
37
 * El execute de este filtro no recorre toda la imagen sino que lo hace en
38
 * funci?n del porcentaje de muestras que quieren tomarse y calculando a partir
39
 * de este porcentaje un incremento.
40
 *
41
 * @author Nacho Brodin (nachobrodin@gmail.com)
42
 */
43
public class TailTrimFilter extends BaseRasterFilter {
44
        public static String[] names             = new String[] {"tailTrim"};
45

    
46
        protected int                         count            = 0;
47
        protected int                         tailSize         = 0;
48
        protected int[]                        tailSizeList     = null;
49
        protected int                         nSamples         = 0;
50
        protected boolean                 removeMaxValue   = false;
51
        protected int                         incX , incY;
52

    
53
        //Par?metros del filtro
54
        protected double                 tailPercent      = 0D;
55
        protected double[]                 tailPercentList  = null;
56
        public double                         percentSamples   = 0D;
57

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

    
67

    
68
        public TailTrimFilter() {
69
                setName(names[0]);
70
        }
71
        
72
        /**
73
         * Calcula el incremento de X y de Y para la toma de muestras en el calculo de
74
         * valores para el recorte
75
         * @throws FilterAddException 
76
         */
77
        public void pre() throws FilterAddException {
78
                super.pre();
79
                if(params.get("tail") != null)
80
                        tailPercent = ((Double) params.get("tail")).doubleValue();
81
                tailPercentList = ((double[]) params.get("tailList"));
82
                if(params.get("samples") != null)
83
                        percentSamples = ((Double) params.get("samples")).doubleValue();
84
                if(params.get("remove") != null)
85
                        removeMaxValue = ((Boolean) params.get("remove")).booleanValue();
86
                stats = ((Statistics) params.get("stats"));
87

    
88
                if(tailPercentList != null)
89
                        tailSizeList = new int[tailPercentList.length];
90
                
91
                if (exec) {
92
                        count = 0;
93

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

    
114
        protected int posInit = 0;
115
        protected int posFin = 0;
116

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

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

    
142
                                if (sampleDec != null)
143
                                        this.calcPosInitEndDec();
144
                        }
145

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

    
149
                        // Como ha podido cambiar nSamples recalculamos tailsize
150
                        tailSize = (int) (nSamples * this.tailPercent);
151
                        if(tailPercentList != null) {
152
                                for (int i = 0; i < tailPercentList.length; i++)
153
                                        tailSizeList[i] = (int) (nSamples * this.tailPercentList[i]);
154
                        }
155
                }
156
        }
157

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

    
174
                for (int i = sample[0].length - 1; i > 0; i--) {
175
                        for (int iBand = 0; iBand < raster.getBandCount(); iBand++) {
176
                                if (sample[0][i] != sample[0][sample[0].length - 1]) {
177
                                        posFin = i;
178
                                        break;
179
                                }
180
                        }
181
                        if (posFin != sample[0].length - 1)
182
                                break;
183
                }
184
        }
185

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

    
202
                for (int i = sampleDec[0].length - 1; i > 0; i--) {
203
                        for (int iBand = 0; iBand < raster.getBandCount(); iBand++) {
204
                                if (sampleDec[0][i] != sampleDec[0][sampleDec[0].length - 1]) {
205
                                        posFin = i;
206
                                        break;
207
                                }
208
                        }
209
                        if (posFin != sampleDec[0].length - 1)
210
                                break;
211
                }
212
        }
213

    
214
        /**
215
         * Obtiene el porcentaje de recorte
216
         * @return porcentaje de recorte
217
         */
218
        public double getTailPercent() {
219
                return tailPercent;
220
        }
221

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

    
239
        public String getGroup() {
240
                return "basics";
241
        }
242

    
243
        public Params getUIParams(String nameFilter) {
244
                Params params = new ParamsImpl();
245
                params.setParam("tail",
246
                                new Double(tailPercent),
247
                                Params.NONE,
248
                                null);
249
                params.setParam("samples",
250
                                new Double(percentSamples),
251
                                Params.NONE,
252
                                null);
253
                params.setParam("remove",
254
                                new Boolean(removeMaxValue),
255
                                Params.NONE,
256
                                null);
257
                if(tailPercentList != null)
258
                        params.setParam("tailList",
259
                                        tailPercentList,
260
                                        Params.NONE,
261
                                        null);
262
                return params;
263
        }
264

    
265
        public int getInRasterDataType() {
266
                return 0;
267
        }
268

    
269
        public int getOutRasterDataType() {
270
                return 0;
271
        }
272

    
273
        public void process(int x, int y) {
274
        }
275

    
276
        public String[] getNames() {
277
                return names;
278
        }
279

    
280
        public boolean isVisible() {
281
                return false;
282
        }
283
}