Revision 11076 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
 *
25 26
 */
26 27
public class Histogram {
27 28
	private long[][] histogram = null;
......
126 127
	 * @return
127 128
	 */
128 129
	public long[][] getAccumulatedHistogram() {
129
		if (histogram != null){
130
		if(histogram != null){
130 131
			long[][] hist = new long[histogram.length][histogram[0].length];
131 132
			for (int iBand = 0; iBand < hist.length; iBand++) {
132
				hist[iBand][0] = histogram[iBand][0];
133
				for (int j = 1; j < hist[iBand].length; j++) {
134
					hist[iBand][j] = hist[iBand][j - 1] + histogram[iBand][j];
133
				for (int j = 0; j < hist[iBand].length; j++) {
134
					if(j == 0)
135
						hist[iBand][j] = histogram[iBand][j];
136
					else
137
						hist[iBand][j] += histogram[iBand][j - 1]; 	
135 138
				}
136 139
			}
137
			return hist;
138 140
		}
139 141
		return null;
140 142
	}
141 143
	
142 144
	/**
143
	 * Devuelve el histograma logaritmico
145
	 * Devuelve el histograma acumulado
144 146
	 * @return
145 147
	 */
146 148
	public long[][] getLogaritmicHistogram() {
147
		if (histogram != null) {
148
			long[][] hist = new long[histogram.length][histogram[0].length];
149
			long min = histogram[0][0];
150
			for (int iBand = 0; iBand < histogram.length; iBand++)
151
				for (int j = 1; j < histogram[iBand].length; j++)
152
					if (min > histogram[iBand][j]) min = histogram[iBand][j];
153
			for (int iBand = 0; iBand < histogram.length; iBand++) {
154
				for (int j = 0; j < histogram[iBand].length; j++)
155
					// Lo multiplico por 1000 para que no se pierdan datos al redondear
156
					hist[iBand][j] = (long) (1000.0 * java.lang.Math.log((double) (histogram[iBand][j] - min + 1)));
157
			}
158
			return hist;
159
		}
160
		return null;
149
		//TODO: FUNCIONALIDAD: Falta el calculo del histograma logaritmico
150
		return histogram;
161 151
	}
162 152
	
163 153
	/**
164 154
	 * N?mero de tipos de histograma definidos en esta clase.
165 155
	 * @return entero con el n?mero de tipos definidos.
166 156
	 */
167
	public static int getHistogramTypesCount() {
157
	public static int getHistogramTypesCount(){
168 158
		return types.length;
169 159
	}
170 160
	
......
173 163
	 * @param pos posici?n en el array del tipo a obtener
174 164
	 * @return Tipo
175 165
	 */
176
	public static String getType(int pos) {
166
	public static String getType(int pos){
177 167
		return types[pos];
178 168
	}
179 169
	
......
193 183
		return null;
194 184
	}
195 185
	
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 ++){
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[][] getBasicStats(boolean[] bands){
203
	  	if(histogram == null)
204
	  		return null;
205
	  	return getBasicStats(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[][] getBasicStats(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
				}
264 276
				
265
				//Calculo del m?nimo
266
				if(histogram[iBand][i] != 0 && i < min[showBandCounter])
267
					min[showBandCounter] = i;
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;
268 284
				
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
}
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
	  }
296
}

Also available in: Unified diff