Revision 19517 trunk/libraries/libRaster/src/org/gvsig/raster/grid/filter/enhancement/EcualizationFilter.java
EcualizationFilter.java | ||
---|---|---|
34 | 34 |
* @author Nacho Brodin (nachobrodin@gmail.com) |
35 | 35 |
*/ |
36 | 36 |
public class EcualizationFilter extends RasterFilter { |
37 |
protected DatasetListStatistics stats = null; |
|
38 |
protected double[] minBandValue = null; |
|
39 |
protected double[] maxBandValue = null; |
|
40 |
protected int nbands = 3; |
|
41 |
protected int[] renderBands = null; |
|
42 |
protected int[] k = null; |
|
43 |
public static String[] names = new String[] {"ecualization"}; |
|
44 |
protected Histogram histogram = null; |
|
45 |
protected long nPixels = 0; |
|
46 |
protected int[][] resValues = null; |
|
47 |
protected long[][] lahe = null;
|
|
48 |
protected long[][] laheNegative = null;
|
|
49 |
protected int nElements = 0; |
|
50 |
protected int[] ecualizedBands = null; |
|
37 |
protected DatasetListStatistics stats = null;
|
|
38 |
protected double[] minBandValue = null;
|
|
39 |
protected double[] maxBandValue = null;
|
|
40 |
protected int nbands = 3;
|
|
41 |
protected int[] renderBands = null;
|
|
42 |
protected int[] k = null;
|
|
43 |
public static String[] names = new String[] {"ecualization"};
|
|
44 |
protected Histogram histogram = null;
|
|
45 |
protected long nPixels = 0;
|
|
46 |
protected int[][] resValues = null;
|
|
47 |
// protected long[][] lahe = null;
|
|
48 |
// protected long[][] laheNegative = null;
|
|
49 |
protected int nElements = 0;
|
|
50 |
protected int[] ecualizedBands = null;
|
|
51 | 51 |
|
52 |
protected double[][] aproximation = null; |
|
53 |
protected double[][] aproximationNeg = null; |
|
54 |
|
|
52 | 55 |
/** |
53 | 56 |
* Construye un LinearEnhancementFilter |
54 | 57 |
*/ |
... | ... | |
125 | 128 |
double[][] accumNormalize = Histogram.convertTableToNormalizeAccumulate(rgbHistogram.getTable()); |
126 | 129 |
double[][] accumNormalizeNeg = Histogram.convertTableToNormalizeAccumulate(rgbHistogram.getNegativeTable()); |
127 | 130 |
|
128 |
lahe = lahe(accumNormalize, 255); |
|
129 |
laheNegative = lahe(accumNormalizeNeg, 255); |
|
130 |
nElements = (laheNegative[0].length - 1); |
|
131 |
double[][] probabilityDistrib = getProbabilityDistribution(raster.getBandCount(), 255); |
|
132 |
aproximation = calcApproximation(accumNormalize, probabilityDistrib); |
|
133 |
aproximationNeg = calcApproximation(accumNormalizeNeg, probabilityDistrib); |
|
134 |
nElements = (aproximation[0].length - 1); |
|
131 | 135 |
|
136 |
// lahe = lahe(accumNormalize, 255); |
|
137 |
// laheNegative = lahe(accumNormalizeNeg, 255); |
|
138 |
// nElements = (laheNegative[0].length - 1); |
|
139 |
|
|
132 | 140 |
nbands = stats.getBandCount(); |
133 | 141 |
rasterResult = RasterBuffer.getBuffer(IBuffer.TYPE_BYTE, raster.getWidth(), raster.getHeight(), raster.getBandCount(), true); |
134 | 142 |
} |
... | ... | |
140 | 148 |
* @param value Valor m?ximo |
141 | 149 |
* @return |
142 | 150 |
*/ |
143 |
private long[][] lahe(double[][] accumNorm, int value) { |
|
144 |
long[][] res = new long[accumNorm.length][accumNorm[0].length]; |
|
145 |
for (int i = 0; i < res.length; i++) |
|
146 |
for (int j = 0; j < res[i].length; j++) |
|
147 |
res[i][j] = Math.round(accumNorm[i][j] * value); |
|
148 |
return res; |
|
149 |
} |
|
151 |
// private long[][] lahe(double[][] accumNorm, int value) {
|
|
152 |
// long[][] res = new long[accumNorm.length][accumNorm[0].length];
|
|
153 |
// for (int i = 0; i < res.length; i++)
|
|
154 |
// for (int j = 0; j < res[i].length; j++)
|
|
155 |
// res[i][j] = Math.round(accumNorm[i][j] * value);
|
|
156 |
// return res;
|
|
157 |
// }
|
|
150 | 158 |
|
151 | 159 |
/** |
152 | 160 |
* Consulta si la ecualizaci?n est? activa para una banda o no |
... | ... | |
167 | 175 |
* @param nValues Valores a distribuir. Tipicamente 255 |
168 | 176 |
* @return |
169 | 177 |
*/ |
170 |
/*private double[][] getProbabilityDistribution(int bands, int nValues) {
|
|
171 |
double[][] res = new double[bands][nValues]; |
|
178 |
private double[][] getProbabilityDistribution(int bands, double nValues) {
|
|
179 |
double[][] res = new double[bands][(int)nValues];
|
|
172 | 180 |
for (int i = 0; i < res.length; i++) |
173 | 181 |
for (int j = 0; j < res[i].length; j++) |
174 |
res[i][j] += (double)((double)j / 255D);
|
|
182 |
res[i][j] += (double)((double)j / nValues);
|
|
175 | 183 |
return res; |
176 |
}*/
|
|
184 |
} |
|
177 | 185 |
|
178 | 186 |
/** |
179 |
* Calcula un histograma aproximado ecualizado. |
|
187 |
* Calcula un histograma aproximado ecualizado. Busca para cada valor del histograma acumulado |
|
188 |
* normalizado el valor m?s cercano que hay en la distribuci?n de probabilidades. Con estos datos |
|
189 |
* crea un array donde cada valor es el nuevo nivel de gris de esa posici?n. |
|
180 | 190 |
* @param accumNorm |
181 | 191 |
* @param distrib |
182 | 192 |
* @return |
183 | 193 |
*/ |
184 |
/*private double[][] calcApproximation(double[][] accumNorm, double[][] distrib) {
|
|
194 |
private double[][] calcApproximation(double[][] accumNorm, double[][] distrib) { |
|
185 | 195 |
double[][] res = new double[accumNorm.length][accumNorm[0].length]; |
186 | 196 |
resValues = new int[accumNorm.length][accumNorm[0].length]; |
187 |
for (int i = 0; i < accumNorm.length; i++) {
|
|
188 |
for (int j = 0; j < accumNorm[i].length; j++) {
|
|
189 |
double[] v = searchProbability(distrib[i], accumNorm[i][j]);
|
|
190 |
if(v != null) {
|
|
191 |
res[i][j] = v[0];
|
|
192 |
resValues[i][j] = (int)v[1];
|
|
193 |
}
|
|
194 |
}
|
|
195 |
}
|
|
196 |
return res;
|
|
197 |
}*/
|
|
197 |
for (int i = 0; i < accumNorm.length; i++) { |
|
198 |
for (int j = 0; j < accumNorm[i].length; j++) { |
|
199 |
double[] v = searchProbability(distrib[i], accumNorm[i][j]); |
|
200 |
if(v != null) { |
|
201 |
res[i][j] = v[0]; |
|
202 |
resValues[i][j] = (int)v[1]; |
|
203 |
} |
|
204 |
} |
|
205 |
} |
|
206 |
return res; |
|
207 |
} |
|
198 | 208 |
|
199 | 209 |
/** |
200 | 210 |
* En un array de distribuci?n de probabilidades busca la m?s cercana |
201 | 211 |
* al valor pasado |
202 | 212 |
* @param distrib Array de probabilidades |
203 | 213 |
* @param value Valor a buscar en el array |
204 |
* @return valor m?s cercano en el array |
|
214 |
* @return valor m?s cercano en el array y posici?n del mismo
|
|
205 | 215 |
*/ |
206 |
/*private double[] searchProbability(double[] distrib, double value) {
|
|
216 |
private double[] searchProbability(double[] distrib, double value) { |
|
207 | 217 |
double dif = Math.abs(distrib[0] - value); |
208 | 218 |
for (int i = 1; i < distrib.length; i++) { |
209 | 219 |
double newDif = Math.abs(distrib[i] - value); |
210 | 220 |
if(newDif < dif) |
211 | 221 |
dif = newDif; |
212 | 222 |
else |
213 |
return new double[]{dif, i - 1};
|
|
223 |
return new double[]{distrib[i - 1], i - 1};
|
|
214 | 224 |
} |
215 |
return null;
|
|
216 |
}*/
|
|
225 |
return new double[]{distrib[distrib.length - 1], distrib.length - 1};
|
|
226 |
} |
|
217 | 227 |
|
218 | 228 |
/* |
219 | 229 |
* (non-Javadoc) |
Also available in: Unified diff