Statistics
| Revision:

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

History | View | Annotate | Download (7.17 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.i18n.Messages;
24
import org.gvsig.raster.dataset.IBuffer;
25
import org.gvsig.raster.dataset.IStatistics;
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 abstract 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
            this.fPriority = 20;
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
        percentSamples = ((Double) params.get("samples")).doubleValue();
79
        removeMaxValue = ((Boolean) params.get("remove")).booleanValue();
80
        stats = ((IStatistics) params.get("stats"));
81

    
82
        if (exec) {
83
            count = 0;
84

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

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

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

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

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

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

    
153
        for (int i = sample[0].length - 1; i > 0; i--) {
154
                  for(int iBand = 0; iBand < raster.getBandCount(); iBand ++){
155
                          if (sample[0][i] != sample[0][sample[0].length - 1]){
156
                                  posFin = i;
157
                                  break;
158
                          }
159
                  }
160
                  if(posFin != sample[0].length - 1)
161
                          break;                  
162
        }
163
    }
164
    
165
    /**
166
     * Calcula la posici?n de inicio y final donde el m?ximo y el m?nimo ya no est?n
167
     *  para valores decimal.
168
     */
169
    private void calcPosInitEndDec(){
170
                    for (int i = 0; i < sampleDec[0].length; i++) {
171
                 for(int iBand = 0; iBand < raster.getBandCount(); iBand ++){
172
                         if (sampleDec[iBand][i] != sampleDec[iBand][0]){
173
                                 posInit = i;
174
                                 break;
175
                         }
176
                 }
177
                 if(posInit != 0)
178
                         break;
179
        }
180

    
181
        for (int i = sampleDec[0].length - 1; i > 0; i--) {
182
                 for(int iBand = 0; iBand < raster.getBandCount(); iBand ++){
183
                         if (sampleDec[0][i] != sampleDec[0][sampleDec[0].length - 1]){
184
                                 posFin = i;
185
                                 break;
186
                         }
187
                 }
188
                 if(posFin != sampleDec[0].length - 1)
189
                         break;                  
190
        }
191
   }
192
    
193
    /**
194
     * Obtiene el porcentaje de recorte
195
     * @return porcentaje de recorte
196
     */
197
    public double getTailPercent() {
198
        return tailPercent;
199
    }
200

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