Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1011 / libraries / libCq_CMS_praster / src / org / cresques / io / datastruct / Statistic.java @ 12904

History | View | Annotate | Download (7.08 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.io.datastruct;
25

    
26
import java.util.ArrayList;
27

    
28

    
29
/**
30
 * Clase para estadisticas
31
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
32
 */
33
public class Statistic {
34
    /**
35
     * M?nimo valor de pixel por banda
36
     */
37
    public double[] minBandValue = {
38
                                    Double.MAX_VALUE, Double.MAX_VALUE,
39
                                    Double.MAX_VALUE
40
                                };
41

    
42
    /**
43
     * M?ximo valor de pixel por banda
44
     */
45
    public double[] maxBandValue = {
46
                                    Double.MIN_VALUE, Double.MIN_VALUE,
47
                                    Double.MIN_VALUE
48
                                };
49

    
50
    /**
51
     * Segundo valor m?s peque?o de pixel por banda
52
     */
53
    public double[] secondMinBandValue = {
54
                                          Double.MAX_VALUE, Double.MAX_VALUE,
55
                                          Double.MAX_VALUE
56
                                      };
57

    
58
    /**
59
     * Segundo valor m?s grande de pixel por banda
60
     */
61
    public double[] secondMaxBandValue = {
62
                                          Double.MIN_VALUE, Double.MIN_VALUE,
63
                                          Double.MIN_VALUE
64
                                      };
65

    
66
    /**
67
     * Historial de valores para una imagen. Esto se usa cuando se solicita varias
68
     * veces para una misma imagen solo tenga que calcular los valores una vez
69
     */
70
    public ArrayList history = new ArrayList();
71

    
72
    /**
73
     * Porcentaje de recorte de colas
74
     */
75
    public double tailPercent = 0D;
76

    
77
    /**
78
     * Constructor
79
     */
80
    public Statistic() {
81
        super();
82
    }
83
    
84
    /**
85
     * Calculo de estad?sticas a partir de un histograma. El resultado de la funci?n es un array
86
     * bidimensional donde el primer ?ndice inndica la estadistica y el segundo el n?mero de banda.
87
     * 
88
     * <UL>
89
     * <LI>m?nimo</LI>
90
     * <LI>m?ximo</LI>
91
     * <LI>media</LI>
92
     * <LI>mediana</LI>
93
     * <LI>N?mero de pixels</LI>
94
     * </UL>
95
     * @param histogram
96
     * @param bandas solicitadas. Cada elemento del vector representa una banda. Si est? a true se calcula la 
97
     * estadistica para esa banda y si est? a false no se calcular?.
98
     * @return
99
     */
100
    public static long[][] getBasicStatsFromHistogram(int[][] histogram, boolean[] bands){
101
            if(histogram == null)
102
                    return null;
103
            return getBasicStatsFromHistogram(histogram, 0, histogram[0].length - 1, bands);
104
    }
105
    /**
106
     * Calculo de estad?sticas a partir de un histograma. El resultado de la funci?n es un array
107
     * bidimensional donde el primer ?ndice inndica la estadistica y el segundo el n?mero de banda.
108
     * 
109
     * <UL>
110
     * <LI>m?nimo</LI>
111
     * <LI>m?ximo</LI>
112
     * <LI>media</LI>
113
     * <LI>mediana</LI>
114
     * <LI>N?mero de pixels</LI>
115
     * </UL>
116
     * @param histogram
117
     * @param beginPos Posici?n de inicio del histograma para contabilizar estadisticas
118
     * @param endPos Posici?n de fin del histograma para contabilizar estadisticas
119
     * @param bandas solicitadas. Cada elemento del vector representa una banda. Si est? a true se calcula la 
120
     * estadistica para esa banda y si est? a false no se calcular?.
121
     * @return
122
     */
123
    public static long[][] getBasicStatsFromHistogram(int[][] histogram, int beginPos, int endPos, boolean[] bands){
124
            if(histogram == null)
125
                    return null;
126
            
127
            //Contamos el n?mero de bandas para las cuales se calcula la estad?stica
128
            int bandCount = 0;
129
            for(int iBand = 0; iBand < bands.length; iBand ++)
130
                    if(bands[iBand])
131
                            bandCount ++;
132
            
133
            int values = 5;
134
            long[][] res = new long[values][];
135
            
136
            long[] min = new long[bandCount];//M?nimo
137
            long[] max = new long[bandCount];//M?ximo
138
            for(int iBand = 0; iBand < bandCount; iBand ++){
139
                    max[iBand] = beginPos;
140
                    min[iBand] = endPos;
141
            }
142
            long[] average = new long[bandCount]; //Valor de pixel medio (Media)
143
            long[] middle = new long[bandCount]; //Mediana
144
            long[] nPixelsBand = new long[bandCount];//N?mero de pixels por banda
145
                                                
146
            int showBandCounter = 0;  //Contador de bandas de las que hay calcular la estadistica
147
            for(int iBand = 0; iBand < histogram.length; iBand ++){
148
                    if(bands[iBand]){
149
                            int pixels = 0; //N?mero de valores por banda (entre 0 y 255)
150
                                for(int i = beginPos; i <= endPos; i ++){
151
                                        
152
                                        //Calculo del m?nimo
153
                                        if(histogram[iBand][i] != 0 && i < min[showBandCounter])
154
                                                min[showBandCounter] = i;
155
                                        
156
                                        //Calculo del m?ximo                                                                                
157
                                        if(histogram[iBand][i] != 0 && i > max[showBandCounter])
158
                                                max[showBandCounter] = i;
159
                                        
160
                                        //Calculo del n?mero de pixeles
161
                                        nPixelsBand[showBandCounter] += (long)histogram[iBand][i];
162
                                        
163
                                        if(histogram[iBand][i] != 0)
164
                                                pixels ++;
165
                                        
166
                                        average[showBandCounter] += histogram[iBand][i] * i;
167
                                }
168
                                //Calculo de la media
169
                                try{
170
                                        average[showBandCounter] /= nPixelsBand[showBandCounter];
171
                                }catch(ArithmeticException exc){
172
                                        average[showBandCounter] = 0;
173
                                }
174
                                
175
                                //Calculo de mediana
176
                                long middlePos = nPixelsBand[showBandCounter] >> 1;
177
                                int aux = 0;
178
                                int i = beginPos;
179
                                for(i = beginPos; aux < middlePos; i++)
180
                                        aux += histogram[iBand][i];
181
                                middle[showBandCounter] = i - 1;
182
                                
183
                                showBandCounter ++;
184
                    }
185
            }
186
     
187
            res[0] = min;
188
            res[1] = max;
189
            res[2] = average;
190
            res[3] = middle;
191
            res[4] = nPixelsBand;
192
            return res;        
193
    }
194
    
195
    /**
196
     * Clase que contiene los valores calculados para una imagen.
197
     * Esto se usa cuando se solicita varias veces para una misma imagen solo
198
     * tenga que calcular los valores una vez.
199
     * @author Nacho Brodin (brodin_ign@gva.es)
200
     */
201
    public class History {
202
        public String file = null;
203
        public double[] min = null;
204
        public double[] max = null;
205
        public double[] secMin = null;
206
        public double[] secMax = null;
207

    
208
        public History(String file, double[] min, double[] max, double[] secMin,
209
                       double[] secMax) {
210
            this.file = file;
211
            this.min = (double[]) min.clone();
212
            this.max = (double[]) max.clone();
213
            this.secMin = (double[]) secMin.clone();
214
            this.secMax = (double[]) secMax.clone();
215
        }
216
    }
217
}