Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / statistics / TailTrimFilter.java @ 11767

History | View | Annotate | Download (8.63 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.IStatistics;
25
import org.gvsig.raster.dataset.Params;
26
import org.gvsig.raster.grid.filter.RasterFilter;
27

    
28

    
29
/**
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.
38
 * @author Nacho Brodin (nachobrodin@gmail.com)
39
 */
40
public class TailTrimFilter extends RasterFilter {
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

    
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
            super();
65
            super.fName = genericName;
66
    }
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"));
80

    
81
        if (exec) {
82
            count = 0;
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
    }
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
                     }
117

    
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
             }
127

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

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

    
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
        }
151

    
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
        }
179

    
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
    }
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.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.IRasterFilter#setParams(org.gvsig.raster.dataset.Params)
237
           */
238
          public void setUIParams(Params value) {
239
          }
240

    
241
          /*
242
           * (non-Javadoc)
243
           * @see org.gvsig.raster.grid.filter.RasterFilter#getInRasterDataType()
244
           */
245
                public int getInRasterDataType() {
246
                        return 0;
247
                }
248

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

    
257
                /*
258
                 * (non-Javadoc)
259
                 * @see org.gvsig.raster.grid.filter.RasterFilter#getResult(java.lang.String)
260
                 */
261
                public Object getResult(String name) {
262
                        return null;
263
                }
264

    
265
                /*
266
                 * (non-Javadoc)
267
                 * @see org.gvsig.raster.grid.filter.RasterFilter#getUIDefaultParams()
268
                 */
269
                public Params getUIDefaultParams() {
270
                        Params params = new Params();
271
                        return params;
272
                }
273

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