Statistics
| Revision:

root / trunk / libraries / libCq_CMS_praster / src / org / cresques / filter / statistics / PercentTailTrimFilter.java @ 8026

History | View | Annotate | Download (6.69 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 *
4
 * Copyright (C) 2004-5.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 * cresques@gmail.com
23
 */
24
package org.cresques.filter.statistics;
25

    
26
import java.util.ArrayList;
27
import java.util.Arrays;
28

    
29
import org.cresques.filter.RasterFilter;
30
import org.cresques.io.datastruct.Statistic;
31
import org.cresques.util.QuickSort;
32
import org.gvsig.i18n.Messages;
33

    
34

    
35
/**
36
 * Filtro de recorte de colas. Este filtro toma pixels de la imagen (todos o algunas muestras
37
 * dependiendo de la variable percentSample) y los ordena. Recorta un porcentaje controlado
38
 * por tailPercenten ambos extremos del vector ordenado. El nuevo m?ximo y m?nimo coinciden
39
 * con el valor de la posici?n del vector recortado. Por arriba para el m?ximo y por abajo
40
 * para el m?nimo.
41
 * El execute de este filtro no recorre toda la imagen sino que lo
42
 * hace en funci?n del porcentaje de muestras que quieren tomarse y
43
 * calculando a partir de este porcentaje un incremento.
44
 * @author Nacho Brodin (brodin_ign@gva.es)
45
 */
46
public abstract class PercentTailTrimFilter extends RasterFilter {
47
    protected int[]                 min = {
48
                              Integer.MAX_VALUE, Integer.MAX_VALUE,
49
                              Integer.MAX_VALUE
50
                                  };
51
    protected int[]                 max = {
52
                              Integer.MIN_VALUE, Integer.MIN_VALUE,
53
                              Integer.MIN_VALUE
54
                                  };
55
    protected int                         count = 0;
56
    protected double[][]                 sample;
57
    private int                         tailSize = 0;
58
    private int                         nSamples = 0;
59
    private boolean                 removeMaxValue = false;
60
    protected int                         incX , incY;
61

    
62
    //Par?metros del filtro
63
    protected double                 tailPercent = 0D;
64
    public double                         percentSamples = 0D;
65
    protected ArrayList         fileList = new ArrayList();
66

    
67
    public PercentTailTrimFilter() {
68
            super.filterName = Messages.getText("tail_trim");
69
    }
70

    
71
    /**
72
     * Calcula el incremento de X y de Y para la toma de muestras en el calculo de
73
     * valores para el recorte
74
     */
75
    public void pre() {
76
        //Obtenemos par?metros comunes
77
        this.stats = (Statistic) params.get("stats");
78
        this.tailPercent = ((Double) params.get("tail")).doubleValue();
79
        this.percentSamples = ((Double) params.get("samples")).doubleValue();
80
        this.removeMaxValue = ((Boolean) params.get("remove")).booleanValue();
81

    
82
        if (exec) {
83
            this.stats.tailPercent = this.tailPercent;
84
            count = 0;
85

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

    
101
    /**
102
     * Ordena las muestras , recorta y asigna m?ximo y m?nimo dependiendo del porcentaje
103
     * de recorte
104
     */
105
    public void post() {
106
        if (exec) {
107
            QuickSort q = new QuickSort();
108

    
109
            int posInit = 0;
110
            int posFin = sample[0].length - 1;
111

    
112
            //Ordenamos los vectores
113
            for (int i = 0; i < 3; i++)
114
                //q.quicksort(sample[i]);
115
                    Arrays.sort(sample[i]);
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
                for (int i = 0; i < sample[0].length; i++) {
121
                    if ((sample[0][i] != sample[0][0]) ||
122
                            (sample[1][i] != sample[1][0]) ||
123
                            (sample[2][i] != sample[2][0])) {
124
                        posInit = i;
125

    
126
                        break;
127
                    }
128
                }
129

    
130
                for (int i = sample[0].length - 1; i > 0; i--) {
131
                    if ((sample[0][i] != sample[0][sample[0].length - 1]) ||
132
                            (sample[1][i] != sample[1][sample[0].length - 1]) ||
133
                            (sample[2][i] != sample[2][sample[0].length - 1])) {
134
                        posFin = i;
135

    
136
                        break;
137
                    }
138
                }
139

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

    
143
                //Como ha podido cambiar nSamples recalculamos tailsize
144
                tailSize = (int) (nSamples * this.tailPercent);
145
            }
146

    
147
            //Ordenamos los elementos y cogemos el minimo y m?ximo para cada banda
148
            for (int i = 0; i < 3; i++) {
149
                stats.minBandValue[i] = sample[i][posInit + tailSize];
150
                stats.maxBandValue[i] = sample[i][(posInit + nSamples) -
151
                                        tailSize - 1];
152
            }
153
        }
154
    }
155

    
156
    /**
157
     * Obtiene el objeto stats con el m?ximo y m?nimo calculado
158
     */
159
    public Object getResult(String name) {
160
        if (name.equals("stats")) {
161
            return (Object) this.stats;
162
        } else {
163
            return null;
164
        }
165
    }
166

    
167
    /**
168
     * Obtiene el porcentaje de recorte
169
     * @return porcentaje de recorte
170
     */
171
    public double getTailPercent() {
172
        return tailPercent;
173
    }
174

    
175
    /**
176
     * Devuelve true si se eliminan los extremos de la serie antes del calculo del recorte de colas
177
     * o false si no se eliminan.
178
     * @return
179
     */
180
    public boolean removeMaxValue() {
181
        return this.removeMaxValue;
182
    }
183
}