Revision 10973 trunk/libraries/libRaster/src/org/gvsig/raster/util/Histogram.java

View differences:

Histogram.java
182 182
			return getLogaritmicHistogram();
183 183
		return null;
184 184
	}
185
	
186
	  /**
187
	   * Calculo de estad?sticas a partir de un histograma. El resultado de la funci?n es un array
188
	   * bidimensional donde el primer ?ndice inndica la estadistica y el segundo el n?mero de banda.
189
	   * 
190
	   * <UL>
191
	   * <LI>m?nimo</LI>
192
	   * <LI>m?ximo</LI>
193
	   * <LI>media</LI>
194
	   * <LI>mediana</LI>
195
	   * <LI>N?mero de pixels</LI>
196
	   * </UL>
197
	   * @param histogram
198
	   * @param bandas solicitadas. Cada elemento del vector representa una banda. Si est? a true se calcula la 
199
	   * estadistica para esa banda y si est? a false no se calcular?.
200
	   * @return
201
	   */
202
	  public long[][] getBasicStatsFromHistogram(boolean[] bands){
203
	  	if(histogram == null)
204
	  		return null;
205
	  	return getBasicStatsFromHistogram(0, histogram[0].length - 1, bands);
206
	  }
207
	  /**
208
	   * Calculo de estad?sticas a partir de un histograma. El resultado de la funci?n es un array
209
	   * bidimensional donde el primer ?ndice inndica la estadistica y el segundo el n?mero de banda.
210
	   * 
211
	   * <UL>
212
	   * <LI>m?nimo</LI>
213
	   * <LI>m?ximo</LI>
214
	   * <LI>media</LI>
215
	   * <LI>mediana</LI>
216
	   * <LI>N?mero de pixels</LI>
217
	   * </UL>
218
	   * @param histogram
219
	   * @param beginPos Posici?n de inicio del histograma para contabilizar estadisticas
220
	   * @param endPos Posici?n de fin del histograma para contabilizar estadisticas
221
	   * @param bandas solicitadas. Cada elemento del vector representa una banda. Si est? a true se calcula la 
222
	   * estadistica para esa banda y si est? a false no se calcular?.
223
	   * @return
224
	   */
225
	  public long[][] getBasicStatsFromHistogram(int beginPos, int endPos, boolean[] bands){
226
	  	if(histogram == null)
227
	  		return null;
228
	  	
229
	  	//Contamos el n?mero de bandas para las cuales se calcula la estad?stica
230
	  	int bandCount = 0;
231
	  	for(int iBand = 0; iBand < bands.length; iBand ++)
232
	  		if(bands[iBand])
233
	  			bandCount ++;
234
	  	
235
	  	int values = 5;
236
	  	long[][] res = new long[values][];
237
	  	
238
	  	long[] min = new long[bandCount];//M?nimo
239
	  	long[] max = new long[bandCount];//M?ximo
240
	  	for(int iBand = 0; iBand < bandCount; iBand ++){
241
	  		max[iBand] = beginPos;
242
	  		min[iBand] = endPos;
243
	  	}
244
	  	long[] average = new long[bandCount]; //Valor de pixel medio (Media)
245
	  	long[] middle = new long[bandCount]; //Mediana
246
	  	long[] nPixelsBand = new long[bandCount];//N?mero de pixels por banda
247
	  	    	    	    	
248
	  	int showBandCounter = 0;  //Contador de bandas de las que hay calcular la estadistica
249
	  	for(int iBand = 0; iBand < histogram.length; iBand ++){
250
	  		if(bands[iBand]){
251
	    		int pixels = 0; //N?mero de valores por banda (entre 0 y 255)
252
				for(int i = beginPos; i <= endPos; i ++){
253
					
254
					//Calculo del m?nimo
255
					if(histogram[iBand][i] != 0 && i < min[showBandCounter])
256
						min[showBandCounter] = i;
257
					
258
					//Calculo del m?ximo										
259
					if(histogram[iBand][i] != 0 && i > max[showBandCounter])
260
						max[showBandCounter] = i;
261
					
262
					//Calculo del n?mero de pixeles
263
					nPixelsBand[showBandCounter] += (long)histogram[iBand][i];
264
					
265
					if(histogram[iBand][i] != 0)
266
						pixels ++;
267
					
268
					average[showBandCounter] += histogram[iBand][i] * i;
269
				}
270
				//Calculo de la media
271
				try{
272
					average[showBandCounter] /= nPixelsBand[showBandCounter];
273
				}catch(ArithmeticException exc){
274
					average[showBandCounter] = 0;
275
				}
276
				
277
				//Calculo de mediana
278
				long middlePos = nPixelsBand[showBandCounter] >> 1;
279
				int aux = 0;
280
				int i = beginPos;
281
				for(i = beginPos; aux < middlePos; i++)
282
					aux += histogram[iBand][i];
283
				middle[showBandCounter] = i - 1;
284
				
285
				showBandCounter ++;
286
	  		}
287
	  	}
288
	   
289
	  	res[0] = min;
290
	  	res[1] = max;
291
	  	res[2] = average;
292
	  	res[3] = middle;
293
	  	res[4] = nPixelsBand;
294
	  	return res;	
295
	  }
185 296
}

Also available in: Unified diff