root / trunk / libraries / libRaster / src / org / gvsig / raster / util / Histogram.java @ 11068
History | View | Annotate | Download (8.84 KB)
1 | 10939 | nacho | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007 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.util; |
||
20 | |||
21 | /**
|
||
22 | * Representa un histograma.
|
||
23 | * @version 27/03/2007
|
||
24 | * @author Nacho Brodin (nachobrodin@gmail.com)
|
||
25 | *
|
||
26 | */
|
||
27 | public class Histogram { |
||
28 | private long[][] histogram = null; |
||
29 | private static String[] types = {"normal", "accumulated", "logaritmic"}; |
||
30 | |||
31 | /**
|
||
32 | * Constructor
|
||
33 | */
|
||
34 | public Histogram() {
|
||
35 | histogram = new long[0][0]; |
||
36 | } |
||
37 | |||
38 | /**
|
||
39 | * Constructor. Inicializa valores a cero
|
||
40 | * @param bands N?mero de bandas
|
||
41 | * @param pxs N?mero de valores o clases
|
||
42 | */
|
||
43 | public Histogram(int bands, int pxs) { |
||
44 | histogram = new long[bands][pxs]; |
||
45 | } |
||
46 | |||
47 | /**
|
||
48 | * Obtiene el histograma sin modificar
|
||
49 | * @return array bidimensional donde el primer elemento es el valor del pixel
|
||
50 | * o rango y el segundo el n?mero de elementos que aparecen.
|
||
51 | */
|
||
52 | public long[][] getHistogram() { |
||
53 | return histogram;
|
||
54 | } |
||
55 | |||
56 | /**
|
||
57 | * Obtiene el n?mero de bandas del histograma
|
||
58 | * @return entero que representa el n?mero de bandas
|
||
59 | */
|
||
60 | public int getNumBands() { |
||
61 | if(histogram != null) |
||
62 | return histogram.length;
|
||
63 | return 0; |
||
64 | } |
||
65 | |||
66 | /**
|
||
67 | * Obtiene la longitud (n?mero de valores) de una banda determinada
|
||
68 | * @param band Banda o obtener la longitud
|
||
69 | * @return entero con la longitud de la banda
|
||
70 | */
|
||
71 | public int getBandLenght(int band) { |
||
72 | if(histogram != null) |
||
73 | return histogram[band].length;
|
||
74 | return 0; |
||
75 | } |
||
76 | |||
77 | /**
|
||
78 | * Obtiene el n?mero de valores o clases del histograma
|
||
79 | * @return entero que representa el n?mero de valores o clases del histograma
|
||
80 | */
|
||
81 | public int getNumValues() { |
||
82 | if(histogram != null) |
||
83 | return histogram[0].length; |
||
84 | return 0; |
||
85 | } |
||
86 | |||
87 | /**
|
||
88 | * Asigna un histograma
|
||
89 | * @param hist histograma asignado
|
||
90 | */
|
||
91 | public void setHistogram(long[][] hist){ |
||
92 | histogram = hist; |
||
93 | } |
||
94 | |||
95 | /**
|
||
96 | * ASigna un valor para una posici?n del histograma
|
||
97 | * @param bands Valor del pixel o clase a asignar
|
||
98 | * @param px Valor del pixel
|
||
99 | * @param value Valor a asignar
|
||
100 | */
|
||
101 | public void setHistogramValue(int band, int px, long value) { |
||
102 | if(histogram != null) |
||
103 | histogram[band][px] = value; |
||
104 | } |
||
105 | |||
106 | /**
|
||
107 | * Obtiene un valor del histograma
|
||
108 | * @param band N?mero de banda del valor a recuperar
|
||
109 | * @param px Pixel o valor de la clase del valor a recuperar
|
||
110 | * @return valor
|
||
111 | */
|
||
112 | public long getHistogramValue(int band, int px) { |
||
113 | return histogram[band][px];
|
||
114 | } |
||
115 | |||
116 | /**
|
||
117 | * Incrementa un valor de una posici?n del histograma
|
||
118 | * @param band N?mero de banda
|
||
119 | * @param px Pixel o valor de la clase
|
||
120 | */
|
||
121 | public void incrementPxValue(int band, int px) { |
||
122 | histogram[band][px] ++; |
||
123 | } |
||
124 | |||
125 | /**
|
||
126 | * Devuelve el histograma acumulado
|
||
127 | * @return
|
||
128 | */
|
||
129 | 10996 | bsanchez | |
130 | 10939 | nacho | public long[][] getAccumulatedHistogram() { |
131 | 10996 | bsanchez | if (histogram != null){ |
132 | 10939 | nacho | long[][] hist = new long[histogram.length][histogram[0].length]; |
133 | for (int iBand = 0; iBand < hist.length; iBand++) { |
||
134 | 10996 | bsanchez | hist[iBand][0] = histogram[iBand][0]; |
135 | for (int j = 1; j < hist[iBand].length; j++) { |
||
136 | hist[iBand][j] = hist[iBand][j - 1] + histogram[iBand][j];
|
||
137 | 10939 | nacho | } |
138 | } |
||
139 | 10996 | bsanchez | return hist;
|
140 | 10939 | nacho | } |
141 | return null; |
||
142 | } |
||
143 | |||
144 | /**
|
||
145 | 11033 | bsanchez | * Devuelve el histograma logaritmico
|
146 | 10939 | nacho | * @return
|
147 | */
|
||
148 | public long[][] getLogaritmicHistogram() { |
||
149 | 11033 | bsanchez | if (histogram != null) { |
150 | long[][] hist = new long[histogram.length][histogram[0].length]; |
||
151 | 11065 | bsanchez | for (int iBand = 0; iBand < histogram.length; iBand++) { |
152 | long min = histogram[iBand][0]; |
||
153 | for (int j = 1; j < histogram[iBand].length; j++) |
||
154 | if (min > histogram[iBand][j]) min = histogram[iBand][j];
|
||
155 | for (int j = 0; j < histogram[iBand].length; j++) |
||
156 | 11033 | bsanchez | // Lo multiplico por 1000 para que no se pierdan datos al redondear
|
157 | hist[iBand][j] = (long) (java.lang.Math.log((double) (histogram[iBand][j] - min + 1))*1000); |
||
158 | } |
||
159 | return hist;
|
||
160 | } |
||
161 | return null; |
||
162 | 10939 | nacho | } |
163 | |||
164 | /**
|
||
165 | * N?mero de tipos de histograma definidos en esta clase.
|
||
166 | * @return entero con el n?mero de tipos definidos.
|
||
167 | */
|
||
168 | public static int getHistogramTypesCount(){ |
||
169 | return types.length;
|
||
170 | } |
||
171 | |||
172 | /**
|
||
173 | * Obtiene un tipo de histograma a partir de su posici?n en el array
|
||
174 | * @param pos posici?n en el array del tipo a obtener
|
||
175 | * @return Tipo
|
||
176 | */
|
||
177 | public static String getType(int pos){ |
||
178 | return types[pos];
|
||
179 | } |
||
180 | |||
181 | 10943 | nacho | /**
|
182 | * Obtiene el histograma correspondiente al tipo pasado por par?metro. Los tipos
|
||
183 | * est?n definidos en esta misma clase de forma est?tica en la variable types.
|
||
184 | * @param type Tipo a devolver
|
||
185 | * @return Histograma
|
||
186 | */
|
||
187 | public long[][] getHistogramByType(String type) { |
||
188 | if(type.equals(types[0])) |
||
189 | return getHistogram();
|
||
190 | if(type.equals(types[1])) |
||
191 | return getAccumulatedHistogram();
|
||
192 | if(type.equals(types[2])) |
||
193 | return getLogaritmicHistogram();
|
||
194 | return null; |
||
195 | } |
||
196 | 10973 | nacho | |
197 | /**
|
||
198 | * Calculo de estad?sticas a partir de un histograma. El resultado de la funci?n es un array
|
||
199 | * bidimensional donde el primer ?ndice inndica la estadistica y el segundo el n?mero de banda.
|
||
200 | *
|
||
201 | * <UL>
|
||
202 | * <LI>m?nimo</LI>
|
||
203 | * <LI>m?ximo</LI>
|
||
204 | * <LI>media</LI>
|
||
205 | * <LI>mediana</LI>
|
||
206 | * <LI>N?mero de pixels</LI>
|
||
207 | * </UL>
|
||
208 | * @param histogram
|
||
209 | * @param bandas solicitadas. Cada elemento del vector representa una banda. Si est? a true se calcula la
|
||
210 | * estadistica para esa banda y si est? a false no se calcular?.
|
||
211 | * @return
|
||
212 | */
|
||
213 | 10980 | bsanchez | public long[][] getBasicStats(boolean[] bands){ |
214 | 10973 | nacho | if(histogram == null) |
215 | return null; |
||
216 | 10980 | bsanchez | return getBasicStats(0, histogram[0].length - 1, bands); |
217 | 10973 | nacho | } |
218 | /**
|
||
219 | * Calculo de estad?sticas a partir de un histograma. El resultado de la funci?n es un array
|
||
220 | * bidimensional donde el primer ?ndice inndica la estadistica y el segundo el n?mero de banda.
|
||
221 | *
|
||
222 | * <UL>
|
||
223 | * <LI>m?nimo</LI>
|
||
224 | * <LI>m?ximo</LI>
|
||
225 | * <LI>media</LI>
|
||
226 | * <LI>mediana</LI>
|
||
227 | * <LI>N?mero de pixels</LI>
|
||
228 | * </UL>
|
||
229 | * @param histogram
|
||
230 | * @param beginPos Posici?n de inicio del histograma para contabilizar estadisticas
|
||
231 | * @param endPos Posici?n de fin del histograma para contabilizar estadisticas
|
||
232 | * @param bandas solicitadas. Cada elemento del vector representa una banda. Si est? a true se calcula la
|
||
233 | * estadistica para esa banda y si est? a false no se calcular?.
|
||
234 | * @return
|
||
235 | */
|
||
236 | 10980 | bsanchez | public long[][] getBasicStats(int beginPos, int endPos, boolean[] bands){ |
237 | 10973 | nacho | if(histogram == null) |
238 | return null; |
||
239 | |||
240 | //Contamos el n?mero de bandas para las cuales se calcula la estad?stica
|
||
241 | int bandCount = 0; |
||
242 | for(int iBand = 0; iBand < bands.length; iBand ++) |
||
243 | if(bands[iBand])
|
||
244 | bandCount ++; |
||
245 | |||
246 | int values = 5; |
||
247 | long[][] res = new long[values][]; |
||
248 | |||
249 | long[] min = new long[bandCount];//M?nimo |
||
250 | long[] max = new long[bandCount];//M?ximo |
||
251 | for(int iBand = 0; iBand < bandCount; iBand ++){ |
||
252 | max[iBand] = beginPos; |
||
253 | min[iBand] = endPos; |
||
254 | } |
||
255 | long[] average = new long[bandCount]; //Valor de pixel medio (Media) |
||
256 | long[] middle = new long[bandCount]; //Mediana |
||
257 | long[] nPixelsBand = new long[bandCount];//N?mero de pixels por banda |
||
258 | |||
259 | int showBandCounter = 0; //Contador de bandas de las que hay calcular la estadistica |
||
260 | for(int iBand = 0; iBand < histogram.length; iBand ++){ |
||
261 | if(bands[iBand]){
|
||
262 | int pixels = 0; //N?mero de valores por banda (entre 0 y 255) |
||
263 | for(int i = beginPos; i <= endPos; i ++){ |
||
264 | |||
265 | //Calculo del m?nimo
|
||
266 | if(histogram[iBand][i] != 0 && i < min[showBandCounter]) |
||
267 | min[showBandCounter] = i; |
||
268 | |||
269 | //Calculo del m?ximo
|
||
270 | if(histogram[iBand][i] != 0 && i > max[showBandCounter]) |
||
271 | max[showBandCounter] = i; |
||
272 | |||
273 | //Calculo del n?mero de pixeles
|
||
274 | nPixelsBand[showBandCounter] += (long)histogram[iBand][i];
|
||
275 | |||
276 | if(histogram[iBand][i] != 0) |
||
277 | pixels ++; |
||
278 | |||
279 | average[showBandCounter] += histogram[iBand][i] * i; |
||
280 | } |
||
281 | //Calculo de la media
|
||
282 | try{
|
||
283 | average[showBandCounter] /= nPixelsBand[showBandCounter]; |
||
284 | }catch(ArithmeticException exc){ |
||
285 | average[showBandCounter] = 0;
|
||
286 | } |
||
287 | |||
288 | //Calculo de mediana
|
||
289 | long middlePos = nPixelsBand[showBandCounter] >> 1; |
||
290 | int aux = 0; |
||
291 | int i = beginPos;
|
||
292 | for(i = beginPos; aux < middlePos; i++)
|
||
293 | aux += histogram[iBand][i]; |
||
294 | middle[showBandCounter] = i - 1;
|
||
295 | |||
296 | showBandCounter ++; |
||
297 | } |
||
298 | } |
||
299 | |||
300 | res[0] = min;
|
||
301 | res[1] = max;
|
||
302 | res[2] = average;
|
||
303 | res[3] = middle;
|
||
304 | res[4] = nPixelsBand;
|
||
305 | return res;
|
||
306 | } |
||
307 | 10939 | nacho | } |