Revision 24831

View differences:

trunk/libraries/libRaster/src/org/gvsig/raster/grid/filter/enhancement/EcualizationShortFilter.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 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.grid.filter.enhancement;
20

  
21
import org.gvsig.raster.dataset.IBuffer;
22
/**
23
 * Filtro de ecualizaci?n de histograma para tipo de datos short.
24
 *
25
 * @version 11/05/2007
26
 * @author Nacho Brodin (nachobrodin@gmail.com)
27
 */
28
public class EcualizationShortFilter extends EqualizationFilter {
29

  
30
	/*
31
	 * (non-Javadoc)
32
	 * @see org.gvsig.raster.grid.filter.enhancement.LinearEnhancementFilter#process(int, int)
33
	 */
34
	public void process(int col, int line) {
35
		for (int iBand = 0; iBand < raster.getBandCount(); iBand++) {
36
			short p = raster.getElemShort(line, col, iBand);
37
			if(!equalizationActive(iBand)) {
38
				rasterResult.setElem(line, col, iBand, p);
39
				continue;
40
			}
41
			
42
			if (p > maxBandValue[renderBands[iBand]])
43
				p = (short) maxBandValue[renderBands[iBand]];
44
			else if (p < minBandValue[renderBands[iBand]])
45
				p = (short) minBandValue[renderBands[iBand]];
46
		
47
			int ecualizationPositive = (int)(aproximation[renderBands[iBand]][p % nClasses] * nClasses);
48
			int ecualizationNegative = (int)(aproximationNeg[renderBands[iBand]][nElements - (p  % nClasses)] * nClasses);
49
			double value = ((nElements - ecualizationNegative) + ecualizationPositive) / 2;
50
			
51
			rasterResult.setElem(line, col, iBand, (short)value);
52
		}
53
	}
54

  
55
	/*
56
	 * (non-Javadoc)
57
	 * @see org.gvsig.raster.grid.filter.enhancement.LinearEnhancementFilter#getInRasterDataType()
58
	 */
59
	public int getInRasterDataType() {
60
		return IBuffer.TYPE_SHORT;
61
	}
62
	
63
	/*
64
	 * (non-Javadoc)
65
	 * @see org.gvsig.raster.grid.filter.RasterFilter#getOutRasterDataType()
66
	 */
67
	public int getOutRasterDataType() {
68
		return IBuffer.TYPE_SHORT;
69
	}
70
}
trunk/libraries/libRaster/src/org/gvsig/raster/grid/filter/enhancement/EqualizationFilter.java
47 47
	protected int[][]                  resValues          = null;
48 48
	protected int                      nElements          = 0;
49 49
	protected int[]                    ecualizedBands     = null;
50
	
51
	protected double[][]               aproximation       = null;
50

  
52 51
	protected double[][]               aproximationNeg    = null;
53 52
	
54 53
	protected int                      nClasses           = RasterLibrary.defaultNumberOfClasses;
55
	protected int[]                    minValueToEcualize = null;
56
	protected int[]                    maxValueToEcualize = null;
57 54
	
58
//	protected long[][]                 lahe               = null;
59
//	protected long[][]                 laheNegative       = null;
55
	protected long[][]                 lahe               = null;
56
	protected long[][]                 laheNegative       = null;
60 57
	
61 58
	/**
62 59
	 * Construye un LinearEnhancementFilter
......
74 71
		stats = (DatasetListStatistics) params.get("stats");
75 72
		histogram = (Histogram) params.get("histogram");
76 73
		
77
		if(histogram == null)
78
			return;
74
		if(histogram == null) {
75
			try {
76
				histogram = raster.getHistogram();
77
			} catch (HistogramException e) {
78
				exec = false;
79
			} catch (InterruptedException e) {
80
				exec = false;
81
			}
82
			//return;
83
		}
79 84
		
80 85
		renderBands = (int[]) params.get("renderBands");
81 86
		if(renderBands != null && renderBands.length < raster.getBandCount()) {
......
93 98
		if(ecualizedBands == null)
94 99
			ecualizedBands = renderBands;
95 100
		
96
		minValueToEcualize = (int[]) params.get("minValueToEcualize");
97
		maxValueToEcualize = (int[]) params.get("maxValueToEcualize");
98
		
99 101
		height = raster.getHeight();
100 102
		width = raster.getWidth();
101 103
		nPixels = height * width;
......
127 129
				maxBandValue = stats.getMax();
128 130
			}
129 131
		}
130
		minValueToEcualize = new int[]{20, 20, 20};
131
		maxValueToEcualize = new int[]{180, 180, 180};
132
		//Si no se han pasado valores m?ximos o m?nimos para aplicar la ecualizaci?n se aplica
133
		//directamente los m?ximos y m?nimos globales.
134
		if(maxValueToEcualize == null) {
135
			maxValueToEcualize = new int[maxBandValue.length];
136
			for (int i = 0; i < maxValueToEcualize.length; i++)
137
				maxValueToEcualize[i] = (int)maxBandValue[i];
138
		}
139
		if(minValueToEcualize == null) {
140
			minValueToEcualize = new int[minBandValue.length];
141
			for (int i = 0; i < minValueToEcualize.length; i++)
142
				minValueToEcualize[i] = (int)minBandValue[i];
143
		}
144
		
132
				
145 133
		Histogram rgbHistogram = null;
146 134
		try {
147 135
			if(raster.getDataType() == IBuffer.TYPE_BYTE)
......
155 143
		}
156 144
		double[][] accumNormalize = Histogram.convertTableToNormalizeAccumulate(rgbHistogram.getTable());
157 145
		double[][] accumNormalizeNeg = Histogram.convertTableToNormalizeAccumulate(rgbHistogram.getNegativeTable());
158

  
159
		double[][] probabilityDistrib = null;
160
		if(raster.getDataType() == IBuffer.TYPE_BYTE)
161
			probabilityDistrib = getProbabilityDistribution(raster.getBandCount(), 255);
162
		else
163
			probabilityDistrib = getProbabilityDistribution(raster.getBandCount(), nClasses);
164 146
		
165
		aproximation = calcApproximation(accumNormalize, probabilityDistrib);
166
		aproximationNeg = calcApproximation(accumNormalizeNeg, probabilityDistrib);
167
		nElements = (aproximation[0].length - 1);
147
		int value = 255;
148
		if(raster.getDataType() != RasterBuffer.TYPE_BYTE)
149
			value = nClasses;
168 150
		
169
//		lahe = lahe(accumNormalize, 255);
170
//		laheNegative = lahe(accumNormalizeNeg, 255);
171
//		nElements = (laheNegative[0].length - 1);
151
		lahe = lahe(accumNormalize, value);
152
		laheNegative = lahe(accumNormalizeNeg, value);
153
		nElements = (laheNegative[0].length - 1);
172 154
		
173 155
		nbands = stats.getBandCount();
174 156
		rasterResult = RasterBuffer.getBuffer(raster.getDataType(), raster.getWidth(), raster.getHeight(), raster.getBandCount(), true);
......
181 163
	 * @param value Valor m?ximo
182 164
	 * @return
183 165
	 */
184
//	private long[][] lahe(double[][] accumNorm, int value) {
185
//		long[][] res = new long[accumNorm.length][accumNorm[0].length];
186
//		for (int i = 0; i < res.length; i++) 
187
//			  for (int j = 0; j < res[i].length; j++) 
188
//				  res[i][j] = Math.round(accumNorm[i][j] * value);
189
//		return res;
190
//	}
166
	private long[][] lahe(double[][] accumNorm, int value) {
167
		long[][] res = new long[accumNorm.length][accumNorm[0].length];
168
		for (int i = 0; i < res.length; i++) 
169
			  for (int j = 0; j < res[i].length; j++) 
170
				  res[i][j] = Math.round(accumNorm[i][j] * value);
171
		return res;
172
	}
191 173
	
192 174
	/**
193 175
	 * Consulta si la ecualizaci?n est? activa para una banda o no
......
201 183
		}
202 184
		return false;
203 185
	}
204
	
205
	/**
206
	 * Obtiene una distribuci?n de probabilidad de niveles de gris.  
207
	 * @param bands N?mero de bandas
208
	 * @param nValues Valores a distribuir. Tipicamente 255
209
	 * @return 
210
	 */
211
	private double[][] getProbabilityDistribution(int bands, double nValues) {
212
		double[][] res = new double[bands][(int)nValues];
213
		for (int i = 0; i < res.length; i++)
214
			for (int j = 0; j < res[i].length; j++) 
215
				res[i][j] += (double)((double)j / nValues);
216
		return res;
217
	}
218
	
219
	/**
220
	 * Calcula un histograma aproximado ecualizado. Busca para cada valor del histograma acumulado
221
	 * normalizado el valor m?s cercano que hay en la distribuci?n de probabilidades. Con estos datos
222
	 * crea un array donde cada valor es el nuevo nivel de gris de esa posici?n.
223
	 * @param accumNorm
224
	 * @param distrib
225
	 * @return
226
	 */
227
	private double[][] calcApproximation(double[][] accumNorm, double[][] distrib) {
228
		double[][] res = new double[accumNorm.length][accumNorm[0].length];
229
		resValues = new int[accumNorm.length][accumNorm[0].length];
230
		for (int i = 0; i < accumNorm.length; i++) {
231
			for (int j = 0; j < accumNorm[i].length; j++) {
232
				double[] v = searchProbability(distrib[i], accumNorm[i][j]);
233
				if(v != null) {
234
					res[i][j] = v[0];
235
					resValues[i][j] = (int)v[1];
236
				}
237
			}
238
		}
239
		return res;
240
	}
241
	
242
	/**
243
	 * En un array de distribuci?n de probabilidades busca la m?s cercana
244
	 * al valor pasado
245
	 * @param distrib Array de probabilidades
246
	 * @param value Valor a buscar en el array
247
	 * @return valor m?s cercano en el array y posici?n del mismo
248
	 */
249
	private double[] searchProbability(double[] distrib, double value) {
250
		double dif = Math.abs(distrib[0] - value);
251
		for (int i = 1; i < distrib.length; i++) {
252
			double newDif = Math.abs(distrib[i] - value);
253
			if(newDif < dif)
254
				dif = newDif;
255
			else
256
				return new double[]{distrib[i - 1], i - 1};
257
		}
258
		return new double[]{distrib[distrib.length - 1], distrib.length - 1};
259
	}
260
	
186
		
261 187
	/*
262 188
	 * (non-Javadoc)
263 189
	 * @see org.gvsig.raster.grid.filter.RasterFilter#getOutRasterDataType()
......
284 210
	 * @see org.gvsig.raster.grid.filter.RasterFilter#getGroup()
285 211
	 */
286 212
	public String getGroup() {
287
		return "radiometricos";
213
		return "realces";
288 214
	}
289 215

  
290 216
	/*
......
333 259
	 * @see org.gvsig.raster.grid.filter.RasterFilter#isVisible()
334 260
	 */
335 261
	public boolean isVisible() {
336
		return false;
262
		return true;
337 263
	}
338 264
}
trunk/libraries/libRaster/src/org/gvsig/raster/grid/filter/enhancement/EqualizationShortFilter.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 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.grid.filter.enhancement;
20

  
21
import org.gvsig.raster.dataset.IBuffer;
22
/**
23
 * Filtro de ecualizaci?n de histograma para tipo de datos short.
24
 *
25
 * @version 11/05/2007
26
 * @author Nacho Brodin (nachobrodin@gmail.com)
27
 */
28
public class EqualizationShortFilter extends EqualizationFilter {
29

  
30
	/*
31
	 * (non-Javadoc)
32
	 * @see org.gvsig.raster.grid.filter.enhancement.LinearEnhancementFilter#process(int, int)
33
	 */
34
	public void process(int col, int line) {
35
		for (int iBand = 0; iBand < raster.getBandCount(); iBand++) {
36
			short p = raster.getElemShort(line, col, iBand);
37
			if(!equalizationActive(iBand)) {
38
				rasterResult.setElem(line, col, iBand, p);
39
				continue;
40
			}
41
			
42
			if (p > maxBandValue[renderBands[iBand]])
43
				p = (short) maxBandValue[renderBands[iBand]];
44
			else if (p < minBandValue[renderBands[iBand]])
45
				p = (short) minBandValue[renderBands[iBand]];
46
		
47
			int pos = (int)(((p + dto[iBand]) * nClasses) / distance[iBand]);
48
			int ecualizationPositive = (int)(lahe[renderBands[iBand]][pos]);
49
			int ecualizationNegative = (int)(lahe[renderBands[iBand]][nElements - pos]);
50
			double value = ((nElements - ecualizationNegative) + ecualizationPositive) / 2;
51
			
52
			rasterResult.setElem(line, col, iBand, (short)value);
53
		}
54
	}
55
	
56
	double[] distance = null;
57
	double[] dto = null;
58
	/*
59
	 * (non-Javadoc)
60
	 * @see org.gvsig.raster.grid.filter.RasterFilter#pre()
61
	 */
62
	public void pre() {
63
		super.pre();
64
		distance = new double[raster.getBandCount()];
65
		dto = new double[raster.getBandCount()];
66
		for (int i = 0; i < raster.getBandCount(); i++) {
67
			distance[i] = maxBandValue[renderBands[i]] - minBandValue[renderBands[i]];
68
			dto[i] = -minBandValue[renderBands[i]];
69
		}
70
	}
71

  
72
	/*
73
	 * (non-Javadoc)
74
	 * @see org.gvsig.raster.grid.filter.enhancement.LinearEnhancementFilter#getInRasterDataType()
75
	 */
76
	public int getInRasterDataType() {
77
		return IBuffer.TYPE_SHORT;
78
	}
79
	
80
	/*
81
	 * (non-Javadoc)
82
	 * @see org.gvsig.raster.grid.filter.RasterFilter#getOutRasterDataType()
83
	 */
84
	public int getOutRasterDataType() {
85
		return IBuffer.TYPE_SHORT;
86
	}
87
}
trunk/libraries/libRaster/src/org/gvsig/raster/grid/filter/enhancement/EqualizationByteFilter.java
45 45
				p = (int) minBandValue[renderBands[iBand]];
46 46

  
47 47
			//M?todo lahe
48
			//long ecualizationPositive = lahe[renderBands[iBand]][p % histogram.getNumValues()];
49
			//long ecualizationNegative = laheNegative[renderBands[iBand]][nElements - (p % histogram.getNumValues())];
48
			int ecualizationPositive = (int)lahe[renderBands[iBand]][p % histogram.getNumValues()];
49
			int ecualizationNegative = (int)laheNegative[renderBands[iBand]][nElements - (p % histogram.getNumValues())];
50 50
			
51
			if(p >= minValueToEcualize[iBand] && p <= maxValueToEcualize[iBand]) {
52
				int ecualizationPositive = (int)(aproximation[renderBands[iBand]][p] * 255);
53
				int ecualizationNegative = (int)(aproximationNeg[renderBands[iBand]][nElements - p] * 255);
54
				double value = ((nElements - ecualizationNegative) + ecualizationPositive) / 2;
55
				rasterResult.setElem(line, col, iBand, (byte)value);
56
			} else
57
				rasterResult.setElem(line, col, iBand, (byte)p);
51
			int value = ((nElements - ecualizationNegative) + ecualizationPositive) / 2;
52
			rasterResult.setElem(line, col, iBand, (byte)(value & 0x000000ff));
58 53
		}
59 54
	}
60 55

  

Also available in: Unified diff