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

View differences:

Histogram.java
22 22
 * Representa un histograma.
23 23
 * @version 27/03/2007
24 24
 * @author Nacho Brodin (nachobrodin@gmail.com)
25
 *
26 25
 */
27 26
public class Histogram {
28 27
	private long[][] histogram = null;
......
126 125
	 * Devuelve el histograma acumulado
127 126
	 * @return
128 127
	 */
129

  
130 128
	public long[][] getAccumulatedHistogram() {
131 129
		if (histogram != null){
132 130
			long[][] hist = new long[histogram.length][histogram[0].length];
......
148 146
	public long[][] getLogaritmicHistogram() {
149 147
		if (histogram != null) {
150 148
			long[][] hist = new long[histogram.length][histogram[0].length];
151
			for (int iBand = 0; iBand < histogram.length; iBand++) {
152
				long min = histogram[iBand][0];
149
			long min = histogram[0][0];
150
			for (int iBand = 0; iBand < histogram.length; iBand++)
153 151
				for (int j = 1; j < histogram[iBand].length; j++)
154 152
					if (min > histogram[iBand][j]) min = histogram[iBand][j];
153
			for (int iBand = 0; iBand < histogram.length; iBand++) {
155 154
				for (int j = 0; j < histogram[iBand].length; j++)
156 155
					// Lo multiplico por 1000 para que no se pierdan datos al redondear
157 156
					hist[iBand][j] = (long) (java.lang.Math.log((double) (histogram[iBand][j] - min + 1))*1000);
......
165 164
	 * N?mero de tipos de histograma definidos en esta clase.
166 165
	 * @return entero con el n?mero de tipos definidos.
167 166
	 */
168
	public static int getHistogramTypesCount(){
167
	public static int getHistogramTypesCount() {
169 168
		return types.length;
170 169
	}
171 170
	
......
174 173
	 * @param pos posici?n en el array del tipo a obtener
175 174
	 * @return Tipo
176 175
	 */
177
	public static String getType(int pos){
176
	public static String getType(int pos) {
178 177
		return types[pos];
179 178
	}
180 179
	
......
194 193
		return null;
195 194
	}
196 195
	
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
	  public long[][] getBasicStats(boolean[] bands){
214
	  	if(histogram == null)
215
	  		return null;
216
	  	return getBasicStats(0, histogram[0].length - 1, bands);
217
	  }
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
	  public long[][] getBasicStats(int beginPos, int endPos, boolean[] bands){
237
	  	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
				}
196
  /**
197
   * Calculo de estad?sticas a partir de un histograma. El resultado de la funci?n es un array
198
   * bidimensional donde el primer ?ndice inndica la estadistica y el segundo el n?mero de banda.
199
   * 
200
   * <UL>
201
   * <LI>m?nimo</LI>
202
   * <LI>m?ximo</LI>
203
   * <LI>media</LI>
204
   * <LI>mediana</LI>
205
   * <LI>N?mero de pixels</LI>
206
   * </UL>
207
   * @param histogram
208
   * @param bandas solicitadas. Cada elemento del vector representa una banda. Si est? a true se calcula la 
209
   * estadistica para esa banda y si est? a false no se calcular?.
210
   * @return
211
   */
212
  public long[][] getBasicStats(boolean[] bands) {
213
  	if(histogram == null)
214
  		return null;
215
  	return getBasicStats(0, histogram[0].length - 1, bands);
216
  }
217

  
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
  public long[][] getBasicStats(int beginPos, int endPos, boolean[] bands){
237
  	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 ++){
287 264
				
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;
265
				//Calculo del m?nimo
266
				if(histogram[iBand][i] != 0 && i < min[showBandCounter])
267
					min[showBandCounter] = i;
295 268
				
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
}
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
}

Also available in: Unified diff