Statistics
| Revision:

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

History | View | Annotate | Download (7.14 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.grid.filter.RasterFilter;
26

    
27
/**
28
 * Filtro de recorte de colas. Este filtro toma pixels de la imagen (todos o algunas muestras
29
 * dependiendo de la variable percentSample) y los ordena. Recorta un porcentaje controlado
30
 * por tailPercenten ambos extremos del vector ordenado. El nuevo m?ximo y m?nimo coinciden
31
 * con el valor de la posici?n del vector recortado. Por arriba para el m?ximo y por abajo
32
 * para el m?nimo.
33
 * El execute de este filtro no recorre toda la imagen sino que lo
34
 * hace en funci?n del porcentaje de muestras que quieren tomarse y
35
 * calculando a partir de este porcentaje un incremento.
36
 * @author Nacho Brodin (nachobrodin@gmail.com)
37
 */
38
public abstract class TailTrimFilter extends RasterFilter {
39
        public static String                genericName = "tailTrim";
40
        
41
    protected int                         count = 0;
42
    protected int                         tailSize = 0;
43
    protected int                         nSamples = 0;
44
    protected boolean                 removeMaxValue = false;
45
    protected int                         incX , incY;
46

    
47
    //Par?metros del filtro
48
    protected double                 tailPercent = 0D;
49
    public double                         percentSamples = 0D;
50
    
51
    protected int[][]                 sample = null;
52
    protected double[][]         sampleDec = null;
53
    protected IStatistics        stats = null;
54
        /**
55
         * Array con el resultado. La primera dimensi?n es el n?mero de bandas y la segunda son dos elementos 
56
         * el m?ximo y el m?nimo para esa banda.
57
         */
58
        protected double[][]        result = null;
59

    
60

    
61
    public TailTrimFilter() {
62
            super();
63
            super.fName = genericName;
64
            this.fPriority = 20;
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 porcentaje
100
     * 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 est?n 
137
     * 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 est?n
165
     *  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
}